]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
Changes as documented in ticket #15937 (stdio file descriptor fixes).
authorShane Kerr <shane@isc.org>
Mon, 27 Mar 2006 09:45:47 +0000 (09:45 +0000)
committerShane Kerr <shane@isc.org>
Mon, 27 Mar 2006 09:45:47 +0000 (09:45 +0000)
client/dhclient.c
relay/dhcrelay.c
server/dhcpd.c

index 29d600e9cdaf58a7461cc0705047f5fc085ecd91..e751ce64b7269f44b03e44140067b22ddd89a333 100644 (file)
@@ -32,7 +32,7 @@
 
 #ifndef lint
 static char ocopyright[] =
-"$Id: dhclient.c,v 1.134 2006/02/27 23:56:12 dhankins Exp $ Copyright (c) 2004-2006 Internet Systems Consortium.  All rights reserved.\n";
+"$Id: dhclient.c,v 1.135 2006/03/27 09:45:47 shane Exp $ Copyright (c) 2004-2006 Internet Systems Consortium.  All rights reserved.\n";
 #endif /* not lint */
 
 #include "dhcpd.h"
@@ -83,6 +83,7 @@ int main (argc, argv, envp)
        int argc;
        char **argv, **envp;
 {
+       int fd;
        int i;
        struct servent *ent;
        struct interface_info *ip;
@@ -102,15 +103,18 @@ int main (argc, argv, envp)
        int no_dhclient_script = 0;
        char *s;
 
-       /* Make sure we have stdin, stdout and stderr. */
-       i = open ("/dev/null", O_RDWR);
-       if (i == 0)
-               i = open ("/dev/null", O_RDWR);
-       if (i == 1) {
-               i = open ("/dev/null", O_RDWR);
-               log_perror = 0; /* No sense logging to /dev/null. */
-       } else if (i != -1)
-               close (i);
+        /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
+           2 (stderr) are open. To do this, we assume that when we
+           open a file the lowest available file decriptor is used. */
+        fd = open ("/dev/null", O_RDWR);
+        if (fd == 0)
+                fd = open ("/dev/null", O_RDWR);
+        if (fd == 1)
+                fd = open ("/dev/null", O_RDWR);
+        if (fd == 2)
+                log_perror = 0; /* No sense logging to /dev/null. */
+        else if (fd != -1)
+                close (fd);
 
 #ifdef SYSLOG_4_2
        openlog ("dhclient", LOG_NDELAY);
@@ -2698,7 +2702,6 @@ void go_daemon ()
 {
        static int state = 0;
        int pid;
-       int i;
 
        /* Don't become a daemon if the user requested otherwise. */
        if (no_daemon) {
@@ -2728,14 +2731,10 @@ void go_daemon ()
         close(2);
 
        /* Reopen them on /dev/null. */
-       i = open ("/dev/null", O_RDWR);
-       if (i == 0)
-               i = open ("/dev/null", O_RDWR);
-       if (i == 1) {
-               i = open ("/dev/null", O_RDWR);
-               log_perror = 0; /* No sense logging to /dev/null. */
-       } else if (i != -1)
-               close (i);
+       open ("/dev/null", O_RDWR);
+       open ("/dev/null", O_RDWR);
+       open ("/dev/null", O_RDWR);
+       log_perror = 0; /* No sense logging to /dev/null. */
 
        write_client_pid_file ();
 }
index 4abc52502e03777678889053cd393d6da1f7afb3..15c0b13edf1a87e2527acd3bfdd9ce5f8d0e4ca2 100644 (file)
@@ -34,7 +34,7 @@
 
 #ifndef lint
 static char ocopyright[] =
-"$Id: dhcrelay.c,v 1.55 2006/02/27 23:56:13 dhankins Exp $ Copyright (c) 2004-2006 Internet Systems Consortium.  All rights reserved.\n";
+"$Id: dhcrelay.c,v 1.56 2006/03/27 09:45:47 shane Exp $ Copyright (c) 2004-2006 Internet Systems Consortium.  All rights reserved.\n";
 #endif /* not lint */
 
 #include "dhcpd.h"
@@ -101,6 +101,7 @@ int main (argc, argv, envp)
        int argc;
        char **argv, **envp;
 {
+       int fd;
        int i;
        struct servent *ent;
        struct server_list *sp = (struct server_list *)0;
@@ -110,15 +111,18 @@ int main (argc, argv, envp)
        char *s;
        struct interface_info *tmp = (struct interface_info *)0;
 
-       /* Make sure we have stdin, stdout and stderr. */
-       i = open ("/dev/null", O_RDWR);
-       if (i == 0)
-               i = open ("/dev/null", O_RDWR);
-       if (i == 1) {
-               i = open ("/dev/null", O_RDWR);
-               log_perror = 0; /* No sense logging to /dev/null. */
-       } else if (i != -1)
-               close (i);
+        /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
+           2 (stderr) are open. To do this, we assume that when we
+           open a file the lowest available file decriptor is used. */
+        fd = open ("/dev/null", O_RDWR);
+        if (fd == 0)
+                fd = open ("/dev/null", O_RDWR);
+        if (fd == 1)
+                fd = open ("/dev/null", O_RDWR);
+        if (fd == 2)
+                log_perror = 0; /* No sense logging to /dev/null. */
+        else if (fd != -1)
+                close (fd);
 
 #ifdef SYSLOG_4_2
        openlog ("dhcrelay", LOG_NDELAY);
index 8f2c86aea29e5e0a682ab56d032e1d3aa9f30309..6e6e5d8fb6e91869fdaa148e8a59a24abb892434 100644 (file)
@@ -34,7 +34,7 @@
 
 #ifndef lint
 static char ocopyright[] =
-"$Id: dhcpd.c,v 1.117 2005/03/17 20:15:27 dhankins Exp $ Copyright 2004-2005 Internet Systems Consortium.";
+"$Id: dhcpd.c,v 1.118 2006/03/27 09:45:47 shane Exp $ Copyright 2004-2005 Internet Systems Consortium.";
 #endif
 
   static char copyright[] =
@@ -197,6 +197,7 @@ int main (argc, argv, envp)
        int argc;
        char **argv, **envp;
 {
+       int fd;
        int i, status;
        struct servent *ent;
        char *s;
@@ -226,15 +227,18 @@ int main (argc, argv, envp)
        char *traceoutfile = (char *)0;
 #endif
 
-       /* Make sure we have stdin, stdout and stderr. */
-       status = open ("/dev/null", O_RDWR);
-       if (status == 0)
-               status = open ("/dev/null", O_RDWR);
-       if (status == 1) {
-               status = open ("/dev/null", O_RDWR);
-               log_perror = 0; /* No sense logging to /dev/null. */
-       } else if (status != -1)
-               close (status);
+        /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
+           2 (stderr) are open. To do this, we assume that when we
+           open a file the lowest available file decriptor is used. */
+        fd = open ("/dev/null", O_RDWR);
+        if (fd == 0)
+                fd = open ("/dev/null", O_RDWR);
+        if (fd == 1)
+                fd = open ("/dev/null", O_RDWR);
+        if (fd == 2)
+                log_perror = 0; /* No sense logging to /dev/null. */
+        else if (fd != -1)
+                close (fd);
 
        /* Set up the client classification system. */
        classification_setup ();
@@ -569,10 +573,18 @@ int main (argc, argv, envp)
 
        if (daemon) {
                /* Become session leader and get pid... */
-               close (0);
-               close (1);
-               close (2);
                pid = setsid ();
+
+                /* Close standard I/O descriptors. */
+                close (0);
+                close (1);
+                close (2);
+
+                /* Reopen them on /dev/null. */
+                open ("/dev/null", O_RDWR);
+                open ("/dev/null", O_RDWR);
+                open ("/dev/null", O_RDWR);
+                log_perror = 0; /* No sense logging to /dev/null. */
        }
 
        /* If we didn't write the pid file earlier because we found a