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