]> Pileus Git - ~andy/fetchmail/blob - lock.c
Only report 'removing stale lockfile' if it was actually removed.
[~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
25 static char *lockfile;          /* name of lockfile */
26 static int lock_acquired;       /* have we acquired a lock */
27
28 void lock_setup(void)
29 /* set up the global lockfile name */
30 {
31     /* set up to do lock protocol */
32 #define FETCHMAIL_PIDFILE       "fetchmail.pid"
33     if (getuid() == ROOT_UID) {
34         lockfile = (char *)xmalloc(
35                 sizeof(PID_DIR) + sizeof(FETCHMAIL_PIDFILE) + 1);
36         strcpy(lockfile, PID_DIR);
37         strcat(lockfile, "/");
38         strcat(lockfile, FETCHMAIL_PIDFILE);
39     } else {
40         lockfile = (char *)xmalloc(strlen(fmhome)+sizeof(FETCHMAIL_PIDFILE)+2);
41         strcpy(lockfile, fmhome);
42         strcat(lockfile, "/");
43         if (fmhome == home)
44            strcat(lockfile, ".");
45         strcat(lockfile, FETCHMAIL_PIDFILE);
46     }
47 #undef FETCHMAIL_PIDFILE
48 }
49
50 static void unlockit(void)
51 /* must-do actions for exit (but we can't count on being able to do malloc) */
52 {
53     if (lockfile && lock_acquired)
54         unlink(lockfile);
55 }
56
57 void lock_dispose(void)
58 /* arrange for a lock to be removed on process exit */
59 {
60 #ifdef HAVE_ATEXIT
61     atexit(unlockit);
62 #endif
63 }
64
65 int lock_state(void)
66 {
67     int         pid, st;
68     FILE        *lockfp;
69     int         bkgd = FALSE;
70
71     if ((lockfp = fopen(lockfile, "r")) != NULL)
72     {
73         int args = fscanf(lockfp, "%d %d", &pid, &st);
74         bkgd = (args == 2);
75
76         if (ferror(lockfp)) {
77             fprintf(stderr, GT_("fetchmail: error reading lockfile \"%s\": %s\n"),
78                     lockfile, strerror(errno));
79             exit(PS_EXCLUDE);
80         }
81
82         if (args == 0 || kill(pid, 0) == -1) {
83             pid = 0;
84             if (unlink(lockfile)) {
85                if (errno != ENOENT) {
86                    perror(lockfile);
87                }
88             } else {
89                 fprintf(stderr,GT_("fetchmail: removing stale lockfile\n"));
90             }
91         }
92         fclose(lockfp); /* not checking should be safe, file mode was "r" */
93     } else {
94         pid = 0;
95         if (errno != ENOENT) {
96             fprintf(stderr, GT_("fetchmail: error opening lockfile \"%s\": %s\n"),
97                     lockfile, strerror(errno));
98             exit(PS_EXCLUDE);
99         }
100     }
101
102     return(bkgd ? -pid : pid);
103 }
104
105 void lock_assert(void)
106 /* assert that we already posess a lock */
107 {
108     lock_acquired = TRUE;
109 }
110
111 void lock_or_die(void)
112 /* get a lock on a given host or exit */
113 {
114     int fd;
115     char        tmpbuf[50];
116
117     if (!lock_acquired) {
118         int e = 0;
119
120         if ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) != -1) {
121             snprintf(tmpbuf, sizeof(tmpbuf), "%ld\n", (long)getpid());
122             if (write(fd, tmpbuf, strlen(tmpbuf)) < strlen(tmpbuf)) e = 1;
123             if (run.poll_interval)
124             {
125                 snprintf(tmpbuf, sizeof(tmpbuf), "%d\n", run.poll_interval);
126                 if (write(fd, tmpbuf, strlen(tmpbuf)) < strlen(tmpbuf)) e = 1;
127             }
128             if (fsync(fd)) e = 1;
129             if (close(fd)) e = 1;
130         } else {
131             e = 1;
132         }
133         if (e == 0) {
134             lock_acquired = TRUE;
135         } else {
136             perror(lockfile);
137             fprintf(stderr, GT_("fetchmail: lock creation failed.\n"));
138             exit(PS_EXCLUDE);
139         }
140     }
141 }
142
143 void fm_lock_release(void)
144 /* release a lock on a given host */
145 {
146     unlink(lockfile);
147 }
148 /* lock.c ends here */