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 |
|
|
static char *lockfile = NULL; /* Lock file to stop new imapfilter |
15 |
|
|
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 |
lefcha |
1.4 |
fprintf(fd, "%s\n", ultostr(getpid(), 10)); |
32 |
lefcha |
1.1 |
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 |
lefcha |
1.4 |
"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.4 |
|
62 |
lefcha |
1.1 |
snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock"); |
63 |
|
|
lockfile = xstrdup(lockf); |
64 |
lefcha |
1.4 |
|
65 |
lefcha |
1.1 |
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 |
return n; |
79 |
lefcha |
1.1 |
} |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
/* |
83 |
|
|
* Unlink the lockfile. |
84 |
|
|
*/ |
85 |
|
|
int lockfile_remove(void) |
86 |
|
|
{ |
87 |
|
|
if (!lockfile) |
88 |
|
|
return 0; |
89 |
lefcha |
1.4 |
|
90 |
lefcha |
1.5.2.1 |
if (unlink(lockfile) && errno != ENOENT) { |
91 |
lefcha |
1.1 |
error("imapfilter: removing lockfile; %s\n", strerror(errno)); |
92 |
|
|
return ERROR_FILE_OPEN; |
93 |
|
|
} |
94 |
|
|
xfree(lockfile); |
95 |
lefcha |
1.4 |
|
96 |
lefcha |
1.1 |
return 0; |
97 |
lefcha |
1.2 |
} |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
/* |
101 |
|
|
* Kill the other instance of imapfilter (if one is running). |
102 |
|
|
*/ |
103 |
|
|
void kill_imapfilter(void) |
104 |
|
|
{ |
105 |
|
|
pid_t n; |
106 |
lefcha |
1.3 |
|
107 |
lefcha |
1.2 |
n = lockfile_pid(); |
108 |
lefcha |
1.4 |
|
109 |
lefcha |
1.2 |
if (n > 0) { |
110 |
|
|
if (kill(n, SIGTERM)) |
111 |
|
|
fprintf(stderr, "imapfilter: killing process with pid %d; %s\n", |
112 |
|
|
n, strerror(errno)); |
113 |
lefcha |
1.5.2.1 |
lockfile_remove(); |
114 |
lefcha |
1.2 |
exit(0); |
115 |
|
|
} else { |
116 |
|
|
fprintf(stderr, "imapfilter: no other imapfilter is running\n"); |
117 |
|
|
exit(ERROR_UNDEFINED); |
118 |
|
|
} |
119 |
lefcha |
1.1 |
} |