1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <limits.h> |
3 |
|
|
#include <string.h> |
4 |
|
|
#include <errno.h> |
5 |
|
|
|
6 |
|
|
#include "imapfilter.h" |
7 |
|
|
|
8 |
|
|
|
9 |
|
|
extern char *home; |
10 |
|
|
|
11 |
|
|
static char *lockfile = NULL; /* Lock file to stop new |
12 |
|
|
imapfilter processes */ |
13 |
|
|
|
14 |
|
|
|
15 |
|
|
/* |
16 |
|
|
* Create lockfile with the PID of imapfilter as its contents. |
17 |
|
|
*/ |
18 |
|
|
void lockfile_create(void) |
19 |
|
|
{ |
20 |
|
|
FILE *fd; |
21 |
|
|
|
22 |
|
|
create_file(lockfile, S_IRUSR | S_IWUSR); |
23 |
|
|
|
24 |
|
|
fd = fopen(lockfile, "w"); |
25 |
|
|
if (!fd) |
26 |
|
|
fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n", |
27 |
|
|
strerror(errno)); |
28 |
|
|
fputs(ultostr(getpid(), 10), fd); |
29 |
|
|
fclose(fd); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
/* |
34 |
|
|
* Check if lockfile exists and if so, print the PID of the other |
35 |
|
|
* imapfilter running. |
36 |
|
|
*/ |
37 |
|
|
void lockfile_check(void) |
38 |
|
|
{ |
39 |
|
|
char lockf[PATH_MAX]; |
40 |
|
|
char line[LINE_MAX]; |
41 |
|
|
FILE *fd; |
42 |
|
|
|
43 |
|
|
snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock"); |
44 |
|
|
lockfile = xstrdup(lockf); |
45 |
|
|
|
46 |
|
|
if (exists_file(lockfile)) { |
47 |
|
|
fd = fopen(lockfile, "r"); |
48 |
|
|
if (!fd) |
49 |
|
|
fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n", |
50 |
|
|
strerror(errno)); |
51 |
|
|
if (fgets(line, LINE_MAX - 1, fd)) { |
52 |
|
|
fclose(fd); |
53 |
|
|
fprintf(stderr, |
54 |
|
|
"imapfilter: another imapfilter is running with pid %s\n", |
55 |
|
|
line); |
56 |
|
|
exit(ERROR_LOCK_FILE); |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
/* |
63 |
|
|
* Unlink the lockfile. |
64 |
|
|
*/ |
65 |
|
|
int lockfile_remove(void) |
66 |
|
|
{ |
67 |
|
|
if (!lockfile) |
68 |
|
|
return 0; |
69 |
|
|
|
70 |
|
|
if (remove(lockfile)) { |
71 |
|
|
error("imapfilter: removing lockfile; %s\n", strerror(errno)); |
72 |
|
|
return ERROR_FILE_OPEN; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
xfree(lockfile); |
76 |
|
|
|
77 |
|
|
return 0; |
78 |
|
|
} |