udhcpc: stop deleting our own pidfile if we daemonize.
authorDenis Vlasenko <vda.linux@googlemail.com>
Thu, 3 May 2007 23:39:35 +0000 (23:39 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Thu, 3 May 2007 23:39:35 +0000 (23:39 -0000)
udhcp[cd]: stop using atexit magic fir pidfile removal.

networking/udhcp/common.c
networking/udhcp/dhcpc.c
networking/udhcp/dhcpd.c

index 721888f..76f8bf7 100644 (file)
@@ -24,16 +24,6 @@ long uptime(void)
        return info.uptime;
 }
 
-#if ENABLE_FEATURE_PIDFILE
-static const char *saved_pidfile;
-
-static void pidfile_delete(void)
-{
-       if (saved_pidfile)
-               remove_pidfile(saved_pidfile);
-}
-#endif
-
 static void create_pidfile(const char *pidfile)
 {
        if (!pidfile)
@@ -43,12 +33,6 @@ static void create_pidfile(const char *pidfile)
                bb_perror_msg("cannot create pidfile %s", pidfile);
                return;
        }
-#if ENABLE_FEATURE_PIDFILE
-       /* lockf(pid_fd, F_LOCK, 0); */
-       if (!saved_pidfile)
-               atexit(pidfile_delete);
-       saved_pidfile = pidfile;
-#endif
 }
 
 void udhcp_make_pidfile(const char *pidfile)
index 362e701..06806ec 100644 (file)
@@ -116,6 +116,9 @@ static void client_background(void)
 #else
 // chdir(/) is problematic. Imagine that e.g. pidfile name is RELATIVE! what will unlink do then, eh?
        bb_daemonize(DAEMON_CHDIR_ROOT);
+       /* rewrite pidfile, as our pid is different now */
+       if (client_config.pidfile)
+               write_pidfile(client_config.pidfile);
        logmode &= ~LOGMODE_STDIO;
 #endif
        client_config.foreground = 1; /* Do not fork again. */
@@ -327,7 +330,8 @@ int udhcpc_main(int argc, char **argv)
                                                client_background();
                                        } else if (client_config.abort_if_no_lease) {
                                                bb_info_msg("No lease, failing");
-                                               return 1;
+                                               retval = 1;
+                                               goto ret;
                                        }
                                        /* wait to try again */
                                        packet_num = 0;
@@ -483,7 +487,7 @@ int udhcpc_main(int argc, char **argv)
                                        if (client_config.quit_after_lease) {
                                                if (client_config.release_on_quit)
                                                        perform_release();
-                                               return 0;
+                                               goto ret0;
                                        }
                                        if (!client_config.foreground)
                                                client_background();
@@ -516,7 +520,7 @@ int udhcpc_main(int argc, char **argv)
                                bb_info_msg("Received SIGTERM");
                                if (client_config.release_on_quit)
                                        perform_release();
-                               return 0;
+                               goto ret0;
                        }
                } else if (retval == -1 && errno == EINTR) {
                        /* a signal was caught */
@@ -524,7 +528,11 @@ int udhcpc_main(int argc, char **argv)
                        /* An error occured */
                        bb_perror_msg("select");
                }
-
-       }
-       return 0;
+       } /* for (;;) */
+ ret0:
+       retval = 0;
+ ret:
+       if (client_config.pidfile)
+               remove_pidfile(client_config.pidfile);
+       return retval;
 }
index d856153..9f96e8b 100644 (file)
@@ -53,11 +53,11 @@ int udhcpd_main(int argc, char **argv)
        udhcp_make_pidfile(server_config.pidfile);
 
        option = find_option(server_config.options, DHCP_LEASE_TIME);
+       server_config.lease = LEASE_TIME;
        if (option) {
                memcpy(&server_config.lease, option->data + 2, 4);
                server_config.lease = ntohl(server_config.lease);
-       } else
-               server_config.lease = LEASE_TIME;
+       }
 
        /* Sanity check */
        num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
@@ -72,8 +72,10 @@ int udhcpd_main(int argc, char **argv)
        read_leases(server_config.lease_file);
 
        if (read_interface(server_config.interface, &server_config.ifindex,
-                          &server_config.server, server_config.arp) < 0)
-               return 1;
+                          &server_config.server, server_config.arp) < 0) {
+               retval = 1;
+               goto ret;
+       }
 
        /* Setup the signal pipe */
        udhcp_sp_setup();
@@ -82,7 +84,8 @@ int udhcpd_main(int argc, char **argv)
        while (1) { /* loop until universe collapses */
 
                if (server_socket < 0) {
-                       server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface);
+                       server_socket = listen_socket(INADDR_ANY, SERVER_PORT,
+                                       server_config.interface);
                }
 
                max_sock = udhcp_sp_fd_set(&rfds, server_socket);
@@ -90,16 +93,17 @@ int udhcpd_main(int argc, char **argv)
                        tv.tv_sec = timeout_end - time(0);
                        tv.tv_usec = 0;
                }
+               retval = 0;
                if (!server_config.auto_time || tv.tv_sec > 0) {
                        retval = select(max_sock + 1, &rfds, NULL, NULL,
                                        server_config.auto_time ? &tv : NULL);
-               } else retval = 0; /* If we already timed out, fall through */
-
+               }
                if (retval == 0) {
                        write_leases();
                        timeout_end = time(0) + server_config.auto_time;
                        continue;
-               } else if (retval < 0 && errno != EINTR) {
+               }
+               if (retval < 0 && errno != EINTR) {
                        DEBUG("error on select");
                        continue;
                }
@@ -113,7 +117,7 @@ int udhcpd_main(int argc, char **argv)
                        continue;
                case SIGTERM:
                        bb_info_msg("Received a SIGTERM");
-                       return 0;
+                       goto ret0;
                case 0: break;          /* no signal */
                default: continue;      /* signal or error (probably EINTR) */
                }
@@ -222,7 +226,8 @@ int udhcpd_main(int argc, char **argv)
                        break;
                case DHCPRELEASE:
                        DEBUG("Received RELEASE");
-                       if (lease) lease->expires = time(0);
+                       if (lease)
+                               lease->expires = time(0);
                        break;
                case DHCPINFORM:
                        DEBUG("Received INFORM");
@@ -232,6 +237,10 @@ int udhcpd_main(int argc, char **argv)
                        bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
                }
        }
-
-       return 0;
+ ret0:
+       retval = 0;
+ ret:
+       if (server_config.pidfile)
+               remove_pidfile(server_config.pidfile);
+       return retval;
 }