33 |
|
|
34 |
fd = fopen(lockfile, "w"); |
fd = fopen(lockfile, "w"); |
35 |
if (fd == NULL) |
if (fd == NULL) |
36 |
fatal(ERROR_FILE_OPEN, "opening lockfile; %s\n", |
fatal(ERROR_FILEOPEN, "opening lockfile; %s\n", |
37 |
strerror(errno)); |
strerror(errno)); |
38 |
fprintf(fd, "%s\n", ultostr(getpid(), 10)); |
fprintf(fd, "%s\n", ultostr(getpid(), 10)); |
39 |
fclose(fd); |
fclose(fd); |
52 |
errno = 0; |
errno = 0; |
53 |
kill(n, 0); |
kill(n, 0); |
54 |
if (errno == ESRCH) { /* Process does not exist. */ |
if (errno == ESRCH) { /* Process does not exist. */ |
55 |
fprintf(stderr, "imapfilter: removing stale lockfile\n"); |
fprintf(stderr, |
56 |
|
"imapfilter: removing stale lockfile\n"); |
57 |
lockfile_remove(); |
lockfile_remove(); |
58 |
} else { |
} else { |
59 |
fprintf(stderr, |
fprintf(stderr, |
66 |
|
|
67 |
|
|
68 |
/* |
/* |
69 |
* Check if lockfile exists and if so, return the PID of the other |
* Check if lockfile exists and if so, return the PID of the other imapfilter |
70 |
* imapfilter running. |
* running. |
71 |
*/ |
*/ |
72 |
pid_t |
pid_t |
73 |
lockfile_pid(void) |
lockfile_pid(void) |
79 |
|
|
80 |
n = 0; |
n = 0; |
81 |
|
|
82 |
snprintf(lockf, PATH_MAX, "%s/%s", home, PATHNAME_LOCK_FILE); |
snprintf(lockf, PATH_MAX, "%s/%s", home, PATHNAME_LOCK); |
83 |
lockfile = xstrdup(lockf); |
lockfile = xstrdup(lockf); |
84 |
|
|
85 |
if (exists_file(lockfile)) { |
if (exists_file(lockfile)) { |
86 |
fd = fopen(lockfile, "r"); |
fd = fopen(lockfile, "r"); |
87 |
if (fd == NULL) |
if (fd == NULL) |
88 |
fatal(ERROR_FILE_OPEN, "opening lockfile; %s\n", |
fatal(ERROR_FILEOPEN, "opening lockfile; %s\n", |
89 |
strerror(errno)); |
strerror(errno)); |
90 |
if (fgets(line, LINE_MAX - 1, fd)) { |
if (fgets(line, LINE_MAX - 1, fd)) { |
91 |
errno = 0; |
errno = 0; |
105 |
int |
int |
106 |
lockfile_remove(void) |
lockfile_remove(void) |
107 |
{ |
{ |
108 |
|
|
109 |
if (lockfile == NULL) |
if (lockfile == NULL) |
110 |
return 0; |
return 0; |
111 |
|
|
112 |
if (unlink(lockfile) && errno != ENOENT) { |
if (unlink(lockfile) && errno != ENOENT) { |
113 |
error("removing lockfile; %s\n", strerror(errno)); |
error("removing lockfile; %s\n", strerror(errno)); |
114 |
return ERROR_FILE_OPEN; |
return ERROR_FILEOPEN; |
115 |
} |
} |
116 |
return 0; |
return 0; |
117 |
} |
} |
129 |
|
|
130 |
if (n > 0) { |
if (n > 0) { |
131 |
if (kill(n, SIGTERM)) |
if (kill(n, SIGTERM)) |
132 |
fprintf(stderr, "imapfilter: killing process with " |
fprintf(stderr, |
133 |
"pid %d; %s\n", (int)(n), strerror(errno)); |
"imapfilter: killing process with pid %d; %s\n", |
134 |
|
(int)(n), strerror(errno)); |
135 |
lockfile_remove(); |
lockfile_remove(); |
136 |
exit(0); |
exit(0); |
137 |
} else { |
} else { |