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 pid_t afpd_pid = -1, cnid_metad_pid = -1;
56 static uint afpd_restarts, cnid_metad_restarts;
57 static struct event_base *base;
58 struct event *sigterm_ev, *sigquit_ev, *sigchld_ev, *timer_ev;
59 static int in_shutdown;
61 /******************************************************************
62 * libevent helper functions
63 ******************************************************************/
65 /* libevent logging callback */
66 static void libevent_logmsg_cb(int severity, const char *msg)
69 case _EVENT_LOG_DEBUG:
70 LOG(log_debug, logtype_default, "libevent: %s", msg);
73 LOG(log_info, logtype_default, "libevent: %s", msg);
76 LOG(log_warning, logtype_default, "libevent: %s", msg);
79 LOG(log_error, logtype_default, "libevent: %s", msg);
82 LOG(log_error, logtype_default, "libevent: %s", msg);
83 break; /* never reached */
87 /******************************************************************
88 * libevent event callbacks
89 ******************************************************************/
91 /* SIGTERM callback */
92 static void sigterm_cb(evutil_socket_t fd, short what, void *arg)
97 LOG(log_info, logtype_afpd, "Exiting on SIGTERM");
103 /* block any signal but SIGCHLD */
105 sigdelset(&sigs, SIGCHLD);
106 sigprocmask(SIG_SETMASK, &sigs, NULL);
108 /* add 10 sec timeout timer, remove all events but SIGCHLD */
109 tv.tv_sec = KILL_GRACETIME;
111 event_base_loopexit(base, &tv);
112 event_del(sigterm_ev);
113 event_del(sigquit_ev);
116 kill_childs(SIGTERM, &afpd_pid, &cnid_metad_pid, NULL);
119 /* SIGQUIT callback */
120 static void sigquit_cb(evutil_socket_t fd, short what, void *arg)
122 LOG(log_note, logtype_afpd, "Exiting on SIGQUIT");
123 kill_childs(SIGQUIT, &afpd_pid, &cnid_metad_pid, NULL);
126 /* SIGQUIT callback */
127 static void sighup_cb(evutil_socket_t fd, short what, void *arg)
129 LOG(log_note, logtype_afpd, "Received SIGHUP, sending all processes signal to reload config");
130 kill_childs(SIGHUP, &afpd_pid, &cnid_metad_pid, NULL);
133 /* SIGCHLD callback */
134 static void sigchld_cb(evutil_socket_t fd, short what, void *arg)
139 LOG(log_debug, logtype_afpd, "Got SIGCHLD event");
141 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
142 if (WIFEXITED(status)) {
143 if (WEXITSTATUS(status))
144 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
146 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
148 if (WIFSIGNALED(status))
149 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
151 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
156 else if (pid == cnid_metad_pid)
159 LOG(log_error, logtype_afpd, "Bad pid: %d", pid);
162 if (in_shutdown && afpd_pid == -1 && cnid_metad_pid == -1)
163 event_base_loopbreak(base);
167 static void timer_cb(evutil_socket_t fd, short what, void *arg)
172 if (afpd_pid == -1) {
174 LOG(log_note, logtype_afpd, "Restarting 'afpd' (restarts: %u)", afpd_restarts);
175 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
176 LOG(log_error, logtype_afpd, "Error starting 'afpd'");
180 if (cnid_metad_pid == -1) {
181 cnid_metad_restarts++;
182 LOG(log_note, logtype_afpd, "Restarting 'cnid_metad' (restarts: %u)", cnid_metad_restarts);
183 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
184 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
189 /******************************************************************
191 ******************************************************************/
193 /* kill processes passed as varargs of type "pid_t *", terminate list with NULL */
194 static void kill_childs(int sig, ...)
201 while ((pid = va_arg(args, pid_t *)) != NULL) {
209 /* this get called when error conditions are met that require us to exit gracefully */
210 static void netatalk_exit(int ret)
212 server_unlock(PATH_NETATALK_LOCK);
216 /* this forks() and exec() "path" with varags as argc[] */
217 static pid_t run_process(const char *path, ...)
224 if ((pid = fork()) < 0) {
225 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
230 myargv[i++] = (char *)path;
231 va_start(args, path);
232 while ((myargv[i++] = va_arg(args, char *)) != NULL)
236 ret = execv(path, myargv);
238 /* Yikes! We're still here, so exec failed... */
239 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
245 static void usage(void)
247 printf("usage: netatalk [-F configfile] \n");
250 int main(int argc, char **argv)
252 int c, ret, debug = 0;
256 /* Log SIGBUS/SIGSEGV SBT */
259 while ((c = getopt(argc, argv, ":dF:")) != -1) {
265 obj.cmdlineconfigfile = strdup(optarg);
273 if (check_lockfile("netatalk", PATH_NETATALK_LOCK) != 0)
276 if (!debug && daemonize(0, 0) != 0)
279 if (create_lockfile("netatalk", PATH_NETATALK_LOCK) != 0)
282 sigfillset(&blocksigs);
283 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
285 if (afp_config_parse(&obj, "netatalk") != 0)
286 netatalk_exit(EXITERR_CONF);
288 event_set_log_callback(libevent_logmsg_cb);
289 event_set_fatal_callback(netatalk_exit);
291 LOG(log_note, logtype_default, "Netatalk AFP server starting");
293 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
294 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
295 netatalk_exit(EXITERR_CONF);
298 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
299 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
300 netatalk_exit(EXITERR_CONF);
303 if ((base = event_base_new()) == NULL) {
304 LOG(log_error, logtype_afpd, "Error starting event loop");
305 netatalk_exit(EXITERR_CONF);
308 sigterm_ev = event_new(base, SIGTERM, EV_SIGNAL, sigterm_cb, NULL);
309 sigquit_ev = event_new(base, SIGQUIT, EV_SIGNAL | EV_PERSIST, sigquit_cb, NULL);
310 sigquit_ev = event_new(base, SIGHUP, EV_SIGNAL | EV_PERSIST, sighup_cb, NULL);
311 sigchld_ev = event_new(base, SIGCHLD, EV_SIGNAL | EV_PERSIST, sigchld_cb, NULL);
312 timer_ev = event_new(base, -1, EV_PERSIST, timer_cb, NULL);
317 event_add(sigterm_ev, NULL);
318 event_add(sigquit_ev, NULL);
319 event_add(sigchld_ev, NULL);
320 event_add(timer_ev, &tv);
322 sigfillset(&blocksigs);
323 sigdelset(&blocksigs, SIGTERM);
324 sigdelset(&blocksigs, SIGQUIT);
325 sigdelset(&blocksigs, SIGCHLD);
326 sigdelset(&blocksigs, SIGHUP);
327 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
329 /* run the event loop */
330 ret = event_base_dispatch(base);
332 if (afpd_pid != -1 || cnid_metad_pid != -1) {
334 LOG(log_error, logtype_afpd, "AFP service did not shutdown, killing it");
335 if (cnid_metad_pid != -1)
336 LOG(log_error, logtype_afpd, "CNID database service did not shutdown, killing it");
337 kill_childs(SIGKILL, &afpd_pid, &cnid_metad_pid, NULL);
340 LOG(log_note, logtype_afpd, "Netatalk AFP server exiting");