]> arthur.barton.de Git - netatalk.git/blob - etc/netatalk/netatalk.c
Add correct copyright and license
[netatalk.git] / etc / netatalk / netatalk.c
1 /* 
2    Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 */
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif /* HAVE_CONFIG_H */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <signal.h>
22 #include <sys/param.h>
23 #include <sys/uio.h>
24 #include <sys/time.h>
25 #include <sys/socket.h>
26 #include <sys/poll.h>
27 #include <errno.h>
28 #include <sys/wait.h>
29 #include <sys/resource.h>
30
31 #include <atalk/logger.h>
32 #include <atalk/adouble.h>
33 #include <atalk/compat.h>
34 #include <atalk/dsi.h>
35 #include <atalk/afp.h>
36 #include <atalk/paths.h>
37 #include <atalk/util.h>
38 #include <atalk/server_child.h>
39 #include <atalk/server_ipc.h>
40 #include <atalk/errchk.h>
41 #include <atalk/globals.h>
42 #include <atalk/netatalk_conf.h>
43
44 #include <event2/event.h>
45
46 /* how many seconds we wait to shutdown from SIGTERM before we send SIGKILL */
47 #define KILL_GRACETIME 5
48
49 /* forward declaration */
50 static pid_t run_process(const char *path, ...);
51 static void kill_childs(int sig, ...);
52
53 /* static variables */
54 static AFPObj obj;
55 static sig_atomic_t got_chldsig;
56 static pid_t afpd_pid = -1,  cnid_metad_pid = -1;
57 static uint afpd_restarts, cnid_metad_restarts;
58 static struct event_base *base;
59 struct event *sigterm_ev, *sigquit_ev, *sigchld_ev, *timer_ev;
60 static int in_shutdown;
61
62 /******************************************************************
63  * libevent helper functions
64  ******************************************************************/
65
66 /* libevent logging callback */
67 static void libevent_logmsg_cb(int severity, const char *msg)
68 {
69     switch (severity) {
70     case _EVENT_LOG_DEBUG:
71         LOG(log_debug, logtype_default, "libevent: %s", msg);
72         break;
73     case _EVENT_LOG_MSG:
74         LOG(log_info, logtype_default, "libevent: %s", msg);
75         break;
76     case _EVENT_LOG_WARN:
77         LOG(log_warning, logtype_default, "libevent: %s", msg);
78         break;
79     case _EVENT_LOG_ERR:
80         LOG(log_error, logtype_default, "libevent: %s", msg);
81         break;
82     default:
83         LOG(log_error, logtype_default, "libevent: %s", msg);
84         break; /* never reached */
85     }
86 }
87
88 /******************************************************************
89  * libevent event callbacks
90  ******************************************************************/
91
92 /* SIGTERM callback */
93 static void sigterm_cb(evutil_socket_t fd, short what, void *arg)
94 {
95     sigset_t sigs;
96     struct timeval tv;
97
98     LOG(log_info, logtype_afpd, "Exiting on SIGTERM");
99
100     if (in_shutdown)
101         return;
102     in_shutdown = 1;
103
104     /* block any signal but SIGCHLD */
105     sigfillset(&sigs);
106     sigdelset(&sigs, SIGCHLD);
107     sigprocmask(SIG_SETMASK, &sigs, NULL);
108
109     /* add 10 sec timeout timer, remove all events but SIGCHLD */
110     tv.tv_sec = KILL_GRACETIME;
111     tv.tv_usec = 0;
112     event_base_loopexit(base, &tv);
113     event_del(sigterm_ev);
114     event_del(sigquit_ev);
115     event_del(timer_ev);
116
117     kill_childs(SIGTERM, &afpd_pid, &cnid_metad_pid, NULL);
118 }
119
120 /* SIGQUIT callback */
121 static void sigquit_cb(evutil_socket_t fd, short what, void *arg)
122 {
123     LOG(log_note, logtype_afpd, "Exiting on SIGQUIT");
124     kill_childs(SIGQUIT, &afpd_pid, &cnid_metad_pid, NULL);
125 }
126
127 /* SIGQUIT callback */
128 static void sighup_cb(evutil_socket_t fd, short what, void *arg)
129 {
130     LOG(log_note, logtype_afpd, "Received SIGHUP, sending all processes signal to reload config");
131     kill_childs(SIGHUP, &afpd_pid, &cnid_metad_pid, NULL);
132 }
133
134 /* SIGCHLD callback */
135 static void sigchld_cb(evutil_socket_t fd, short what, void *arg)
136 {
137     int status, i;
138     pid_t pid;
139
140     LOG(log_debug, logtype_afpd, "Got SIGCHLD event");
141   
142     while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
143         if (WIFEXITED(status)) {
144             if (WEXITSTATUS(status))
145                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
146             else
147                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
148         } else {
149             if (WIFSIGNALED(status))
150                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
151             else
152                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
153         }
154
155         if (pid == afpd_pid)
156             afpd_pid = -1;
157         else if (pid == cnid_metad_pid)
158             cnid_metad_pid = -1;
159         else
160             LOG(log_error, logtype_afpd, "Bad pid: %d", pid);
161     }
162
163     if (in_shutdown && afpd_pid == -1 && cnid_metad_pid == -1)
164         event_base_loopbreak(base);
165 }
166
167 /* timer callback */
168 static void timer_cb(evutil_socket_t fd, short what, void *arg)
169 {
170     static int i = 0;
171
172     if (in_shutdown)
173         return;
174
175     if (afpd_pid == -1) {
176         afpd_restarts++;
177         LOG(log_note, logtype_afpd, "Restarting 'afpd' (restarts: %u)", afpd_restarts);
178         if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
179             LOG(log_error, logtype_afpd, "Error starting 'afpd'");
180         }
181     }
182
183     if (cnid_metad_pid == -1) {
184         cnid_metad_restarts++;
185         LOG(log_note, logtype_afpd, "Restarting 'cnid_metad' (restarts: %u)", cnid_metad_restarts);
186         if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
187             LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
188         }
189     }
190 }
191
192 /******************************************************************
193  * helper functions
194  ******************************************************************/
195
196 /* kill processes passed as varargs of type "pid_t *", terminate list with NULL */
197 static void kill_childs(int sig, ...)
198 {
199     va_list args;
200     pid_t *pid;
201
202     va_start(args, sig);
203
204     while ((pid = va_arg(args, pid_t *)) != NULL) {
205         if (*pid == -1)
206             continue;
207         kill(*pid, sig);
208     }
209     va_end(args);
210 }
211
212 /* this get called when error conditions are met that require us to exit gracefully */
213 static void netatalk_exit(int ret)
214 {
215     server_unlock(_PATH_NETATALK_LOCK);
216     exit(ret);
217 }
218
219 /* this forks() and exec() "path" with varags as argc[] */
220 static pid_t run_process(const char *path, ...)
221 {
222     int ret, i = 0;
223     char *myargv[10];
224     va_list args;
225     pid_t pid;
226
227     if ((pid = fork()) < 0) {
228         LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
229         return -1;
230     }
231
232     if (pid == 0) {
233         myargv[i++] = (char *)path;
234         va_start(args, path);
235         while ((myargv[i++] = va_arg(args, char *)) != NULL)
236             ;
237         va_end(args);
238
239         ret = execv(path, myargv);
240
241         /* Yikes! We're still here, so exec failed... */
242         LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
243         exit(1);
244     }
245     return pid;
246 }
247
248 static void usage(void)
249 {
250     printf("usage: netatalk [-F configfile] \n");
251 }
252
253 int main(int argc, char **argv)
254 {
255     const char *configfile = NULL;
256     int c, ret, debug = 0;
257     sigset_t blocksigs;
258     struct timeval tv;
259
260     /* Log SIGBUS/SIGSEGV SBT */
261     fault_setup(NULL);
262
263     while ((c = getopt(argc, argv, ":dF:")) != -1) {
264         switch(c) {
265         case 'd':
266             debug = 1;
267             break;
268         case 'F':
269             obj.cmdlineconfigfile = strdup(optarg);
270             break;
271         default:
272             usage();
273             exit(EXIT_FAILURE);
274         }
275     }
276
277     if (check_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
278         exit(EXITERR_SYS);
279
280     if (!debug && daemonize(0, 0) != 0)
281         exit(EXITERR_SYS);
282
283     if (create_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
284         exit(EXITERR_SYS);
285
286     sigfillset(&blocksigs);
287     sigprocmask(SIG_SETMASK, &blocksigs, NULL);
288     
289     if (afp_config_parse(&obj, "netatalk") != 0)
290         netatalk_exit(EXITERR_CONF);
291
292     event_set_log_callback(libevent_logmsg_cb);
293     event_set_fatal_callback(netatalk_exit);
294
295     LOG(log_note, logtype_default, "Netatalk AFP server starting");
296
297     if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
298         LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
299         netatalk_exit(EXITERR_CONF);
300     }
301
302     if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
303         LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
304         netatalk_exit(EXITERR_CONF);
305     }
306
307     if ((base = event_base_new()) == NULL) {
308         LOG(log_error, logtype_afpd, "Error starting event loop");
309         netatalk_exit(EXITERR_CONF);
310     }
311
312     sigterm_ev = event_new(base, SIGTERM, EV_SIGNAL, sigterm_cb, NULL);
313     sigquit_ev = event_new(base, SIGQUIT, EV_SIGNAL | EV_PERSIST, sigquit_cb, NULL);
314     sigquit_ev = event_new(base, SIGHUP,  EV_SIGNAL | EV_PERSIST, sighup_cb, NULL);
315     sigchld_ev = event_new(base, SIGCHLD, EV_SIGNAL | EV_PERSIST, sigchld_cb, NULL);
316     timer_ev = event_new(base, -1, EV_PERSIST, timer_cb, NULL);
317
318     tv.tv_sec = 1;
319     tv.tv_usec = 0;
320
321     event_add(sigterm_ev, NULL);
322     event_add(sigquit_ev, NULL);
323     event_add(sigchld_ev, NULL);
324     event_add(timer_ev, &tv);
325
326     sigfillset(&blocksigs);
327     sigdelset(&blocksigs, SIGTERM);
328     sigdelset(&blocksigs, SIGQUIT);
329     sigdelset(&blocksigs, SIGCHLD);
330     sigdelset(&blocksigs, SIGHUP);
331     sigprocmask(SIG_SETMASK, &blocksigs, NULL);
332
333     /* run the event loop */
334     ret = event_base_dispatch(base);
335
336     if (afpd_pid != -1 || cnid_metad_pid != -1) {
337         if (afpd_pid != -1)
338             LOG(log_error, logtype_afpd, "AFP service did not shutdown, killing it");
339         if (cnid_metad_pid != -1)
340             LOG(log_error, logtype_afpd, "CNID database service did not shutdown, killing it");
341         kill_childs(SIGKILL, &afpd_pid, &cnid_metad_pid, NULL);
342     }
343
344     LOG(log_note, logtype_afpd, "Netatalk AFP server exiting");
345
346     netatalk_exit(ret);
347 }