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

Annotation of /imapfilter/lock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide 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 lefcha 1.1 #include <stdio.h>
2 lefcha 1.12 #include <stdlib.h>
3 lefcha 1.13 #include <sys/types.h>
4     #include <unistd.h>
5 lefcha 1.1 #include <string.h>
6     #include <errno.h>
7 lefcha 1.11 #include <limits.h>
8 lefcha 1.13 #include <sys/stat.h>
9 lefcha 1.11 #include <signal.h>
10 lefcha 1.1
11 lefcha 1.11 #include "config.h"
12 lefcha 1.1 #include "imapfilter.h"
13 lefcha 1.11 #include "pathnames.h"
14 lefcha 1.1
15    
16     extern char *home;
17 lefcha 1.4
18 lefcha 1.14 static char *lockfile = NULL; /* Lock file to stop new
19     * imapfilter processes. */
20 lefcha 1.1
21    
22 lefcha 1.11 pid_t lockfile_pid(void);
23    
24    
25 lefcha 1.1 /*
26     * Create lockfile with the PID of imapfilter as its contents.
27     */
28 lefcha 1.10 void
29     lockfile_create(void)
30 lefcha 1.1 {
31 lefcha 1.10 FILE *fd;
32 lefcha 1.1
33 lefcha 1.10 create_file(lockfile, S_IRUSR | S_IWUSR);
34 lefcha 1.1
35 lefcha 1.10 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 lefcha 1.1 }
42    
43    
44     /*
45 lefcha 1.2 * Die if another instance of imapfilter is running.
46     */
47 lefcha 1.10 void
48     lockfile_check(void)
49 lefcha 1.2 {
50 lefcha 1.10 pid_t n;
51 lefcha 1.2
52 lefcha 1.10 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 lefcha 1.7 }
65 lefcha 1.2 }
66    
67    
68     /*
69     * Check if lockfile exists and if so, return the PID of the other
70 lefcha 1.1 * imapfilter running.
71     */
72 lefcha 1.10 pid_t
73     lockfile_pid(void)
74 lefcha 1.1 {
75 lefcha 1.10 char line[LINE_MAX];
76     char lockf[PATH_MAX];
77     FILE *fd;
78     pid_t n;
79    
80     n = 0;
81    
82 lefcha 1.11 snprintf(lockf, PATH_MAX, "%s/%s", home, PATHNAME_LOCK_FILE);
83 lefcha 1.10 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 lefcha 1.1 }
98 lefcha 1.10 return n;
99 lefcha 1.1 }
100    
101    
102     /*
103     * Unlink the lockfile.
104     */
105 lefcha 1.10 int
106     lockfile_remove(void)
107 lefcha 1.1 {
108 lefcha 1.10 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 lefcha 1.1 return 0;
116 lefcha 1.2 }
117    
118    
119     /*
120     * Kill the other instance of imapfilter (if one is running).
121     */
122 lefcha 1.10 void
123     kill_imapfilter(void)
124 lefcha 1.2 {
125 lefcha 1.10 pid_t n;
126 lefcha 1.3
127 lefcha 1.10 n = lockfile_pid();
128 lefcha 1.4
129 lefcha 1.10 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 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26