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 |
|
|
|
14 |
|
|
static char *lockfile = NULL; /* Lock file to stop new |
15 |
lefcha |
1.3 |
imapfilter processes. */ |
16 |
lefcha |
1.1 |
|
17 |
|
|
|
18 |
|
|
/* |
19 |
|
|
* Create lockfile with the PID of imapfilter as its contents. |
20 |
|
|
*/ |
21 |
|
|
void 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) |
29 |
|
|
fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n", |
30 |
|
|
strerror(errno)); |
31 |
|
|
fputs(ultostr(getpid(), 10), fd); |
32 |
|
|
fclose(fd); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/* |
37 |
lefcha |
1.2 |
* Die if another instance of imapfilter is running. |
38 |
|
|
*/ |
39 |
|
|
void lockfile_check(void) |
40 |
|
|
{ |
41 |
|
|
pid_t n; |
42 |
|
|
|
43 |
|
|
if ((n = lockfile_pid())) { |
44 |
|
|
fprintf(stderr, |
45 |
|
|
"imapfilter: another imapfilter is running with pid %d\n", n); |
46 |
lefcha |
1.3 |
exit(ERROR_LOCKFILE); |
47 |
lefcha |
1.2 |
} |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
/* |
52 |
|
|
* Check if lockfile exists and if so, return the PID of the other |
53 |
lefcha |
1.1 |
* imapfilter running. |
54 |
|
|
*/ |
55 |
lefcha |
1.2 |
pid_t lockfile_pid(void) |
56 |
lefcha |
1.1 |
{ |
57 |
|
|
char lockf[PATH_MAX]; |
58 |
|
|
char line[LINE_MAX]; |
59 |
|
|
FILE *fd; |
60 |
lefcha |
1.2 |
pid_t n = 0; |
61 |
lefcha |
1.1 |
|
62 |
|
|
snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock"); |
63 |
|
|
lockfile = xstrdup(lockf); |
64 |
|
|
|
65 |
|
|
if (exists_file(lockfile)) { |
66 |
|
|
fd = fopen(lockfile, "r"); |
67 |
|
|
if (!fd) |
68 |
|
|
fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n", |
69 |
|
|
strerror(errno)); |
70 |
|
|
if (fgets(line, LINE_MAX - 1, fd)) { |
71 |
lefcha |
1.2 |
errno = 0; |
72 |
|
|
n = strtoul(line, NULL, 10); |
73 |
|
|
if (errno) |
74 |
|
|
n = 0; |
75 |
lefcha |
1.1 |
} |
76 |
lefcha |
1.2 |
fclose(fd); |
77 |
lefcha |
1.1 |
} |
78 |
lefcha |
1.2 |
|
79 |
|
|
return n; |
80 |
lefcha |
1.1 |
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/* |
84 |
|
|
* Unlink the lockfile. |
85 |
|
|
*/ |
86 |
|
|
int lockfile_remove(void) |
87 |
|
|
{ |
88 |
|
|
if (!lockfile) |
89 |
|
|
return 0; |
90 |
|
|
|
91 |
|
|
if (remove(lockfile)) { |
92 |
|
|
error("imapfilter: removing lockfile; %s\n", strerror(errno)); |
93 |
|
|
return ERROR_FILE_OPEN; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
xfree(lockfile); |
97 |
|
|
|
98 |
|
|
return 0; |
99 |
lefcha |
1.2 |
} |
100 |
|
|
|
101 |
|
|
|
102 |
|
|
/* |
103 |
|
|
* Kill the other instance of imapfilter (if one is running). |
104 |
|
|
*/ |
105 |
|
|
void kill_imapfilter(void) |
106 |
|
|
{ |
107 |
|
|
pid_t n; |
108 |
lefcha |
1.3 |
|
109 |
lefcha |
1.2 |
n = lockfile_pid(); |
110 |
|
|
|
111 |
|
|
if (n > 0) { |
112 |
|
|
if (kill(n, SIGTERM)) |
113 |
|
|
fprintf(stderr, "imapfilter: killing process with pid %d; %s\n", |
114 |
|
|
n, strerror(errno)); |
115 |
lefcha |
1.3 |
lockfile_remove(); |
116 |
lefcha |
1.2 |
exit(0); |
117 |
|
|
} else { |
118 |
|
|
fprintf(stderr, "imapfilter: no other imapfilter is running\n"); |
119 |
|
|
exit(ERROR_UNDEFINED); |
120 |
|
|
} |
121 |
lefcha |
1.1 |
} |