2 * Copyright (C) 1997-2008 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 are
17 * - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
18 * \ref audioc.c, \ref fsck.c,
19 * - Server: \ref server_command, \ref sender,
20 * - Audio file selector: \ref audio_format_handler, \ref mood, \ref afs_table,
21 * - Client: \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
24 * The gory details, listed by topic:
26 * - Audio format handlers: \ref mp3_afh.c, \ref ogg_afh.c, \ref aac_afh.c,
27 * - Decoders: \ref mp3dec.c, \ref oggdec.c, \ref aacdec.c,
28 * - Volume normalizer: \ref compress.c,
29 * - Output: \ref alsa_write.c, \ref osx_write.c,
30 * - http: \ref http_recv.c, \ref http_send.c,
31 * - ortp: \ref ortp_recv.c, \ref ortp_send.c,
32 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
33 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
34 * - Afs structures: \ref afs_table, \ref audio_file_data,
35 * \ref afs_info \ref afh_info,
36 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
37 * \ref attribute.c, \ref score.c,
38 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
42 * - Scheduling: \ref sched.c, \ref sched.h,
43 * - Networking: \ref net.c,
44 * - File descriptors: \ref fd.c,
45 * - Signals: \ref signal.c,
46 * - Daemons: \ref daemon.c,
47 * - Strings: \ref string.c, \ref string.h,
48 * - Time: \ref time.c,
49 * - Spawning processes: \ref exec.c,
50 * - Inter process communication: \ref ipc.c,
51 * - The object storage layer: \ref osl.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.
56 * Low-level data structures:
58 * - Doubly linked lists: \ref list.h,
59 * - Red-black trees: \ref rbtree.h, \ref rbtree.c,
60 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
61 * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c,
62 * - Crypto: \ref crypt.c.
67 #include <sys/types.h>
72 #include "server.cmdline.h"
79 #include "close_on_fork.h"
88 #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 /** the file containing user information (public key, permissions) */
111 char *user_list_file = NULL;
113 /* global variables for server-internal use */
114 static FILE *logfile;
115 static int mmd_mutex, mmd_shm_id;
116 static int signal_pipe;
119 * para_server's log function
121 * \param ll the log level
122 * \param fmt the format string describing the log message
124 void para_log(int ll, const char* fmt,...)
130 char str[MAXLINE] = "";
133 if (ll < conf.loglevel_arg)
135 outfd = logfile? logfile : stderr;
138 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
139 fprintf(outfd, "%s ", str);
140 if (conf.loglevel_arg <= INFO)
141 fprintf(outfd, "%i: ", ll);
143 if (conf.loglevel_arg <= INFO)
144 fprintf(outfd, "(%d) ", (int)mypid);
146 vfprintf(outfd, fmt, argp);
151 * setup shared memory area and get mutex for locking
153 static void shm_init(void)
156 int ret = shm_new(sizeof(struct misc_meta_data));
161 ret = shm_attach(ret, ATTACH_RW, &shm);
173 mmd->num_commands = 0;
175 mmd->num_connects = 0;
176 mmd->active_connections = 0;
177 mmd->vss_status_flags = VSS_NEXT;
178 mmd->new_vss_status_flags = VSS_NEXT;
181 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
186 * lock the shared memory area containing the mmd struct
188 * \sa semop(2), struct misc_meta_data.
192 mutex_lock(mmd_mutex);
196 * unlock the shared memory area containing the mmd struct
198 * \sa semop(2), struct misc_meta_data
201 void mmd_unlock(void)
203 mutex_unlock(mmd_mutex);
206 static void parse_config(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 = stat(cf, &statbuf);
223 if (ret && conf.config_file_given) {
225 PARA_EMERG_LOG("can not stat 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,
237 server_cmdline_parser_config_file(cf, &conf, ¶ms);
238 conf.daemon_given = tmp;
240 if (conf.logfile_given)
241 logfile = open_log(conf.logfile_arg);
248 free(user_list_file);
249 user_list_file = NULL;
253 static void setup_signal_handling(void)
255 signal_pipe = para_signal_init(); /* always successful */
257 PARA_NOTICE_LOG("setting up signal handlers\n");
258 if (para_install_sighandler(SIGINT) < 0)
260 if (para_install_sighandler(SIGTERM) < 0)
262 if (para_install_sighandler(SIGHUP) < 0)
264 if (para_install_sighandler(SIGCHLD) < 0)
266 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
268 if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
270 add_close_on_fork_list(signal_pipe);
273 PARA_EMERG_LOG("could not install signal handlers\n");
277 static unsigned init_network(void)
279 int fd, ret = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.port_arg);
284 ret = mark_fd_nonblocking(fd);
287 add_close_on_fork_list(fd); /* child doesn't need the listener */
290 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
294 static void init_random_seed(void)
297 int fd, ret = para_open("/dev/urandom", O_RDONLY, 0);
302 ret = read(fd, &seed, sizeof(seed));
304 ret = -ERRNO_TO_PARA_ERROR(errno);
307 if (ret != sizeof(seed)) {
308 ret = -ERRNO_TO_PARA_ERROR(EIO);
318 PARA_EMERG_LOG("can not seed pseudo random number generator: %s\n",
319 para_strerror(-ret));
323 uint32_t afs_socket_cookie;
325 static pid_t afs_pid;
327 static void init_afs(void)
329 int ret, afs_server_socket[2];
331 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
334 afs_socket_cookie = para_random((uint32_t)-1);
338 if (!afs_pid) { /* child (afs) */
339 close(afs_server_socket[0]);
340 afs_init(afs_socket_cookie, afs_server_socket[1]);
342 close(afs_server_socket[1]);
343 afs_socket = afs_server_socket[0];
344 ret = mark_fd_nonblocking(afs_socket);
347 add_close_on_fork_list(afs_socket);
348 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket,
349 (unsigned) afs_socket_cookie);
352 static unsigned server_init(int argc, char **argv)
354 /* connector's address information */
356 struct server_cmdline_parser_params params = {
360 .check_ambiguity = 0,
364 /* parse command line options */
365 server_cmdline_parser_ext(argc, argv, &conf, ¶ms);
366 HANDLE_VERSION_FLAG("server", conf);
367 para_drop_privileges(conf.user_arg, conf.group_arg);
368 /* parse config file, open log and set defaults */
370 log_welcome("para_server", conf.loglevel_arg);
371 shm_init(); /* init mmd struct */
372 server_uptime(UPTIME_SET); /* reset server uptime */
373 init_user_list(user_list_file);
375 if (conf.daemon_given)
377 PARA_NOTICE_LOG("initializing audio format handlers\n");
379 PARA_NOTICE_LOG("initializing virtual streaming system\n");
380 mmd->server_pid = getpid();
381 setup_signal_handling();
382 PARA_NOTICE_LOG("initializing the audio file selector\n");
386 /* init network socket */
387 PARA_NOTICE_LOG("initializing tcp command socket\n");
388 sockfd = init_network();
389 PARA_NOTICE_LOG("server init complete\n");
394 * called when server gets SIGHUP or when client invokes hup command.
396 static void handle_sighup(void)
398 PARA_NOTICE_LOG("SIGHUP\n");
399 close_log(logfile); /* gets reopened if necessary by parse_config */
401 parse_config(1); /* reopens log */
402 init_user_list(user_list_file); /* reload user list */
404 kill(afs_pid, SIGHUP);
407 static void status_refresh(void)
409 static int prev_uptime = -1, prev_events = -1;
410 int uptime = server_uptime(UPTIME_GET), ret = 1;
412 if (prev_events != mmd->events)
414 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
416 if (uptime / 60 != prev_uptime / 60)
420 prev_uptime = uptime;
421 prev_events = mmd->events;
422 mmd->vss_status_flags = mmd->new_vss_status_flags;
424 PARA_DEBUG_LOG("%d events, forcing status update\n",
431 * the main function of para_server
433 * \param argc usual argument count
434 * \param argv usual argument vector
436 * \return EXIT_SUCCESS or EXIT_FAILURE
439 int main(int argc, char *argv[])
441 /* listen on sock_fd, new connection on new_fd */
444 int i, max_fileno, ret;
447 struct timeval *timeout;
450 sockfd = server_init(argc, argv);
455 /* check socket and signal pipe in any case */
456 para_fd_set(sockfd, &rfds, &max_fileno);
457 para_fd_set(signal_pipe, &rfds, &max_fileno);
458 timeout = vss_preselect(&rfds, &wfds, &max_fileno);
461 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
465 vss_post_select(&rfds, &wfds);
467 if (FD_ISSET(signal_pipe, &rfds)) {
470 sig = para_next_signal();
477 ret = para_reap_child(&pid);
482 PARA_EMERG_LOG("fatal: afs died\n");
486 /* die on sigint/sigterm. Kill all children too. */
489 PARA_EMERG_LOG("terminating on signal %d\n", sig);
492 mutex_destroy(mmd_mutex);
494 shm_destroy(mmd_shm_id);
499 if (!FD_ISSET(sockfd, &rfds))
502 new_fd = para_accept(sockfd, NULL, 0);
505 peer_name = remote_name(new_fd);
506 PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
508 mmd->active_connections++;
512 PARA_CRIT_LOG("fork failed\n");
517 /* parent keeps accepting connections */
520 alarm(ALARM_TIMEOUT);
522 para_signal_shutdown();
524 * put info on who we are serving into argv[0] to make
525 * client ip visible in top/ps
527 for (i = argc - 1; i >= 0; i--)
528 memset(argv[i], 0, strlen(argv[i]));
529 sprintf(argv[0], "para_server (serving %s)", peer_name);
530 return handle_connect(new_fd, peer_name);