aboutsummaryrefslogtreecommitdiffstats
path: root/lock.c
diff options
context:
space:
mode:
authorMatthias Andree <matthias.andree@gmx.de>2020-10-13 17:21:47 +0200
committerMatthias Andree <matthias.andree@gmx.de>2020-10-13 18:31:16 +0200
commit460c55279cbe55ba56f7068ace6e7cadbb6d6df1 (patch)
treebcbb640c25636c4c0d1cc2ffaef90295976a9556 /lock.c
parent0dd0a8e86c01f375c159ac5e538b2f983a367204 (diff)
downloadfetchmail-460c55279cbe55ba56f7068ace6e7cadbb6d6df1.tar.gz
fetchmail-460c55279cbe55ba56f7068ace6e7cadbb6d6df1.tar.bz2
fetchmail-460c55279cbe55ba56f7068ace6e7cadbb6d6df1.zip
pidfile/lockfile handling bugfixes
pidfile/locking: log errors thru syslog, too This works by replacing perror()/fprintf(stderr, ...) by report() and strerror(). If the pidfile cannot be unlinked, truncate it. Bump release version to 6.4.13.rc1, and move KNOWN BUGS section up in NEWS.
Diffstat (limited to 'lock.c')
-rw-r--r--lock.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/lock.c b/lock.c
index 7f8b8667..9533e25f 100644
--- a/lock.c
+++ b/lock.c
@@ -60,7 +60,7 @@ static void unlockit(void)
/* must-do actions for exit (but we can't count on being able to do malloc) */
{
if (lockfile && lock_acquired)
- unlink(lockfile);
+ unlink(lockfile) && truncate(lockfile, (off_t)0);
}
void fm_lock_dispose(void)
@@ -84,7 +84,7 @@ int fm_lock_state(void)
bkgd = (args == 2);
if (ferror(lockfp)) {
- fprintf(stderr, GT_("fetchmail: error reading lockfile \"%s\": %s\n"),
+ report(stderr, GT_("fetchmail: error reading lockfile \"%s\": %s\n"),
lockfile, strerror(errno));
fclose(lockfp); /* not checking should be safe, file mode was "r" */
exit(PS_EXCLUDE);
@@ -95,10 +95,13 @@ int fm_lock_state(void)
/* ^ could not read PID || process does not exist */
/* => lockfile is stale, unlink it */
pid = 0;
- fprintf(stderr,GT_("fetchmail: removing stale lockfile\n"));
+ report(stderr,GT_("fetchmail: removing stale lockfile \"%s\"\n"), lockfile);
if (unlink(lockfile)) {
if (errno != ENOENT) {
- perror(lockfile);
+ if (outlevel >= O_VERBOSE) {
+ report(stderr, GT_("fetchmail: cannot unlink lockfile \"%s\" (%s), trying to write to it\n"),
+ lockfile, strerror(errno));
+ }
/* we complain but we don't exit; it might be
* writable for us, but in a directory we cannot
* write to. This means we can write the new PID to
@@ -110,7 +113,8 @@ int fm_lock_state(void)
/* but if we cannot truncate the file either,
* assume that we cannot write to it later,
* complain and quit. */
- perror(lockfile);
+ report(stderr, GT_("fetchmail: cannot write to lockfile \"%s\" either (%s), exiting\n"),
+ strerror(errno), lockfile);
exit(PS_EXCLUDE);
}
}
@@ -119,7 +123,7 @@ int fm_lock_state(void)
} else {
pid = 0;
if (errno != ENOENT) {
- fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
+ report(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
lockfile, strerror(errno));
exit(PS_EXCLUDE);
}
@@ -143,7 +147,11 @@ void fm_lock_or_die(void)
if (!lock_acquired) {
int e = 0;
- if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) {
+ fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666);
+ if (fd == -1 && EEXIST == errno) {
+ fd = open(lockfile, O_WRONLY|O_TRUNC, 0666);
+ }
+ if (-1 != fd) {
ssize_t wr;
snprintf(tmpbuf, sizeof(tmpbuf), "%ld\n", (long)getpid());
@@ -165,8 +173,7 @@ void fm_lock_or_die(void)
if (e == 0) {
lock_acquired = TRUE;
} else {
- perror(lockfile);
- fprintf(stderr, GT_("fetchmail: lock creation failed.\n"));
+ report(stderr, GT_("fetchmail: lock creation failed, pidfile \"%s\": %s\n"), lockfile, strerror(errno));
exit(PS_EXCLUDE);
}
}
@@ -175,6 +182,10 @@ void fm_lock_or_die(void)
void fm_lock_release(void)
/* release a lock on a given host */
{
- unlink(lockfile);
+ if (unlink(lockfile)) {
+ if (truncate(lockfile, (off_t)0)) {
+ report(stderr, GT_("fetchmail: cannot remove or truncate pidfile \"%s\": %s\n"), strerror(errno));
+ }
+ }
}
/* lock.c ends here */