]> Pileus Git - ~andy/fetchmail/blob - lock.c
Support lockfiles in non-writable directories.
[~andy/fetchmail] / lock.c
1 /**
2  * \file lock.c cross-platform concurrency locking for fetchmail
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6 #include "config.h"
7
8 #include <stdio.h>
9 #ifdef HAVE_STRING_H
10 #include <string.h> /* strcat() */
11 #endif
12 #if defined(STDC_HEADERS)
13 #include <stdlib.h>
14 #endif
15 #if defined(HAVE_UNISTD_H)
16 #include <unistd.h>
17 #endif
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <signal.h>
21
22 #include "fetchmail.h"
23 #include "i18n.h"
24 #include "lock.h"
25
26 static char *lockfile;          /** name of lockfile */
27 static int lock_acquired;       /** flag if have we acquired a lock */
28
29 void fm_lock_setup(struct runctl *ctl)
30 /* set up the global lockfile name */
31 {
32     /* set up to do lock protocol */
33     const char *const FETCHMAIL_PIDFILE="fetchmail.pid";
34
35     /* command-line option override */
36     if (ctl->pidfile) {
37         lockfile = xstrdup(ctl->pidfile);
38         return;
39     }
40
41     /* defaults */
42     if (getuid() == ROOT_UID) {
43         lockfile = (char *)xmalloc(strlen(PID_DIR)
44                 + strlen(FETCHMAIL_PIDFILE) + 2); /* 2: "/" and NUL */
45         strcpy(lockfile, PID_DIR);
46         strcat(lockfile, "/");
47         strcat(lockfile, FETCHMAIL_PIDFILE);
48     } else {
49         lockfile = (char *)xmalloc(strlen(fmhome)
50                 + strlen(FETCHMAIL_PIDFILE) + 3); /* 3: "/", "." and NUL */
51         strcpy(lockfile, fmhome);
52         strcat(lockfile, "/");
53         if (fmhome == home)
54            strcat(lockfile, ".");
55         strcat(lockfile, FETCHMAIL_PIDFILE);
56     }
57 }
58
59 static void unlockit(void)
60 /* must-do actions for exit (but we can't count on being able to do malloc) */
61 {
62     if (lockfile && lock_acquired)
63         unlink(lockfile);
64 }
65
66 void fm_lock_dispose(void)
67 /* arrange for a lock to be removed on process exit */
68 {
69 #ifdef HAVE_ATEXIT
70     atexit(unlockit);
71 #endif
72 }
73
74 int fm_lock_state(void)
75 {
76     int         pid, st;
77     FILE        *lockfp;
78     int         bkgd = FALSE;
79
80     if ((lockfp = fopen(lockfile, "r")) != NULL)
81     {
82         int args = fscanf(lockfp, "%d %d", &pid, &st);
83         bkgd = (args == 2);
84
85         if (ferror(lockfp)) {
86             fprintf(stderr, GT_("fetchmail: error reading lockfile \"%s\": %s\n"),
87                     lockfile, strerror(errno));
88             fclose(lockfp); /* not checking should be safe, file mode was "r" */
89             exit(PS_EXCLUDE);
90         }
91         fclose(lockfp); /* not checking should be safe, file mode was "r" */
92
93         if (args == EOF || args == 0 || kill(pid, 0) == -1) {
94             /* ^ could not read PID  || process does not exist */
95             /* => lockfile is stale, unlink it */
96             pid = 0;
97             fprintf(stderr,GT_("fetchmail: removing stale lockfile\n"));
98             if (unlink(lockfile)) {
99                if (errno != ENOENT) {
100                    perror(lockfile);
101                    /* we complain but we don't exit; it might be
102                     * writable for us, but in a directory we cannot
103                     * write to. This means we can write the new PID to
104                     * the file. Truncate to be safe in case the PID is
105                     * recycled by another process later.
106                     * \bug we should use fcntl() style locks or
107                     * something else instead in a future release. */
108                    if (truncate(lockfile, (off_t)0)) {
109                        /* but if we cannot truncate the file either,
110                         * assume that we cannot write to it later,
111                         * complain and quit. */
112                        perror(lockfile);
113                        exit(PS_EXCLUDE);
114                    }
115                }
116             }
117         }
118     } else {
119         pid = 0;
120         if (errno != ENOENT) {
121             fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
122                     lockfile, strerror(errno));
123             exit(PS_EXCLUDE);
124         }
125     }
126
127     return(bkgd ? -pid : pid);
128 }
129
130 void fm_lock_assert(void)
131 /* assert that we already possess a lock */
132 {
133     lock_acquired = TRUE;
134 }
135
136 void fm_lock_or_die(void)
137 /* get a lock on a given host or exit */
138 {
139     int fd;
140     char        tmpbuf[50];
141
142     if (!lock_acquired) {
143         int e = 0;
144
145         if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) {
146             ssize_t wr;
147
148             snprintf(tmpbuf, sizeof(tmpbuf), "%ld\n", (long)getpid());
149             wr = write(fd, tmpbuf, strlen(tmpbuf));
150             if (wr == -1 || (size_t)wr != strlen(tmpbuf))
151                 e = 1;
152             if (run.poll_interval)
153             {
154                 snprintf(tmpbuf, sizeof(tmpbuf), "%d\n", run.poll_interval);
155                 wr = write(fd, tmpbuf, strlen(tmpbuf));
156                 if (wr == -1 || (size_t)wr != strlen(tmpbuf))
157                     e = 1;
158             }
159             if (fsync(fd)) e = 1;
160             if (close(fd)) e = 1;
161         } else {
162             e = 1;
163         }
164         if (e == 0) {
165             lock_acquired = TRUE;
166         } else {
167             perror(lockfile);
168             fprintf(stderr, GT_("fetchmail: lock creation failed.\n"));
169             exit(PS_EXCLUDE);
170         }
171     }
172 }
173
174 void fm_lock_release(void)
175 /* release a lock on a given host */
176 {
177     unlink(lockfile);
178 }
179 /* lock.c ends here */