From: Evan Hunt Date: Sat, 27 Oct 2007 18:58:59 +0000 (+0000) Subject: Don't delete PID files before writing to them. [rt17030] X-Git-Tag: v4_0_0b3~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=182b187ea54b201b405561f342fecf674faf8b68;p=thirdparty%2Fdhcp.git Don't delete PID files before writing to them. [rt17030] --- diff --git a/client/dhclient.c b/client/dhclient.c index 3a74269a7..9798119f2 100644 --- a/client/dhclient.c +++ b/client/dhclient.c @@ -138,7 +138,11 @@ main(int argc, char **argv) { dhcp_interface_startup_hook = dhclient_interface_startup_hook; for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-4")) { + if (!strcmp(argv[i], "-r")) { + release_mode = 1; + no_daemon = 1; +#ifdef DHCPv6 + } else if (!strcmp(argv[i], "-4")) { if (local_family_set && local_family != AF_INET) log_fatal("Client can only do v4 or v6, not " "both."); @@ -150,9 +154,7 @@ main(int argc, char **argv) { "both."); local_family_set = 1; local_family = AF_INET6; - } else if (!strcmp(argv[i], "-r")) { - release_mode = 1; - no_daemon = 1; +#endif /* DHCPv6 */ } else if (!strcmp (argv [i], "-x")) { /* eXit, no release */ release_mode = 0; no_daemon = 0; @@ -306,10 +308,8 @@ main(int argc, char **argv) { oldpid = (pid_t)temp; if (e != 0 && e != EOF) { - if (oldpid) { - if (kill(oldpid, SIGTERM) == 0) - unlink(path_dhclient_pid); - } + if (oldpid) + kill(oldpid, SIGTERM); } fclose(pidfd); } @@ -555,7 +555,12 @@ static void usage () log_info (arr); log_info (url); - log_error ("Usage: dhclient [-1dvrx] [-nw] [-p ] %s", + log_error ("Usage: dhclient %s %s", +#ifdef DHCPv6 + "[-4|-6] [-1dvrx] [-nw] [-p ]", +#else /* DHCPv6 */ + "[-1dvrx] [-nw] [-p ]", +#endif /* DHCPv6 */ "[-s server]"); log_error (" [-cf config-file] [-lf lease-file]%s", "[-pf pid-file] [-e VAR=val]"); diff --git a/server/dhcpd.c b/server/dhcpd.c index 744bbef12..d61e31b0c 100644 --- a/server/dhcpd.c +++ b/server/dhcpd.c @@ -202,7 +202,6 @@ main(int argc, char **argv) { int cftest = 0; int lftest = 0; #ifndef DEBUG - int pidfilewritten = 0; int pid; char pbuf [20]; int daemon = 1; @@ -629,28 +628,32 @@ main(int argc, char **argv) { /* Read previous pid file. */ if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) { - status = read (i, pbuf, (sizeof pbuf) - 1); + status = read(i, pbuf, (sizeof pbuf) - 1); close (i); if (status > 0) { - pbuf [status] = 0; - pid = atoi (pbuf); - - /* If the previous server process is not still running, - write a new pid file immediately. */ - if (pid && (pid == getpid() || kill (pid, 0) < 0)) { - 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; - } - } else - log_fatal ("There's already a DHCP server running."); + pbuf[status] = 0; + pid = atoi(pbuf); + + /* + * If there was a previous server process and it's + * is still running, abort + */ + if (!pid || (pid != getpid() && kill(pid, 0) == 0)) + log_fatal("There's already a " + "DHCP server running."); } } + /* Write new pid file. */ + if ((i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0) { + sprintf(pbuf, "%d\n", (int) getpid()); + write(i, pbuf, strlen(pbuf)); + close(i); + } else { + log_error("Can't create PID file %s: %m.", path_dhcpd_pid); + } + + /* If we were requested to log to stdout on the command line, keep doing so; otherwise, stop. */ if (log_perror == -1) @@ -658,21 +661,6 @@ 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();