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 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, *timer_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)
90 LOG(log_info, logtype_afpd, "Exiting on SIGTERM");
96 /* block any signal but SIGCHLD */
98 sigdelset(&sigs, SIGCHLD);
99 sigprocmask(SIG_SETMASK, &sigs, NULL);
101 /* add 10 sec timeout timer, remove all events but SIGCHLD */
102 tv.tv_sec = KILL_GRACETIME;
104 event_base_loopexit(base, &tv);
105 event_del(sigterm_ev);
106 event_del(sigquit_ev);
109 kill_childs(SIGTERM, &afpd_pid, &cnid_metad_pid, NULL);
112 /* SIGQUIT callback */
113 static void sigquit_cb(evutil_socket_t fd, short what, void *arg)
115 LOG(log_note, logtype_afpd, "Exiting on SIGQUIT");
116 kill_childs(SIGQUIT, &afpd_pid, &cnid_metad_pid, NULL);
119 /* SIGCHLD callback */
120 static void sigchld_cb(evutil_socket_t fd, short what, void *arg)
125 LOG(log_debug, logtype_afpd, "Got SIGCHLD event");
127 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
128 if (WIFEXITED(status)) {
129 if (WEXITSTATUS(status))
130 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
132 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
134 if (WIFSIGNALED(status))
135 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
137 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
142 else if (pid = cnid_metad_pid)
145 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);
153 static void timer_cb(evutil_socket_t fd, short what, void *arg)
156 LOG(log_debug, logtype_afpd, "looping: %i", i++);
161 if (afpd_pid == -1) {
163 LOG(log_note, logtype_afpd, "Restarting 'afpd' (restarts: %u)", afpd_restarts);
164 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
165 LOG(log_error, logtype_afpd, "Error starting 'afpd'");
169 if (cnid_metad_pid == -1) {
170 cnid_metad_restarts++;
171 LOG(log_note, logtype_afpd, "Restarting 'cnid_metad' (restarts: %u)", cnid_metad_restarts);
172 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
173 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
178 /******************************************************************
180 ******************************************************************/
182 /* kill processes passed as varargs of type "pid_t *", terminate list with NULL */
183 static void kill_childs(int sig, ...)
190 while ((pid = va_arg(args, pid_t *)) != NULL) {
198 /* this get called when error conditions are met that require us to exit gracefully */
199 static void netatalk_exit(int ret)
201 server_unlock(_PATH_NETATALK_LOCK);
205 /* this forks() and exec() "path" with varags as argc[] */
206 static pid_t run_process(const char *path, ...)
213 if ((pid = fork()) < 0) {
214 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
219 myargv[i++] = (char *)path;
220 va_start(args, path);
221 while ((myargv[i++] = va_arg(args, char *)) != NULL)
225 ret = execv(path, myargv);
227 /* Yikes! We're still here, so exec failed... */
228 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
234 static void usage(void)
236 printf("usage: netatalk [-F configfile] \n");
239 int main(int argc, char **argv)
241 const char *configfile = NULL;
242 int c, ret, debug = 0;
246 /* Log SIGBUS/SIGSEGV SBT */
249 while ((c = getopt(argc, argv, ":dF:")) != -1) {
255 obj.cmdlineconfigfile = strdup(optarg);
263 if (check_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
266 if (!debug && daemonize(0, 0) != 0)
269 if (create_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
272 sigfillset(&blocksigs);
273 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
275 if (afp_config_parse(&obj, "netatalk") != 0)
276 netatalk_exit(EXITERR_CONF);
278 event_set_log_callback(libevent_logmsg_cb);
279 event_set_fatal_callback(netatalk_exit);
281 LOG(log_note, logtype_default, "Netatalk AFP server starting");
283 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
284 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
285 netatalk_exit(EXITERR_CONF);
288 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
289 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
290 netatalk_exit(EXITERR_CONF);
293 if ((base = event_base_new()) == NULL) {
294 LOG(log_error, logtype_afpd, "Error starting event loop");
295 netatalk_exit(EXITERR_CONF);
298 sigterm_ev = event_new(base, SIGTERM, EV_SIGNAL, sigterm_cb, NULL);
299 sigquit_ev = event_new(base, SIGQUIT, EV_SIGNAL | EV_PERSIST, sigquit_cb, NULL);
300 sigchld_ev = event_new(base, SIGCHLD, EV_SIGNAL | EV_PERSIST, sigchld_cb, NULL);
301 timer_ev = event_new(base, -1, EV_PERSIST, timer_cb, NULL);
306 event_add(sigterm_ev, NULL);
307 event_add(sigquit_ev, NULL);
308 event_add(sigchld_ev, NULL);
309 event_add(timer_ev, &tv);
311 sigfillset(&blocksigs);
312 sigdelset(&blocksigs, SIGTERM);
313 sigdelset(&blocksigs, SIGQUIT);
314 sigdelset(&blocksigs, SIGCHLD);
315 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
317 /* run the event loop */
318 ret = event_base_dispatch(base);
320 if (afpd_pid != -1 || cnid_metad_pid != -1) {
322 LOG(log_error, logtype_afpd, "AFP service did not shutdown, killing it");
323 if (cnid_metad_pid != -1)
324 LOG(log_error, logtype_afpd, "CNID database service did not shutdown, killing it");
325 kill_childs(SIGKILL, &afpd_pid, &cnid_metad_pid, NULL);
328 LOG(log_note, logtype_afpd, "Netatalk AFP server exiting");