2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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.
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.
16 #endif /* HAVE_CONFIG_H */
22 #include <sys/param.h>
25 #include <sys/socket.h>
29 #include <sys/resource.h>
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>
44 #include <event2/event.h>
46 /* how many seconds we wait to shutdown from SIGTERM before we send SIGKILL */
47 #define KILL_GRACETIME 5
49 /* forward declaration */
50 static pid_t run_process(const char *path, ...);
51 static void kill_childs(int sig, ...);
53 /* static variables */
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;
62 /******************************************************************
63 * libevent helper functions
64 ******************************************************************/
66 /* libevent logging callback */
67 static void libevent_logmsg_cb(int severity, const char *msg)
70 case _EVENT_LOG_DEBUG:
71 LOG(log_debug, logtype_default, "libevent: %s", msg);
74 LOG(log_info, logtype_default, "libevent: %s", msg);
77 LOG(log_warning, logtype_default, "libevent: %s", msg);
80 LOG(log_error, logtype_default, "libevent: %s", msg);
83 LOG(log_error, logtype_default, "libevent: %s", msg);
84 break; /* never reached */
88 /******************************************************************
89 * libevent event callbacks
90 ******************************************************************/
92 /* SIGTERM callback */
93 static void sigterm_cb(evutil_socket_t fd, short what, void *arg)
98 LOG(log_info, logtype_afpd, "Exiting on SIGTERM");
104 /* block any signal but SIGCHLD */
106 sigdelset(&sigs, SIGCHLD);
107 sigprocmask(SIG_SETMASK, &sigs, NULL);
109 /* add 10 sec timeout timer, remove all events but SIGCHLD */
110 tv.tv_sec = KILL_GRACETIME;
112 event_base_loopexit(base, &tv);
113 event_del(sigterm_ev);
114 event_del(sigquit_ev);
117 kill_childs(SIGTERM, &afpd_pid, &cnid_metad_pid, NULL);
120 /* SIGQUIT callback */
121 static void sigquit_cb(evutil_socket_t fd, short what, void *arg)
123 LOG(log_note, logtype_afpd, "Exiting on SIGQUIT");
124 kill_childs(SIGQUIT, &afpd_pid, &cnid_metad_pid, NULL);
127 /* SIGQUIT callback */
128 static void sighup_cb(evutil_socket_t fd, short what, void *arg)
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);
134 /* SIGCHLD callback */
135 static void sigchld_cb(evutil_socket_t fd, short what, void *arg)
140 LOG(log_debug, logtype_afpd, "Got SIGCHLD event");
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));
147 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
149 if (WIFSIGNALED(status))
150 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
152 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
157 else if (pid == cnid_metad_pid)
160 LOG(log_error, logtype_afpd, "Bad pid: %d", pid);
163 if (in_shutdown && afpd_pid == -1 && cnid_metad_pid == -1)
164 event_base_loopbreak(base);
168 static void timer_cb(evutil_socket_t fd, short what, void *arg)
175 if (afpd_pid == -1) {
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'");
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'");
192 /******************************************************************
194 ******************************************************************/
196 /* kill processes passed as varargs of type "pid_t *", terminate list with NULL */
197 static void kill_childs(int sig, ...)
204 while ((pid = va_arg(args, pid_t *)) != NULL) {
212 /* this get called when error conditions are met that require us to exit gracefully */
213 static void netatalk_exit(int ret)
215 server_unlock(_PATH_NETATALK_LOCK);
219 /* this forks() and exec() "path" with varags as argc[] */
220 static pid_t run_process(const char *path, ...)
227 if ((pid = fork()) < 0) {
228 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
233 myargv[i++] = (char *)path;
234 va_start(args, path);
235 while ((myargv[i++] = va_arg(args, char *)) != NULL)
239 ret = execv(path, myargv);
241 /* Yikes! We're still here, so exec failed... */
242 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
248 static void usage(void)
250 printf("usage: netatalk [-F configfile] \n");
253 int main(int argc, char **argv)
255 const char *configfile = NULL;
256 int c, ret, debug = 0;
260 /* Log SIGBUS/SIGSEGV SBT */
263 while ((c = getopt(argc, argv, ":dF:")) != -1) {
269 obj.cmdlineconfigfile = strdup(optarg);
277 if (check_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
280 if (!debug && daemonize(0, 0) != 0)
283 if (create_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
286 sigfillset(&blocksigs);
287 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
289 if (afp_config_parse(&obj, "netatalk") != 0)
290 netatalk_exit(EXITERR_CONF);
292 event_set_log_callback(libevent_logmsg_cb);
293 event_set_fatal_callback(netatalk_exit);
295 LOG(log_note, logtype_default, "Netatalk AFP server starting");
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);
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);
307 if ((base = event_base_new()) == NULL) {
308 LOG(log_error, logtype_afpd, "Error starting event loop");
309 netatalk_exit(EXITERR_CONF);
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);
321 event_add(sigterm_ev, NULL);
322 event_add(sigquit_ev, NULL);
323 event_add(sigchld_ev, NULL);
324 event_add(timer_ev, &tv);
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);
333 /* run the event loop */
334 ret = event_base_dispatch(base);
336 if (afpd_pid != -1 || cnid_metad_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);
344 LOG(log_note, logtype_afpd, "Netatalk AFP server exiting");