Strip whitespace for secrets
[apps/pfixtools.git] / daemon.c
index 1d40424..392dccb 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                  */
 /*          ~~~~~~~~~                                                         */
 /*  ________________________________________________________________________  */
 /*                                                                            */
@@ -57,7 +57,7 @@ static int setnonblock(int sock)
 }
 
 
-int tcp_listen(const struct sockaddr *addr, socklen_t len)
+int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
 {
     int sock;
 
@@ -127,3 +127,25 @@ int accept_nonblock(int fd)
 
     return sock;
 }
+
+int daemon_detach(void)
+{
+    pid_t pid;
+
+    close(STDIN_FILENO);
+    close(STDOUT_FILENO);
+    close(STDERR_FILENO);
+
+    open("/dev/null", O_RDWR);
+    open("/dev/null", O_RDWR);
+    open("/dev/null", O_RDWR);
+
+    pid = fork();
+    if (pid < 0)
+        return -1;
+    if (pid)
+        exit(0);
+
+    setsid();
+    return 0;
+}