]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Improve reliability of afpd child handler
[netatalk.git] / etc / afpd / main.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <signal.h>
14 #include <sys/param.h>
15 #include <sys/uio.h>
16 #include <sys/time.h>
17 #include <sys/socket.h>
18 #include <sys/poll.h>
19 #include <errno.h>
20 #include <sys/wait.h>
21 #include <sys/resource.h>
22
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>
35
36 #include "afp_config.h"
37 #include "status.h"
38 #include "fork.h"
39 #include "uam_auth.h"
40 #include "afp_zeroconf.h"
41 #include "afpstats.h"
42
43 #define AFP_LISTENERS 32
44 #define FDSET_SAFETY  5
45
46 unsigned char nologin = 0;
47
48 static AFPObj obj;
49 static server_child_t *server_children;
50 static sig_atomic_t reloadconfig = 0;
51 static sig_atomic_t gotsigchld = 0;
52
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 */
58
59 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children);
60
61 static void afp_exit(int ret)
62 {
63     exit(ret);
64 }
65
66
67 /* ------------------
68    initialize fd set we are waiting for.
69 */
70 static void fd_set_listening_sockets(const AFPObj *config)
71 {
72     DSI *dsi;
73
74     for (dsi = config->dsi; dsi; dsi = dsi->next) {
75         fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
76                      &fdset,
77                      &polldata,
78                      &fdset_used,
79                      &fdset_size,
80                      dsi->serversock,
81                      LISTEN_FD,
82                      dsi);
83     }
84 }
85  
86 static void fd_reset_listening_sockets(const AFPObj *config)
87 {
88     const DSI *dsi;
89
90     for (dsi = config->dsi; dsi; dsi = dsi->next) {
91         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, dsi->serversock);
92     }
93 }
94
95 /* ------------------ */
96 static void afp_goaway(int sig)
97 {
98     switch( sig ) {
99
100     case SIGTERM:
101     case SIGQUIT:
102         LOG(log_note, logtype_afpd, "AFP Server shutting down");
103         if (server_children)
104             server_child_kill(server_children, SIGTERM);
105         _exit(0);
106         break;
107
108     case SIGUSR1 :
109         nologin++;
110         auth_unload();
111         LOG(log_info, logtype_afpd, "disallowing logins");        
112
113         if (server_children)
114             server_child_kill(server_children, sig);
115         break;
116
117     case SIGHUP :
118         /* w/ a configuration file, we can force a re-read if we want */
119         reloadconfig = 1;
120         break;
121
122     case SIGCHLD:
123         /* w/ a configuration file, we can force a re-read if we want */
124         gotsigchld = 1;
125         break;
126
127     default :
128         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
129     }
130     return;
131 }
132
133 static void child_handler(void)
134 {
135     int fd;
136     int status, i;
137     pid_t pid;
138   
139 #ifndef WAIT_ANY
140 #define WAIT_ANY (-1)
141 #endif /* ! WAIT_ANY */
142
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));
147             else
148                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
149         } else {
150             if (WIFSIGNALED(status))
151                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
152             else
153                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
154         }
155
156         fd = server_child_remove(server_children, pid);
157         if (fd == -1) {
158             LOG(log_error, logtype_afpd, "child[%d]: bad IPC fd", pid);
159             continue;
160         }
161         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
162     }
163 }
164
165 static int setlimits(void)
166 {
167     struct rlimit rlim;
168
169     if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
170         LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
171         return -1;
172     }
173     if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
174         rlim.rlim_cur = 65535;
175         if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
176             rlim.rlim_max = 65535;
177         if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
178             LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
179             return -1;
180         }
181     }
182     return 0;
183 }
184
185 int main(int ac, char **av)
186 {
187     struct sigaction    sv;
188     sigset_t            sigs;
189     int                 ret;
190
191     /* Parse argv args and initialize default options */
192     afp_options_parse_cmdline(&obj, ac, av);
193
194     if (!(obj.cmdlineflags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
195         exit(EXITERR_SYS);
196
197     /* Log SIGBUS/SIGSEGV SBT */
198     fault_setup(NULL);
199
200     if (afp_config_parse(&obj, "afpd") != 0)
201         afp_exit(EXITERR_CONF);
202
203     /* Save the user's current umask */
204     obj.options.save_mask = umask(obj.options.umask);
205
206     /* install child handler for asp and dsi. we do this before afp_goaway
207      * as afp_goaway references stuff from here. 
208      * XXX: this should really be setup after the initial connections. */
209     if (!(server_children = server_child_alloc(obj.options.connections))) {
210         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
211         afp_exit(EXITERR_SYS);
212     }
213     
214     sigemptyset(&sigs);
215     pthread_sigmask(SIG_SETMASK, &sigs, NULL);
216
217     memset(&sv, 0, sizeof(sv));    
218     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
219        even if the file is open with O_LARGEFILE ! */
220 #ifdef SIGXFSZ
221     sv.sa_handler = SIG_IGN;
222     sigemptyset( &sv.sa_mask );
223     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
224         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
225         afp_exit(EXITERR_SYS);
226     }
227 #endif
228
229     sv.sa_handler = SIG_IGN;
230     sigemptyset( &sv.sa_mask );
231     if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
232         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
233         afp_exit(EXITERR_SYS);
234     }
235     
236     sv.sa_handler = afp_goaway; /* handler for all sigs */
237
238     sigemptyset( &sv.sa_mask );
239     sigaddset(&sv.sa_mask, SIGALRM);
240     sigaddset(&sv.sa_mask, SIGHUP);
241     sigaddset(&sv.sa_mask, SIGTERM);
242     sigaddset(&sv.sa_mask, SIGUSR1);
243     sigaddset(&sv.sa_mask, SIGQUIT);    
244     sv.sa_flags = SA_RESTART;
245     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
246         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
247         afp_exit(EXITERR_SYS);
248     }
249
250     sigemptyset( &sv.sa_mask );
251     sigaddset(&sv.sa_mask, SIGALRM);
252     sigaddset(&sv.sa_mask, SIGTERM);
253     sigaddset(&sv.sa_mask, SIGHUP);
254     sigaddset(&sv.sa_mask, SIGCHLD);
255     sigaddset(&sv.sa_mask, SIGQUIT);
256     sv.sa_flags = SA_RESTART;
257     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
258         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
259         afp_exit(EXITERR_SYS);
260     }
261
262     sigemptyset( &sv.sa_mask );
263     sigaddset(&sv.sa_mask, SIGALRM);
264     sigaddset(&sv.sa_mask, SIGTERM);
265     sigaddset(&sv.sa_mask, SIGUSR1);
266     sigaddset(&sv.sa_mask, SIGCHLD);
267     sigaddset(&sv.sa_mask, SIGQUIT);
268     sv.sa_flags = SA_RESTART;
269     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
270         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
271         afp_exit(EXITERR_SYS);
272     }
273
274     sigemptyset( &sv.sa_mask );
275     sigaddset(&sv.sa_mask, SIGALRM);
276     sigaddset(&sv.sa_mask, SIGHUP);
277     sigaddset(&sv.sa_mask, SIGUSR1);
278     sigaddset(&sv.sa_mask, SIGCHLD);
279     sigaddset(&sv.sa_mask, SIGQUIT);
280     sv.sa_flags = SA_RESTART;
281     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
282         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
283         afp_exit(EXITERR_SYS);
284     }
285
286     sigemptyset( &sv.sa_mask );
287     sigaddset(&sv.sa_mask, SIGALRM);
288     sigaddset(&sv.sa_mask, SIGHUP);
289     sigaddset(&sv.sa_mask, SIGUSR1);
290     sigaddset(&sv.sa_mask, SIGCHLD);
291     sigaddset(&sv.sa_mask, SIGTERM);
292     sv.sa_flags = SA_RESTART;
293     if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
294         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
295         afp_exit(EXITERR_SYS);
296     }
297
298     /* afp.conf:  not in config file: lockfile, configfile
299      *            preference: command-line provides defaults.
300      *                        config file over-writes defaults.
301      *
302      * we also need to make sure that killing afpd during startup
303      * won't leave any lingering registered names around.
304      */
305
306     sigemptyset(&sigs);
307     sigaddset(&sigs, SIGALRM);
308     sigaddset(&sigs, SIGHUP);
309     sigaddset(&sigs, SIGUSR1);
310 #if 0
311     /* don't block SIGTERM */
312     sigaddset(&sigs, SIGTERM);
313 #endif
314     sigaddset(&sigs, SIGCHLD);
315
316     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
317 #ifdef HAVE_DBUS_GLIB
318     /* Run dbus AFP statics thread */
319     if (obj.options.flags & OPTION_DBUS_AFPSTATS)
320         (void)afpstats_init(server_children);
321 #endif
322     if (configinit(&obj) != 0) {
323         LOG(log_error, logtype_afpd, "main: no servers configured");
324         afp_exit(EXITERR_CONF);
325     }
326     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
327
328     /* Initialize */
329     cnid_init();
330
331     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
332     fd_set_listening_sockets(&obj);
333
334     /* set limits */
335     (void)setlimits();
336
337     afp_child_t *child;
338     int recon_ipc_fd;
339     pid_t pid;
340     int saveerrno;
341
342     /* wait for an appleshare connection. parent remains in the loop
343      * while the children get handled by afp_over_{asp,dsi}.  this is
344      * currently vulnerable to a denial-of-service attack if a
345      * connection is made without an actual login attempt being made
346      * afterwards. establishing timeouts for logins is a possible 
347      * solution. */
348     while (1) {
349         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
350         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
351         ret = poll(fdset, fdset_used, -1);
352         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
353         saveerrno = errno;
354
355         if (gotsigchld) {
356             gotsigchld = 0;
357             child_handler();
358             continue;
359         }
360
361         if (reloadconfig) {
362             nologin++;
363
364             fd_reset_listening_sockets(&obj);
365
366             LOG(log_info, logtype_afpd, "re-reading configuration file");
367
368             configfree(&obj, NULL);
369             afp_config_free(&obj);
370
371             if (afp_config_parse(&obj, "afpd") != 0)
372                 afp_exit(EXITERR_CONF);
373
374             if (configinit(&obj) != 0) {
375                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
376                 afp_exit(EXITERR_CONF);
377             }
378
379             fd_set_listening_sockets(&obj);
380
381             nologin = 0;
382             reloadconfig = 0;
383             errno = saveerrno;
384             continue;
385         }
386
387         if (ret == 0)
388             continue;
389         
390         if (ret < 0) {
391             if (errno == EINTR)
392                 continue;
393             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
394             break;
395         }
396
397         for (int i = 0; i < fdset_used; i++) {
398             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
399                 switch (polldata[i].fdtype) {
400
401                 case LISTEN_FD:
402                     if ((child = dsi_start(&obj, (DSI *)polldata[i].data, server_children))) {
403                         /* Add IPC fd to select fd set */
404                         fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
405                                      &fdset,
406                                      &polldata,
407                                      &fdset_used,
408                                      &fdset_size,
409                                      child->afpch_ipc_fd,
410                                      IPC_FD,
411                                      child);
412                     }
413                     break;
414
415                 case IPC_FD:
416                     child = (afp_child_t *)polldata[i].data;
417                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->afpch_pid);
418
419                     if (ipc_server_read(server_children, child->afpch_ipc_fd) != 0) {
420                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->afpch_ipc_fd);
421                         close(child->afpch_ipc_fd);
422                         child->afpch_ipc_fd = -1;
423                     }
424                     break;
425
426                 default:
427                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
428                     break;
429                 } /* switch */
430             }  /* if */
431         } /* for (i)*/
432     } /* while (1) */
433
434     return 0;
435 }
436
437 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children)
438 {
439     afp_child_t *child = NULL;
440
441     if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
442         LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
443         return NULL;
444     }
445
446     /* we've forked. */
447     if (child == NULL) {
448         configfree(obj, dsi);
449         afp_over_dsi(obj); /* start a session */
450         exit (0);
451     }
452
453     return child;
454 }