41 |
pid_t n; |
pid_t n; |
42 |
|
|
43 |
if ((n = lockfile_pid())) { |
if ((n = lockfile_pid())) { |
44 |
fprintf(stderr, |
errno = 0; |
45 |
"imapfilter: another imapfilter is running with pid %d\n", n); |
kill(n, 0); |
46 |
exit(ERROR_LOCKFILE); |
if (errno == ESRCH) { /* Process does not exist. */ |
47 |
|
fprintf(stderr, "imapfilter: removing stale lockfile\n"); |
48 |
|
lockfile_remove(); |
49 |
|
} else { |
50 |
|
fprintf(stderr, |
51 |
|
"imapfilter: another imapfilter " |
52 |
|
"is running with pid %d\n", (int)(n)); |
53 |
|
exit(ERROR_LOCKFILE); |
54 |
|
} |
55 |
} |
} |
56 |
} |
} |
57 |
|
|
62 |
*/ |
*/ |
63 |
pid_t lockfile_pid(void) |
pid_t lockfile_pid(void) |
64 |
{ |
{ |
|
char lockf[PATH_MAX]; |
|
65 |
char line[LINE_MAX]; |
char line[LINE_MAX]; |
66 |
|
char lockf[PATH_MAX]; |
67 |
FILE *fd; |
FILE *fd; |
68 |
pid_t n = 0; |
pid_t n = 0; |
69 |
|
|
95 |
if (!lockfile) |
if (!lockfile) |
96 |
return 0; |
return 0; |
97 |
|
|
98 |
if (unlink(lockfile)) { |
if (unlink(lockfile) && errno != ENOENT) { |
99 |
error("imapfilter: removing lockfile; %s\n", strerror(errno)); |
error("imapfilter: removing lockfile; %s\n", strerror(errno)); |
100 |
return ERROR_FILE_OPEN; |
return ERROR_FILE_OPEN; |
101 |
} |
} |
|
xfree(lockfile); |
|
|
|
|
102 |
return 0; |
return 0; |
103 |
} |
} |
104 |
|
|
115 |
if (n > 0) { |
if (n > 0) { |
116 |
if (kill(n, SIGTERM)) |
if (kill(n, SIGTERM)) |
117 |
fprintf(stderr, "imapfilter: killing process with pid %d; %s\n", |
fprintf(stderr, "imapfilter: killing process with pid %d; %s\n", |
118 |
n, strerror(errno)); |
(int)(n), strerror(errno)); |
119 |
|
lockfile_remove(); |
120 |
exit(0); |
exit(0); |
121 |
} else { |
} else { |
122 |
fprintf(stderr, "imapfilter: no other imapfilter is running\n"); |
fprintf(stderr, "imapfilter: no other imapfilter is running\n"); |