mixer: Trivial whitespace fix.
[paraslash.git] / server.c
1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file server.c Paraslash's main server. */
4
5 /**
6  * \mainpage Main data structures and selected APIs:
7  *
8  *      - Senders: \ref sender,
9  *      - Audio file selector: \ref afs_info, \ref afs_table,
10  *      - Audio format handler: \ref audio_format_handler, \ref afh_info
11  *      - Receivers/filters/writers: \ref receiver, \ref receiver_node,
12  *        \ref filter, \ref filter_node, \ref writer_node, \ref writer.
13  *      - Scheduling: \ref sched.h,
14  *      - Buffer trees: \ref buffer_tree.h,
15  *      - Sideband API: \ref sideband.h,
16  *      - Crypto: \ref crypt.h, \ref crypt_backend.h,
17  *      - Error subsystem: \ref error.h,
18  *      - Inter process communication: \ref ipc.h,
19  *      - Forward error correction: \ref fec.h,
20  *      - Daemons: \ref daemon.h,
21  *      - Mixer API: \ref mix.h,
22  *      - Interactive sessions: \ref interactive.h,
23  *      - File descriptors: \ref fd.h,
24  *      - Signals: \ref signal.h,
25  *      - Networking: \ref net.h,
26  *      - Time: \ref time.c,
27  *      - Doubly linked lists: \ref list.h.
28  */
29
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <signal.h>
33 #include <regex.h>
34 #include <osl.h>
35 #include <sys/types.h>
36 #include <arpa/inet.h>
37 #include <sys/un.h>
38 #include <netdb.h>
39 #include <lopsub.h>
40
41 #include "server.lsg.h"
42 #include "para.h"
43 #include "error.h"
44 #include "lsu.h"
45 #include "crypt.h"
46 #include "afh.h"
47 #include "string.h"
48 #include "afs.h"
49 #include "net.h"
50 #include "server.h"
51 #include "list.h"
52 #include "send.h"
53 #include "sched.h"
54 #include "vss.h"
55 #include "config.h"
56 #include "close_on_fork.h"
57 #include "daemon.h"
58 #include "ipc.h"
59 #include "fd.h"
60 #include "signal.h"
61 #include "user_list.h"
62 #include "color.h"
63 #include "version.h"
64
65 /** Array of error strings. */
66 DEFINE_PARA_ERRLIST;
67
68 __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
69
70 /** Shut down non-authorized connections after that many seconds. */
71 #define ALARM_TIMEOUT 10
72
73 /**
74  * Pointer to shared memory area for communication between para_server
75  * and its children. Exported to vss.c, command.c and to afs.
76  */
77 struct misc_meta_data *mmd;
78
79 /**
80  * The active value for all config options of para_server.
81  *
82  * It is computed by merging the parse result of the command line options with
83  * the parse result of the config file.
84  */
85 struct lls_parse_result *server_lpr = NULL;
86
87 /* Command line options (no config file options). Used in handle_sighup(). */
88 static struct lls_parse_result *cmdline_lpr;
89
90 /**
91  * A random number used to "authenticate" the afs connection.
92  *
93  * para_server picks this number by random before it forks the afs process. The
94  * command handlers know this number as well and write it to the afs socket,
95  * together with the id of the shared memory area which contains the payload of
96  * the afs command. A local process has to know this number to abuse the afs
97  * service provided by the local socket.
98  */
99 uint32_t afs_socket_cookie;
100
101 /** The mutex protecting the shared memory area containing the mmd struct. */
102 int mmd_mutex;
103
104 /* Serializes log output. */
105 static int log_mutex;
106
107 static struct sched sched;
108 static struct signal_task *signal_task;
109
110 /** The process id of the audio file selector process. */
111 pid_t afs_pid = 0;
112
113 /* The the main server process (parent of afs and the command handlers). */
114 static pid_t server_pid;
115
116 /**
117  * Tell whether the executing process is a command handler.
118  *
119  * Cleanup on exit must be performed differently for command handlers.
120  *
121  * \return True if the pid of the executing process is neither the server pid
122  * nor the afs pid.
123  */
124 bool process_is_command_handler(void)
125 {
126         pid_t pid = getpid();
127
128         return pid != afs_pid && pid != server_pid;
129 }
130
131 /** The task responsible for server command handling. */
132 struct server_command_task {
133         unsigned num_listen_fds; /* only one by default */
134         /** TCP socket(s) on which para_server listens for connections. */
135         int *listen_fds;
136         /* File descriptor for the accepted socket. */
137         int child_fd;
138         /** Copied from para_server's main function. */
139         int argc;
140         /** Argument vector passed to para_server's main function. */
141         char **argv;
142         /** The command task structure for scheduling. */
143         struct task *task;
144 };
145
146 /**
147  * Return the list of tasks for the server process.
148  *
149  * This is called from \a com_tasks(). The helper is necessary since command
150  * handlers can not access the scheduler structure directly.
151  *
152  * \return A dynamically allocated string that must be freed by the caller.
153  */
154 char *server_get_tasks(void)
155 {
156         return get_task_list(&sched);
157 }
158
159 static void pre_log_hook(void)
160 {
161         mutex_lock(log_mutex);
162 }
163
164 static void post_log_hook(void)
165 {
166         mutex_unlock(log_mutex);
167 }
168
169 /* Setup shared memory area and init mutexes */
170 static void init_ipc_or_die(void)
171 {
172         void *shm;
173         int shmid, ret = shm_new(sizeof(struct misc_meta_data));
174
175         if (ret < 0)
176                 goto err_out;
177         shmid = ret;
178         ret = shm_attach(shmid, ATTACH_RW, &shm);
179         shm_destroy(shmid);
180         if (ret < 0)
181                 goto err_out;
182         mmd = shm;
183
184         ret = mutex_new();
185         if (ret < 0)
186                 goto err_out;
187         mmd_mutex = ret;
188         ret = mutex_new();
189         if (ret < 0)
190                 goto destroy_mmd_mutex;
191         log_mutex = ret;
192
193         mmd->num_played = 0;
194         mmd->num_commands = 0;
195         mmd->events = 0;
196         mmd->num_connects = 0;
197         mmd->active_connections = 0;
198         mmd->vss_status_flags = VSS_NEXT;
199         mmd->new_vss_status_flags = VSS_NEXT;
200         return;
201 destroy_mmd_mutex:
202         mutex_destroy(mmd_mutex);
203 err_out:
204         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
205         exit(EXIT_FAILURE);
206 }
207
208 /**
209  * (Re-)read the server configuration files.
210  *
211  * \param reload Whether config file overrides command line.
212  *
213  * This function also re-opens the logfile and the user list. On SIGHUP it is
214  * called from both server and afs context.
215  */
216 void parse_config_or_die(bool reload)
217 {
218         int ret;
219         unsigned flags = MCF_DONT_FREE;
220
221         if (server_lpr != cmdline_lpr)
222                 lls_free_parse_result(server_lpr, CMD_PTR);
223         server_lpr = cmdline_lpr;
224         if (reload)
225                 flags |= MCF_OVERRIDE;
226         ret = lsu_merge_config_file_options(OPT_STRING_VAL(CONFIG_FILE),
227                 "server.conf", &server_lpr, CMD_PTR, server_suite, flags);
228         if (ret < 0) {
229                 PARA_EMERG_LOG("failed to parse config file: %s\n",
230                         para_strerror(-ret));
231                 exit(EXIT_FAILURE);
232         }
233         daemon_set_loglevel(ENUM_STRING_VAL(LOGLEVEL));
234         if (OPT_GIVEN(LOGFILE)) {
235                 daemon_set_logfile(OPT_STRING_VAL(LOGFILE));
236                 daemon_open_log_or_die();
237         }
238         if (daemon_init_colors_or_die(OPT_UINT32_VAL(COLOR), COLOR_AUTO,
239                         COLOR_NO, OPT_GIVEN(LOGFILE))) {
240                 int i;
241                 for (i = 0; i < OPT_GIVEN(LOG_COLOR); i++)
242                         daemon_set_log_color_or_die(lls_string_val(i,
243                                 OPT_RESULT(LOG_COLOR)));
244         }
245         daemon_set_flag(DF_LOG_PID);
246         daemon_set_flag(DF_LOG_LL);
247         daemon_set_flag(DF_LOG_TIME);
248         if (OPT_GIVEN(LOG_TIMING))
249                 daemon_set_flag(DF_LOG_TIMING);
250         daemon_set_priority(OPT_UINT32_VAL(PRIORITY));
251         if (!reload || getpid() != afs_pid) {
252                 char *user_list_file;
253                 if (OPT_GIVEN(USER_LIST))
254                         user_list_file = para_strdup(OPT_STRING_VAL(USER_LIST));
255                 else {
256                         char *home = para_homedir();
257                         user_list_file = make_message("%s/.paraslash/server.users", home);
258                         free(home);
259                 }
260                 user_list_init(user_list_file);
261                 free(user_list_file);
262         }
263         return;
264 }
265
266 /*
267  * called when server gets SIGHUP or when client invokes hup command.
268  */
269 static void handle_sighup(void)
270 {
271
272         PARA_NOTICE_LOG("SIGHUP\n");
273         parse_config_or_die(true);
274         if (afs_pid != 0)
275                 kill(afs_pid, SIGHUP);
276 }
277
278 static int signal_post_select(struct sched *s, __a_unused void *context)
279 {
280         int ret, signum;
281
282         ret = task_get_notification(signal_task->task);
283         if (ret < 0)
284                 return ret;
285         signum = para_next_signal(&s->rfds);
286         switch (signum) {
287         case 0:
288                 return 0;
289         case SIGHUP:
290                 handle_sighup();
291                 break;
292         case SIGCHLD:
293                 for (;;) {
294                         pid_t pid;
295                         ret = para_reap_child(&pid);
296                         if (ret <= 0)
297                                 break;
298                         if (pid != afs_pid)
299                                 continue;
300                         PARA_EMERG_LOG("fatal: afs died\n");
301                         kill(0, SIGTERM);
302                         goto cleanup;
303                 }
304                 break;
305         /* die on sigint/sigterm. Kill all children too. */
306         case SIGINT:
307         case SIGTERM:
308                 PARA_EMERG_LOG("terminating on signal %d\n", signum);
309                 kill(0, SIGTERM);
310                 /*
311                  * We must wait for all of our children to die. For the afs
312                  * process or a command handler might want to use the
313                  * shared memory area and the mmd mutex.  If we destroy this
314                  * mutex too early and afs tries to lock the shared memory
315                  * area, the call to mutex_lock() will fail and terminate the
316                  * afs process. This leads to dirty osl tables.
317                  */
318                 PARA_INFO_LOG("waiting for child processes to die\n");
319                 mutex_unlock(mmd_mutex);
320                 while (wait(NULL) != -1 || errno != ECHILD)
321                         ; /* still at least one child alive */
322                 mutex_lock(mmd_mutex);
323 cleanup:
324                 free(mmd->afd.afhi.chunk_table);
325                 task_notify_all(s, E_DEADLY_SIGNAL);
326                 return -E_DEADLY_SIGNAL;
327         }
328         return 0;
329 }
330
331 static void init_signal_task(void)
332 {
333         signal_task = signal_init_or_die();
334         para_install_sighandler(SIGINT);
335         para_install_sighandler(SIGTERM);
336         para_install_sighandler(SIGHUP);
337         para_install_sighandler(SIGCHLD);
338         para_sigaction(SIGPIPE, SIG_IGN);
339         add_close_on_fork_list(signal_task->fd);
340         signal_task->task = task_register(&(struct task_info) {
341                 .name = "signal",
342                 .pre_select = signal_pre_select,
343                 .post_select = signal_post_select,
344                 .context = signal_task,
345
346         }, &sched);
347 }
348
349 static void command_pre_select(struct sched *s, void *context)
350 {
351         unsigned n;
352         struct server_command_task *sct = context;
353
354         for (n = 0; n < sct->num_listen_fds; n++)
355                 para_fd_set(sct->listen_fds[n], &s->rfds, &s->max_fileno);
356 }
357
358 static int command_task_accept(unsigned listen_idx, struct sched *s,
359                 struct server_command_task *sct)
360 {
361         int new_fd, ret, i;
362         char *peer_name;
363         pid_t child_pid;
364         uint32_t *chunk_table;
365
366         ret = para_accept(sct->listen_fds[listen_idx], &s->rfds, NULL, 0, &new_fd);
367         if (ret <= 0)
368                 goto out;
369         mmd->num_connects++;
370         mmd->active_connections++;
371         /*
372          * The chunk table is a pointer located in the mmd struct that points
373          * to dynamically allocated memory, i.e. it must be freed by the parent
374          * and the child. However, as the mmd struct is in a shared memory
375          * area, there's no guarantee that after the fork this pointer is still
376          * valid in child context. As it is not used in the child anyway, we
377          * save it to a local variable before the fork and free the memory via
378          * that copy in the child directly after the fork.
379          */
380         chunk_table = mmd->afd.afhi.chunk_table;
381         child_pid = fork();
382         if (child_pid < 0) {
383                 ret = -ERRNO_TO_PARA_ERROR(errno);
384                 goto out;
385         }
386         if (child_pid) {
387                 /* avoid problems with non-fork-safe PRNGs */
388                 unsigned char buf[16];
389                 get_random_bytes_or_die(buf, sizeof(buf));
390                 close(new_fd);
391                 /* parent keeps accepting connections */
392                 return 0;
393         }
394         peer_name = remote_name(new_fd);
395         PARA_INFO_LOG("accepted connection from %s\n", peer_name);
396         /* mmd might already have changed at this point */
397         free(chunk_table);
398         sct->child_fd = new_fd;
399         /*
400          * put info on who we are serving into argv[0] to make
401          * client ip visible in top/ps
402          */
403         for (i = sct->argc - 1; i >= 0; i--)
404                 memset(sct->argv[i], 0, strlen(sct->argv[i]));
405         i = sct->argc - 1 - lls_num_inputs(cmdline_lpr);
406         sprintf(sct->argv[i], "para_server (serving %s)", peer_name);
407         /* ask other tasks to terminate */
408         task_notify_all(s, E_CHILD_CONTEXT);
409         /*
410          * After we return, the scheduler calls server_select() with a minimal
411          * timeout value, because the remaining tasks have a notification
412          * pending. Next it calls the ->post_select method of these tasks,
413          * which will return negative in view of the notification. This causes
414          * schedule() to return as there are no more runnable tasks.
415          *
416          * Note that semaphores are not inherited across a fork(), so we don't
417          * hold the lock at this point. Since server_select() drops the lock
418          * prior to calling para_select(), we need to acquire it here.
419          */
420         mutex_lock(mmd_mutex);
421         return -E_CHILD_CONTEXT;
422 out:
423         if (ret < 0)
424                 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
425         return 0;
426 }
427
428 static int command_post_select(struct sched *s, void *context)
429 {
430         struct server_command_task *sct = context;
431         unsigned n;
432         int ret;
433
434         ret = task_get_notification(sct->task);
435         if (ret < 0)
436                 return ret;
437         for (n = 0; n < sct->num_listen_fds; n++) {
438                 ret = command_task_accept(n, s, sct);
439                 if (ret < 0) {
440                         free(sct->listen_fds);
441                         return ret;
442                 }
443         }
444         return 0;
445 }
446
447 static void init_server_command_task(struct server_command_task *sct,
448                 int argc, char **argv)
449 {
450         int ret;
451         unsigned n;
452         uint32_t port = OPT_UINT32_VAL(PORT);
453
454         PARA_NOTICE_LOG("initializing tcp command socket\n");
455         sct->child_fd = -1;
456         sct->argc = argc;
457         sct->argv = argv;
458         if (!OPT_GIVEN(LISTEN_ADDRESS)) {
459                 sct->num_listen_fds = 1;
460                 sct->listen_fds = para_malloc(sizeof(int));
461                 ret = para_listen_simple(IPPROTO_TCP, port);
462                 if (ret < 0)
463                         goto err;
464                 sct->listen_fds[0] = ret;
465         } else {
466                 sct->num_listen_fds = OPT_GIVEN(LISTEN_ADDRESS);
467                 sct->listen_fds = para_malloc(sct->num_listen_fds * sizeof(int));
468                 for (n = 0; n < OPT_GIVEN(LISTEN_ADDRESS); n++) {
469                         const char *arg;
470                         arg = lls_string_val(n, OPT_RESULT(LISTEN_ADDRESS));
471                         ret = para_listen(IPPROTO_TCP, arg, port);
472                         if (ret < 0)
473                                 goto err;
474                         sct->listen_fds[n] = ret;
475                 }
476         }
477         for (n = 0; n < sct->num_listen_fds; n++) {
478                 ret = mark_fd_nonblocking(sct->listen_fds[n]);
479                 if (ret < 0)
480                         goto err;
481                 /* child doesn't need the listener */
482                 add_close_on_fork_list(sct->listen_fds[n]);
483         }
484
485         sct->task = task_register(&(struct task_info) {
486                 .name = "server command",
487                 .pre_select = command_pre_select,
488                 .post_select = command_post_select,
489                 .context = sct,
490         }, &sched);
491         /*
492          * Detect whether the abstract Unix domain socket space is supported,
493          * but do not create the socket. We check this once in server context
494          * so that the command handlers inherit this bit of information and
495          * don't need to check again.
496          */
497         create_local_socket(NULL);
498         return;
499 err:
500         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
501         exit(EXIT_FAILURE);
502 }
503
504 static int init_afs(int argc, char **argv)
505 {
506         int ret, afs_server_socket[2];
507         char c;
508
509         ret = socketpair(PF_UNIX, SOCK_STREAM, 0, afs_server_socket);
510         if (ret < 0)
511                 exit(EXIT_FAILURE);
512         get_random_bytes_or_die((unsigned char *)&afs_socket_cookie,
513                 sizeof(afs_socket_cookie));
514         afs_pid = fork();
515         if (afs_pid < 0)
516                 exit(EXIT_FAILURE);
517         if (afs_pid == 0) { /* child (afs) */
518                 int i;
519
520                 afs_pid = getpid();
521                 crypt_shutdown();
522                 user_list_deplete();
523                 for (i = argc - 1; i >= 0; i--)
524                         memset(argv[i], 0, strlen(argv[i]));
525                 i = argc - lls_num_inputs(cmdline_lpr) - 1;
526                 sprintf(argv[i], "para_server (afs)");
527                 close(afs_server_socket[0]);
528                 afs_init(afs_server_socket[1]);
529         }
530         close(afs_server_socket[1]);
531         if (read(afs_server_socket[0], &c, 1) <= 0) {
532                 PARA_EMERG_LOG("early afs exit\n");
533                 exit(EXIT_FAILURE);
534         }
535         ret = mark_fd_nonblocking(afs_server_socket[0]);
536         if (ret < 0)
537                 exit(EXIT_FAILURE);
538         add_close_on_fork_list(afs_server_socket[0]);
539         PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
540                 afs_server_socket[0], (unsigned) afs_socket_cookie);
541         return afs_server_socket[0];
542 }
543
544 static void handle_help_flags(void)
545 {
546         char *help;
547         bool d = OPT_GIVEN(DETAILED_HELP);
548
549         if (d)
550                 help = lls_long_help(CMD_PTR);
551         else if (OPT_GIVEN(HELP))
552                 help = lls_short_help(CMD_PTR);
553         else
554                 return;
555         printf("%s\n", help);
556         free(help);
557         exit(EXIT_SUCCESS);
558 }
559
560 static void server_init(int argc, char **argv, struct server_command_task *sct)
561 {
562         int ret, afs_socket, daemon_pipe = -1;
563         char *errctx;
564
565         valid_fd_012();
566         /* parse command line options */
567         ret = lls(lls_parse(argc, argv, CMD_PTR, &cmdline_lpr, &errctx));
568         if (ret < 0)
569                 goto fail;
570         server_lpr = cmdline_lpr;
571         daemon_set_loglevel(ENUM_STRING_VAL(LOGLEVEL));
572         daemon_drop_privileges_or_die(OPT_STRING_VAL(USER),
573                 OPT_STRING_VAL(GROUP));
574         version_handle_flag("server", OPT_GIVEN(VERSION));
575         handle_help_flags();
576         parse_config_or_die(false);
577         /* become daemon */
578         if (OPT_GIVEN(DAEMON))
579                 daemon_pipe = daemonize(true /* parent waits for SIGTERM */);
580         server_pid = getpid();
581         crypt_init();
582         daemon_log_welcome("server");
583         init_ipc_or_die(); /* init mmd struct, mmd and log mutex */
584         daemon_set_start_time();
585         daemon_set_hooks(pre_log_hook, post_log_hook);
586         PARA_NOTICE_LOG("initializing audio format handlers\n");
587         afh_init();
588
589         /*
590          * Although afs uses its own signal handling we must ignore SIGUSR1
591          * _before_ the afs child process gets born by init_afs() below.  It's
592          * racy to do this in the child because the parent might send SIGUSR1
593          * before the child gets a chance to ignore this signal.
594          *
595          * We also have to block SIGCHLD before the afs process is created
596          * because otherwise para_server does not notice if afs dies before the
597          * SIGCHLD handler has been installed for the parent process by
598          * init_signal_task() below.
599          */
600         para_sigaction(SIGUSR1, SIG_IGN);
601         para_block_signal(SIGCHLD);
602         PARA_NOTICE_LOG("initializing the audio file selector\n");
603         afs_socket = init_afs(argc, argv);
604         init_signal_task();
605         para_unblock_signal(SIGCHLD);
606         PARA_NOTICE_LOG("initializing virtual streaming system\n");
607         vss_init(afs_socket, &sched);
608         init_server_command_task(sct, argc, argv);
609         if (daemon_pipe >= 0) {
610                 if (write(daemon_pipe, "\0", 1) < 0) {
611                         PARA_EMERG_LOG("daemon_pipe: %s", strerror(errno));
612                         exit(EXIT_FAILURE);
613                 }
614                 close(daemon_pipe);
615         }
616         PARA_NOTICE_LOG("server init complete\n");
617         return;
618 fail:
619         assert(ret < 0);
620         if (errctx)
621                 PARA_ERROR_LOG("%s\n", errctx);
622         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
623         exit(EXIT_FAILURE);
624 }
625
626 static void status_refresh(void)
627 {
628         static int prev_uptime = -1, prev_events = -1;
629         int uptime = daemon_get_uptime(now);
630
631         if (prev_events != mmd->events)
632                 goto out;
633         if (mmd->new_vss_status_flags != mmd->vss_status_flags)
634                 goto out_inc_events;
635         if (uptime / 60 != prev_uptime / 60)
636                 goto out_inc_events;
637         return;
638 out_inc_events:
639         mmd->events++;
640 out:
641         prev_uptime = uptime;
642         prev_events = mmd->events;
643         mmd->vss_status_flags = mmd->new_vss_status_flags;
644         PARA_DEBUG_LOG("%u events, forcing status update\n", mmd->events);
645         killpg(0, SIGUSR1);
646 }
647
648 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
649                 struct timeval *timeout_tv)
650 {
651         int ret;
652
653         status_refresh();
654         mutex_unlock(mmd_mutex);
655         ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
656         mutex_lock(mmd_mutex);
657         return ret;
658 }
659
660 /**
661  * Deallocate all lopsub parse results.
662  *
663  * The server allocates a parse result for command line options and optionally
664  * a second parse result for the effective configuration, defined by merging
665  * the command line options with the options stored in the configuration file.
666  * This function frees both structures.
667  */
668 void free_lpr(void)
669 {
670         lls_free_parse_result(server_lpr, CMD_PTR);
671         if (server_lpr != cmdline_lpr)
672                 lls_free_parse_result(cmdline_lpr, CMD_PTR);
673 }
674
675 /**
676  * The main function of para_server.
677  *
678  * \param argc Usual argument count.
679  * \param argv Usual argument vector.
680  *
681  * \return EXIT_SUCCESS or EXIT_FAILURE.
682  */
683 int main(int argc, char *argv[])
684 {
685         int ret;
686         struct server_command_task server_command_task_struct,
687                 *sct = &server_command_task_struct;
688
689         sched.default_timeout.tv_sec = 1;
690         sched.select_function = server_select;
691
692         server_init(argc, argv, sct);
693         mutex_lock(mmd_mutex);
694         ret = schedule(&sched);
695         /*
696          * We hold the mmd lock: it was re-acquired in server_select()
697          * after the select call.
698          */
699         mutex_unlock(mmd_mutex);
700         sched_shutdown(&sched);
701         crypt_shutdown();
702         signal_shutdown(signal_task);
703         if (!process_is_command_handler()) { /* parent (server) */
704                 mutex_destroy(mmd_mutex);
705                 daemon_set_hooks(NULL, NULL); /* only one process remaining */
706                 mutex_destroy(log_mutex);
707                 deplete_close_on_fork_list();
708                 if (ret < 0)
709                         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
710         } else {
711                 alarm(ALARM_TIMEOUT);
712                 close_listed_fds();
713                 ret = handle_connect(sct->child_fd);
714         }
715         vss_shutdown();
716         shm_detach(mmd);
717         user_list_deplete();
718         free_lpr();
719         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
720 }