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,
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 /** Array of error strings. */
71 __printf_2_3
void (*para_log
)(int, const char*, ...) = daemon_log
;
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 if (daemon_init_colors_or_die(conf
.color_arg
, color_arg_auto
, color_arg_no
,
211 conf
.logfile_given
)) {
213 for (i
= 0; i
< conf
.log_color_given
; i
++)
214 daemon_set_log_color_or_die(conf
.log_color_arg
[i
]);
216 daemon_set_flag(DF_LOG_PID
);
217 daemon_set_flag(DF_LOG_LL
);
218 daemon_set_flag(DF_LOG_TIME
);
219 if (conf
.log_timing_given
)
220 daemon_set_flag(DF_LOG_TIMING
);
227 free(user_list_file
);
228 user_list_file
= NULL
;
233 * called when server gets SIGHUP or when client invokes hup command.
235 static void handle_sighup(void)
237 PARA_NOTICE_LOG("SIGHUP\n");
238 parse_config_or_die(1); /* reopens log */
239 init_user_list(user_list_file
); /* reload user list */
241 kill(mmd
->afs_pid
, SIGHUP
);
244 static int signal_post_select(struct sched
*s
, __a_unused
void *context
)
246 int signum
= para_next_signal(&s
->rfds
);
257 int ret
= para_reap_child(&pid
);
260 if (pid
!= mmd
->afs_pid
)
262 PARA_EMERG_LOG("fatal: afs died\n");
267 /* die on sigint/sigterm. Kill all children too. */
270 PARA_EMERG_LOG("terminating on signal %d\n", signum
);
273 * We must wait for afs because afs catches SIGINT/SIGTERM.
274 * Before reacting to the signal, afs might want to use the
275 * shared memory area and the mmd mutex. If we destroy this
276 * mutex too early and afs tries to lock the shared memory
277 * area, the call to mutex_lock() will fail and terminate the
278 * afs process. This leads to dirty osl tables.
280 * There's no such problem with the other children of the
281 * server process (the command handlers) as these reset their
282 * SIGINT/SIGTERM handlers to the default action, i.e. these
283 * processes get killed immediately by the above kill().
285 PARA_INFO_LOG("waiting for afs (pid %d) to die\n",
287 waitpid(mmd
->afs_pid
, NULL
, 0);
289 free(mmd
->afd
.afhi
.chunk_table
);
291 mutex_destroy(mmd_mutex
);
298 static void init_signal_task(void)
300 signal_task
= signal_init_or_die();
301 para_install_sighandler(SIGINT
);
302 para_install_sighandler(SIGTERM
);
303 para_install_sighandler(SIGHUP
);
304 para_install_sighandler(SIGCHLD
);
305 para_sigaction(SIGPIPE
, SIG_IGN
);
306 add_close_on_fork_list(signal_task
->fd
);
307 signal_task
->task
= task_register(&(struct task_info
) {
309 .pre_select
= signal_pre_select
,
310 .post_select
= signal_post_select
,
311 .context
= signal_task
,
316 static void command_pre_select(struct sched
*s
, void *context
)
318 struct server_command_task
*sct
= context
;
319 para_fd_set(sct
->listen_fd
, &s
->rfds
, &s
->max_fileno
);
322 static int command_post_select(struct sched
*s
, void *context
)
324 struct server_command_task
*sct
= context
;
329 uint32_t *chunk_table
;
331 ret
= para_accept(sct
->listen_fd
, &s
->rfds
, NULL
, 0, &new_fd
);
334 peer_name
= remote_name(new_fd
);
335 PARA_INFO_LOG("got connection from %s, forking\n", peer_name
);
337 mmd
->active_connections
++;
339 * The chunk table is a pointer located in the mmd struct that points
340 * to dynamically allocated memory, i.e. it must be freed by the parent
341 * and the child. However, as the mmd struct is in a shared memory
342 * area, there's no guarantee that after the fork this pointer is still
343 * valid in child context. As it is not used in the child anyway, we
344 * save it to a local variable before the fork and free the memory via
345 * that copy in the child directly after the fork.
347 chunk_table
= mmd
->afd
.afhi
.chunk_table
;
350 ret
= -ERRNO_TO_PARA_ERROR(errno
);
354 /* avoid problems with non-fork-safe PRNGs */
355 unsigned char buf
[16];
356 get_random_bytes_or_die(buf
, sizeof(buf
));
358 /* parent keeps accepting connections */
361 /* mmd might already have changed at this point */
363 alarm(ALARM_TIMEOUT
);
365 signal_shutdown(signal_task
);
367 * put info on who we are serving into argv[0] to make
368 * client ip visible in top/ps
370 for (i
= sct
->argc
- 1; i
>= 0; i
--)
371 memset(sct
->argv
[i
], 0, strlen(sct
->argv
[i
]));
372 sprintf(sct
->argv
[0], "para_server (serving %s)", peer_name
);
373 handle_connect(new_fd
, peer_name
);
377 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
381 static void init_server_command_task(int argc
, char **argv
)
384 static struct server_command_task server_command_task_struct
,
385 *sct
= &server_command_task_struct
;
387 PARA_NOTICE_LOG("initializing tcp command socket\n");
390 ret
= para_listen_simple(IPPROTO_TCP
, conf
.port_arg
);
393 sct
->listen_fd
= ret
;
394 ret
= mark_fd_nonblocking(sct
->listen_fd
);
397 add_close_on_fork_list(sct
->listen_fd
); /* child doesn't need the listener */
398 sct
->task
= task_register(&(struct task_info
) {
399 .name
= "server command",
400 .pre_select
= command_pre_select
,
401 .post_select
= command_post_select
,
406 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
410 static int init_afs(int argc
, char **argv
)
412 int ret
, afs_server_socket
[2];
416 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, afs_server_socket
);
419 get_random_bytes_or_die((unsigned char *)&afs_socket_cookie
,
420 sizeof(afs_socket_cookie
));
424 if (afs_pid
== 0) { /* child (afs) */
427 for (i
= argc
- 1; i
>= 0; i
--)
428 memset(argv
[i
], 0, strlen(argv
[i
]));
429 sprintf(argv
[0], "para_server (afs)");
430 close(afs_server_socket
[0]);
431 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
433 mmd
->afs_pid
= afs_pid
;
434 close(afs_server_socket
[1]);
435 if (read(afs_server_socket
[0], &c
, 1) <= 0) {
436 PARA_EMERG_LOG("early afs exit\n");
439 ret
= mark_fd_nonblocking(afs_server_socket
[0]);
442 add_close_on_fork_list(afs_server_socket
[0]);
443 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
444 afs_server_socket
[0], (unsigned) afs_socket_cookie
);
445 return afs_server_socket
[0];
448 __noreturn
static void print_help_and_die(void)
450 struct ggo_help h
= DEFINE_GGO_HELP(server
);
451 bool d
= conf
.detailed_help_given
;
453 ggo_print_help(&h
, d
? GPH_STANDARD_FLAGS_DETAILED
: GPH_STANDARD_FLAGS
);
457 static void server_init(int argc
, char **argv
)
459 struct server_cmdline_parser_params params
= {
463 .check_ambiguity
= 0,
466 int afs_socket
, daemon_pipe
= -1;
469 init_random_seed_or_die();
470 /* parse command line options */
471 server_cmdline_parser_ext(argc
, argv
, &conf
, ¶ms
);
472 daemon_set_loglevel(conf
.loglevel_arg
);
473 version_handle_flag("server", conf
.version_given
);
474 if (conf
.help_given
|| conf
.detailed_help_given
)
475 print_help_and_die();
476 daemon_set_priority(conf
.priority_arg
);
477 daemon_drop_privileges_or_die(conf
.user_arg
, conf
.group_arg
);
478 /* parse config file, open log and set defaults */
479 parse_config_or_die(0);
480 daemon_log_welcome("server");
481 init_ipc_or_die(); /* init mmd struct and mmd->lock */
482 daemon_set_start_time();
483 init_user_list(user_list_file
);
485 if (conf
.daemon_given
)
486 daemon_pipe
= daemonize(true /* parent waits for us */);
487 PARA_NOTICE_LOG("initializing audio format handlers\n");
491 * Although afs uses its own signal handling we must ignore SIGUSR1
492 * _before_ the afs child process gets born by init_afs() below. It's
493 * racy to do this in the child because the parent might send SIGUSR1
494 * before the child gets a chance to ignore this signal -- only the
497 para_sigaction(SIGUSR1
, SIG_IGN
);
499 * We have to block SIGCHLD before the afs process is being forked off.
500 * Otherwise, para_server does not notice if afs dies before the
501 * SIGCHLD handler has been installed for the parent process by
502 * init_signal_task() below.
504 para_block_signal(SIGCHLD
);
505 PARA_NOTICE_LOG("initializing the audio file selector\n");
506 afs_socket
= init_afs(argc
, argv
);
508 para_unblock_signal(SIGCHLD
);
509 PARA_NOTICE_LOG("initializing virtual streaming system\n");
510 init_vss_task(afs_socket
, &sched
);
511 init_server_command_task(argc
, argv
);
512 if (daemon_pipe
>= 0) {
513 if (write(daemon_pipe
, "\0", 1) < 0) {
514 PARA_EMERG_LOG("daemon_pipe: %s", strerror(errno
));
519 PARA_NOTICE_LOG("server init complete\n");
522 static void status_refresh(void)
524 static int prev_uptime
= -1, prev_events
= -1;
525 int uptime
= daemon_get_uptime(now
);
527 if (prev_events
!= mmd
->events
)
529 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
531 if (uptime
/ 60 != prev_uptime
/ 60)
537 prev_uptime
= uptime
;
538 prev_events
= mmd
->events
;
539 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
540 PARA_DEBUG_LOG("%u events, forcing status update\n", mmd
->events
);
544 static int server_select(int max_fileno
, fd_set
*readfds
, fd_set
*writefds
,
545 struct timeval
*timeout_tv
)
550 mutex_unlock(mmd_mutex
);
551 ret
= para_select(max_fileno
+ 1, readfds
, writefds
, timeout_tv
);
552 mutex_lock(mmd_mutex
);
557 * The main function of para_server.
559 * \param argc Usual argument count.
560 * \param argv Usual argument vector.
562 * \return EXIT_SUCCESS or EXIT_FAILURE.
564 int main(int argc
, char *argv
[])
568 sched
.default_timeout
.tv_sec
= 1;
569 sched
.select_function
= server_select
;
571 server_init(argc
, argv
);
572 mutex_lock(mmd_mutex
);
573 ret
= schedule(&sched
);
574 sched_shutdown(&sched
);
576 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));