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

Diff of /imapfilter/lock.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by lefcha, Wed Jan 30 19:17:19 2002 UTC revision 1.2 by lefcha, Thu Jan 31 17:08:13 2002 UTC
# Line 1  Line 1 
1  #include <stdio.h>  #include <stdio.h>
2    #include <stdlib.h>
3  #include <limits.h>  #include <limits.h>
4  #include <string.h>  #include <string.h>
5  #include <errno.h>  #include <errno.h>
6    #include <sys/types.h>
7    #include <signal.h>
8    
9  #include "imapfilter.h"  #include "imapfilter.h"
10    
# Line 31  void lockfile_create(void) Line 34  void lockfile_create(void)
34    
35    
36  /*  /*
37   * Check if lockfile exists and if so, print the PID of the other   * Die if another instance of imapfilter is running.
  * imapfilter running.  
38   */   */
39  void lockfile_check(void)  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     * imapfilter running.
54     */
55    pid_t lockfile_pid(void)
56    {
57      char lockf[PATH_MAX];      char lockf[PATH_MAX];
58      char line[LINE_MAX];      char line[LINE_MAX];
59      FILE *fd;      FILE *fd;
60        pid_t n = 0;
61            
62      snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock");      snprintf(lockf, PATH_MAX, "%s/%s", home, ".imapfilter/lock");
63      lockfile = xstrdup(lockf);      lockfile = xstrdup(lockf);
# Line 49  void lockfile_check(void) Line 68  void lockfile_check(void)
68              fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n",              fatal(ERROR_FILE_OPEN, "imapfilter: opening lockfile; %s\n",
69                    strerror(errno));                    strerror(errno));
70          if (fgets(line, LINE_MAX - 1, fd)) {          if (fgets(line, LINE_MAX - 1, fd)) {
71              fclose(fd);              errno = 0;
72              fprintf(stderr,              n = strtoul(line, NULL, 10);
73                      "imapfilter: another imapfilter is running with pid %s\n",              if (errno)
74                      line);                  n = 0;
             exit(ERROR_LOCK_FILE);  
75          }          }
76            fclose(fd);
77      }      }
78        
79        return n;
80  }  }
81    
82    
# Line 76  int lockfile_remove(void) Line 97  int lockfile_remove(void)
97            
98      return 0;      return 0;
99  }  }
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    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26