aboutsummaryrefslogtreecommitdiffstats
path: root/lock.c
diff options
context:
space:
mode:
Diffstat (limited to 'lock.c')
-rw-r--r--lock.c69
1 files changed, 39 insertions, 30 deletions
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 <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 */