]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Write the pidfile before we fork so we can easily be stopped by other processes.
authorRoy Marples <roy@marples.name>
Sat, 20 Oct 2007 16:58:13 +0000 (16:58 +0000)
committerRoy Marples <roy@marples.name>
Sat, 20 Oct 2007 16:58:13 +0000 (16:58 +0000)
Makefile
client.c
common.c
common.h
dhcpcd.c
interface.h

index 9031ad2c426412644577575e99e926c40cd79f78..11f2d04a95489276b192046f638e19d946394d92 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
 # such as the need to link to libresolv and/or librt so please forgive the
 # embedded code :)
 
-VERSION = 3.1.7_pre1
+VERSION = 3.1.7_pre2
 CFLAGS += -O2 -pipe
 
 INSTALL ?= install
index 3ca1abca603df46c0cd66a2eea0f05dfb0f05cf4..7c4deb3d1ba4423a46e77f81f5eb783e3d352c49 100644 (file)
--- a/client.c
+++ b/client.c
@@ -22,7 +22,6 @@
 
 #ifdef __linux__
 # define _BSD_SOURCE
-# define _XOPEN_SOURCE 500 /* needed for pwrite */
 #endif
 
 #include <sys/types.h>
 static pid_t daemonise (int *pidfd)
 {
        pid_t pid;
-       char spid[16];
 
 #ifndef THERE_IS_NO_FORK
        logger (LOG_DEBUG, "forking to background");
@@ -166,14 +164,11 @@ static pid_t daemonise (int *pidfd)
        free (argv);
 #endif
 
+       /* Done with the fd now */
        if (pid != 0) {
-               if (ftruncate (*pidfd, 0) == -1) {
-                       logger (LOG_ERR, "ftruncate: %s", strerror (errno));
-               } else {
-                       snprintf (spid, sizeof (spid), "%u", pid);
-                       if (pwrite (*pidfd, spid, strlen (spid), 0) != (ssize_t) strlen (spid))
-                               logger (LOG_ERR, "pwrite: %s", strerror (errno));
-               }
+               writepid (*pidfd, pid);
+               close (*pidfd);
+               *pidfd = -1;
        }
 
        return (pid);
@@ -437,12 +432,12 @@ int dhcp_run (const options_t *options, int *pidfd)
                        switch (sig) {
                                case SIGINT:
                                        logger (LOG_INFO, "received SIGINT, stopping");
-                                       retval = (! daemonised);
+                                       retval = daemonised ? EXIT_SUCCESS : EXIT_FAILURE;
                                        goto eexit;
 
                                case SIGTERM:
                                        logger (LOG_INFO, "received SIGTERM, stopping");
-                                       retval = (! daemonised);
+                                       retval = daemonised ? EXIT_SUCCESS : EXIT_FAILURE;
                                        goto eexit;
 
                                case SIGALRM:
@@ -480,7 +475,7 @@ int dhcp_run (const options_t *options, int *pidfd)
                                        else
                                                logger (LOG_ERR,
                                                                "received SIGHUP, but we no have lease to release");
-                                       retval = 0;
+                                       retval = daemonised ? EXIT_SUCCESS : EXIT_FAILURE;
                                        goto eexit;
 
                                default:
@@ -891,14 +886,8 @@ eexit:
                free_route (iface->previous_routes);
                free (iface);
        }
-
        free (buffer);
 
-       if (*pidfd != -1) {
-               close (*pidfd);
-               *pidfd = -1;
-       }
-
        if (daemonised)
                unlink (options->pidfile);
 
index 517ab677597c486460395488f47810a7d8a9faea..1fb5018723558f7d0def081c87474e475873ce4a 100644 (file)
--- a/common.c
+++ b/common.c
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#ifdef __linux__
+# define _XOPEN_SOURCE 500 /* needed for pwrite */
+#endif
+
 #include <sys/time.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -131,6 +135,18 @@ time_t uptime (void)
        return (tp.tv_sec);
 }
 
+void writepid (int fd, pid_t pid)
+{
+       char spid[16];
+       if (ftruncate (fd, 0) == -1) {
+               logger (LOG_ERR, "ftruncate: %s", strerror (errno));
+       } else {
+               snprintf (spid, sizeof (spid), "%u", pid);
+               if (pwrite (fd, spid, strlen (spid), 0) != (ssize_t) strlen (spid))
+                       logger (LOG_ERR, "pwrite: %s", strerror (errno));
+       }
+}
+
 void *xmalloc (size_t s)
 {
        void *value = malloc (s);
index 1c8170874d719514d3ae62b25c54c6039b01ef81..87bce782cbc1001f07ab467f137a5f22b356fbdd 100644 (file)
--- a/common.h
+++ b/common.h
@@ -40,6 +40,7 @@ void srandomdev (void);
 void close_fds (void);
 int get_time (struct timeval *tp);
 time_t uptime (void);
+void writepid (int fd, pid_t pid);
 void *xmalloc (size_t size);
 char *xstrdup (const char *str);
 
index 55641b2af5757e0b137c0d075e25f4d077c177ce..ac4de0848c7ce0e90a8bb3e10d966732f778e041 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -531,6 +531,7 @@ int main(int argc, char **argv)
                        fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1)
                        logger (LOG_ERR, "fcntl: %s", strerror (errno));
 
+               writepid (pidfd, getpid ());
                logger (LOG_INFO, PACKAGE " " VERSION " starting");
        }
 
@@ -541,6 +542,12 @@ int main(int argc, char **argv)
        if (dhcp_run (options, &pidfd) == 0)
                i = EXIT_SUCCESS;
 
+       /* If we didn't daemonise then we need to punt the pidfile now */
+       if (pidfd > -1) {
+               close (pidfd);
+               unlink (options->pidfile);
+       }
+
        free (options);
 
 #ifdef THERE_IS_NO_FORK
@@ -551,8 +558,5 @@ int main(int argc, char **argv)
 
        logger (LOG_INFO, "exiting");
        
-       if (pidfd > -1)
-               close (pidfd);
-
        exit (i);
 }
index 47dfe539bf2ef33db2bb8023d0b08053fb40a150..ac682eb474d31bee4775f7dc99cd390ec805a519 100644 (file)
@@ -72,7 +72,6 @@
 # define IN_LINKLOCAL(addr) ((ntohl (addr) & IN_CLASSB_NET) == LINKLOCAL_ADDR)
 #endif
 
-
 typedef struct route_t
 {
        struct in_addr destination;