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

Annotation of /imapfilter/lock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Thu Jan 31 17:08:13 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.1: +49 -7 lines
File MIME type: text/plain
Added daemon mode.

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    
14     static char *lockfile = NULL; /* Lock file to stop new
15     imapfilter processes */
16    
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     fputs(ultostr(getpid(), 10), fd);
32     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     fprintf(stderr,
45     "imapfilter: another imapfilter is running with pid %d\n", n);
46     exit(ERROR_LOCK_FILE);
47     }
48     }
49    
50    
51     /*
52     * Check if lockfile exists and if so, return the PID of the other
53 lefcha 1.1 * imapfilter running.
54     */
55 lefcha 1.2 pid_t lockfile_pid(void)
56 lefcha 1.1 {
57     char lockf[PATH_MAX];
58     char line[LINE_MAX];
59     FILE *fd;
60 lefcha 1.2 pid_t n = 0;
61 lefcha 1.1
62     snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock");
63     lockfile = xstrdup(lockf);
64    
65     if (exists_file(lockfile)) {
66     fd = fopen(lockfile, "r");
67     if (!fd)
68     fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n",
69     strerror(errno));
70     if (fgets(line, LINE_MAX - 1, fd)) {
71 lefcha 1.2 errno = 0;
72     n = strtoul(line, NULL, 10);
73     if (errno)
74     n = 0;
75 lefcha 1.1 }
76 lefcha 1.2 fclose(fd);
77 lefcha 1.1 }
78 lefcha 1.2
79     return n;
80 lefcha 1.1 }
81    
82    
83     /*
84     * Unlink the lockfile.
85     */
86     int lockfile_remove(void)
87     {
88     if (!lockfile)
89     return 0;
90    
91     if (remove(lockfile)) {
92     error("imapfilter: removing lockfile; %s\n", strerror(errno));
93     return ERROR_FILE_OPEN;
94     }
95    
96     xfree(lockfile);
97    
98     return 0;
99 lefcha 1.2 }
100    
101    
102     /*
103     * Kill the other instance of imapfilter (if one is running).
104     */
105     void kill_imapfilter(void)
106     {
107     pid_t n;
108    
109     n = lockfile_pid();
110    
111     if (n > 0) {
112     if (kill(n, SIGTERM))
113     fprintf(stderr, "imapfilter: killing process with pid %d; %s\n",
114     n, strerror(errno));
115     exit(0);
116     } else {
117     fprintf(stderr, "imapfilter: no other imapfilter is running\n");
118     exit(ERROR_UNDEFINED);
119     }
120 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26