1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <limits.h> |
4 |
#include <string.h> |
5 |
#include <errno.h> |
6 |
#include <sys/types.h> |
7 |
#include <signal.h> |
8 |
|
9 |
#include "imapfilter.h" |
10 |
|
11 |
|
12 |
extern char *home; |
13 |
|
14 |
static char *lockfile = NULL; /* Lock file to stop new imapfilter processes. */ |
15 |
|
16 |
|
17 |
/* |
18 |
* Create lockfile with the PID of imapfilter as its contents. |
19 |
*/ |
20 |
void |
21 |
lockfile_create(void) |
22 |
{ |
23 |
FILE *fd; |
24 |
|
25 |
create_file(lockfile, S_IRUSR | S_IWUSR); |
26 |
|
27 |
fd = fopen(lockfile, "w"); |
28 |
if (fd == NULL) |
29 |
fatal(ERROR_FILE_OPEN, "opening lockfile; %s\n", |
30 |
strerror(errno)); |
31 |
fprintf(fd, "%s\n", ultostr(getpid(), 10)); |
32 |
fclose(fd); |
33 |
} |
34 |
|
35 |
|
36 |
/* |
37 |
* Die if another instance of imapfilter is running. |
38 |
*/ |
39 |
void |
40 |
lockfile_check(void) |
41 |
{ |
42 |
pid_t n; |
43 |
|
44 |
if ((n = lockfile_pid())) { |
45 |
errno = 0; |
46 |
kill(n, 0); |
47 |
if (errno == ESRCH) { /* Process does not exist. */ |
48 |
fprintf(stderr, "imapfilter: removing stale lockfile\n"); |
49 |
lockfile_remove(); |
50 |
} else { |
51 |
fprintf(stderr, |
52 |
"imapfilter: another imapfilter " |
53 |
"is running with pid %d\n", (int)(n)); |
54 |
exit(ERROR_LOCKFILE); |
55 |
} |
56 |
} |
57 |
} |
58 |
|
59 |
|
60 |
/* |
61 |
* Check if lockfile exists and if so, return the PID of the other |
62 |
* imapfilter running. |
63 |
*/ |
64 |
pid_t |
65 |
lockfile_pid(void) |
66 |
{ |
67 |
char line[LINE_MAX]; |
68 |
char lockf[PATH_MAX]; |
69 |
FILE *fd; |
70 |
pid_t n; |
71 |
|
72 |
n = 0; |
73 |
|
74 |
snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock"); |
75 |
lockfile = xstrdup(lockf); |
76 |
|
77 |
if (exists_file(lockfile)) { |
78 |
fd = fopen(lockfile, "r"); |
79 |
if (fd == NULL) |
80 |
fatal(ERROR_FILE_OPEN, "opening lockfile; %s\n", |
81 |
strerror(errno)); |
82 |
if (fgets(line, LINE_MAX - 1, fd)) { |
83 |
errno = 0; |
84 |
n = strtoul(line, NULL, 10); |
85 |
if (errno) |
86 |
n = 0; |
87 |
} |
88 |
fclose(fd); |
89 |
} |
90 |
return n; |
91 |
} |
92 |
|
93 |
|
94 |
/* |
95 |
* Unlink the lockfile. |
96 |
*/ |
97 |
int |
98 |
lockfile_remove(void) |
99 |
{ |
100 |
if (lockfile == NULL) |
101 |
return 0; |
102 |
|
103 |
if (unlink(lockfile) && errno != ENOENT) { |
104 |
error("removing lockfile; %s\n", strerror(errno)); |
105 |
return ERROR_FILE_OPEN; |
106 |
} |
107 |
return 0; |
108 |
} |
109 |
|
110 |
|
111 |
/* |
112 |
* Kill the other instance of imapfilter (if one is running). |
113 |
*/ |
114 |
void |
115 |
kill_imapfilter(void) |
116 |
{ |
117 |
pid_t n; |
118 |
|
119 |
n = lockfile_pid(); |
120 |
|
121 |
if (n > 0) { |
122 |
if (kill(n, SIGTERM)) |
123 |
fprintf(stderr, "imapfilter: killing process with " |
124 |
"pid %d; %s\n", (int)(n), strerror(errno)); |
125 |
lockfile_remove(); |
126 |
exit(0); |
127 |
} else { |
128 |
fprintf(stderr, "imapfilter: no other imapfilter is running\n"); |
129 |
exit(ERROR_UNDEFINED); |
130 |
} |
131 |
} |