]> Pileus Git - ~andy/fetchmail/blob - lock.c
If the lockfile ends before the process ID, treat it as stale and unlink it.
[~andy/fetchmail] / lock.c
1 /*
2  * 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;       /* 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             pid = 0;
95
96             fprintf(stderr,GT_("fetchmail: removing stale lockfile\n"));
97             if (unlink(lockfile)) {
98                if (errno != ENOENT) {
99                    perror(lockfile);
100                }
101             }
102         }
103     } else {
104         pid = 0;
105         if (errno != ENOENT) {
106             fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
107                     lockfile, strerror(errno));
108             exit(PS_EXCLUDE);
109         }
110     }
111
112     return(bkgd ? -pid : pid);
113 }
114
115 void fm_lock_assert(void)
116 /* assert that we already posess a lock */
117 {
118     lock_acquired = TRUE;
119 }
120
121 void fm_lock_or_die(void)
122 /* get a lock on a given host or exit */
123 {
124     int fd;
125     char        tmpbuf[50];
126
127     if (!lock_acquired) {
128         int e = 0;
129
130         if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) {
131             ssize_t wr;
132
133             snprintf(tmpbuf, sizeof(tmpbuf), "%ld\n", (long)getpid());
134             wr = write(fd, tmpbuf, strlen(tmpbuf));
135             if (wr == -1 || (size_t)wr != strlen(tmpbuf))
136                 e = 1;
137             if (run.poll_interval)
138             {
139                 snprintf(tmpbuf, sizeof(tmpbuf), "%d\n", run.poll_interval);
140                 wr = write(fd, tmpbuf, strlen(tmpbuf));
141                 if (wr == -1 || (size_t)wr != strlen(tmpbuf))
142                     e = 1;
143             }
144             if (fsync(fd)) e = 1;
145             if (close(fd)) e = 1;
146         } else {
147             e = 1;
148         }
149         if (e == 0) {
150             lock_acquired = TRUE;
151         } else {
152             perror(lockfile);
153             fprintf(stderr, GT_("fetchmail: lock creation failed.\n"));
154             exit(PS_EXCLUDE);
155         }
156     }
157 }
158
159 void fm_lock_release(void)
160 /* release a lock on a given host */
161 {
162     unlink(lockfile);
163 }
164 /* lock.c ends here */