2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/param.h>
17 #include <sys/socket.h>
21 #include <sys/resource.h>
23 #include <atalk/logger.h>
24 #include <atalk/adouble.h>
25 #include <atalk/compat.h>
26 #include <atalk/dsi.h>
27 #include <atalk/afp.h>
28 #include <atalk/paths.h>
29 #include <atalk/util.h>
30 #include <atalk/server_child.h>
31 #include <atalk/server_ipc.h>
32 #include <atalk/errchk.h>
33 #include <atalk/globals.h>
34 #include <atalk/netatalk_conf.h>
36 #include <event2/event.h>
38 /* how many seconds we wait to shutdown from SIGTERM before we send SIGKILL */
39 #define KILL_GRACETIME 5
41 /* forward declaration */
42 static pid_t run_process(const char *path, ...);
43 static void kill_childs(int count, int sig, ...);
45 /* static variables */
47 static sig_atomic_t got_chldsig;
48 static pid_t afpd_pid = -1, cnid_metad_pid = -1;
49 static uint afpd_restarts, cnid_metad_restarts;
50 static struct event_base *base;
51 struct event *sigterm_ev, *sigquit_ev, *sigchld_ev;
52 static int in_shutdown;
54 /******************************************************************
55 * libevent helper functions
56 ******************************************************************/
58 /* libevent logging callback */
59 static void libevent_logmsg_cb(int severity, const char *msg)
62 case _EVENT_LOG_DEBUG:
63 LOG(log_debug, logtype_default, "libevent: %s", msg);
66 LOG(log_info, logtype_default, "libevent: %s", msg);
69 LOG(log_warning, logtype_default, "libevent: %s", msg);
72 LOG(log_error, logtype_default, "libevent: %s", msg);
75 LOG(log_error, logtype_default, "libevent: %s", msg);
76 break; /* never reached */
80 /******************************************************************
81 * libevent event callbacks
82 ******************************************************************/
84 /* SIGTERM callback */
85 static void sigterm_cb(evutil_socket_t fd, short what, void *arg)
87 LOG(log_note, logtype_afpd, "Exiting on SIGTERM");
89 event_base_loopbreak(base);
92 /* SIGQUIT callback */
93 static void sigquit_cb(evutil_socket_t fd, short what, void *arg)
95 LOG(log_note, logtype_afpd, "Exiting on SIGQUIT");
97 event_base_loopbreak(base);
100 /* SIGCHLD callback */
101 static void sigchld_cb(evutil_socket_t fd, short what, void *arg)
106 LOG(log_debug, logtype_afpd, "Got SIGCHLD event");
108 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
109 if (WIFEXITED(status)) {
110 if (WEXITSTATUS(status))
111 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
113 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
115 if (WIFSIGNALED(status))
116 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
118 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
121 if (pid == afpd_pid) {
127 LOG(log_note, logtype_afpd, "Restarting 'afpd' (restarts: %u)", afpd_restarts);
128 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
129 LOG(log_error, logtype_afpd, "Error starting 'afpd'");
132 } else if (pid = cnid_metad_pid) {
137 cnid_metad_restarts++;
138 LOG(log_note, logtype_afpd, "Restarting 'cnid_metad' (restarts: %u)", cnid_metad_restarts);
139 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
140 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
144 LOG(log_error, logtype_afpd, "Bad pid: %d", pid);
148 if (in_shutdown && afpd_pid == -1 && cnid_metad_pid == -1)
149 event_base_loopbreak(base);
152 /******************************************************************
154 ******************************************************************/
156 /* kill "count" processes passed as varargs of type "pid_t *" */
157 static void kill_childs(int count, int sig, ...)
165 pid = va_arg(args, pid_t *);
173 /* this get called when error conditions are met that require us to exit gracefully */
174 static void netatalk_exit(int ret)
176 server_unlock(_PATH_NETATALK_LOCK);
180 /* this forks() and exec() "path" with varags as argc[] */
181 static pid_t run_process(const char *path, ...)
188 if ((pid = fork()) < 0) {
189 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
194 myargv[i++] = (char *)path;
195 va_start(args, path);
196 while ((myargv[i++] = va_arg(args, char *)) != NULL)
200 ret = execv(path, myargv);
202 /* Yikes! We're still here, so exec failed... */
203 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
209 static void usage(void)
211 printf("usage: netatalk [-F configfile] \n");
214 int main(int argc, char **argv)
216 const char *configfile = NULL;
217 int c, ret, debug = 0;
220 /* Log SIGBUS/SIGSEGV SBT */
223 while ((c = getopt(argc, argv, ":dF:")) != -1) {
229 obj.cmdlineconfigfile = strdup(optarg);
237 if (check_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
240 if (!debug && daemonize(0, 0) != 0)
243 if (create_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
246 sigfillset(&blocksigs);
247 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
249 if (afp_config_parse(&obj) != 0)
250 netatalk_exit(EXITERR_CONF);
252 set_processname("netatalk");
253 setuplog(obj.options.logconfig, obj.options.logfile);
254 event_set_log_callback(libevent_logmsg_cb);
255 event_set_fatal_callback(netatalk_exit);
257 LOG(log_note, logtype_default, "Netatalk AFP server starting");
259 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
260 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
261 netatalk_exit(EXITERR_CONF);
264 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
265 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
266 netatalk_exit(EXITERR_CONF);
269 if ((base = event_base_new()) == NULL) {
270 LOG(log_error, logtype_afpd, "Error starting event loop");
271 netatalk_exit(EXITERR_CONF);
274 sigterm_ev = event_new(base, SIGTERM, EV_SIGNAL, sigterm_cb, NULL);
275 sigquit_ev = event_new(base, SIGQUIT, EV_SIGNAL, sigquit_cb, NULL);
276 sigchld_ev = event_new(base, SIGCHLD, EV_SIGNAL | EV_PERSIST, sigchld_cb, NULL);
278 event_add(sigterm_ev, NULL);
279 event_add(sigquit_ev, NULL);
280 event_add(sigchld_ev, NULL);
282 sigfillset(&blocksigs);
283 sigdelset(&blocksigs, SIGTERM);
284 sigdelset(&blocksigs, SIGQUIT);
285 sigdelset(&blocksigs, SIGCHLD);
286 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
288 /* run the event loop */
289 ret = event_base_dispatch(base);
291 /* got SIGTERM or similar, so we're going to shutdown */
293 /* block any signal but SIGCHLD */
294 sigfillset(&blocksigs);
295 sigdelset(&blocksigs, SIGCHLD);
296 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
298 /* setup new events: remove SIGTERM and SIGQUIT cbs, add timeout */
300 tv.tv_sec = KILL_GRACETIME;
302 event_base_loopexit(base, &tv);
303 event_del(sigterm_ev);
304 event_del(sigquit_ev);
306 /* run the event loop again, waiting for child to exit on SIGTERM for KILL_GRACETIME seconds */
307 kill_childs(2, SIGTERM, &afpd_pid, &cnid_metad_pid);
308 ret = event_base_dispatch(base);
310 if (afpd_pid != -1 || cnid_metad_pid != -1) {
312 LOG(log_error, logtype_afpd, "AFP service did not shutdown, killing it");
313 if (cnid_metad_pid != -1)
314 LOG(log_error, logtype_afpd, "CNID database service did not shutdown, killing it");
315 kill_childs(2, SIGKILL, &afpd_pid, &cnid_metad_pid);