From 2e80e13e826b7ac2547352a46d95fd67d2317041 Mon Sep 17 00:00:00 2001 From: "Michael K. Johnson" Date: Tue, 28 Sep 1999 19:00:51 +0000 Subject: really fix pidfile locking... --- src/ppp-watch.c | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) (limited to 'src/ppp-watch.c') diff --git a/src/ppp-watch.c b/src/ppp-watch.c index 043b8612..a8345d14 100644 --- a/src/ppp-watch.c +++ b/src/ppp-watch.c @@ -124,6 +124,8 @@ detach(int now, int parentExitCode, char *device) { switch (exitCode) { case 0: break; + case 33: + fprintf(stderr, "%s already up, initiating redial\n", device); case 34: fprintf(stderr, "Failed to activate %s, retrying in the background\n", device); break; @@ -148,34 +150,12 @@ detach(int now, int parentExitCode, char *device) { -static void -checkPidFile(char *device) { - char *pidFilePath; - FILE *f; - pid_t previous; - int killed; - - pidFilePath = alloca(strlen(device) + 25); - sprintf(pidFilePath, "/var/run/pppwatch-%s.pid", device); - f = fopen(pidFilePath, "r"); - if (f) { - fscanf(f, "%d\n", &previous); - fclose(f); - - killed = kill(previous, SIGHUP); - if (!killed) { - fprintf(stderr, "%s already up, initiating redial\n", device); - exit(33); - } - } -} - static void doPidFile(char *device) { static char *pidFileName = NULL; char *pidFilePath; int fd; FILE *f; - int pid = 0; + pid_t pid = 0; if (pidFileName) { /* remove it */ @@ -191,6 +171,7 @@ doPidFile(char *device) { sprintf(pidFilePath, "/var/run/pppwatch-%s.pid", pidFileName); fd = open(pidFilePath, O_WRONLY|O_TRUNC|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR | S_IRGRP | S_IROTH); + if (fd == -1) { /* file already existed, or terrible things have happened... */ fd = open(pidFilePath, O_RDONLY); @@ -206,15 +187,14 @@ doPidFile(char *device) { if (fscanf(f, "%d", &pid) && pid) kill(pid, SIGHUP); fclose(f); - cleanExit(0); + cleanExit(33); } + f = fdopen(fd, "w"); - if (f) { - fprintf(f, "%d\n", getpid()); - fclose(f); - } else { + if (!f) cleanExit(31); - } + fprintf(f, "%d\n", getpid()); + fclose(f); } } @@ -427,8 +407,6 @@ main(int argc, char **argv) { device = argv[1]; } - checkPidFile(device); - detach(0, 0, device); /* prepare */ if (argc > 2 && !strcmp("boot", argv[2])) { -- cgit v1.2.1