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 /* forward declaration */
39 static pid_t run_process(const char *path, ...);
41 /* static variables */
43 static sig_atomic_t got_chldsig;
44 static pid_t afpd_pid = -1, cnid_metad_pid = -1;
45 static uint afpd_restarts, cnid_metad_restarts;
46 static struct event_base *base;
49 static void libevent_logmsg_cb(int severity, const char *msg)
52 case _EVENT_LOG_DEBUG:
53 LOG(log_debug, logtype_default, "libevent: %s", msg);
56 LOG(log_info, logtype_default, "libevent: %s", msg);
59 LOG(log_warning, logtype_default, "libevent: %s", msg);
62 LOG(log_error, logtype_default, "libevent: %s", msg);
65 LOG(log_error, logtype_default, "libevent: %s", msg);
66 break; /* never reached */
70 static void sigterm_cb(evutil_socket_t fd, short what, void *arg)
72 LOG(log_note, logtype_afpd, "Exiting on SIGTERM");
74 kill(afpd_pid, SIGTERM);
75 if (cnid_metad_pid != -1)
76 kill(cnid_metad_pid, SIGTERM);
77 server_unlock(_PATH_NETATALK_LOCK);
78 event_base_loopbreak(base);
81 static void sigquit_cb(evutil_socket_t fd, short what, void *arg)
83 LOG(log_note, logtype_afpd, "Exiting on SIGQUIT");
85 kill(afpd_pid, SIGTERM);
86 if (cnid_metad_pid != -1)
87 kill(cnid_metad_pid, SIGTERM);
88 server_unlock(_PATH_NETATALK_LOCK);
89 event_base_loopbreak(base);
92 static void sigchld_cb(evutil_socket_t fd, short what, void *arg)
97 LOG(log_debug, logtype_afpd, "Got SIGCHLD event");
99 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
100 if (WIFEXITED(status)) {
101 if (WEXITSTATUS(status))
102 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
104 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
106 if (WIFSIGNALED(status))
107 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
109 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
112 if (pid == afpd_pid) {
115 LOG(log_note, logtype_afpd, "Restarting 'afpd' (restarts: %u)", afpd_restarts);
116 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
117 LOG(log_error, logtype_afpd, "Error starting 'afpd'");
119 } else if (pid = cnid_metad_pid) {
121 cnid_metad_restarts++;
122 LOG(log_note, logtype_afpd, "Restarting 'cnid_metad' (restarts: %u)", cnid_metad_restarts);
123 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
124 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
127 LOG(log_error, logtype_afpd, "Bad pid: %d", pid);
132 /* other functions */
134 static void netatalk_exit(int ret)
137 kill(afpd_pid, SIGTERM);
138 if (cnid_metad_pid != -1)
139 kill(cnid_metad_pid, SIGTERM);
140 server_unlock(_PATH_NETATALK_LOCK);
144 static pid_t run_process(const char *path, ...)
151 if ((pid = fork()) < 0) {
152 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
157 myargv[i++] = (char *)path;
158 va_start(args, path);
159 while ((myargv[i++] = va_arg(args, char *)) != NULL)
163 ret = execv(path, myargv);
165 /* Yikes! We're still here, so exec failed... */
166 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
172 static void usage(void)
174 printf("usage: netatalk [-F configfile] \n");
177 int main(int argc, char **argv)
179 const char *configfile = NULL;
180 int c, ret, debug = 0;
183 /* Log SIGBUS/SIGSEGV SBT */
186 while ((c = getopt(argc, argv, ":dF:")) != -1) {
192 obj.cmdlineconfigfile = strdup(optarg);
200 if (check_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
203 if (!debug && daemonize(0, 0) != 0)
206 if (create_lockfile("netatalk", _PATH_NETATALK_LOCK) != 0)
209 sigfillset(&blocksigs);
210 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
212 if (afp_config_parse(&obj) != 0)
213 netatalk_exit(EXITERR_CONF);
215 set_processname("netatalk");
216 setuplog(obj.options.logconfig, obj.options.logfile);
217 event_set_log_callback(libevent_logmsg_cb);
218 event_set_fatal_callback(netatalk_exit);
220 LOG(log_note, logtype_default, "Netatalk AFP server starting");
222 if ((afpd_pid = run_process(_PATH_AFPD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
223 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
224 netatalk_exit(EXITERR_CONF);
227 if ((cnid_metad_pid = run_process(_PATH_CNID_METAD, "-d", "-F", obj.options.configfile, NULL)) == -1) {
228 LOG(log_error, logtype_afpd, "Error starting 'cnid_metad'");
229 netatalk_exit(EXITERR_CONF);
232 if ((base = event_base_new()) == NULL) {
233 LOG(log_error, logtype_afpd, "Error starting event loop");
234 netatalk_exit(EXITERR_CONF);
237 struct event *sigterm_ev, *sigquit_ev, *sigchld_ev;
238 sigterm_ev = event_new(base, SIGTERM, EV_SIGNAL|EV_PERSIST, sigterm_cb, NULL);
239 sigquit_ev = event_new(base, SIGQUIT, EV_SIGNAL|EV_PERSIST, sigquit_cb, NULL);
240 sigchld_ev = event_new(base, SIGCHLD, EV_SIGNAL|EV_PERSIST, sigchld_cb, NULL);
242 event_add(sigterm_ev, NULL);
243 event_add(sigquit_ev, NULL);
244 event_add(sigchld_ev, NULL);
246 sigfillset(&blocksigs);
247 sigdelset(&blocksigs, SIGTERM);
248 sigdelset(&blocksigs, SIGQUIT);
249 sigdelset(&blocksigs, SIGCHLD);
250 sigprocmask(SIG_SETMASK, &blocksigs, NULL);
252 /* run the event loop */
253 ret = event_base_dispatch(base);