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