2 * \file lock.c cross-platform concurrency locking for fetchmail
4 * For license terms, see the file COPYING in this directory.
9 #include <string.h> /* strcat() */
16 #include "fetchmail.h"
20 static char *lockfile; /** name of lockfile */
21 static int lock_acquired; /** flag if have we acquired a lock */
23 void fm_lock_setup(struct runctl *ctl)
24 /* set up the global lockfile name */
26 /* set up to do lock protocol */
27 const char *const FETCHMAIL_PIDFILE="fetchmail.pid";
29 /* command-line option override */
31 lockfile = xstrdup(ctl->pidfile);
36 if (getuid() == ROOT_UID) {
37 lockfile = (char *)xmalloc(strlen(PID_DIR)
38 + strlen(FETCHMAIL_PIDFILE) + 2); /* 2: "/" and NUL */
39 strcpy(lockfile, PID_DIR);
40 strcat(lockfile, "/");
41 strcat(lockfile, FETCHMAIL_PIDFILE);
43 lockfile = (char *)xmalloc(strlen(fmhome)
44 + strlen(FETCHMAIL_PIDFILE) + 3); /* 3: "/", "." and NUL */
45 strcpy(lockfile, fmhome);
46 strcat(lockfile, "/");
48 strcat(lockfile, ".");
49 strcat(lockfile, FETCHMAIL_PIDFILE);
53 static void unlockit(void)
54 /* must-do actions for exit (but we can't count on being able to do malloc) */
56 if (lockfile && lock_acquired)
60 void fm_lock_dispose(void)
61 /* arrange for a lock to be removed on process exit */
66 int fm_lock_state(void)
73 if ((lockfp = fopen(lockfile, "r")) != NULL)
75 int args = fscanf(lockfp, "%ld %d", &pid, &st);
79 fprintf(stderr, GT_("fetchmail: error reading lockfile \"%s\": %s\n"),
80 lockfile, strerror(errno));
81 fclose(lockfp); /* not checking should be safe, file mode was "r" */
84 fclose(lockfp); /* not checking should be safe, file mode was "r" */
86 if (args == EOF || args == 0 || kill(pid, 0) == -1) {
87 /* ^ could not read PID || process does not exist */
88 /* => lockfile is stale, unlink it */
90 fprintf(stderr,GT_("fetchmail: removing stale lockfile\n"));
91 if (unlink(lockfile)) {
92 if (errno != ENOENT) {
94 /* we complain but we don't exit; it might be
95 * writable for us, but in a directory we cannot
96 * write to. This means we can write the new PID to
97 * the file. Truncate to be safe in case the PID is
98 * recycled by another process later.
99 * \bug we should use fcntl() style locks or
100 * something else instead in a future release. */
101 if (truncate(lockfile, (off_t)0)) {
102 /* but if we cannot truncate the file either,
103 * assume that we cannot write to it later,
104 * complain and quit. */
113 if (errno != ENOENT) {
114 fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
115 lockfile, strerror(errno));
120 return(bkgd ? -pid : pid);
123 void fm_lock_assert(void)
124 /* assert that we already possess a lock */
126 lock_acquired = TRUE;
129 void fm_lock_or_die(void)
130 /* get a lock on a given host or exit */
135 if (!lock_acquired) {
138 if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) {
141 snprintf(tmpbuf, sizeof(tmpbuf), "%ld\n", (long)getpid());
142 wr = write(fd, tmpbuf, strlen(tmpbuf));
143 if (wr == -1 || (size_t)wr != strlen(tmpbuf))
145 if (run.poll_interval)
147 snprintf(tmpbuf, sizeof(tmpbuf), "%d\n", run.poll_interval);
148 wr = write(fd, tmpbuf, strlen(tmpbuf));
149 if (wr == -1 || (size_t)wr != strlen(tmpbuf))
152 if (fsync(fd)) e = 1;
153 if (close(fd)) e = 1;
158 lock_acquired = TRUE;
161 fprintf(stderr, GT_("fetchmail: lock creation failed.\n"));
167 void fm_lock_release(void)
168 /* release a lock on a given host */
172 /* lock.c ends here */