From: Evan Hunt Date: Thu, 7 Jun 2007 15:29:31 +0000 (+0000) Subject: Chdir to "/" before going into daemon mode. Use realpath() to X-Git-Tag: v4_0_0a2~36 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a546f2a714b15049860d0cb60a6e8e2ea8ee7418;p=thirdparty%2Fdhcp.git Chdir to "/" before going into daemon mode. Use realpath() to canonicalize relative pathnames. [rt16729] --- diff --git a/RELNOTES b/RELNOTES index 706489787..7c99f43db 100644 --- a/RELNOTES +++ b/RELNOTES @@ -66,6 +66,9 @@ suggested fixes to . - Added -x option to dhclient, which triggers dhclient processes to exit gracefully without releasing leases first +- All binaries (client, server, relay) now change directories + to / before going into daemon mode, so as not to hold $CWD open + Changes since 4.0.0-20070413 - Old (expired) leases are now cleaned. diff --git a/client/dhclient.c b/client/dhclient.c index 5466af928..f05a94ef4 100644 --- a/client/dhclient.c +++ b/client/dhclient.c @@ -32,7 +32,7 @@ #ifndef lint static char ocopyright[] = -"$Id: dhclient.c,v 1.152 2007/06/06 22:57:32 each Exp $ Copyright (c) 2004-2007 Internet Systems Consortium. All rights reserved.\n"; +"$Id: dhclient.c,v 1.153 2007/06/07 15:29:30 each Exp $ Copyright (c) 2004-2007 Internet Systems Consortium. All rights reserved.\n"; #endif /* not lint */ #include "dhcpd.h" @@ -276,7 +276,29 @@ main(int argc, char **argv) { else log_fatal("Impossible condition at %s:%d.", MDL); - /* first kill of any currently running client */ + /* + * convert relative path names to absolute, for files that need + * to be reopened after chdir() has been called + */ + if (path_dhclient_db[0] != '/') { + char *path = dmalloc(PATH_MAX, MDL); + if (path == NULL) + log_fatal("No memory for filename\n"); + path_dhclient_db = realpath(path_dhclient_db, path); + if (path_dhclient_db == NULL) + log_fatal("%s: %s", path, strerror(errno)); + } + + if (path_dhclient_script[0] != '/') { + char *path = dmalloc(PATH_MAX, MDL); + if (path == NULL) + log_fatal("No memory for filename\n"); + path_dhclient_script = realpath(path_dhclient_script, path); + if (path_dhclient_script == NULL) + log_fatal("%s: %s", path, strerror(errno)); + } + + /* first kill off any currently running client */ if (release_mode || exit_mode) { FILE *pidfd; pid_t oldpid; @@ -3049,6 +3071,8 @@ void go_daemon () open("/dev/null", O_RDWR); write_client_pid_file (); + + chdir("/"); } void write_client_pid_file () diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c index d3bfbcce1..96e0a97d0 100644 --- a/relay/dhcrelay.c +++ b/relay/dhcrelay.c @@ -34,7 +34,7 @@ #ifndef lint static char ocopyright[] = -"$Id: dhcrelay.c,v 1.62 2007/05/19 19:16:26 dhankins Exp $ Copyright (c) 2004-2007 Internet Systems Consortium. All rights reserved.\n"; +"$Id: dhcrelay.c,v 1.63 2007/06/07 15:29:31 each Exp $ Copyright (c) 2004-2007 Internet Systems Consortium. All rights reserved.\n"; #endif /* not lint */ #include "dhcpd.h" @@ -312,6 +312,8 @@ main(int argc, char **argv) { close (1); close (2); pid = setsid (); + + chdir("/"); } /* Start dispatching packets and timeouts... */ diff --git a/server/dhcpd.c b/server/dhcpd.c index 59c3f9f95..913fe1f1b 100644 --- a/server/dhcpd.c +++ b/server/dhcpd.c @@ -34,7 +34,7 @@ #ifndef lint static char ocopyright[] = -"$Id: dhcpd.c,v 1.126 2007/05/29 18:11:56 each Exp $ Copyright 2004-2007 Internet Systems Consortium."; +"$Id: dhcpd.c,v 1.127 2007/06/07 15:29:31 each Exp $ Copyright 2004-2007 Internet Systems Consortium."; #endif static char copyright[] = @@ -46,6 +46,7 @@ static char url [] = "For info, please visit http://www.isc.org/sw/dhcp/"; #include "dhcpd.h" #include #include +#include static void usage PROTO ((void)); @@ -378,6 +379,19 @@ main(int argc, char **argv) { path_dhcpd_pid = s; } + /* + * convert relative path names to absolute, for files that need + * to be reopened after chdir() has been called + */ + if (path_dhcpd_db[0] != '/') { + char *path = dmalloc(PATH_MAX, MDL); + if (path == NULL) + log_fatal("No memory for filename\n"); + path_dhcpd_db = realpath(path_dhcpd_db, path); + if (path_dhcpd_db == NULL) + log_fatal("%s: %s", path, strerror(errno)); + } + if (!quiet) { log_info("%s %s", message, PACKAGE_VERSION); log_info (copyright); @@ -638,6 +652,21 @@ main(int argc, char **argv) { else log_perror = 0; + /* If we didn't write the pid file earlier because we found a + process running the logged pid, but we made it to here, + meaning nothing is listening on the bootp port, then write + the pid file out - what's in it now is bogus anyway. */ + if (!pidfilewritten) { + unlink (path_dhcpd_pid); + if ((i = open (path_dhcpd_pid, + O_WRONLY | O_CREAT, 0644)) >= 0) { + sprintf (pbuf, "%d\n", (int)getpid ()); + write (i, pbuf, strlen (pbuf)); + close (i); + pidfilewritten = 1; + } + } + if (daemon) { /* Become session leader and get pid... */ pid = setsid(); @@ -652,21 +681,8 @@ main(int argc, char **argv) { 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 - process running the logged pid, but we made it to here, - meaning nothing is listening on the bootp port, then write - the pid file out - what's in it now is bogus anyway. */ - if (!pidfilewritten) { - unlink (path_dhcpd_pid); - if ((i = open (path_dhcpd_pid, - O_WRONLY | O_CREAT, 0644)) >= 0) { - sprintf (pbuf, "%d\n", (int)getpid ()); - write (i, pbuf, strlen (pbuf)); - close (i); - pidfilewritten = 1; - } + chdir("/"); } #endif /* !DEBUG */