summaryrefslogtreecommitdiffstats
path: root/dhcpcd.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2007-05-13 12:28:54 +0000
committerRoy Marples <roy@marples.name>2007-05-13 12:28:54 +0000
commit438263e0571dc945a5bf85445e4904c0f853cce6 (patch)
tree868773ee7ffe3dd25f17d2a2b247ebec4b0c0d5a /dhcpcd.c
parent9afd5b02383bcf1f6ed2b12ff4016e5f3720d17f (diff)
downloaddhcpcd-438263e0571dc945a5bf85445e4904c0f853cce6.tar.xz
flock the pidfile and stop writing the parent pid to it so we're more robst.
Diffstat (limited to 'dhcpcd.c')
-rw-r--r--dhcpcd.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/dhcpcd.c b/dhcpcd.c
index 5f264413..09e4b43e 100644
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -24,6 +24,7 @@
#define _POSIX_SOURCE
#endif
+#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
@@ -76,19 +77,6 @@ static pid_t read_pid(const char *pidfile)
return pid;
}
-void make_pid (const char *pidfile)
-{
- FILE *fp;
-
- if ((fp = fopen (pidfile, "w")) == NULL) {
- logger (LOG_ERR, "fopen `%s': %s", pidfile, strerror (errno));
- return;
- }
-
- fprintf (fp, "%u\n", getpid ());
- fclose (fp);
-}
-
static void usage ()
{
printf ("usage: "PACKAGE" [-adknpEGHMNRSY] [-c script] [-h hostame] [-i classID]\n"
@@ -108,6 +96,7 @@ int main(int argc, char **argv)
pid_t pid;
int debug = 0;
int i;
+ int pidfd;
const struct option longopts[] = {
{"arp", no_argument, NULL, 'a'},
@@ -404,10 +393,23 @@ int main(int argc, char **argv)
exit (EXIT_FAILURE);
}
- make_pid (options.pidfile);
+ pidfd = open (options.pidfile,
+ O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0660);
+ if (pidfd == -1) {
+ logger (LOG_ERR, "open `%s': %s", options.pidfile, strerror (errno));
+ exit (EXIT_FAILURE);
+ }
+
+ /* Lock the file so that only one instance of dhcpcd runs on an interface */
+ if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) {
+ logger (LOG_ERR, "flock `%s': %s", options.pidfile, strerror (errno));
+ exit (EXIT_FAILURE);
+ }
logger (LOG_INFO, PACKAGE " " VERSION " starting");
- if (dhcp_run (&options)) {
+ if (dhcp_run (&options, &pidfd)) {
+ if (pidfd > -1)
+ close (pidfd);
unlink (options.pidfile);
exit (EXIT_FAILURE);
}