Anyone have a problem w/ closing fd's in daemonize

Alex Kiernan alexk at demon.net
Fri Aug 23 13:53:31 UTC 2002


I've been running with this for a while, its just adding stuff which
daemon() has done for ages (ever?).

Anyone see any issues? It fixes the "hang on logout" issue which you
otherwise see with current OpenSSH on (at least) Solaris.

===================================================================
RCS file: /dist1/cvs/isc/inn/inn/lib/daemonize.c,v
retrieving revision 1.3
diff -u -r1.3 daemonize.c
--- lib/daemonize.c	2002/03/31 22:35:46	1.3
+++ lib/daemonize.c	2002/08/23 13:31:33
@@ -24,7 +24,7 @@
 daemonize(const char *path)
 {
     int status;
-    int fd UNUSED;
+    int fd;
 
     /* Fork and exit in the parent to disassociate from the current process
        group and become the leader of a new process group. */
@@ -49,4 +49,12 @@
 
     if (chdir(path) < 0)
         syswarn("cant chdir to %s", path);
+
+    if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
+	dup2(fd, STDIN_FILENO);
+	dup2(fd, STDOUT_FILENO);
+	dup2(fd, STDERR_FILENO);
+	if (fd > 2)
+	    close(fd);
+    }
 }

I've been running it a while & it all seems OK, its just that as it
wasn't there it kind of makes me wary.

-- 
Alex Kiernan, Principal Engineer, Development, THUS plc


More information about the inn-workers mailing list