2 * Copyright (C) 1997-2011 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 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,
26 * \ref ogg_afh.c, \ref aac_afh.c, \ref wma_afh.c, \ref spx_afh.c
27 * - Decoders: \ref mp3dec_filter.c, \ref oggdec_filter.c,
28 * \ref aacdec_filter.c, \ref wmadec_filter.c, spxdec_filter.c,
29 * - Volume normalizer: \ref compress_filter.c,
30 * - Output: \ref alsa_write.c, \ref osx_write.c, \ref oss_write.c,
31 * - http: \ref http_recv.c, \ref http_send.c,
32 * - udp: \ref udp_recv.c, \ref udp_send.c,
33 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
34 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
35 * - Afs structures: \ref afs_table, \ref audio_file_data,
36 * \ref afs_info \ref afh_info,
37 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
38 * \ref attribute.c, \ref score.c,
39 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
43 * - Scheduling: \ref sched.c, \ref sched.h,
44 * - Networking: \ref net.c,
45 * - File descriptors: \ref fd.c,
46 * - Signals: \ref signal.c,
47 * - Daemons: \ref daemon.c,
48 * - Strings: \ref string.c, \ref string.h,
49 * - Time: \ref time.c,
50 * - Spawning processes: \ref exec.c,
51 * - Inter process communication: \ref ipc.c,
52 * - Blob tables: \ref blob.c,
53 * - The error subssystem: \ref error.h.
54 * - Access control for paraslash senders: \ref acl.c, \ref acl.h.
56 * Low-level data structures:
58 * - Doubly linked lists: \ref list.h,
59 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
60 * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c,
61 * - Crypto: \ref crypt.c.
62 * - Forward error correction: \ref fec.c.
68 #include <openssl/rc4.h>
76 #include "server.cmdline.h"
85 #include "close_on_fork.h"
92 #include "user_list.h"
96 /** Define the array of error lists needed by para_server. */
99 /** Shut down non-authorized connections after that many seconds. */
100 #define ALARM_TIMEOUT 10
103 * Pointer to shared memory area for communication between para_server
104 * and its children. Exported to vss.c. command.c and to afs.
106 struct misc_meta_data
*mmd
;
109 * The configuration of para_server
111 * It also contains the options for the audio file selector, audio format
112 * handler and all supported senders.
114 struct server_args_info conf
;
116 /** A random value used in child context for authentication. */
117 uint32_t afs_socket_cookie
;
119 /** The mutex protecting the shared memory area containing the mmd struct. */
122 /** The file containing user information (public key, permissions). */
123 static char *user_list_file
= NULL
;
126 /** The task responsible for server command handling. */
127 struct server_command_task
{
128 /** TCP port on which para_server listens for connections. */
130 /** Copied from para_server's main function. */
132 /** Argument vector passed to para_server's main function. */
134 /** The command task structure for scheduling. */
138 static int want_colors(void)
140 if (conf
.color_arg
== color_arg_no
)
142 if (conf
.color_arg
== color_arg_yes
)
144 if (conf
.logfile_given
)
146 return isatty(STDERR_FILENO
);
149 static void init_colors_or_die(void)
155 daemon_set_flag(DF_COLOR_LOG
);
156 daemon_set_default_log_colors();
157 for (i
= 0; i
< conf
.log_color_given
; i
++)
158 daemon_set_log_color_or_die(conf
.log_color_arg
[i
]);
162 * setup shared memory area and get mutex for locking
164 static void init_ipc_or_die(void)
167 int shmid
, ret
= shm_new(sizeof(struct misc_meta_data
));
172 ret
= shm_attach(shmid
, ATTACH_RW
, &shm
);
184 mmd
->num_commands
= 0;
186 mmd
->num_connects
= 0;
187 mmd
->active_connections
= 0;
188 mmd
->vss_status_flags
= VSS_NEXT
;
189 mmd
->new_vss_status_flags
= VSS_NEXT
;
192 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
197 * (Re-)read the server configuration files.
199 * \param override Passed to gengetopt to activate the override feature.
201 * This function also re-opens the logfile and sets the global \a
202 * user_list_file variable.
204 void parse_config_or_die(int override
)
206 char *home
= para_homedir();
211 if (conf
.config_file_given
)
212 cf
= para_strdup(conf
.config_file_arg
);
214 cf
= make_message("%s/.paraslash/server.conf", home
);
215 free(user_list_file
);
216 if (!conf
.user_list_given
)
217 user_list_file
= make_message("%s/.paraslash/server.users", home
);
219 user_list_file
= para_strdup(conf
.user_list_arg
);
220 ret
= file_exists(cf
);
221 if (conf
.config_file_given
&& !ret
) {
223 PARA_EMERG_LOG("can not read config file %s\n", cf
);
227 int tmp
= conf
.daemon_given
;
228 struct server_cmdline_parser_params params
= {
229 .override
= override
,
232 .check_ambiguity
= 0,
233 .print_errors
= !conf
.daemon_given
235 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
236 conf
.daemon_given
= tmp
;
238 if (conf
.logfile_given
) {
239 daemon_set_logfile(conf
.logfile_arg
);
240 daemon_open_log_or_die();
242 daemon_set_loglevel(conf
.loglevel_arg
);
243 init_colors_or_die();
244 daemon_set_flag(DF_LOG_PID
);
245 daemon_set_flag(DF_LOG_LL
);
246 daemon_set_flag(DF_LOG_TIME
);
247 if (conf
.log_timing_given
)
248 daemon_set_flag(DF_LOG_TIMING
);
255 free(user_list_file
);
256 user_list_file
= NULL
;
260 static void signal_pre_select(struct sched
*s
, struct task
*t
)
262 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
263 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
267 * called when server gets SIGHUP or when client invokes hup command.
269 static void handle_sighup(void)
271 PARA_NOTICE_LOG("SIGHUP\n");
272 parse_config_or_die(1); /* reopens log */
273 init_user_list(user_list_file
); /* reload user list */
275 kill(mmd
->afs_pid
, SIGHUP
);
278 static void signal_post_select(struct sched
*s
, __a_unused
struct task
*t
)
280 int signum
= para_next_signal(&s
->rfds
);
291 int ret
= para_reap_child(&pid
);
294 if (pid
!= mmd
->afs_pid
)
296 PARA_EMERG_LOG("fatal: afs died\n");
301 /* die on sigint/sigterm. Kill all children too. */
304 PARA_EMERG_LOG("terminating on signal %d\n", signum
);
307 * We must wait for afs because afs catches SIGINT/SIGTERM.
308 * Before reacting to the signal, afs might want to use the
309 * shared memory area and the mmd mutex. If we destroy this
310 * mutex too early and afs tries to lock the shared memory
311 * area, the call to mutex_lock() will fail and terminate the
312 * afs process. This leads to dirty osl tables.
314 * There's no such problem with the other children of the
315 * server process (the command handlers) as these reset their
316 * SIGINT/SIGTERM handlers to the default action, i.e. these
317 * processes get killed immediately by the above kill().
319 PARA_INFO_LOG("waiting for afs (pid %d) to die\n",
321 waitpid(mmd
->afs_pid
, NULL
, 0);
323 free(mmd
->afd
.afhi
.chunk_table
);
325 mutex_destroy(mmd_mutex
);
331 static void init_signal_task(void)
333 static struct signal_task signal_task_struct
,
334 *st
= &signal_task_struct
;
336 st
->task
.pre_select
= signal_pre_select
;
337 st
->task
.post_select
= signal_post_select
;
338 sprintf(st
->task
.status
, "signal task");
340 PARA_NOTICE_LOG("setting up signal handling\n");
341 st
->fd
= para_signal_init(); /* always successful */
342 para_install_sighandler(SIGINT
);
343 para_install_sighandler(SIGTERM
);
344 para_install_sighandler(SIGHUP
);
345 para_install_sighandler(SIGCHLD
);
346 para_sigaction(SIGPIPE
, SIG_IGN
);
347 add_close_on_fork_list(st
->fd
);
348 register_task(&st
->task
);
351 static void command_pre_select(struct sched
*s
, struct task
*t
)
353 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
354 para_fd_set(sct
->listen_fd
, &s
->rfds
, &s
->max_fileno
);
357 static void command_post_select(struct sched
*s
, struct task
*t
)
359 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
364 uint32_t *chunk_table
;
366 ret
= para_accept(sct
->listen_fd
, &s
->rfds
, NULL
, 0, &new_fd
);
369 peer_name
= remote_name(new_fd
);
370 PARA_INFO_LOG("got connection from %s, forking\n", peer_name
);
372 mmd
->active_connections
++;
374 * The chunk table is a pointer located in the mmd struct that points
375 * to dynamically allocated memory, i.e. it must be freed by the parent
376 * and the child. However, as the mmd struct is in a shared memory
377 * area, there's no guarantee that after the fork this pointer is still
378 * valid in child context. As it is not used in the child anyway, we
379 * save it to a local variable before the fork and free the memory via
380 * that copy in the child directly after the fork.
382 chunk_table
= mmd
->afd
.afhi
.chunk_table
;
385 ret
= -ERRNO_TO_PARA_ERROR(errno
);
390 /* parent keeps accepting connections */
393 /* mmd might already have changed at this point */
395 alarm(ALARM_TIMEOUT
);
397 para_signal_shutdown();
399 * put info on who we are serving into argv[0] to make
400 * client ip visible in top/ps
402 for (i
= sct
->argc
- 1; i
>= 0; i
--)
403 memset(sct
->argv
[i
], 0, strlen(sct
->argv
[i
]));
404 sprintf(sct
->argv
[0], "para_server (serving %s)", peer_name
);
405 return handle_connect(new_fd
, peer_name
);
408 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
411 static void init_server_command_task(int argc
, char **argv
)
414 static struct server_command_task server_command_task_struct
,
415 *sct
= &server_command_task_struct
;
417 PARA_NOTICE_LOG("initializing tcp command socket\n");
418 sct
->task
.pre_select
= command_pre_select
;
419 sct
->task
.post_select
= command_post_select
;
422 ret
= para_listen_simple(IPPROTO_TCP
, conf
.port_arg
);
425 sct
->listen_fd
= ret
;
426 ret
= mark_fd_nonblocking(sct
->listen_fd
);
429 add_close_on_fork_list(sct
->listen_fd
); /* child doesn't need the listener */
430 sprintf(sct
->task
.status
, "server command task");
431 register_task(&sct
->task
);
434 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
438 static int init_afs(void)
440 int ret
, afs_server_socket
[2];
443 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
446 get_random_bytes_or_die((unsigned char *)&afs_socket_cookie
,
447 sizeof(afs_socket_cookie
));
451 if (afs_pid
== 0) { /* child (afs) */
452 close(afs_server_socket
[0]);
453 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
455 mmd
->afs_pid
= afs_pid
;
456 close(afs_server_socket
[1]);
457 ret
= mark_fd_nonblocking(afs_server_socket
[0]);
460 add_close_on_fork_list(afs_server_socket
[0]);
461 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
462 afs_server_socket
[0], (unsigned) afs_socket_cookie
);
463 return afs_server_socket
[0];
466 __noreturn
static void tmp_sigchld_handler(__a_unused
int s
)
468 PARA_EMERG_LOG("caught early SIGCHLD\n");
472 static void server_init(int argc
, char **argv
)
474 struct server_cmdline_parser_params params
= {
478 .check_ambiguity
= 0,
484 init_random_seed_or_die();
485 /* parse command line options */
486 server_cmdline_parser_ext(argc
, argv
, &conf
, ¶ms
);
487 HANDLE_VERSION_FLAG("server", conf
);
488 drop_privileges_or_die(conf
.user_arg
, conf
.group_arg
);
489 /* parse config file, open log and set defaults */
490 parse_config_or_die(0);
491 log_welcome("para_server");
492 init_ipc_or_die(); /* init mmd struct and mmd->lock */
493 /* make sure, the global now pointer is uptodate */
494 gettimeofday(now
, NULL
);
495 server_uptime(UPTIME_SET
); /* reset server uptime */
496 init_user_list(user_list_file
);
498 if (conf
.daemon_given
)
500 PARA_NOTICE_LOG("initializing audio format handlers\n");
504 * Although afs uses its own signal handling we must ignore SIGUSR1
505 * _before_ the afs child process gets born by init_afs() below. It's
506 * racy to do this in the child because the parent might send SIGUSR1
507 * before the child gets a chance to ignore this signal -- only the
510 para_sigaction(SIGUSR1
, SIG_IGN
);
512 * We have to install a SIGCHLD handler before the afs process is being
513 * forked off. Otherwise, para_server does not notice if afs dies before
514 * the SIGCHLD handler has been installed by init_signal_task() below.
516 para_sigaction(SIGCHLD
, tmp_sigchld_handler
);
517 PARA_NOTICE_LOG("initializing the audio file selector\n");
518 afs_socket
= init_afs();
520 PARA_NOTICE_LOG("initializing virtual streaming system\n");
521 init_vss_task(afs_socket
);
522 init_server_command_task(argc
, argv
);
523 PARA_NOTICE_LOG("server init complete\n");
526 static void status_refresh(void)
528 static int prev_uptime
= -1, prev_events
= -1;
529 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
531 if (prev_events
!= mmd
->events
)
533 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
535 if (uptime
/ 60 != prev_uptime
/ 60)
541 prev_uptime
= uptime
;
542 prev_events
= mmd
->events
;
543 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
545 PARA_DEBUG_LOG("%d events, forcing status update\n",
551 static int server_select(int max_fileno
, fd_set
*readfds
, fd_set
*writefds
,
552 struct timeval
*timeout_tv
)
557 mutex_unlock(mmd_mutex
);
558 ret
= para_select(max_fileno
+ 1, readfds
, writefds
, timeout_tv
);
559 mutex_lock(mmd_mutex
);
564 * The main function of para_server.
566 * \param argc Usual argument count.
567 * \param argv Usual argument vector.
569 * \return EXIT_SUCCESS or EXIT_FAILURE.
571 int main(int argc
, char *argv
[])
574 static struct sched s
= {
579 .select_function
= server_select
581 server_init(argc
, argv
);
582 mutex_lock(mmd_mutex
);
585 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));