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

Diff of /imapfilter/lock.c

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

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

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26