|
24 | 24 |
|
25 | 25 | const int def_stdio_fd[3] = { STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO }; |
26 | 26 |
|
27 | | -void _alert(const char *libdir, const char *msgtype, const char *description) |
28 | | -{ |
29 | | - static char none[1] = ""; |
30 | | - char *cmd; |
31 | | - int silenced __attribute__((unused)); |
32 | | - |
33 | | - if ( description==NULL ) description = none; |
34 | | - |
35 | | - cmd = allocstr("%s/alert '%s' '%s' &",libdir,msgtype,description); |
36 | | - logmsg(LOG_INFO,"executing '%s'",cmd); |
37 | | - |
38 | | - /* Assign return value to temp variable to remove compiler |
39 | | - * warnings. We're already trying to generate a warning; there's |
40 | | - * no sense in generating another warning when this gives an |
41 | | - * error. |
42 | | - */ |
43 | | - silenced = system(cmd); |
44 | | - |
45 | | - free(cmd); |
46 | | -} |
47 | | - |
48 | 27 | int execute(const char *cmd, const char **args, int nargs, int stdio_fd[3], int err2out) |
49 | 28 | { |
50 | 29 | pid_t pid, child_pid; |
@@ -143,112 +122,6 @@ int execute(const char *cmd, const char **args, int nargs, int stdio_fd[3], int |
143 | 122 | return -1; |
144 | 123 | } |
145 | 124 |
|
146 | | -int exitsignalled; |
147 | | - |
148 | | -void sig_handler(int sig) |
149 | | -{ |
150 | | - logmsg(LOG_DEBUG, "Signal %d received", sig); |
151 | | - |
152 | | - switch ( sig ) { |
153 | | - case SIGTERM: |
154 | | - case SIGHUP: |
155 | | - case SIGINT: |
156 | | - exitsignalled = 1; |
157 | | - break; |
158 | | - } |
159 | | -} |
160 | | - |
161 | | -void initsignals() |
162 | | -{ |
163 | | - struct sigaction sa; |
164 | | - sigset_t newmask, oldmask; |
165 | | - |
166 | | - exitsignalled = 0; |
167 | | - |
168 | | - /* unmask all signals */ |
169 | | - memset(&newmask, 0, sizeof(newmask)); |
170 | | - if ( sigprocmask(SIG_SETMASK, &newmask, &oldmask)!=0 ) { |
171 | | - error(errno,"unmasking signals"); |
172 | | - } |
173 | | - |
174 | | - logmsg(LOG_DEBUG, "Installing signal handlers"); |
175 | | - |
176 | | - sa.sa_handler = &sig_handler; |
177 | | - sa.sa_mask = newmask; |
178 | | - sa.sa_flags = 0; |
179 | | - |
180 | | - if ( sigaction(SIGTERM,&sa,NULL)!=0 ) error(errno,"installing signal handler"); |
181 | | - if ( sigaction(SIGHUP ,&sa,NULL)!=0 ) error(errno,"installing signal handler"); |
182 | | - if ( sigaction(SIGINT ,&sa,NULL)!=0 ) error(errno,"installing signal handler"); |
183 | | -} |
184 | | - |
185 | | - |
186 | | -char *pidfile; |
187 | | - |
188 | | -/* Function to remove PID file at process exit. */ |
189 | | -void remove_pidfile() |
190 | | -{ |
191 | | - unlink(pidfile); |
192 | | -} |
193 | | - |
194 | | -void daemonize(const char *_pidfile) |
195 | | -{ |
196 | | - pid_t pid; |
197 | | - int fd, maxfd; |
198 | | - char str[15]; |
199 | | - |
200 | | - switch ( pid = fork() ) { |
201 | | - case -1: error(errno, "cannot fork daemon"); |
202 | | - case 0: break; /* child process: do nothing here. */ |
203 | | - default: _exit(0); /* parent process: exit. */ |
204 | | - } |
205 | | - |
206 | | - /* Check and write PID to file */ |
207 | | - if ( _pidfile!=NULL ) { |
208 | | - pidfile = strdup(_pidfile); |
209 | | - if ( (fd=open(pidfile, O_RDWR|O_CREAT|O_EXCL, 0640))<0 ) { |
210 | | - error(errno, "cannot create pidfile '%s'", pidfile); |
211 | | - } |
212 | | - sprintf(str, "%d\n", pid); |
213 | | - if ( write(fd, str, strlen(str))<(ssize_t)strlen(str) ) { |
214 | | - error(errno, "failed writing PID to file"); |
215 | | - } |
216 | | - if ( close(fd)!=0 ) error(errno, "closing pidfile '%s'", pidfile); |
217 | | - atexit(remove_pidfile); |
218 | | - } |
219 | | - |
220 | | - /* Notify user with daemon PID before detaching from TTY. */ |
221 | | - logmsg(LOG_NOTICE, "daemonizing with PID = %d", pid); |
222 | | - |
223 | | - /* Reopen std{in,out,err} file descriptors to /dev/null. |
224 | | - Closing them gives error when the daemon or a child process |
225 | | - tries to read/write to them. */ |
226 | | - if ( freopen("/dev/null", "r", stdin )!=NULL || |
227 | | - freopen("/dev/null", "w", stdout)!=NULL || |
228 | | - freopen("/dev/null", "w", stderr)!=NULL ) { |
229 | | - error(errno, "cannot reopen stdio files to /dev/null"); |
230 | | - } |
231 | | - |
232 | | - /* Close all other file descriptors. */ |
233 | | - maxfd = sysconf(_SC_OPEN_MAX); |
234 | | - for(fd=3; fd<maxfd; fd++) close(fd); |
235 | | - |
236 | | - /* Start own process group, detached from any tty */ |
237 | | - if ( setsid()<0 ) error(errno, "cannot set daemon process group"); |
238 | | -} |
239 | | - |
240 | | -char *stripendline(char *str) |
241 | | -{ |
242 | | - size_t i, j; |
243 | | - |
244 | | - for(i=0, j=0; str[i]!=0; i++) { |
245 | | - if ( ! (str[i]=='\n' || str[i]=='\r') ) str[j++] = str[i]; |
246 | | - } |
247 | | - |
248 | | - str[j] = 0; |
249 | | - |
250 | | - return str; |
251 | | -} |
252 | 125 |
|
253 | 126 | void version(const char *prog, const char *vers) |
254 | 127 | { |
|
0 commit comments