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.
55 * - Internal crypto API: \ref crypt.h.
57 * Low-level data structures:
59 * - Doubly linked lists: \ref list.h,
60 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
61 * - openssl: \ref crypt.c
62 * - libgcrypt: \ref gcrypt.c
63 * - Forward error correction: \ref fec.c.
75 #include "server.cmdline.h"
85 #include "close_on_fork.h"
91 #include "user_list.h"
95 __printf_2_3
void (*para_log
)(int, const char*, ...) = daemon_log
;
97 /** Define the array of error lists needed by para_server. */
100 /** Shut down non-authorized connections after that many seconds. */
101 #define ALARM_TIMEOUT 10
104 * Pointer to shared memory area for communication between para_server
105 * and its children. Exported to vss.c. command.c and to afs.
107 struct misc_meta_data
*mmd
;
110 * The configuration of para_server
112 * It also contains the options for the audio file selector, audio format
113 * handler and all supported senders.
115 struct server_args_info conf
;
117 /** A random value used in child context for authentication. */
118 uint32_t afs_socket_cookie
;
120 /** The mutex protecting the shared memory area containing the mmd struct. */
123 /** The file containing user information (public key, permissions). */
124 static char *user_list_file
= NULL
;
126 static struct sched sched
;
128 /** The task responsible for server command handling. */
129 struct server_command_task
{
130 /** TCP port on which para_server listens for connections. */
132 /** Copied from para_server's main function. */
134 /** Argument vector passed to para_server's main function. */
136 /** The command task structure for scheduling. */
140 static int want_colors(void)
142 if (conf
.color_arg
== color_arg_no
)
144 if (conf
.color_arg
== color_arg_yes
)
146 if (conf
.logfile_given
)
148 return isatty(STDERR_FILENO
);
151 static void init_colors_or_die(void)
157 daemon_set_flag(DF_COLOR_LOG
);
158 daemon_set_default_log_colors();
159 for (i
= 0; i
< conf
.log_color_given
; i
++)
160 daemon_set_log_color_or_die(conf
.log_color_arg
[i
]);
164 * setup shared memory area and get mutex for locking
166 static void init_ipc_or_die(void)
169 int shmid
, ret
= shm_new(sizeof(struct misc_meta_data
));
174 ret
= shm_attach(shmid
, ATTACH_RW
, &shm
);
186 mmd
->num_commands
= 0;
188 mmd
->num_connects
= 0;
189 mmd
->active_connections
= 0;
190 mmd
->vss_status_flags
= VSS_NEXT
;
191 mmd
->new_vss_status_flags
= VSS_NEXT
;
194 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
199 * (Re-)read the server configuration files.
201 * \param override Passed to gengetopt to activate the override feature.
203 * This function also re-opens the logfile and sets the global \a
204 * user_list_file variable.
206 void parse_config_or_die(int override
)
208 char *home
= para_homedir();
213 if (conf
.config_file_given
)
214 cf
= para_strdup(conf
.config_file_arg
);
216 cf
= make_message("%s/.paraslash/server.conf", home
);
217 free(user_list_file
);
218 if (!conf
.user_list_given
)
219 user_list_file
= make_message("%s/.paraslash/server.users", home
);
221 user_list_file
= para_strdup(conf
.user_list_arg
);
222 ret
= file_exists(cf
);
223 if (conf
.config_file_given
&& !ret
) {
225 PARA_EMERG_LOG("can not read config file %s\n", cf
);
229 int tmp
= conf
.daemon_given
;
230 struct server_cmdline_parser_params params
= {
231 .override
= override
,
234 .check_ambiguity
= 0,
235 .print_errors
= !conf
.daemon_given
237 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
238 conf
.daemon_given
= tmp
;
240 if (conf
.logfile_given
) {
241 daemon_set_logfile(conf
.logfile_arg
);
242 daemon_open_log_or_die();
244 daemon_set_loglevel(conf
.loglevel_arg
);
245 init_colors_or_die();
246 daemon_set_flag(DF_LOG_PID
);
247 daemon_set_flag(DF_LOG_LL
);
248 daemon_set_flag(DF_LOG_TIME
);
249 if (conf
.log_timing_given
)
250 daemon_set_flag(DF_LOG_TIMING
);
257 free(user_list_file
);
258 user_list_file
= NULL
;
262 static void signal_pre_select(struct sched
*s
, struct task
*t
)
264 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
265 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
269 * called when server gets SIGHUP or when client invokes hup command.
271 static void handle_sighup(void)
273 PARA_NOTICE_LOG("SIGHUP\n");
274 parse_config_or_die(1); /* reopens log */
275 init_user_list(user_list_file
); /* reload user list */
277 kill(mmd
->afs_pid
, SIGHUP
);
280 static void signal_post_select(struct sched
*s
, __a_unused
struct task
*t
)
282 int signum
= para_next_signal(&s
->rfds
);
293 int ret
= para_reap_child(&pid
);
296 if (pid
!= mmd
->afs_pid
)
298 PARA_EMERG_LOG("fatal: afs died\n");
303 /* die on sigint/sigterm. Kill all children too. */
306 PARA_EMERG_LOG("terminating on signal %d\n", signum
);
309 * We must wait for afs because afs catches SIGINT/SIGTERM.
310 * Before reacting to the signal, afs might want to use the
311 * shared memory area and the mmd mutex. If we destroy this
312 * mutex too early and afs tries to lock the shared memory
313 * area, the call to mutex_lock() will fail and terminate the
314 * afs process. This leads to dirty osl tables.
316 * There's no such problem with the other children of the
317 * server process (the command handlers) as these reset their
318 * SIGINT/SIGTERM handlers to the default action, i.e. these
319 * processes get killed immediately by the above kill().
321 PARA_INFO_LOG("waiting for afs (pid %d) to die\n",
323 waitpid(mmd
->afs_pid
, NULL
, 0);
325 free(mmd
->afd
.afhi
.chunk_table
);
327 mutex_destroy(mmd_mutex
);
333 static void init_signal_task(void)
335 static struct signal_task signal_task_struct
,
336 *st
= &signal_task_struct
;
338 st
->task
.pre_select
= signal_pre_select
;
339 st
->task
.post_select
= signal_post_select
;
340 sprintf(st
->task
.status
, "signal task");
342 PARA_NOTICE_LOG("setting up signal handling\n");
343 st
->fd
= para_signal_init(); /* always successful */
344 para_install_sighandler(SIGINT
);
345 para_install_sighandler(SIGTERM
);
346 para_install_sighandler(SIGHUP
);
347 para_install_sighandler(SIGCHLD
);
348 para_sigaction(SIGPIPE
, SIG_IGN
);
349 add_close_on_fork_list(st
->fd
);
350 register_task(&sched
, &st
->task
);
353 static void command_pre_select(struct sched
*s
, struct task
*t
)
355 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
356 para_fd_set(sct
->listen_fd
, &s
->rfds
, &s
->max_fileno
);
359 static void command_post_select(struct sched
*s
, struct task
*t
)
361 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
366 uint32_t *chunk_table
;
368 ret
= para_accept(sct
->listen_fd
, &s
->rfds
, NULL
, 0, &new_fd
);
371 peer_name
= remote_name(new_fd
);
372 PARA_INFO_LOG("got connection from %s, forking\n", peer_name
);
374 mmd
->active_connections
++;
376 * The chunk table is a pointer located in the mmd struct that points
377 * to dynamically allocated memory, i.e. it must be freed by the parent
378 * and the child. However, as the mmd struct is in a shared memory
379 * area, there's no guarantee that after the fork this pointer is still
380 * valid in child context. As it is not used in the child anyway, we
381 * save it to a local variable before the fork and free the memory via
382 * that copy in the child directly after the fork.
384 chunk_table
= mmd
->afd
.afhi
.chunk_table
;
387 ret
= -ERRNO_TO_PARA_ERROR(errno
);
392 /* parent keeps accepting connections */
395 /* mmd might already have changed at this point */
397 alarm(ALARM_TIMEOUT
);
399 para_signal_shutdown();
401 * put info on who we are serving into argv[0] to make
402 * client ip visible in top/ps
404 for (i
= sct
->argc
- 1; i
>= 0; i
--)
405 memset(sct
->argv
[i
], 0, strlen(sct
->argv
[i
]));
406 sprintf(sct
->argv
[0], "para_server (serving %s)", peer_name
);
407 return handle_connect(new_fd
, peer_name
);
410 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
413 static void init_server_command_task(int argc
, char **argv
)
416 static struct server_command_task server_command_task_struct
,
417 *sct
= &server_command_task_struct
;
419 PARA_NOTICE_LOG("initializing tcp command socket\n");
420 sct
->task
.pre_select
= command_pre_select
;
421 sct
->task
.post_select
= command_post_select
;
424 ret
= para_listen_simple(IPPROTO_TCP
, conf
.port_arg
);
427 sct
->listen_fd
= ret
;
428 ret
= mark_fd_nonblocking(sct
->listen_fd
);
431 add_close_on_fork_list(sct
->listen_fd
); /* child doesn't need the listener */
432 sprintf(sct
->task
.status
, "server command task");
433 register_task(&sched
, &sct
->task
);
436 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
440 static int init_afs(int argc
, char **argv
)
442 int ret
, afs_server_socket
[2];
445 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
448 get_random_bytes_or_die((unsigned char *)&afs_socket_cookie
,
449 sizeof(afs_socket_cookie
));
453 if (afs_pid
== 0) { /* child (afs) */
455 for (i
= argc
- 1; i
>= 0; i
--)
456 memset(argv
[i
], 0, strlen(argv
[i
]));
457 sprintf(argv
[0], "para_server (afs)");
458 close(afs_server_socket
[0]);
459 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
461 mmd
->afs_pid
= afs_pid
;
462 close(afs_server_socket
[1]);
463 ret
= mark_fd_nonblocking(afs_server_socket
[0]);
466 add_close_on_fork_list(afs_server_socket
[0]);
467 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
468 afs_server_socket
[0], (unsigned) afs_socket_cookie
);
469 return afs_server_socket
[0];
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 set_server_start_time(now
);
496 init_user_list(user_list_file
);
498 if (conf
.daemon_given
)
499 daemonize(true /* parent waits for SIGTERM */);
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 block SIGCHLD before the afs process is being forked off.
513 * Otherwise, para_server does not notice if afs dies before the
514 * SIGCHLD handler has been installed for the parent process by
515 * init_signal_task() below.
517 para_block_signal(SIGCHLD
);
518 PARA_NOTICE_LOG("initializing the audio file selector\n");
519 afs_socket
= init_afs(argc
, argv
);
521 para_unblock_signal(SIGCHLD
);
522 PARA_NOTICE_LOG("initializing virtual streaming system\n");
523 init_vss_task(afs_socket
, &sched
);
524 init_server_command_task(argc
, argv
);
525 if (conf
.daemon_given
)
526 kill(getppid(), SIGTERM
);
527 PARA_NOTICE_LOG("server init complete\n");
530 static void status_refresh(void)
532 static int prev_uptime
= -1, prev_events
= -1;
533 int uptime
= get_server_uptime(now
);
535 if (prev_events
!= mmd
->events
)
537 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
539 if (uptime
/ 60 != prev_uptime
/ 60)
545 prev_uptime
= uptime
;
546 prev_events
= mmd
->events
;
547 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
548 PARA_DEBUG_LOG("%d events, forcing status update\n", mmd
->events
);
552 static int server_select(int max_fileno
, fd_set
*readfds
, fd_set
*writefds
,
553 struct timeval
*timeout_tv
)
558 mutex_unlock(mmd_mutex
);
559 ret
= para_select(max_fileno
+ 1, readfds
, writefds
, timeout_tv
);
560 mutex_lock(mmd_mutex
);
565 * The main function of para_server.
567 * \param argc Usual argument count.
568 * \param argv Usual argument vector.
570 * \return EXIT_SUCCESS or EXIT_FAILURE.
572 int main(int argc
, char *argv
[])
576 sched
.default_timeout
.tv_sec
= 1;
577 sched
.select_function
= server_select
;
579 server_init(argc
, argv
);
580 mutex_lock(mmd_mutex
);
581 ret
= schedule(&sched
);
583 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));