Commit 534a84d1 authored by Matthias Andree's avatar Matthias Andree

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
parent 56d52162
......@@ -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:
......
......@@ -15,6 +15,7 @@
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
......@@ -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 */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment