1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.2 |
#include <stdlib.h> |
3 |
lefcha |
1.1 |
#include <limits.h> |
4 |
|
|
#include <string.h> |
5 |
|
|
#include <errno.h> |
6 |
lefcha |
1.2 |
#include <sys/types.h> |
7 |
|
|
#include <signal.h> |
8 |
lefcha |
1.1 |
|
9 |
|
|
#include "imapfilter.h" |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
extern char *home; |
13 |
lefcha |
1.4 |
|
14 |
lefcha |
1.10 |
static char *lockfile = NULL; /* Lock file to stop new imapfilter processes. */ |
15 |
lefcha |
1.1 |
|
16 |
|
|
|
17 |
|
|
/* |
18 |
|
|
* Create lockfile with the PID of imapfilter as its contents. |
19 |
|
|
*/ |
20 |
lefcha |
1.10 |
void |
21 |
|
|
lockfile_create(void) |
22 |
lefcha |
1.1 |
{ |
23 |
lefcha |
1.10 |
FILE *fd; |
24 |
lefcha |
1.1 |
|
25 |
lefcha |
1.10 |
create_file(lockfile, S_IRUSR | S_IWUSR); |
26 |
lefcha |
1.1 |
|
27 |
lefcha |
1.10 |
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 |
lefcha |
1.1 |
} |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/* |
37 |
lefcha |
1.2 |
* Die if another instance of imapfilter is running. |
38 |
|
|
*/ |
39 |
lefcha |
1.10 |
void |
40 |
|
|
lockfile_check(void) |
41 |
lefcha |
1.2 |
{ |
42 |
lefcha |
1.10 |
pid_t n; |
43 |
lefcha |
1.2 |
|
44 |
lefcha |
1.10 |
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 |
lefcha |
1.7 |
} |
57 |
lefcha |
1.2 |
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
/* |
61 |
|
|
* Check if lockfile exists and if so, return the PID of the other |
62 |
lefcha |
1.1 |
* imapfilter running. |
63 |
|
|
*/ |
64 |
lefcha |
1.10 |
pid_t |
65 |
|
|
lockfile_pid(void) |
66 |
lefcha |
1.1 |
{ |
67 |
lefcha |
1.10 |
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 |
lefcha |
1.1 |
} |
90 |
lefcha |
1.10 |
return n; |
91 |
lefcha |
1.1 |
} |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
/* |
95 |
|
|
* Unlink the lockfile. |
96 |
|
|
*/ |
97 |
lefcha |
1.10 |
int |
98 |
|
|
lockfile_remove(void) |
99 |
lefcha |
1.1 |
{ |
100 |
lefcha |
1.10 |
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 |
lefcha |
1.1 |
return 0; |
108 |
lefcha |
1.2 |
} |
109 |
|
|
|
110 |
|
|
|
111 |
|
|
/* |
112 |
|
|
* Kill the other instance of imapfilter (if one is running). |
113 |
|
|
*/ |
114 |
lefcha |
1.10 |
void |
115 |
|
|
kill_imapfilter(void) |
116 |
lefcha |
1.2 |
{ |
117 |
lefcha |
1.10 |
pid_t n; |
118 |
lefcha |
1.3 |
|
119 |
lefcha |
1.10 |
n = lockfile_pid(); |
120 |
lefcha |
1.4 |
|
121 |
lefcha |
1.10 |
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 |
lefcha |
1.1 |
} |