2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file server.c Paraslash's main server. */
10 * \mainpage Main data structures and selected APIs:
12 * - Senders: \ref sender,
13 * - Audio file selector: \ref afs_info, \ref afs_table,
14 * - Audio format handler: \ref audio_format_handler, \ref afh_info
15 * - Receivers/filters/writers: \ref receiver, \ref receiver_node,
16 * \ref filter, \ref filter_node, \ref writer_node, \ref writer.
17 * - Scheduling: \ref sched.h,
18 * - Buffer trees: \ref buffer_tree.h,
19 * - Sideband API: \ref sideband.h,
20 * - Crypto: \ref crypt.h, \ref crypt_backend.h,
21 * - Error subsystem: \ref error.h, \ref error2.c,
22 * - Inter process communication: \ref ipc.h,
23 * - Forward error correction: \ref fec.h,
24 * - Daemons: \ref daemon.h,
25 * - Mixer API: \ref mix.h,
26 * - Interactive sessions: \ref interactive.h,
27 * - File descriptors: \ref fd.h,
28 * - Signals: \ref signal.h,
29 * - Networking: \ref net.h,
30 * - Time: \ref time.c,
31 * - Doubly linked lists: \ref list.h.
34 #include <netinet/in.h>
35 #include <sys/socket.h>
39 #include <sys/types.h>
40 #include <arpa/inet.h>
47 #include "server.cmdline.h"
57 #include "close_on_fork.h"
63 #include "user_list.h"
68 __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
70 /** Define the array of error lists needed by para_server. */
73 /** Shut down non-authorized connections after that many seconds. */
74 #define ALARM_TIMEOUT 10
77 * Pointer to shared memory area for communication between para_server
78 * and its children. Exported to vss.c, command.c and to afs.
80 struct misc_meta_data *mmd;
83 * The configuration of para_server
85 * It also contains the options for the audio file selector, audio format
86 * handler and all supported senders.
88 struct server_args_info conf;
90 /** A random value used in child context for authentication. */
91 uint32_t afs_socket_cookie;
93 /** The mutex protecting the shared memory area containing the mmd struct. */
96 /** The file containing user information (public key, permissions). */
97 static char *user_list_file = NULL;
99 static struct sched sched;
100 static struct signal_task *signal_task;
102 /** The task responsible for server command handling. */
103 struct server_command_task {
104 /** TCP port on which para_server listens for connections. */
106 /** Copied from para_server's main function. */
108 /** Argument vector passed to para_server's main function. */
110 /** The command task structure for scheduling. */
115 * Return the list of tasks for the server process.
117 * This is called from \a com_tasks(). The helper is necessary since command
118 * handlers can not access the scheduler structure directly.
120 * \return A dynamically allocated string that must be freed by the caller.
122 char *server_get_tasks(void)
124 return get_task_list(&sched);
128 * setup shared memory area and get mutex for locking
130 static void init_ipc_or_die(void)
133 int shmid, ret = shm_new(sizeof(struct misc_meta_data));
138 ret = shm_attach(shmid, ATTACH_RW, &shm);
150 mmd->num_commands = 0;
152 mmd->num_connects = 0;
153 mmd->active_connections = 0;
154 mmd->vss_status_flags = VSS_NEXT;
155 mmd->new_vss_status_flags = VSS_NEXT;
158 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
163 * (Re-)read the server configuration files.
165 * \param override Passed to gengetopt to activate the override feature.
167 * This function also re-opens the logfile and sets the global \a
168 * user_list_file variable.
170 void parse_config_or_die(int override)
172 char *home = para_homedir();
177 if (conf.config_file_given)
178 cf = para_strdup(conf.config_file_arg);
180 cf = make_message("%s/.paraslash/server.conf", home);
181 free(user_list_file);
182 if (!conf.user_list_given)
183 user_list_file = make_message("%s/.paraslash/server.users", home);
185 user_list_file = para_strdup(conf.user_list_arg);
186 ret = file_exists(cf);
187 if (conf.config_file_given && !ret) {
189 PARA_EMERG_LOG("can not read config file %s\n", cf);
193 int tmp = conf.daemon_given;
194 struct server_cmdline_parser_params params = {
195 .override = override,
198 .check_ambiguity = 0,
199 .print_errors = !conf.daemon_given
201 server_cmdline_parser_config_file(cf, &conf, ¶ms);
202 daemon_set_loglevel(conf.loglevel_arg);
203 conf.daemon_given = tmp;
205 if (conf.logfile_given) {
206 daemon_set_logfile(conf.logfile_arg);
207 daemon_open_log_or_die();
210 daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no,
211 conf.logfile_given, conf.log_color_arg, conf.log_color_given);
212 daemon_set_flag(DF_LOG_PID);
213 daemon_set_flag(DF_LOG_LL);
214 daemon_set_flag(DF_LOG_TIME);
215 if (conf.log_timing_given)
216 daemon_set_flag(DF_LOG_TIMING);
223 free(user_list_file);
224 user_list_file = NULL;
229 * called when server gets SIGHUP or when client invokes hup command.
231 static void handle_sighup(void)
233 PARA_NOTICE_LOG("SIGHUP\n");
234 parse_config_or_die(1); /* reopens log */
235 init_user_list(user_list_file); /* reload user list */
237 kill(mmd->afs_pid, SIGHUP);
240 static int signal_post_select(struct sched *s, __a_unused void *context)
242 int signum = para_next_signal(&s->rfds);
253 int ret = para_reap_child(&pid);
256 if (pid != mmd->afs_pid)
258 PARA_EMERG_LOG("fatal: afs died\n");
263 /* die on sigint/sigterm. Kill all children too. */
266 PARA_EMERG_LOG("terminating on signal %d\n", signum);
269 * We must wait for afs because afs catches SIGINT/SIGTERM.
270 * Before reacting to the signal, afs might want to use the
271 * shared memory area and the mmd mutex. If we destroy this
272 * mutex too early and afs tries to lock the shared memory
273 * area, the call to mutex_lock() will fail and terminate the
274 * afs process. This leads to dirty osl tables.
276 * There's no such problem with the other children of the
277 * server process (the command handlers) as these reset their
278 * SIGINT/SIGTERM handlers to the default action, i.e. these
279 * processes get killed immediately by the above kill().
281 PARA_INFO_LOG("waiting for afs (pid %d) to die\n",
283 waitpid(mmd->afs_pid, NULL, 0);
285 free(mmd->afd.afhi.chunk_table);
287 mutex_destroy(mmd_mutex);
294 static void init_signal_task(void)
296 signal_task = signal_init_or_die();
297 para_install_sighandler(SIGINT);
298 para_install_sighandler(SIGTERM);
299 para_install_sighandler(SIGHUP);
300 para_install_sighandler(SIGCHLD);
301 para_sigaction(SIGPIPE, SIG_IGN);
302 add_close_on_fork_list(signal_task->fd);
303 signal_task->task = task_register(&(struct task_info) {
305 .pre_select = signal_pre_select,
306 .post_select = signal_post_select,
307 .context = signal_task,
312 static void command_pre_select(struct sched *s, void *context)
314 struct server_command_task *sct = context;
315 para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
318 static int command_post_select(struct sched *s, void *context)
320 struct server_command_task *sct = context;
325 uint32_t *chunk_table;
327 ret = para_accept(sct->listen_fd, &s->rfds, NULL, 0, &new_fd);
330 peer_name = remote_name(new_fd);
331 PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
333 mmd->active_connections++;
335 * The chunk table is a pointer located in the mmd struct that points
336 * to dynamically allocated memory, i.e. it must be freed by the parent
337 * and the child. However, as the mmd struct is in a shared memory
338 * area, there's no guarantee that after the fork this pointer is still
339 * valid in child context. As it is not used in the child anyway, we
340 * save it to a local variable before the fork and free the memory via
341 * that copy in the child directly after the fork.
343 chunk_table = mmd->afd.afhi.chunk_table;
346 ret = -ERRNO_TO_PARA_ERROR(errno);
350 /* avoid problems with non-fork-safe PRNGs */
351 unsigned char buf[16];
352 get_random_bytes_or_die(buf, sizeof(buf));
354 /* parent keeps accepting connections */
357 /* mmd might already have changed at this point */
359 alarm(ALARM_TIMEOUT);
361 signal_shutdown(signal_task);
363 * put info on who we are serving into argv[0] to make
364 * client ip visible in top/ps
366 for (i = sct->argc - 1; i >= 0; i--)
367 memset(sct->argv[i], 0, strlen(sct->argv[i]));
368 sprintf(sct->argv[0], "para_server (serving %s)", peer_name);
369 handle_connect(new_fd, peer_name);
373 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
377 static void init_server_command_task(int argc, char **argv)
380 static struct server_command_task server_command_task_struct,
381 *sct = &server_command_task_struct;
383 PARA_NOTICE_LOG("initializing tcp command socket\n");
386 ret = para_listen_simple(IPPROTO_TCP, conf.port_arg);
389 sct->listen_fd = ret;
390 ret = mark_fd_nonblocking(sct->listen_fd);
393 add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */
394 sct->task = task_register(&(struct task_info) {
395 .name = "server command",
396 .pre_select = command_pre_select,
397 .post_select = command_post_select,
402 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
406 static int init_afs(int argc, char **argv)
408 int ret, afs_server_socket[2];
411 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
414 get_random_bytes_or_die((unsigned char *)&afs_socket_cookie,
415 sizeof(afs_socket_cookie));
419 if (afs_pid == 0) { /* child (afs) */
421 for (i = argc - 1; i >= 0; i--)
422 memset(argv[i], 0, strlen(argv[i]));
423 sprintf(argv[0], "para_server (afs)");
424 close(afs_server_socket[0]);
425 afs_init(afs_socket_cookie, afs_server_socket[1]);
427 mmd->afs_pid = afs_pid;
428 close(afs_server_socket[1]);
429 ret = mark_fd_nonblocking(afs_server_socket[0]);
432 add_close_on_fork_list(afs_server_socket[0]);
433 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
434 afs_server_socket[0], (unsigned) afs_socket_cookie);
435 return afs_server_socket[0];
438 __noreturn static void print_help_and_die(void)
440 struct ggo_help h = DEFINE_GGO_HELP(server);
441 bool d = conf.detailed_help_given;
443 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
447 static void server_init(int argc, char **argv)
449 struct server_cmdline_parser_params params = {
453 .check_ambiguity = 0,
459 init_random_seed_or_die();
460 /* parse command line options */
461 server_cmdline_parser_ext(argc, argv, &conf, ¶ms);
462 daemon_set_loglevel(conf.loglevel_arg);
463 version_handle_flag("server", conf.version_given);
464 if (conf.help_given || conf.detailed_help_given)
465 print_help_and_die();
466 daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg);
467 /* parse config file, open log and set defaults */
468 parse_config_or_die(0);
469 daemon_log_welcome("server");
470 init_ipc_or_die(); /* init mmd struct and mmd->lock */
471 daemon_set_start_time();
472 init_user_list(user_list_file);
474 if (conf.daemon_given)
475 daemonize(true /* parent waits for SIGTERM */);
476 PARA_NOTICE_LOG("initializing audio format handlers\n");
480 * Although afs uses its own signal handling we must ignore SIGUSR1
481 * _before_ the afs child process gets born by init_afs() below. It's
482 * racy to do this in the child because the parent might send SIGUSR1
483 * before the child gets a chance to ignore this signal -- only the
486 para_sigaction(SIGUSR1, SIG_IGN);
488 * We have to block SIGCHLD before the afs process is being forked off.
489 * Otherwise, para_server does not notice if afs dies before the
490 * SIGCHLD handler has been installed for the parent process by
491 * init_signal_task() below.
493 para_block_signal(SIGCHLD);
494 PARA_NOTICE_LOG("initializing the audio file selector\n");
495 afs_socket = init_afs(argc, argv);
497 para_unblock_signal(SIGCHLD);
498 PARA_NOTICE_LOG("initializing virtual streaming system\n");
499 init_vss_task(afs_socket, &sched);
500 init_server_command_task(argc, argv);
501 if (conf.daemon_given)
502 kill(getppid(), SIGTERM);
503 PARA_NOTICE_LOG("server init complete\n");
506 static void status_refresh(void)
508 static int prev_uptime = -1, prev_events = -1;
509 int uptime = daemon_get_uptime(now);
511 if (prev_events != mmd->events)
513 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
515 if (uptime / 60 != prev_uptime / 60)
521 prev_uptime = uptime;
522 prev_events = mmd->events;
523 mmd->vss_status_flags = mmd->new_vss_status_flags;
524 PARA_DEBUG_LOG("%d events, forcing status update\n", mmd->events);
528 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
529 struct timeval *timeout_tv)
534 mutex_unlock(mmd_mutex);
535 ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
536 mutex_lock(mmd_mutex);
541 * The main function of para_server.
543 * \param argc Usual argument count.
544 * \param argv Usual argument vector.
546 * \return EXIT_SUCCESS or EXIT_FAILURE.
548 int main(int argc, char *argv[])
552 sched.default_timeout.tv_sec = 1;
553 sched.select_function = server_select;
555 server_init(argc, argv);
556 mutex_lock(mmd_mutex);
557 ret = schedule(&sched);
558 sched_shutdown(&sched);
560 PARA_EMERG_LOG("%s\n", para_strerror(-ret));