From 534a84d166e0733a30e6f3701505797541d78532 Mon Sep 17 00:00:00 2001 From: Matthias Andree Date: Sat, 23 Jul 2005 15:05:09 +0000 Subject: Fix Debian bug #230615, making PID file format FHS 2.3 compliant. Add more error checking to writing/reading PID file. svn path=/trunk/; revision=4167 --- NEWS | 4 +++- lock.c | 69 +++++++++++++++++++++++++++++++++++++----------------------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/NEWS b/NEWS index 65777b2f..40e09a51 100644 --- a/NEWS +++ b/NEWS @@ -131,7 +131,9 @@ OTHER CHANGES: * Really fix #207919, patch by Tobias Diedrich. The 6.2.5 NEWS claimed Gregan's patch had fixed #207919 but it had fixed #212484 instead and #207919 remained unfixed in 6.2.5. The entry - below has been corrected to read #212484 now. + below has been corrected to read #212484 now. (MA) +* When writing the PID file, write a FHS 2.3 compliant PID file. + Fixes Debian bug #230615. Matthias Andree. fetchmail-6.2.5 (Wed Oct 15 18:39:22 EDT 2003), 23079 lines: diff --git a/lock.c b/lock.c index d3419dd7..82d57af9 100644 --- a/lock.c +++ b/lock.c @@ -15,6 +15,7 @@ #if defined(HAVE_UNISTD_H) #include #endif +#include #include #include @@ -70,21 +71,31 @@ void lock_dispose(void) int lock_state(void) { - int pid, st; + int pid, st; FILE *lockfp; int bkgd = FALSE; - pid = 0; - if ((lockfp = fopen(lockfile, "r")) != NULL ) + if ((lockfp = fopen(lockfile, "r")) != NULL) { - bkgd = (fscanf(lockfp, "%d %d", &pid, &st) == 2); + int args = fscanf(lockfp, "%d %d", &pid, &st); + bkgd = (args == 2); - if (pid == 0 || kill(pid, 0) == -1) { + if (ferror(lockfp)) + fprintf(stderr, GT_("fetchmail: error reading lockfile \"%s\": %s\n"), + lockfile, strerror(errno)); + + if (args == 0 || kill(pid, 0) == -1) { fprintf(stderr,GT_("fetchmail: removing stale lockfile\n")); pid = 0; - unlink(lockfile); + if (unlink(lockfile)) + perror(lockfile); } - fclose(lockfp); /* not checking should be safe, file mode was "r" */ + fclose(lockfp); /* not checking should be safe, file mode was "r" */ + } else { + pid = 0; + if (errno != ENOENT) + fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"), + lockfile, strerror(errno)); } return(bkgd ? -pid : pid); @@ -102,28 +113,27 @@ void lock_or_die(void) int fd; char tmpbuf[50]; -#ifndef O_SYNC -#define O_SYNC 0 /* use it if we have it */ -#endif - if (!lock_acquired) - { - if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_SYNC, 0666)) != -1) - { - snprintf(tmpbuf, sizeof(tmpbuf), "%ld", (long)getpid()); - write(fd, tmpbuf, strlen(tmpbuf)); - if (run.poll_interval) - { - snprintf(tmpbuf, sizeof(tmpbuf), " %d", run.poll_interval); - write(fd, tmpbuf, strlen(tmpbuf)); - } - close(fd); /* should be safe, fd was opened with O_SYNC */ - lock_acquired = TRUE; - } - else - { - fprintf(stderr, GT_("fetchmail: lock creation failed.\n")); - exit(PS_EXCLUDE); - } + if (!lock_acquired) { + int e = 0; + + if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) { + snprintf(tmpbuf, sizeof(tmpbuf), "%ld\n", (long)getpid()); + if (write(fd, tmpbuf, strlen(tmpbuf)) < strlen(tmpbuf)) e = 1; + if (run.poll_interval) + { + snprintf(tmpbuf, sizeof(tmpbuf), "%d\n", run.poll_interval); + if (write(fd, tmpbuf, strlen(tmpbuf)) < strlen(tmpbuf)) e = 1; + } + if (fsync(fd)) e = 1; + if (close(fd)) e = 1; + } + if (e == 0) { + lock_acquired = TRUE; + } else { + perror(lockfile); + fprintf(stderr, GT_("fetchmail: lock creation failed.\n")); + exit(PS_EXCLUDE); + } } } @@ -132,5 +142,4 @@ void lock_release(void) { unlink(lockfile); } - /* lock.c ends here */ -- cgit v1.2.3