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

Annotation of /imapfilter/lock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Thu Dec 5 07:32:50 2002 UTC (21 years, 3 months ago) by lefcha
Branch: MAIN
Changes since 1.8: +1 -1 lines
File MIME type: text/plain
Typo error fix.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26