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

Contents of /imapfilter/lock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations)
Sat Feb 14 19:14:43 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +7 -5 lines
File MIME type: text/plain
Indentation.

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 imapfilter processes. */
19
20
21 pid_t lockfile_pid(void);
22
23
24 /*
25 * Create lockfile with the PID of imapfilter as its contents.
26 */
27 void
28 lockfile_create(void)
29 {
30 FILE *fd;
31
32 create_file(lockfile, S_IRUSR | S_IWUSR);
33
34 fd = fopen(lockfile, "w");
35 if (fd == NULL)
36 fatal(ERROR_FILEOPEN, "opening lockfile; %s\n",
37 strerror(errno));
38 fprintf(fd, "%s\n", ultostr(getpid(), 10));
39 fclose(fd);
40 }
41
42
43 /*
44 * Die if another instance of imapfilter is running.
45 */
46 void
47 lockfile_check(void)
48 {
49 pid_t n;
50
51 if ((n = lockfile_pid())) {
52 errno = 0;
53 kill(n, 0);
54 if (errno == ESRCH) { /* Process does not exist. */
55 fprintf(stderr,
56 "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 imapfilter
70 * 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);
83 lockfile = xstrdup(lockf);
84
85 if (exists_file(lockfile)) {
86 fd = fopen(lockfile, "r");
87 if (fd == NULL)
88 fatal(ERROR_FILEOPEN, "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
109 if (lockfile == NULL)
110 return 0;
111
112 if (unlink(lockfile) && errno != ENOENT) {
113 error("removing lockfile; %s\n", strerror(errno));
114 return ERROR_FILEOPEN;
115 }
116 return 0;
117 }
118
119
120 /*
121 * Kill the other instance of imapfilter (if one is running).
122 */
123 void
124 kill_imapfilter(void)
125 {
126 pid_t n;
127
128 n = lockfile_pid();
129
130 if (n > 0) {
131 if (kill(n, SIGTERM))
132 fprintf(stderr,
133 "imapfilter: killing process with pid %d; %s\n",
134 (int)(n), strerror(errno));
135 lockfile_remove();
136 exit(0);
137 } else {
138 fprintf(stderr, "imapfilter: no other imapfilter is running\n");
139 exit(ERROR_UNDEFINED);
140 }
141 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26