2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file server.c Paraslash's main server. */
11 * \mainpage Paraslash API Reference
13 * Starting points for getting an overview:
16 * - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
17 * \ref audioc.c, \ref afh.c
18 * - Server: \ref server_command, \ref sender,
19 * - Audio file selector: \ref audio_format_handler, \ref mood, \ref afs_table,
20 * - Client: \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
23 * The gory details, listed by topic:
25 * - Audio format handlers: \ref send_common.c \ref mp3_afh.c, \ref ogg_afh.c, \ref aac_afh.c,
26 * - Decoders: \ref mp3dec_filter.c, \ref oggdec_filter.c, \ref aacdec_filter.c,
27 * - Volume normalizer: \ref compress_filter.c,
28 * - Output: \ref alsa_write.c, \ref osx_write.c,
29 * - http: \ref http_recv.c, \ref http_send.c,
30 * - udp: \ref udp_recv.c, \ref udp_send.c,
31 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
32 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
33 * - Afs structures: \ref afs_table, \ref audio_file_data,
34 * \ref afs_info \ref afh_info,
35 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
36 * \ref attribute.c, \ref score.c,
37 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
41 * - Scheduling: \ref sched.c, \ref sched.h,
42 * - Networking: \ref net.c,
43 * - File descriptors: \ref fd.c,
44 * - Signals: \ref signal.c,
45 * - Daemons: \ref daemon.c,
46 * - Strings: \ref string.c, \ref string.h,
47 * - Time: \ref time.c,
48 * - Spawning processes: \ref exec.c,
49 * - Inter process communication: \ref ipc.c,
50 * - Blob tables: \ref blob.c,
51 * - The error subssystem: \ref error.h.
52 * - Access control for paraslash senders: \ref acl.c, \ref acl.h.
54 * Low-level data structures:
56 * - Doubly linked lists: \ref list.h,
57 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
58 * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c,
59 * - Crypto: \ref crypt.c.
60 * - Forward error correction: \ref fec.c
66 #include <openssl/rc4.h>
73 #include "server.cmdline.h"
80 #include "close_on_fork.h"
89 #include "user_list.h"
92 /** Define the array of error lists needed by para_server. */
95 /** Shut down non-authorized connections after that many seconds. */
96 #define ALARM_TIMEOUT 10
99 * Pointer to shared memory area for communication between para_server
100 * and its children. Exported to vss.c. command.c and to afs.
102 struct misc_meta_data *mmd;
105 * The configuration of para_server
107 * It also contains the options for the audio file selector, audio format
108 * handler and all supported senders.
110 struct server_args_info conf;
112 /** A random value used in child context for authentication. */
113 uint32_t afs_socket_cookie;
115 /** The mutex protecting the shared memory area containing the mmd struct. */
118 /** The file containing user information (public key, permissions). */
119 static char *user_list_file = NULL;
122 /** The task responsible for server command handling. */
123 struct server_command_task {
124 /** TCP port on which para_server listens for connections. */
126 /** Copied from para_server's main function. */
128 /** Argument vector passed to para_server's main function. */
130 /** The command task structure for scheduling. */
134 static int want_colors(void)
136 if (conf.color_arg == color_arg_no)
138 if (conf.color_arg == color_arg_yes)
140 if (conf.logfile_given)
142 return isatty(STDERR_FILENO);
145 static void init_colors_or_die(void)
151 daemon_set_flag(DF_COLOR_LOG);
152 daemon_set_default_log_colors();
153 for (i = 0; i < conf.log_color_given; i++) {
154 ret = daemon_set_log_color(conf.log_color_arg[i]);
161 * setup shared memory area and get mutex for locking
163 static void init_ipc_or_die(void)
166 int shmid, ret = shm_new(sizeof(struct misc_meta_data));
171 ret = shm_attach(shmid, ATTACH_RW, &shm);
183 mmd->num_commands = 0;
185 mmd->num_connects = 0;
186 mmd->active_connections = 0;
187 mmd->vss_status_flags = VSS_NEXT;
188 mmd->new_vss_status_flags = VSS_NEXT;
191 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
196 * (Re-)read the server configuration files.
198 * \param override Passed to gengetopt to activate the override feature.
200 * This function also re-opens the logfile and sets the global \a
201 * user_list_file variable.
203 void parse_config_or_die(int override)
205 char *home = para_homedir();
210 if (conf.config_file_given)
211 cf = para_strdup(conf.config_file_arg);
213 cf = make_message("%s/.paraslash/server.conf", home);
214 free(user_list_file);
215 if (!conf.user_list_given)
216 user_list_file = make_message("%s/.paraslash/server.users", home);
218 user_list_file = para_strdup(conf.user_list_arg);
219 ret = file_exists(cf);
220 if (conf.config_file_given && !ret) {
222 PARA_EMERG_LOG("can not read config file %s\n", cf);
226 int tmp = conf.daemon_given;
227 struct server_cmdline_parser_params params = {
228 .override = override,
231 .check_ambiguity = 0,
232 .print_errors = !conf.daemon_given
234 server_cmdline_parser_config_file(cf, &conf, ¶ms);
235 conf.daemon_given = tmp;
237 if (conf.logfile_given) {
238 daemon_set_logfile(conf.logfile_arg);
239 daemon_open_log_or_die();
241 daemon_set_loglevel(conf.loglevel_arg);
242 init_colors_or_die();
243 daemon_set_flag(DF_LOG_PID);
244 daemon_set_flag(DF_LOG_LL);
245 daemon_set_flag(DF_LOG_TIME);
246 if (conf.log_timing_given)
247 daemon_set_flag(DF_LOG_TIMING);
254 free(user_list_file);
255 user_list_file = NULL;
259 static void signal_pre_select(struct sched *s, struct task *t)
261 struct signal_task *st = container_of(t, struct signal_task, task);
262 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
266 * called when server gets SIGHUP or when client invokes hup command.
268 static void handle_sighup(void)
270 PARA_NOTICE_LOG("SIGHUP\n");
271 parse_config_or_die(1); /* reopens log */
272 init_user_list(user_list_file); /* reload user list */
274 kill(mmd->afs_pid, SIGHUP);
277 static void signal_post_select(struct sched *s, struct task *t)
279 struct signal_task *st = container_of(t, struct signal_task, task);
281 if (!FD_ISSET(st->fd, &s->rfds))
284 st->signum = para_next_signal();
285 switch (st->signum) {
292 int ret = para_reap_child(&pid);
295 if (pid != mmd->afs_pid)
297 PARA_EMERG_LOG("fatal: afs died\n");
302 /* die on sigint/sigterm. Kill all children too. */
305 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
308 * We must wait for afs because afs catches SIGINT/SIGTERM.
309 * Before reacting to the signal, afs might want to use the
310 * shared memory area and the mmd mutex. If we destroy this
311 * mutex too early and afs tries to lock the shared memory
312 * area, the call to mutex_lock() will fail and terminate the
313 * afs process. This leads to dirty osl tables.
315 * There's no such problem with the other children of the
316 * server process (the command handlers) as these reset their
317 * SIGINT/SIGTERM handlers to the default action, i.e. these
318 * processes get killed immediately by the above kill().
320 PARA_INFO_LOG("waiting for afs (pid %d) to die\n",
322 waitpid(mmd->afs_pid, NULL, 0);
324 free(mmd->afd.afhi.chunk_table);
326 mutex_destroy(mmd_mutex);
332 static void init_signal_task(void)
334 static struct signal_task signal_task_struct,
335 *st = &signal_task_struct;
337 st->task.pre_select = signal_pre_select;
338 st->task.post_select = signal_post_select;
339 sprintf(st->task.status, "signal task");
341 PARA_NOTICE_LOG("setting up signal handling\n");
342 st->fd = para_signal_init(); /* always successful */
343 para_install_sighandler(SIGINT);
344 para_install_sighandler(SIGTERM);
345 para_install_sighandler(SIGHUP);
346 para_install_sighandler(SIGCHLD);
347 para_sigaction(SIGPIPE, SIG_IGN);
348 add_close_on_fork_list(st->fd);
349 register_task(&st->task);
352 static void command_pre_select(struct sched *s, struct task *t)
354 struct server_command_task *sct = container_of(t, struct server_command_task, task);
355 para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
358 static void command_post_select(struct sched *s, struct task *t)
360 struct server_command_task *sct = container_of(t, struct server_command_task, task);
365 uint32_t *chunk_table;
367 if (!FD_ISSET(sct->listen_fd, &s->rfds))
369 ret = para_accept(sct->listen_fd, NULL, 0);
373 peer_name = remote_name(new_fd);
374 PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
376 mmd->active_connections++;
378 * The chunk table is a pointer located in the mmd struct that points
379 * to dynamically allocated memory, i.e. it must be freed by the parent
380 * and the child. However, as the mmd struct is in a shared memory
381 * area, there's no guarantee that after the fork this pointer is still
382 * valid in child context. As it is not used in the child anyway, we
383 * save it to a local variable before the fork and free the memory via
384 * that copy in the child directly after the fork.
386 chunk_table = mmd->afd.afhi.chunk_table;
389 ret = -ERRNO_TO_PARA_ERROR(errno);
394 /* parent keeps accepting connections */
397 /* mmd might already have changed at this point */
399 alarm(ALARM_TIMEOUT);
401 para_signal_shutdown();
403 * put info on who we are serving into argv[0] to make
404 * client ip visible in top/ps
406 for (i = sct->argc - 1; i >= 0; i--)
407 memset(sct->argv[i], 0, strlen(sct->argv[i]));
408 sprintf(sct->argv[0], "para_server (serving %s)", peer_name);
409 return handle_connect(new_fd, peer_name);
412 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
415 static void init_server_command_task(int argc, char **argv)
418 static struct server_command_task server_command_task_struct,
419 *sct = &server_command_task_struct;
421 PARA_NOTICE_LOG("initializing tcp command socket\n");
422 sct->task.pre_select = command_pre_select;
423 sct->task.post_select = command_post_select;
426 ret = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.port_arg);
429 sct->listen_fd = ret;
430 ret = mark_fd_nonblocking(sct->listen_fd);
433 add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */
434 register_task(&sct->task);
437 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
441 static int init_afs(void)
443 int ret, afs_server_socket[2];
446 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
449 get_random_bytes_or_die((unsigned char *)&afs_socket_cookie,
450 sizeof(afs_socket_cookie));
454 if (afs_pid == 0) { /* child (afs) */
455 close(afs_server_socket[0]);
456 afs_init(afs_socket_cookie, afs_server_socket[1]);
458 mmd->afs_pid = afs_pid;
459 close(afs_server_socket[1]);
460 ret = mark_fd_nonblocking(afs_server_socket[0]);
463 add_close_on_fork_list(afs_server_socket[0]);
464 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
465 afs_server_socket[0], (unsigned) afs_socket_cookie);
466 return afs_server_socket[0];
469 __noreturn static void tmp_sigchld_handler(__a_unused int s)
471 PARA_EMERG_LOG("caught early SIGCHLD\n");
475 static void server_init(int argc, char **argv)
477 struct server_cmdline_parser_params params = {
481 .check_ambiguity = 0,
487 init_random_seed_or_die();
488 /* parse command line options */
489 server_cmdline_parser_ext(argc, argv, &conf, ¶ms);
490 HANDLE_VERSION_FLAG("server", conf);
491 drop_privileges_or_die(conf.user_arg, conf.group_arg);
492 /* parse config file, open log and set defaults */
493 parse_config_or_die(0);
494 log_welcome("para_server");
495 init_ipc_or_die(); /* init mmd struct and mmd->lock */
496 /* make sure, the global now pointer is uptodate */
497 gettimeofday(now, NULL);
498 server_uptime(UPTIME_SET); /* reset server uptime */
499 init_user_list(user_list_file);
501 if (conf.daemon_given)
503 PARA_NOTICE_LOG("initializing audio format handlers\n");
507 * Although afs uses its own signal handling we must ignore SIGUSR1
508 * _before_ the afs child process gets born by init_afs() below. It's
509 * racy to do this in the child because the parent might send SIGUSR1
510 * before the child gets a chance to ignore this signal -- only the
513 para_sigaction(SIGUSR1, SIG_IGN);
515 * We have to install a SIGCHLD handler before the afs process is being
516 * forked off. Otherwise, para_server does not notice if afs dies before
517 * the SIGCHLD handler has been installed by init_signal_task() below.
519 para_sigaction(SIGCHLD, tmp_sigchld_handler);
520 PARA_NOTICE_LOG("initializing the audio file selector\n");
521 afs_socket = init_afs();
523 PARA_NOTICE_LOG("initializing virtual streaming system\n");
524 init_vss_task(afs_socket);
525 init_server_command_task(argc, argv);
526 PARA_NOTICE_LOG("server init complete\n");
529 static void status_refresh(void)
531 static int prev_uptime = -1, prev_events = -1;
532 int uptime = server_uptime(UPTIME_GET), ret = 1;
534 if (prev_events != mmd->events)
536 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
538 if (uptime / 60 != prev_uptime / 60)
544 prev_uptime = uptime;
545 prev_events = mmd->events;
546 mmd->vss_status_flags = mmd->new_vss_status_flags;
548 PARA_DEBUG_LOG("%d events, forcing status update\n",
554 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
555 struct timeval *timeout_tv)
560 mutex_unlock(mmd_mutex);
561 ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
562 mutex_lock(mmd_mutex);
567 * The main function of para_server.
569 * \param argc Usual argument count.
570 * \param argv Usual argument vector.
572 * \return EXIT_SUCCESS or EXIT_FAILURE.
574 int main(int argc, char *argv[])
577 static struct sched s = {
582 .select_function = server_select
584 server_init(argc, argv);
585 mutex_lock(mmd_mutex);
588 PARA_EMERG_LOG("%s\n", para_strerror(-ret));