2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file server.c Paraslash's main server. */
10 /** \mainpage Paraslash API Reference
12 * Starting points for getting an overview are
16 * - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
17 * \ref audioc.c, \ref fsck.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 mp3_afh.c, \ref ogg_afh.c \ref aac_afh.c
26 * - Decoders: \ref mp3dec.c, \ref \ref oggdec.c, \ref aacdec.c
27 * - Volume normalizer: \ref compress.c
28 * - Output: \ref alsa_write.c, \ref osx_write.c
29 * - http: \ref http_recv.c, \ref http_send.c
30 * - ortp: \ref ortp_recv.c, \ref ortp_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 audio_format_info,
35 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
36 * \ref attribute.c, \ref score.c.
40 * - Scheduling: \ref sched.c, \ref sched.h,
41 * - Networking: \ref net.c,
42 * - File descriptors: \ref fd.c,
43 * - Signals: \ref signal.c,
44 * - Daemons: \ref daemon.c,
45 * - Strings: \ref string.c, \ref string.h,
46 * - Time: \ref time.c,
47 * - Spawning processes: \ref exec.c
48 * - Inter process communication: \ref ipc.c,
49 * - The object storage layer: \ref osl.c,
50 * - Blob tables: \ref blob.c,
51 * - Queueing chunks of audio data: \ref chunk_queue.c,
52 * - The error subssystem: \ref error.h.
54 * Lov-level data structures:
56 * - Doubly linked lists: \ref list.h,
57 * - Red-black trees: \ref rbtree.h, \ref rbtree.c,
58 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
59 * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c,
60 * - Crypto: \ref crypt.c.
65 #include <sys/types.h>
70 #include "server.cmdline.h"
77 #include "close_on_fork.h"
86 #include "user_list.h"
88 /** define the array of error lists needed by para_server */
91 /** shut down non-authorized connections after that many seconds */
92 #define ALARM_TIMEOUT 10
95 * Pointer to shared memory area for communication between para_server
96 * and its children. Exported to vss.c. command.c and to afs.
98 struct misc_meta_data *mmd;
101 * the configuration of para_server
103 * It also contains the options for the audio file selector, audio format
104 * handler and all supported senders.
106 struct server_args_info conf;
108 /** the file containing user information (public key, permissions) */
109 char *user_list_file = NULL;
111 extern void dccp_send_init(struct sender *);
112 extern void http_send_init(struct sender *);
113 extern void ortp_send_init(struct sender *);
115 /** the list of supported senders */
116 struct sender senders[] = {
119 .init = http_send_init,
123 .init = dccp_send_init,
128 .init = ortp_send_init,
137 /* global variables for server-internal use */
138 static FILE *logfile;
139 static int mmd_mutex, mmd_shm_id;
140 static int signal_pipe;
143 * para_server's log function
145 * \param ll the log level
146 * \param fmt the format string describing the log message
148 void para_log(int ll, const char* fmt,...)
154 char str[MAXLINE] = "";
157 if (ll < conf.loglevel_arg)
159 outfd = logfile? logfile : stderr;
162 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
163 fprintf(outfd, "%s ", str);
164 if (conf.loglevel_arg <= INFO)
165 fprintf(outfd, "%i: ", ll);
167 if (conf.loglevel_arg <= INFO)
168 fprintf(outfd, "(%d) ", mypid);
170 vfprintf(outfd, fmt, argp);
175 * setup shared memory area and get mutex for locking
177 static void shm_init(void)
180 int ret = shm_new(sizeof(struct misc_meta_data));
185 ret = shm_attach(ret, ATTACH_RW, &shm);
197 mmd->num_commands = 0;
199 mmd->num_connects = 0;
200 mmd->active_connections = 0;
201 strcpy(mmd->afd.path, "(none)");
202 mmd->vss_status_flags = VSS_NEXT;
203 mmd->new_vss_status_flags = VSS_NEXT;
204 mmd->sender_cmd_data.cmd_num = -1;
207 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
212 * lock the shared memory area containing the mmd struct
214 * \sa semop(2), struct misc_meta_data.
218 mutex_lock(mmd_mutex);
222 * unlock the shared memory area containing the mmd struct
224 * \sa semop(2), struct misc_meta_data
227 void mmd_unlock(void)
229 mutex_unlock(mmd_mutex);
232 static void parse_config(int override)
234 char *home = para_homedir();
239 if (conf.config_file_given)
240 cf = para_strdup(conf.config_file_arg);
242 cf = make_message("%s/.paraslash/server.conf", home);
243 free(user_list_file);
244 if (!conf.user_list_given)
245 user_list_file = make_message("%s/.paraslash/server.users", home);
247 user_list_file = para_strdup(conf.user_list_arg);
248 ret = stat(cf, &statbuf);
249 if (ret && conf.config_file_given) {
251 PARA_EMERG_LOG("can not stat config file %s\n", cf);
255 int tmp = conf.daemon_given;
256 struct server_cmdline_parser_params params = {
257 .override = override,
262 server_cmdline_parser_config_file(cf, &conf, ¶ms);
263 conf.daemon_given = tmp;
266 if (!conf.logfile_given && conf.daemon_given) {
268 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
271 if (conf.logfile_given)
272 logfile = open_log(conf.logfile_arg);
279 free(user_list_file);
280 user_list_file = NULL;
284 static void setup_signal_handling(void)
288 signal_pipe = para_signal_init();
289 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
290 ret += para_install_sighandler(SIGINT);
291 ret += para_install_sighandler(SIGTERM);
292 ret += para_install_sighandler(SIGHUP);
293 ret += para_install_sighandler(SIGCHLD);
294 ret += para_install_sighandler(SIGUSR1);
295 signal(SIGPIPE, SIG_IGN);
297 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
302 static unsigned init_network(void)
304 int fd, ret = init_tcp_socket(conf.port_arg);
309 ret = mark_fd_nonblock(fd);
314 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
318 static void init_random_seed(void)
322 size_t len = sizeof(unsigned int);
324 fd = open("/dev/urandom", O_RDONLY);
328 if (read(fd, &seed, len) != len)
337 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
342 uint32_t afs_socket_cookie;
344 static pid_t afs_pid;
346 static void init_afs(void)
348 int ret, afs_server_socket[2];
350 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
353 afs_socket_cookie = para_random((uint32_t)-1);
357 if (!afs_pid) { /* child (afs) */
358 close(afs_server_socket[0]);
359 afs_init(afs_socket_cookie, afs_server_socket[1]);
361 close(afs_server_socket[1]);
362 afs_socket = afs_server_socket[0];
363 ret = mark_fd_nonblock(afs_socket);
366 add_close_on_fork_list(afs_socket);
367 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket,
368 (unsigned) afs_socket_cookie);
372 static unsigned do_inits(int argc, char **argv)
374 /* connector's address information */
378 /* parse command line options */
379 server_cmdline_parser(argc, argv, &conf);
380 HANDLE_VERSION_FLAG("server", conf);
381 para_drop_privileges(conf.user_arg, conf.group_arg);
382 /* parse config file, open log and set defaults */
384 log_welcome("para_server", conf.loglevel_arg);
385 shm_init(); /* init mmd struct */
386 server_uptime(UPTIME_SET); /* reset server uptime */
387 init_user_list(user_list_file);
389 if (conf.daemon_given)
391 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
394 mmd->server_pid = getpid();
395 setup_signal_handling();
398 /* init network socket */
399 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
400 sockfd = init_network();
401 PARA_NOTICE_LOG("%s", "init complete\n");
406 * called when server gets SIGHUP or when client invokes hup command.
408 static void handle_sighup(void)
410 PARA_NOTICE_LOG("%s", "SIGHUP\n");
411 close_log(logfile); /* gets reopened if necessary by parse_config */
413 parse_config(1); /* reopens log */
414 init_user_list(user_list_file); /* reload user list */
416 kill(afs_pid, SIGHUP);
419 static void status_refresh(void)
421 static int prev_uptime = -1, prev_events = -1;
422 int uptime = server_uptime(UPTIME_GET), ret = 1;
424 if (prev_events != mmd->events)
426 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
428 if (uptime / 60 != prev_uptime / 60)
432 prev_uptime = uptime;
433 prev_events = mmd->events;
434 mmd->vss_status_flags = mmd->new_vss_status_flags;
436 PARA_DEBUG_LOG("%d events, forcing status update\n",
443 * the main function of para_server
445 * \param argc usual argument count
446 * \param argv usual argument vector
448 * \return EXIT_SUCCESS or EXIT_FAILURE
451 int main(int argc, char *argv[])
453 /* listen on sock_fd, new connection on new_fd */
455 struct sockaddr_in their_addr;
456 int i, max_fileno, ret;
459 struct timeval *timeout;
462 sockfd = do_inits(argc, argv);
467 /* check socket and signal pipe in any case */
468 para_fd_set(sockfd, &rfds, &max_fileno);
469 para_fd_set(signal_pipe, &rfds, &max_fileno);
470 timeout = vss_preselect(&rfds, &wfds, &max_fileno);
472 for (i = 0; senders[i].name; i++) {
473 if (senders[i].status != SENDER_ON)
475 if (!senders[i].pre_select)
477 senders[i].pre_select(&max_fileno, &rfds, &wfds);
480 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
482 vss_post_select(&rfds, &wfds);
485 for (i = 0; senders[i].name; i++) {
486 if (senders[i].status != SENDER_ON)
488 if (!senders[i].post_select)
490 senders[i].post_select(&rfds, &wfds);
494 if (FD_ISSET(signal_pipe, &rfds)) {
497 sig = para_next_signal();
504 ret = para_reap_child(&pid);
509 PARA_EMERG_LOG("fatal: afs died\n");
513 /* die on sigint/sigterm. Kill all children too. */
516 PARA_EMERG_LOG("terminating on signal %d\n", sig);
519 mutex_destroy(mmd_mutex);
521 shm_destroy(mmd_shm_id);
526 if (mmd->sender_cmd_data.cmd_num >= 0) {
527 int num = mmd->sender_cmd_data.cmd_num,
528 s = mmd->sender_cmd_data.sender_num;
530 if (senders[s].client_cmds[num])
531 senders[s].client_cmds[num](&mmd->sender_cmd_data);
532 mmd->sender_cmd_data.cmd_num = -1;
534 if (!FD_ISSET(sockfd, &rfds))
537 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
540 PARA_INFO_LOG("got connection from %s, forking\n",
541 inet_ntoa(their_addr.sin_addr));
543 mmd->active_connections++;
547 PARA_CRIT_LOG("%s", "fork failed\n");
552 /* parent keeps accepting connections */
555 alarm(ALARM_TIMEOUT);
557 close(sockfd); /* child doesn't need the listener */
559 * put info on who we are serving into argv[0] to make
560 * client ip visible in top/ps
562 for (i = argc - 1; i >= 0; i--)
563 memset(argv[i], 0, strlen(argv[i]));
564 sprintf(argv[0], "para_server (serving %s)",
565 inet_ntoa(their_addr.sin_addr));
566 return handle_connect(new_fd, &their_addr);