From: Roy Marples Date: Sat, 20 Oct 2007 16:58:13 +0000 (+0000) Subject: Write the pidfile before we fork so we can easily be stopped by other processes. X-Git-Tag: v3.2.3~181 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cfbd159f4963a70033562a1ac16c7d0d932497ab;p=thirdparty%2Fdhcpcd.git Write the pidfile before we fork so we can easily be stopped by other processes. --- diff --git a/Makefile b/Makefile index 9031ad2c..11f2d04a 100644 --- 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 diff --git a/client.c b/client.c index 3ca1abca..7c4deb3d 100644 --- a/client.c +++ b/client.c @@ -22,7 +22,6 @@ #ifdef __linux__ # define _BSD_SOURCE -# define _XOPEN_SOURCE 500 /* needed for pwrite */ #endif #include @@ -125,7 +124,6 @@ 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); diff --git a/common.c b/common.c index 517ab677..1fb50187 100644 --- a/common.c +++ b/common.c @@ -19,6 +19,10 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#ifdef __linux__ +# define _XOPEN_SOURCE 500 /* needed for pwrite */ +#endif + #include #include #include @@ -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); diff --git a/common.h b/common.h index 1c817087..87bce782 100644 --- 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); diff --git a/dhcpcd.c b/dhcpcd.c index 55641b2a..ac4de084 100644 --- 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); } diff --git a/interface.h b/interface.h index 47dfe539..ac682eb4 100644 --- a/interface.h +++ b/interface.h @@ -72,7 +72,6 @@ # define IN_LINKLOCAL(addr) ((ntohl (addr) & IN_CLASSB_NET) == LINKLOCAL_ADDR) #endif - typedef struct route_t { struct in_addr destination;