]> Pileus Git - ~andy/fetchmail/blob - lock.c
Fix typo repsonsible -> responsible.
[~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 #include <string.h> /* strcat() */
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <signal.h>
15
16 #include "fetchmail.h"
17 #include "gettext.h"
18 #include "lock.h"
19
20 static char *lockfile;          /** name of lockfile */
21 static int lock_acquired;       /** flag if have we acquired a lock */
22
23 void fm_lock_setup(struct runctl *ctl)
24 /* set up the global lockfile name */
25 {
26     /* set up to do lock protocol */
27     const char *const FETCHMAIL_PIDFILE="fetchmail.pid";
28
29     /* command-line option override */
30     if (ctl->pidfile) {
31         lockfile = xstrdup(ctl->pidfile);
32         return;
33     }
34
35     /* defaults */
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);
42     } else {
43         lockfile = (char *)xmalloc(strlen(fmhome)
44                 + strlen(FETCHMAIL_PIDFILE) + 3); /* 3: "/", "." and NUL */
45         strcpy(lockfile, fmhome);
46         strcat(lockfile, "/");
47         if (fmhome == home)
48            strcat(lockfile, ".");
49         strcat(lockfile, FETCHMAIL_PIDFILE);
50     }
51 }
52
53 static void unlockit(void)
54 /* must-do actions for exit (but we can't count on being able to do malloc) */
55 {
56     if (lockfile && lock_acquired)
57         unlink(lockfile);
58 }
59
60 void fm_lock_dispose(void)
61 /* arrange for a lock to be removed on process exit */
62 {
63     atexit(unlockit);
64 }
65
66 int fm_lock_state(void)
67 {
68     long        pid;
69     int         st;
70     FILE        *lockfp;
71     int         bkgd = FALSE;
72
73     if ((lockfp = fopen(lockfile, "r")) != NULL)
74     {
75         int args = fscanf(lockfp, "%ld %d", &pid, &st);
76         bkgd = (args == 2);
77
78         if (ferror(lockfp)) {
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" */
82             exit(PS_EXCLUDE);
83         }
84         fclose(lockfp); /* not checking should be safe, file mode was "r" */
85
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 */
89             pid = 0;
90             fprintf(stderr,GT_("fetchmail: removing stale lockfile\n"));
91             if (unlink(lockfile)) {
92                if (errno != ENOENT) {
93                    perror(lockfile);
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. */
105                        perror(lockfile);
106                        exit(PS_EXCLUDE);
107                    }
108                }
109             }
110         }
111     } else {
112         pid = 0;
113         if (errno != ENOENT) {
114             fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
115                     lockfile, strerror(errno));
116             exit(PS_EXCLUDE);
117         }
118     }
119
120     return(bkgd ? -pid : pid);
121 }
122
123 void fm_lock_assert(void)
124 /* assert that we already possess a lock */
125 {
126     lock_acquired = TRUE;
127 }
128
129 void fm_lock_or_die(void)
130 /* get a lock on a given host or exit */
131 {
132     int fd;
133     char        tmpbuf[50];
134
135     if (!lock_acquired) {
136         int e = 0;
137
138         if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) {
139             ssize_t wr;
140
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))
144                 e = 1;
145             if (run.poll_interval)
146             {
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))
150                     e = 1;
151             }
152             if (fsync(fd)) e = 1;
153             if (close(fd)) e = 1;
154         } else {
155             e = 1;
156         }
157         if (e == 0) {
158             lock_acquired = TRUE;
159         } else {
160             perror(lockfile);
161             fprintf(stderr, GT_("fetchmail: lock creation failed.\n"));
162             exit(PS_EXCLUDE);
163         }
164     }
165 }
166
167 void fm_lock_release(void)
168 /* release a lock on a given host */
169 {
170     unlink(lockfile);
171 }
172 /* lock.c ends here */