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"
89 /** Define the array of error lists needed by para_server. */
92 /** Shut down non-authorized connections after that many seconds. */
93 #define ALARM_TIMEOUT 10
96 * Pointer to shared memory area for communication between para_server
97 * and its children. Exported to vss.c. command.c and to afs.
99 struct misc_meta_data *mmd;
102 * The configuration of para_server
104 * It also contains the options for the audio file selector, audio format
105 * handler and all supported senders.
107 struct server_args_info conf;
109 /** A random value used in child context for authentication. */
110 uint32_t afs_socket_cookie;
112 /** The mutex protecting the shared memory area containing the mmd struct. */
115 /* global variables for server-internal use */
116 static FILE *logfile;
117 /** The file containing user information (public key, permissions). */
118 static char *user_list_file = NULL;
119 static int mmd_shm_id;
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. */
135 * Para_server's log function.
137 * \param ll The log level.
138 * \param fmt The format string describing the log message.
140 __printf_2_3 void para_log(int ll, const char* fmt,...)
146 char str[MAXLINE] = "";
149 if (ll < conf.loglevel_arg)
151 outfd = logfile? logfile : stderr;
154 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
155 fprintf(outfd, "%s ", str);
156 if (conf.loglevel_arg <= INFO)
157 fprintf(outfd, "%i: ", ll);
159 if (conf.loglevel_arg <= INFO)
160 fprintf(outfd, "(%d) ", (int)mypid);
162 vfprintf(outfd, fmt, argp);
167 * setup shared memory area and get mutex for locking
169 static void init_ipc_or_die(void)
172 int ret = shm_new(sizeof(struct misc_meta_data));
177 ret = shm_attach(ret, ATTACH_RW, &shm);
189 mmd->num_commands = 0;
191 mmd->num_connects = 0;
192 mmd->active_connections = 0;
193 mmd->vss_status_flags = VSS_NEXT;
194 mmd->new_vss_status_flags = VSS_NEXT;
197 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
201 void parse_config_or_die(int override)
203 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 = stat(cf, &statbuf);
220 if (ret && conf.config_file_given) {
222 PARA_EMERG_LOG("can not stat 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,
234 server_cmdline_parser_config_file(cf, &conf, ¶ms);
235 conf.daemon_given = tmp;
237 if (conf.logfile_given)
238 logfile = open_log(conf.logfile_arg);
245 free(user_list_file);
246 user_list_file = NULL;
250 static void signal_pre_select(struct sched *s, struct task *t)
252 struct signal_task *st = container_of(t, struct signal_task, task);
253 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
257 * called when server gets SIGHUP or when client invokes hup command.
259 static void handle_sighup(void)
261 PARA_NOTICE_LOG("SIGHUP\n");
262 parse_config_or_die(1); /* reopens log */
263 init_user_list(user_list_file); /* reload user list */
265 kill(mmd->afs_pid, SIGHUP);
268 static void signal_post_select(struct sched *s, struct task *t)
270 struct signal_task *st = container_of(t, struct signal_task, task);
272 if (!FD_ISSET(st->fd, &s->rfds))
275 st->signum = para_next_signal();
276 switch (st->signum) {
283 int ret = para_reap_child(&pid);
286 if (pid != mmd->afs_pid)
288 PARA_EMERG_LOG("fatal: afs died\n");
292 /* die on sigint/sigterm. Kill all children too. */
295 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
298 mutex_destroy(mmd_mutex);
300 shm_destroy(mmd_shm_id);
306 static void init_signal_task(void)
308 static struct signal_task signal_task_struct,
309 *st = &signal_task_struct;
311 st->task.pre_select = signal_pre_select;
312 st->task.post_select = signal_post_select;
313 sprintf(st->task.status, "signal task");
315 st->fd = para_signal_init(); /* always successful */
317 PARA_NOTICE_LOG("setting up signal handlers\n");
318 if (para_install_sighandler(SIGINT) < 0)
320 if (para_install_sighandler(SIGTERM) < 0)
322 if (para_install_sighandler(SIGHUP) < 0)
324 if (para_install_sighandler(SIGCHLD) < 0)
326 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
328 if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
330 add_close_on_fork_list(st->fd);
331 register_task(&st->task);
334 PARA_EMERG_LOG("could not install signal handlers\n");
338 static void command_pre_select(struct sched *s, struct task *t)
340 struct server_command_task *sct = container_of(t, struct server_command_task, task);
341 para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
344 static void command_post_select(struct sched *s, struct task *t)
346 struct server_command_task *sct = container_of(t, struct server_command_task, task);
352 if (!FD_ISSET(sct->listen_fd, &s->rfds))
354 ret = para_accept(sct->listen_fd, NULL, 0);
358 peer_name = remote_name(new_fd);
359 PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
361 mmd->active_connections++;
365 ret = -ERRNO_TO_PARA_ERROR(errno);
370 /* parent keeps accepting connections */
373 alarm(ALARM_TIMEOUT);
375 para_signal_shutdown();
377 * put info on who we are serving into argv[0] to make
378 * client ip visible in top/ps
380 for (i = sct->argc - 1; i >= 0; i--)
381 memset(sct->argv[i], 0, strlen(sct->argv[i]));
382 sprintf(sct->argv[0], "para_server (serving %s)", peer_name);
383 return handle_connect(new_fd, peer_name);
386 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
389 static void init_server_command_task(int argc, char **argv)
392 static struct server_command_task server_command_task_struct,
393 *sct = &server_command_task_struct;
395 PARA_NOTICE_LOG("initializing tcp command socket\n");
396 sct->task.pre_select = command_pre_select;
397 sct->task.post_select = command_post_select;
400 ret = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.port_arg);
403 sct->listen_fd = ret;
404 ret = mark_fd_nonblocking(sct->listen_fd);
407 add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */
408 register_task(&sct->task);
411 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
415 static void init_random_seed(void)
418 int fd, ret = para_open("/dev/urandom", O_RDONLY, 0);
423 ret = read(fd, &seed, sizeof(seed));
425 ret = -ERRNO_TO_PARA_ERROR(errno);
428 if (ret != sizeof(seed)) {
429 ret = -ERRNO_TO_PARA_ERROR(EIO);
439 PARA_EMERG_LOG("can not seed pseudo random number generator: %s\n",
440 para_strerror(-ret));
444 static int init_afs(void)
446 int ret, afs_server_socket[2];
448 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
451 afs_socket_cookie = para_random((uint32_t)-1);
452 mmd->afs_pid = fork();
453 if (mmd->afs_pid < 0)
455 if (!mmd->afs_pid) { /* child (afs) */
456 close(afs_server_socket[0]);
457 afs_init(afs_socket_cookie, afs_server_socket[1]);
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 static void server_init(int argc, char **argv)
471 struct server_cmdline_parser_params params = {
475 .check_ambiguity = 0,
482 /* parse command line options */
483 server_cmdline_parser_ext(argc, argv, &conf, ¶ms);
484 HANDLE_VERSION_FLAG("server", conf);
485 drop_privileges_or_die(conf.user_arg, conf.group_arg);
486 /* parse config file, open log and set defaults */
487 parse_config_or_die(0);
488 log_welcome("para_server", conf.loglevel_arg);
489 init_ipc_or_die(); /* init mmd struct and mmd->lock */
490 /* make sure, the global now pointer is uptodate */
491 gettimeofday(now, NULL);
492 server_uptime(UPTIME_SET); /* reset server uptime */
493 init_user_list(user_list_file);
495 if (conf.daemon_given)
497 PARA_NOTICE_LOG("initializing audio format handlers\n");
499 PARA_NOTICE_LOG("initializing the audio file selector\n");
500 afs_socket = init_afs();
502 PARA_NOTICE_LOG("initializing virtual streaming system\n");
503 init_vss_task(afs_socket);
504 init_server_command_task(argc, argv);
505 PARA_NOTICE_LOG("server init complete\n");
508 static void status_refresh(void)
510 static int prev_uptime = -1, prev_events = -1;
511 int uptime = server_uptime(UPTIME_GET), ret = 1;
513 if (prev_events != mmd->events)
515 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
517 if (uptime / 60 != prev_uptime / 60)
523 prev_uptime = uptime;
524 prev_events = mmd->events;
525 mmd->vss_status_flags = mmd->new_vss_status_flags;
527 PARA_DEBUG_LOG("%d events, forcing status update\n",
533 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
534 struct timeval *timeout_tv)
539 mutex_unlock(mmd_mutex);
540 ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
541 mutex_lock(mmd_mutex);
546 * The main function of para_server.
548 * \param argc Usual argument count.
549 * \param argv Usual argument vector.
551 * \return EXIT_SUCCESS or EXIT_FAILURE.
553 int main(int argc, char *argv[])
556 static struct sched s = {
561 .select_function = server_select
563 server_init(argc, argv);
564 mutex_lock(mmd_mutex);
567 PARA_EMERG_LOG("%s\n", para_strerror(-ret));