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 fsck.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 * - The object storage layer: \ref osl.c,
51 * - Blob tables: \ref blob.c,
52 * - The error subssystem: \ref error.h.
53 * - Access control for paraslash senders: \ref acl.c, \ref acl.h.
55 * Low-level data structures:
57 * - Doubly linked lists: \ref list.h,
58 * - Red-black trees: \ref rbtree.h, \ref rbtree.c,
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.
71 #include "server.cmdline.h"
78 #include "close_on_fork.h"
87 #include "user_list.h"
90 /** Define the array of error lists needed by para_server. */
93 /** Shut down non-authorized connections after that many seconds. */
94 #define ALARM_TIMEOUT 10
97 * Pointer to shared memory area for communication between para_server
98 * and its children. Exported to vss.c. command.c and to afs.
100 struct misc_meta_data
*mmd
;
103 * The configuration of para_server
105 * It also contains the options for the audio file selector, audio format
106 * handler and all supported senders.
108 struct server_args_info conf
;
110 /** A random value used in child context for authentication. */
111 uint32_t afs_socket_cookie
;
113 /** The mutex protecting the shared memory area containing the mmd struct. */
116 /** The file containing user information (public key, permissions). */
117 static char *user_list_file
= NULL
;
120 /** The task responsible for server command handling. */
121 struct server_command_task
{
122 /** TCP port on which para_server listens for connections. */
124 /** Copied from para_server's main function. */
126 /** Argument vector passed to para_server's main function. */
128 /** The command task structure for scheduling. */
132 static int want_colors(void)
134 if (conf
.color_arg
== color_arg_no
)
136 if (conf
.color_arg
== color_arg_yes
)
138 if (conf
.logfile_given
)
140 return isatty(STDERR_FILENO
);
143 static void init_colors_or_die(void)
149 daemon_set_flag(DF_COLOR_LOG
);
150 daemon_set_default_log_colors();
151 for (i
= 0; i
< conf
.log_color_given
; i
++) {
152 ret
= daemon_set_log_color(conf
.log_color_arg
[i
]);
159 * setup shared memory area and get mutex for locking
161 static void init_ipc_or_die(void)
164 int shmid
, ret
= shm_new(sizeof(struct misc_meta_data
));
169 ret
= shm_attach(shmid
, ATTACH_RW
, &shm
);
181 mmd
->num_commands
= 0;
183 mmd
->num_connects
= 0;
184 mmd
->active_connections
= 0;
185 mmd
->vss_status_flags
= VSS_NEXT
;
186 mmd
->new_vss_status_flags
= VSS_NEXT
;
189 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
194 * (Re-)read the server configuration files.
196 * \param override Passed to gengetopt to activate the override feature.
198 * This function also re-opens the logfile and sets the global \a
199 * user_list_file variable.
201 void parse_config_or_die(int override
)
203 char *home
= para_homedir();
208 if (conf
.config_file_given
)
209 cf
= para_strdup(conf
.config_file_arg
);
211 cf
= make_message("%s/.paraslash/server.conf", home
);
212 free(user_list_file
);
213 if (!conf
.user_list_given
)
214 user_list_file
= make_message("%s/.paraslash/server.users", home
);
216 user_list_file
= para_strdup(conf
.user_list_arg
);
217 ret
= file_exists(cf
);
218 if (conf
.config_file_given
&& !ret
) {
220 PARA_EMERG_LOG("can not read config file %s\n", cf
);
224 int tmp
= conf
.daemon_given
;
225 struct server_cmdline_parser_params params
= {
226 .override
= override
,
229 .check_ambiguity
= 0,
230 .print_errors
= !conf
.daemon_given
232 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
233 conf
.daemon_given
= tmp
;
235 if (conf
.logfile_given
) {
236 daemon_set_logfile(conf
.logfile_arg
);
237 daemon_open_log_or_die();
239 daemon_set_loglevel(conf
.loglevel_arg
);
240 init_colors_or_die();
241 daemon_set_flag(DF_LOG_PID
);
242 daemon_set_flag(DF_LOG_LL
);
243 daemon_set_flag(DF_LOG_TIME
);
250 free(user_list_file
);
251 user_list_file
= NULL
;
255 static void signal_pre_select(struct sched
*s
, struct task
*t
)
257 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
258 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
262 * called when server gets SIGHUP or when client invokes hup command.
264 static void handle_sighup(void)
266 PARA_NOTICE_LOG("SIGHUP\n");
267 parse_config_or_die(1); /* reopens log */
268 init_user_list(user_list_file
); /* reload user list */
270 kill(mmd
->afs_pid
, SIGHUP
);
273 static void signal_post_select(struct sched
*s
, struct task
*t
)
275 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
277 if (!FD_ISSET(st
->fd
, &s
->rfds
))
280 st
->signum
= para_next_signal();
281 switch (st
->signum
) {
288 int ret
= para_reap_child(&pid
);
291 if (pid
!= mmd
->afs_pid
)
293 PARA_EMERG_LOG("fatal: afs died\n");
297 /* die on sigint/sigterm. Kill all children too. */
300 PARA_EMERG_LOG("terminating on signal %d\n", st
->signum
);
303 mutex_destroy(mmd_mutex
);
309 static void init_signal_task(void)
311 static struct signal_task signal_task_struct
,
312 *st
= &signal_task_struct
;
314 st
->task
.pre_select
= signal_pre_select
;
315 st
->task
.post_select
= signal_post_select
;
316 sprintf(st
->task
.status
, "signal task");
318 st
->fd
= para_signal_init(); /* always successful */
320 PARA_NOTICE_LOG("setting up signal handlers\n");
321 if (para_install_sighandler(SIGINT
) < 0)
323 if (para_install_sighandler(SIGTERM
) < 0)
325 if (para_install_sighandler(SIGHUP
) < 0)
327 if (para_install_sighandler(SIGCHLD
) < 0)
329 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
331 add_close_on_fork_list(st
->fd
);
332 register_task(&st
->task
);
335 PARA_EMERG_LOG("could not install signal handlers\n");
339 static void command_pre_select(struct sched
*s
, struct task
*t
)
341 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
342 para_fd_set(sct
->listen_fd
, &s
->rfds
, &s
->max_fileno
);
345 static void command_post_select(struct sched
*s
, struct task
*t
)
347 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
353 if (!FD_ISSET(sct
->listen_fd
, &s
->rfds
))
355 ret
= para_accept(sct
->listen_fd
, NULL
, 0);
359 peer_name
= remote_name(new_fd
);
360 PARA_INFO_LOG("got connection from %s, forking\n", peer_name
);
362 mmd
->active_connections
++;
366 ret
= -ERRNO_TO_PARA_ERROR(errno
);
371 /* parent keeps accepting connections */
374 alarm(ALARM_TIMEOUT
);
376 para_signal_shutdown();
378 * put info on who we are serving into argv[0] to make
379 * client ip visible in top/ps
381 for (i
= sct
->argc
- 1; i
>= 0; i
--)
382 memset(sct
->argv
[i
], 0, strlen(sct
->argv
[i
]));
383 sprintf(sct
->argv
[0], "para_server (serving %s)", peer_name
);
384 return handle_connect(new_fd
, peer_name
);
387 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
390 static void init_server_command_task(int argc
, char **argv
)
393 static struct server_command_task server_command_task_struct
,
394 *sct
= &server_command_task_struct
;
396 PARA_NOTICE_LOG("initializing tcp command socket\n");
397 sct
->task
.pre_select
= command_pre_select
;
398 sct
->task
.post_select
= command_post_select
;
401 ret
= para_listen(AF_UNSPEC
, IPPROTO_TCP
, conf
.port_arg
);
404 sct
->listen_fd
= ret
;
405 ret
= mark_fd_nonblocking(sct
->listen_fd
);
408 add_close_on_fork_list(sct
->listen_fd
); /* child doesn't need the listener */
409 register_task(&sct
->task
);
412 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
416 static void init_random_seed(void)
419 int fd
, ret
= para_open("/dev/urandom", O_RDONLY
, 0);
424 ret
= read(fd
, &seed
, sizeof(seed
));
426 ret
= -ERRNO_TO_PARA_ERROR(errno
);
429 if (ret
!= sizeof(seed
)) {
430 ret
= -ERRNO_TO_PARA_ERROR(EIO
);
440 PARA_EMERG_LOG("can not seed pseudo random number generator: %s\n",
441 para_strerror(-ret
));
445 static int init_afs(void)
447 int ret
, afs_server_socket
[2];
449 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
452 afs_socket_cookie
= para_random((uint32_t)-1);
453 mmd
->afs_pid
= fork();
454 if (mmd
->afs_pid
< 0)
456 if (!mmd
->afs_pid
) { /* child (afs) */
457 close(afs_server_socket
[0]);
458 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
460 close(afs_server_socket
[1]);
461 ret
= mark_fd_nonblocking(afs_server_socket
[0]);
464 add_close_on_fork_list(afs_server_socket
[0]);
465 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
466 afs_server_socket
[0], (unsigned) afs_socket_cookie
);
467 return afs_server_socket
[0];
470 static void server_init(int argc
, char **argv
)
472 struct server_cmdline_parser_params params
= {
476 .check_ambiguity
= 0,
483 /* parse command line options */
484 server_cmdline_parser_ext(argc
, argv
, &conf
, ¶ms
);
485 HANDLE_VERSION_FLAG("server", conf
);
486 drop_privileges_or_die(conf
.user_arg
, conf
.group_arg
);
487 /* parse config file, open log and set defaults */
488 parse_config_or_die(0);
489 log_welcome("para_server");
490 init_ipc_or_die(); /* init mmd struct and mmd->lock */
491 /* make sure, the global now pointer is uptodate */
492 gettimeofday(now
, NULL
);
493 server_uptime(UPTIME_SET
); /* reset server uptime */
494 init_user_list(user_list_file
);
496 if (conf
.daemon_given
)
498 PARA_NOTICE_LOG("initializing audio format handlers\n");
502 * Although afs uses its own signal handling we must ignore SIGUSR1
503 * _before_ the afs child process gets born by init_afs() below. It's
504 * racy to do this in the child because the parent might send SIGUSR1
505 * before the child gets a chance to ignore this signal -- only the
508 if (signal(SIGUSR1
, SIG_IGN
) == SIG_ERR
) {
509 PARA_EMERG_LOG("failed to ignore SIGUSR1\n");
512 PARA_NOTICE_LOG("initializing the audio file selector\n");
513 afs_socket
= init_afs();
515 PARA_NOTICE_LOG("initializing virtual streaming system\n");
516 init_vss_task(afs_socket
);
517 init_server_command_task(argc
, argv
);
518 PARA_NOTICE_LOG("server init complete\n");
521 static void status_refresh(void)
523 static int prev_uptime
= -1, prev_events
= -1;
524 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
526 if (prev_events
!= mmd
->events
)
528 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
530 if (uptime
/ 60 != prev_uptime
/ 60)
536 prev_uptime
= uptime
;
537 prev_events
= mmd
->events
;
538 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
540 PARA_DEBUG_LOG("%d events, forcing status update\n",
546 static int server_select(int max_fileno
, fd_set
*readfds
, fd_set
*writefds
,
547 struct timeval
*timeout_tv
)
552 mutex_unlock(mmd_mutex
);
553 ret
= para_select(max_fileno
+ 1, readfds
, writefds
, timeout_tv
);
554 mutex_lock(mmd_mutex
);
559 * The main function of para_server.
561 * \param argc Usual argument count.
562 * \param argv Usual argument vector.
564 * \return EXIT_SUCCESS or EXIT_FAILURE.
566 int main(int argc
, char *argv
[])
569 static struct sched s
= {
574 .select_function
= server_select
576 server_init(argc
, argv
);
577 mutex_lock(mmd_mutex
);
580 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));