From: Shane Kerr Date: Mon, 27 Mar 2006 09:45:47 +0000 (+0000) Subject: Changes as documented in ticket #15937 (stdio file descriptor fixes). X-Git-Tag: DHCPv6_parsing_base~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=185d16f9f9ba753cfcf7903030c6895f1b9b2879;p=thirdparty%2Fdhcp.git Changes as documented in ticket #15937 (stdio file descriptor fixes). --- diff --git a/client/dhclient.c b/client/dhclient.c index 29d600e9c..e751ce64b 100644 --- a/client/dhclient.c +++ b/client/dhclient.c @@ -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 (); } diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c index 4abc52502..15c0b13ed 100644 --- a/relay/dhcrelay.c +++ b/relay/dhcrelay.c @@ -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); diff --git a/server/dhcpd.c b/server/dhcpd.c index 8f2c86aea..6e6e5d8fb 100644 --- a/server/dhcpd.c +++ b/server/dhcpd.c @@ -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