diff options
author | Michael K. Johnson <johnsonm@redhat.com> | 1999-09-28 17:25:51 +0000 |
---|---|---|
committer | Michael K. Johnson <johnsonm@redhat.com> | 1999-09-28 17:25:51 +0000 |
commit | 167594b1a3c84074c8964daf1a973db61dbf3f0d (patch) | |
tree | afe6ba06ca83d51046621ae93be3f1697da7cc2c /src/ppp-watch.c | |
parent | 657e862839bed6c32d85c9e9117ece3bc34af27b (diff) | |
download | initscripts-167594b1a3c84074c8964daf1a973db61dbf3f0d.tar initscripts-167594b1a3c84074c8964daf1a973db61dbf3f0d.tar.gz initscripts-167594b1a3c84074c8964daf1a973db61dbf3f0d.tar.bz2 initscripts-167594b1a3c84074c8964daf1a973db61dbf3f0d.tar.xz initscripts-167594b1a3c84074c8964daf1a973db61dbf3f0d.zip |
proper file locking
Diffstat (limited to 'src/ppp-watch.c')
-rw-r--r-- | src/ppp-watch.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/ppp-watch.c b/src/ppp-watch.c index fefbc8b9..043b8612 100644 --- a/src/ppp-watch.c +++ b/src/ppp-watch.c @@ -175,6 +175,7 @@ doPidFile(char *device) { static char *pidFileName = NULL; char *pidFilePath; int fd; FILE *f; + int pid = 0; if (pidFileName) { /* remove it */ @@ -188,8 +189,25 @@ doPidFile(char *device) { pidFileName = device; pidFilePath = alloca(strlen(pidFileName) + 25); sprintf(pidFilePath, "/var/run/pppwatch-%s.pid", pidFileName); - fd = open(pidFilePath, O_WRONLY|O_TRUNC|O_CREAT, + 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); + if (fd == -1) + cleanExit(36); /* terrible things have happened */ + /* already running, send a SIGHUP (we presume that they + * are calling ifup for a reason, so they probably want + * to redial) and then exit cleanly and let things go + * on in the background + */ + f = fdopen(fd, "r"); + if (!f) cleanExit(37); + if (fscanf(f, "%d", &pid) && pid) + kill(pid, SIGHUP); + fclose(f); + cleanExit(0); + } f = fdopen(fd, "w"); if (f) { fprintf(f, "%d\n", getpid()); @@ -478,7 +496,7 @@ main(int argc, char **argv) { if (physicalDevice) { free(physicalDevice); physicalDevice = NULL; } physicalDevice = pppLogicalToPhysical(&pppdPid, device); if (physicalDevice) { free(physicalDevice); physicalDevice = NULL; } - if (!pppdPid) cleanExit(34); + if (!pppdPid) cleanExit(35); kill(pppdPid, sendsig); if (sendsig == SIGKILL) { kill(-pppdPid, SIGTERM); /* give it a chance to die nicely */ |