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