ze-filter  (ze-filter-0.8.0-develop-180218)
ze-proc-witness.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : Mon Apr 2 11:40:00 CEST 2007
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 
26 #include <ze-sys.h>
27 #include <ze-filter.h>
28 #include <ze-proc-witness.h>
29 
30 
31 char *milter_sock_file = NULL;
32 
33 
34 /* ****************************************************************************
35  * *
36  * *
37  **************************************************************************** */
38 static char *pid_file = NULL;
39 
40 bool
42  char *fname;
43 {
44  FILE *fpid;
45 
46  if (fname == NULL || strlen(fname) == 0) {
47  ZE_MessageInfo(0, "pid_file : NULL pointer");
48  return FALSE;
49  }
50 
51  if (access(fname, F_OK) == 0) {
52  bool running = FALSE;
53 
54  if ((fpid = fopen(fname, "r")) != NULL) {
55  char buf[256];
56 
57  if (fgets(buf, sizeof (buf), fpid) != NULL) {
58  long pid;
59 
60  errno = 0;
61  pid = strtol(buf, NULL, 10);
62  if (errno == 0 && pid > 0) {
63  if (kill(pid, 0) == 0)
64  running = TRUE;
65  }
66  }
67  fclose(fpid);
68  }
69 
70  if (!running) {
71  ZE_LogMsgWarning(0, "PID_FILE %s exists, but ze-filter isn't running !",
72  fname);
73  (void) remove(fname);
74  } else {
76  "PID_FILE %s exists. Is there another ze-filter running ?",
77  fname);
78  exit(EX_SOFTWARE);
79  }
80  }
81 
82  if ((fpid = fopen(fname, "w")) != NULL) {
83  fprintf(fpid, "%d\n", (int) getpid());
84  fclose(fpid);
85 
86  if (chmod(fname, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
87  ZE_LogSysError("error changing pid file mode %s", fname);
88 
89  pid_file = strdup(fname);
90  if (pid_file == NULL)
91  ZE_LogSysError("error strdup(%s)", fname);
92 
93  return TRUE;
94  }
95 
96  ZE_LogSysError("PID_FILE %s : can't create", fname);
97 
98  exit(EX_CANTCREAT);
99 }
100 
101 /* ****************************************************************************
102  * *
103  * *
104  **************************************************************************** */
105 void
107 {
108  pid_t pid = -1;
109  FILE *fpid = NULL;
110  char buf[32];
111 
112  pid = getpid();
113 
114  ZE_LogMsgDebug(20, "remove_pid_file : %d", pid);
115 
116  if (pid_file == NULL || strlen(pid_file) == 0)
117  return;
118 
119  /*
120  * check if contents of pid_file equals to pid
121  */
122  if ((fpid = fopen(pid_file, "r")) == NULL)
123  return;
124 
125  memset(buf, 0, sizeof (buf));
126  if (fgets(buf, sizeof (buf), fpid) == NULL);
127 
128  fclose(fpid);
129 
130  if (atoi(buf) == pid)
131  remove(pid_file);
132 }
133 
134 /* ****************************************************************************
135  * *
136  * *
137  **************************************************************************** */
138 void
140 {
141  char *sock_file;
142 
143  sock_file = milter_sock_file;
144 
145  if (sock_file == NULL || strlen(milter_sock_file) == 0)
146  goto end;
147 
148  if (!zeStrRegex(sock_file, "^(unix|local):", NULL, NULL, TRUE))
149  goto end;
150 
151  if (strncasecmp(sock_file, "unix:", strlen("unix:")) == 0)
152  sock_file += strlen("unix:");
153 
154  if (strncasecmp(sock_file, "local:", strlen("local:")) == 0)
155  sock_file += strlen("local:");
156 
157  if (strlen(sock_file) > 0 && *sock_file == '/') {
158  struct stat buf;
159 
160  if (lstat(sock_file, &buf) == 0) {
161  ZE_MessageWarning(9, "Removing SOCK_FILE : %s", sock_file);
162  remove(sock_file);
163  }
164  }
165 
166 end:
167  /*
168  * milter_sock_file = NULL;
169  */
170  return;
171 }
172 
173 /* ****************************************************************************
174  * *
175  * *
176  **************************************************************************** */
177 char *
178 define_milter_sock(cf, arg_p, arg_u, arg_i)
179  char *cf;
180  char *arg_p;
181  char *arg_u;
182  char *arg_i;
183 {
184  static char sm_sock[256];
185  char *sock = NULL;
186 
187  memset(sm_sock, 0, sizeof (sm_sock));
188 
189  sock = cf;
190  if (sock != NULL && strlen(sock) > 0)
191  strlcpy(sm_sock, sock, sizeof (sm_sock));
192 
193  if ((sock = getenv("ZEFILTER_SOCKET")) != NULL)
194  strlcpy(sm_sock, sock, sizeof (sm_sock));
195 
196  if (arg_i != NULL)
197  snprintf(sm_sock, sizeof (sm_sock), "inet:%s@localhost", arg_i);
198 
199  if (arg_u != NULL)
200  snprintf(sm_sock, sizeof (sm_sock), "local:%s", arg_u);
201 
202  if (arg_p != NULL)
203  strlcpy(sm_sock, arg_p, sizeof (sm_sock));
204 
205  ZE_MessageInfo(12, "SM_SOCK = %s", sm_sock);
206 
208 
209  return milter_sock_file;
210 }
char * milter_sock_file
char sm_sock[]
char * define_milter_sock(char *cf, char *arg_p, char *arg_u, char *arg_i)
void remove_pid_file()
#define FALSE
Definition: macros.h:160
#define strlcpy
Definition: zeString.h:32
#define ZE_LogMsgError(level,...)
Definition: zeSyslog.h:113
bool zeStrRegex(char *, char *, long *, long *, bool)
Definition: zeStrings.c:544
bool create_pid_file(char *fname)
#define ZE_LogMsgDebug(level,...)
Definition: zeSyslog.h:109
void remove_milter_sock()
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
#define ZE_MessageWarning(level,...)
Definition: zeSyslog.h:92
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
#define ZE_LogMsgWarning(level,...)
Definition: zeSyslog.h:112