]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
Don't delete PID files before writing to them. [rt17030]
authorEvan Hunt <each@isc.org>
Sat, 27 Oct 2007 18:58:59 +0000 (18:58 +0000)
committerEvan Hunt <each@isc.org>
Sat, 27 Oct 2007 18:58:59 +0000 (18:58 +0000)
client/dhclient.c
server/dhcpd.c

index 3a74269a78965ba4710f699f4284c87192e77e8a..9798119f2be1eba5d7e9e203aa440e175c574aef 100644 (file)
@@ -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 <port>] %s",
+       log_error ("Usage: dhclient %s %s",
+#ifdef DHCPv6
+                   "[-4|-6] [-1dvrx] [-nw] [-p <port>]",
+#else /* DHCPv6 */
+                   "[-1dvrx] [-nw] [-p <port>]",
+#endif /* DHCPv6 */
                   "[-s server]");
        log_error ("                [-cf config-file] [-lf lease-file]%s",
                   "[-pf pid-file] [-e VAR=val]");
index 744bbef12de0993e370c8235d743ecf7b95c2a50..d61e31b0c2d7df691dc3d766fa7eb5d7bd95a277 100644 (file)
@@ -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();