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 "afp_config.h"
40 #include "afp_zeroconf.h"
43 #define AFP_LISTENERS 32
44 #define FDSET_SAFETY 5
46 unsigned char nologin = 0;
49 static server_child_t *server_children;
50 static sig_atomic_t reloadconfig = 0;
51 static sig_atomic_t gotsigchld = 0;
53 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
54 static struct pollfd *fdset;
55 static struct polldata *polldata;
56 static int fdset_size; /* current allocated size */
57 static int fdset_used; /* number of used elements */
59 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children);
61 static void afp_exit(int ret)
68 initialize fd set we are waiting for.
70 static void fd_set_listening_sockets(const AFPObj *config)
74 for (dsi = config->dsi; dsi; dsi = dsi->next) {
75 fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
86 static void fd_reset_listening_sockets(const AFPObj *config)
90 for (dsi = config->dsi; dsi; dsi = dsi->next) {
91 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, dsi->serversock);
95 /* ------------------ */
96 static void afp_goaway(int sig)
102 LOG(log_note, logtype_afpd, "AFP Server shutting down");
104 server_child_kill(server_children, SIGTERM);
111 LOG(log_info, logtype_afpd, "disallowing logins");
114 server_child_kill(server_children, sig);
118 /* w/ a configuration file, we can force a re-read if we want */
123 /* w/ a configuration file, we can force a re-read if we want */
128 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
133 static void child_handler(void)
140 #define WAIT_ANY (-1)
141 #endif /* ! WAIT_ANY */
143 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
144 if (WIFEXITED(status)) {
145 if (WEXITSTATUS(status))
146 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
148 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
150 if (WIFSIGNALED(status))
151 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
153 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
156 fd = server_child_remove(server_children, pid);
160 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
164 static int setlimits(void)
168 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
169 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
172 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
173 rlim.rlim_cur = 65535;
174 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
175 rlim.rlim_max = 65535;
176 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
177 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
184 int main(int ac, char **av)
190 /* Parse argv args and initialize default options */
191 afp_options_parse_cmdline(&obj, ac, av);
193 if (!(obj.cmdlineflags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
196 /* Log SIGBUS/SIGSEGV SBT */
199 if (afp_config_parse(&obj, "afpd") != 0)
200 afp_exit(EXITERR_CONF);
202 /* Save the user's current umask */
203 obj.options.save_mask = umask(obj.options.umask);
205 /* install child handler for asp and dsi. we do this before afp_goaway
206 * as afp_goaway references stuff from here.
207 * XXX: this should really be setup after the initial connections. */
208 if (!(server_children = server_child_alloc(obj.options.connections))) {
209 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
210 afp_exit(EXITERR_SYS);
214 pthread_sigmask(SIG_SETMASK, &sigs, NULL);
216 memset(&sv, 0, sizeof(sv));
217 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
218 even if the file is open with O_LARGEFILE ! */
220 sv.sa_handler = SIG_IGN;
221 sigemptyset( &sv.sa_mask );
222 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
223 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
224 afp_exit(EXITERR_SYS);
228 sv.sa_handler = SIG_IGN;
229 sigemptyset( &sv.sa_mask );
230 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
231 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
232 afp_exit(EXITERR_SYS);
235 sv.sa_handler = afp_goaway; /* handler for all sigs */
237 sigemptyset( &sv.sa_mask );
238 sigaddset(&sv.sa_mask, SIGALRM);
239 sigaddset(&sv.sa_mask, SIGHUP);
240 sigaddset(&sv.sa_mask, SIGTERM);
241 sigaddset(&sv.sa_mask, SIGUSR1);
242 sigaddset(&sv.sa_mask, SIGQUIT);
243 sv.sa_flags = SA_RESTART;
244 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
245 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
246 afp_exit(EXITERR_SYS);
249 sigemptyset( &sv.sa_mask );
250 sigaddset(&sv.sa_mask, SIGALRM);
251 sigaddset(&sv.sa_mask, SIGTERM);
252 sigaddset(&sv.sa_mask, SIGHUP);
253 sigaddset(&sv.sa_mask, SIGCHLD);
254 sigaddset(&sv.sa_mask, SIGQUIT);
255 sv.sa_flags = SA_RESTART;
256 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
257 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
258 afp_exit(EXITERR_SYS);
261 sigemptyset( &sv.sa_mask );
262 sigaddset(&sv.sa_mask, SIGALRM);
263 sigaddset(&sv.sa_mask, SIGTERM);
264 sigaddset(&sv.sa_mask, SIGUSR1);
265 sigaddset(&sv.sa_mask, SIGCHLD);
266 sigaddset(&sv.sa_mask, SIGQUIT);
267 sv.sa_flags = SA_RESTART;
268 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
269 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
270 afp_exit(EXITERR_SYS);
273 sigemptyset( &sv.sa_mask );
274 sigaddset(&sv.sa_mask, SIGALRM);
275 sigaddset(&sv.sa_mask, SIGHUP);
276 sigaddset(&sv.sa_mask, SIGUSR1);
277 sigaddset(&sv.sa_mask, SIGCHLD);
278 sigaddset(&sv.sa_mask, SIGQUIT);
279 sv.sa_flags = SA_RESTART;
280 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
281 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
282 afp_exit(EXITERR_SYS);
285 sigemptyset( &sv.sa_mask );
286 sigaddset(&sv.sa_mask, SIGALRM);
287 sigaddset(&sv.sa_mask, SIGHUP);
288 sigaddset(&sv.sa_mask, SIGUSR1);
289 sigaddset(&sv.sa_mask, SIGCHLD);
290 sigaddset(&sv.sa_mask, SIGTERM);
291 sv.sa_flags = SA_RESTART;
292 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
293 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
294 afp_exit(EXITERR_SYS);
297 /* afp.conf: not in config file: lockfile, configfile
298 * preference: command-line provides defaults.
299 * config file over-writes defaults.
301 * we also need to make sure that killing afpd during startup
302 * won't leave any lingering registered names around.
306 sigaddset(&sigs, SIGALRM);
307 sigaddset(&sigs, SIGHUP);
308 sigaddset(&sigs, SIGUSR1);
310 /* don't block SIGTERM */
311 sigaddset(&sigs, SIGTERM);
313 sigaddset(&sigs, SIGCHLD);
315 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
316 #ifdef HAVE_DBUS_GLIB
317 /* Run dbus AFP statics thread */
318 if (obj.options.flags & OPTION_DBUS_AFPSTATS)
319 (void)afpstats_init(server_children);
321 if (configinit(&obj) != 0) {
322 LOG(log_error, logtype_afpd, "main: no servers configured");
323 afp_exit(EXITERR_CONF);
325 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
330 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
331 fd_set_listening_sockets(&obj);
341 /* wait for an appleshare connection. parent remains in the loop
342 * while the children get handled by afp_over_{asp,dsi}. this is
343 * currently vulnerable to a denial-of-service attack if a
344 * connection is made without an actual login attempt being made
345 * afterwards. establishing timeouts for logins is a possible
348 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
349 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
350 ret = poll(fdset, fdset_used, -1);
351 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
363 fd_reset_listening_sockets(&obj);
365 LOG(log_info, logtype_afpd, "re-reading configuration file");
367 configfree(&obj, NULL);
368 afp_config_free(&obj);
370 if (afp_config_parse(&obj, "afpd") != 0)
371 afp_exit(EXITERR_CONF);
373 if (configinit(&obj) != 0) {
374 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
375 afp_exit(EXITERR_CONF);
378 fd_set_listening_sockets(&obj);
392 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
396 for (int i = 0; i < fdset_used; i++) {
397 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
398 switch (polldata[i].fdtype) {
401 if ((child = dsi_start(&obj, (DSI *)polldata[i].data, server_children))) {
402 /* Add IPC fd to select fd set */
403 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
415 child = (afp_child_t *)polldata[i].data;
416 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->afpch_pid);
418 if (ipc_server_read(server_children, child->afpch_ipc_fd) != 0) {
419 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->afpch_ipc_fd);
420 close(child->afpch_ipc_fd);
421 child->afpch_ipc_fd = -1;
426 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
436 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children)
438 afp_child_t *child = NULL;
440 if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
441 LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
447 configfree(obj, dsi);
448 afp_over_dsi(obj); /* start a session */