Rename project -> pfixtools.
[apps/pfixtools.git] / daemon.c
index ec8be65..64f9c68 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -1,5 +1,5 @@
 /******************************************************************************/
-/*          postlicyd: a postfix policy daemon with a lot of features         */
+/*          pfixtools: a collection of postfix related tools                  */
 /*          ~~~~~~~~~                                                         */
 /*  ________________________________________________________________________  */
 /*                                                                            */
  */
 
 #include <sys/un.h>
+#include <fcntl.h>
 
-#include "postlicyd.h"
+#include "common.h"
 #include "daemon.h"
 
+static int setnonblock(int sock)
+{
+    int res = fcntl(sock, F_GETFL);
+
+    if (res < 0) {
+        UNIXERR("fcntl");
+        return -1;
+    }
+
+    if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0) {
+        UNIXERR("fcntl");
+        return -1;
+    }
+
+    return 0;
+}
+
+
 int tcp_listen(const struct sockaddr *addr, socklen_t len)
 {
     int sock;
@@ -78,6 +97,11 @@ int tcp_listen(const struct sockaddr *addr, socklen_t len)
         return -1;
     }
 
+    if (setnonblock(sock)) {
+        close(sock);
+        return -1;
+    }
+
     if (listen(sock, 0) < 0) {
         UNIXERR("bind");
         close(sock);
@@ -87,3 +111,19 @@ int tcp_listen(const struct sockaddr *addr, socklen_t len)
     return sock;
 }
 
+int accept_nonblock(int fd)
+{
+    int sock = accept(fd, NULL, 0);
+
+    if (sock < 0) {
+        UNIXERR("accept");
+        return -1;
+    }
+
+    if (setnonblock(sock)) {
+        close(sock);
+        return -1;
+    }
+
+    return sock;
+}