/[imapfilter]/imapfilter/lock.c
ViewVC logotype

Contents of /imapfilter/lock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations)
Mon Feb 9 20:49:01 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.13: +2 -1 lines
File MIME type: text/plain
Wrap some long >80 lines.

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include <sys/stat.h>
9 #include <signal.h>
10
11 #include "config.h"
12 #include "imapfilter.h"
13 #include "pathnames.h"
14
15
16 extern char *home;
17
18 static char *lockfile = NULL; /* Lock file to stop new
19 * imapfilter processes. */
20
21
22 pid_t lockfile_pid(void);
23
24
25 /*
26 * Create lockfile with the PID of imapfilter as its contents.
27 */
28 void
29 lockfile_create(void)
30 {
31 FILE *fd;
32
33 create_file(lockfile, S_IRUSR | S_IWUSR);
34
35 fd = fopen(lockfile, "w");
36 if (fd == NULL)
37 fatal(ERROR_FILE_OPEN, "opening lockfile; %s\n",
38 strerror(errno));
39 fprintf(fd, "%s\n", ultostr(getpid(), 10));
40 fclose(fd);
41 }
42
43
44 /*
45 * Die if another instance of imapfilter is running.
46 */
47 void
48 lockfile_check(void)
49 {
50 pid_t n;
51
52 if ((n = lockfile_pid())) {
53 errno = 0;
54 kill(n, 0);
55 if (errno == ESRCH) { /* Process does not exist. */
56 fprintf(stderr, "imapfilter: removing stale lockfile\n");
57 lockfile_remove();
58 } else {
59 fprintf(stderr,
60 "imapfilter: another imapfilter "
61 "is running with pid %d\n", (int)(n));
62 exit(ERROR_LOCKFILE);
63 }
64 }
65 }
66
67
68 /*
69 * Check if lockfile exists and if so, return the PID of the other
70 * imapfilter running.
71 */
72 pid_t
73 lockfile_pid(void)
74 {
75 char line[LINE_MAX];
76 char lockf[PATH_MAX];
77 FILE *fd;
78 pid_t n;
79
80 n = 0;
81
82 snprintf(lockf, PATH_MAX, "%s/%s", home, PATHNAME_LOCK_FILE);
83 lockfile = xstrdup(lockf);
84
85 if (exists_file(lockfile)) {
86 fd = fopen(lockfile, "r");
87 if (fd == NULL)
88 fatal(ERROR_FILE_OPEN, "opening lockfile; %s\n",
89 strerror(errno));
90 if (fgets(line, LINE_MAX - 1, fd)) {
91 errno = 0;
92 n = strtoul(line, NULL, 10);
93 if (errno)
94 n = 0;
95 }
96 fclose(fd);
97 }
98 return n;
99 }
100
101
102 /*
103 * Unlink the lockfile.
104 */
105 int
106 lockfile_remove(void)
107 {
108 if (lockfile == NULL)
109 return 0;
110
111 if (unlink(lockfile) && errno != ENOENT) {
112 error("removing lockfile; %s\n", strerror(errno));
113 return ERROR_FILE_OPEN;
114 }
115 return 0;
116 }
117
118
119 /*
120 * Kill the other instance of imapfilter (if one is running).
121 */
122 void
123 kill_imapfilter(void)
124 {
125 pid_t n;
126
127 n = lockfile_pid();
128
129 if (n > 0) {
130 if (kill(n, SIGTERM))
131 fprintf(stderr, "imapfilter: killing process with "
132 "pid %d; %s\n", (int)(n), strerror(errno));
133 lockfile_remove();
134 exit(0);
135 } else {
136 fprintf(stderr, "imapfilter: no other imapfilter is running\n");
137 exit(ERROR_UNDEFINED);
138 }
139 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26