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 * Good starting points for reading are probably \ref audio_file_selector,
13 * \ref sender, \ref receiver, \ref receiver_node, \ref filter, \ref
20 #include "server.cmdline.h"
21 #include "afs_common.h"
26 #include "close_on_fork.h"
37 #include "user_list.h"
40 /** define the array of error lists needed by para_server */
43 /** shut down non-authorized connections after that many seconds */
44 #define ALARM_TIMEOUT 10
47 * pointer to shared memory area for communication between para_server
48 * and its children. exported to vss.c. command.c and to all selectors.
50 struct misc_meta_data *mmd;
53 * the configuration of para_server
55 * It also contains the options for all audio file selectors, audio format handler
56 * and all supported senders.
58 struct server_args_info conf;
60 /** the file containing user information (public key, permissions) */
61 char *user_list_file = NULL;
63 extern void dccp_send_init(struct sender *);
64 extern void http_send_init(struct sender *);
65 extern void ortp_send_init(struct sender *);
67 /* TODO: This is better handled by autoconf */
68 /** the list of supported audio file selectors */
69 struct audio_file_selector selectors[] = {
72 .init = random_selector_init,
73 .update_audio_file = NULL,
77 .init = playlist_selector_init,
78 .update_audio_file = NULL,
85 .init = mysql_selector_init,
86 .update_audio_file = NULL,
96 /** the list of supported senders */
97 struct sender senders[] = {
100 .init = http_send_init,
104 .init = dccp_send_init,
109 .init = ortp_send_init,
118 /* global variables for server-internal use */
119 static FILE *logfile;
120 static int mmd_mutex, mmd_shm_id;
121 static int signal_pipe;
124 * para_server's log function
126 * \param ll the log level
127 * \param fmt the format string describing the log message
129 void para_log(int ll, const char* fmt,...)
135 char str[MAXLINE] = "";
138 if (ll < conf.loglevel_arg)
140 outfd = logfile? logfile : stderr;
143 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
144 fprintf(outfd, "%s ", str);
145 if (conf.loglevel_arg <= INFO)
146 fprintf(outfd, "%i: ", ll);
148 if (conf.loglevel_arg <= INFO)
149 fprintf(outfd, "(%d) ", mypid);
151 vfprintf(outfd, fmt, argp);
156 * setup shared memory area and get mutex for locking
158 static void shm_init(void)
161 int ret = shm_new(sizeof(struct misc_meta_data));
166 ret = shm_attach(ret, ATTACH_RW, &shm);
177 mmd->selector_num = 0;
179 mmd->num_commands = 0;
181 mmd->num_connects = 0;
182 mmd->active_connections = 0;
183 strcpy(mmd->filename, "(none)");
184 mmd->audio_format = -1;
185 mmd->vss_status_flags = VSS_NEXT;
186 mmd->new_vss_status_flags = VSS_NEXT;
187 mmd->sender_cmd_data.cmd_num = -1;
190 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
195 * lock the shared memory area containing the mmd struct
197 * \sa semop(2), struct misc_meta_data
201 mutex_lock(mmd_mutex);
205 * unlock the shared memory area containing the mmd struct
207 * \sa semop(2), struct misc_meta_data
210 void mmd_unlock(void)
212 mutex_unlock(mmd_mutex);
215 static void parse_config(int override)
217 char *home = para_homedir();
222 if (conf.config_file_given)
223 cf = conf.config_file_arg;
225 cf = make_message("%s/.paraslash/server.conf", home);
226 free(user_list_file);
227 if (!conf.user_list_given)
228 user_list_file = make_message("%s/.paraslash/server.users", home);
230 user_list_file = para_strdup(conf.user_list_arg);
231 ret = stat(cf, &statbuf);
232 if (ret && conf.config_file_given) {
234 PARA_EMERG_LOG("can not stat config file %s\n", cf);
238 int tmp = conf.daemon_given;
239 struct server_cmdline_parser_params params = {
240 .override = override,
245 server_cmdline_parser_config_file(cf, &conf, ¶ms);
246 conf.daemon_given = tmp;
249 if (!conf.logfile_given && conf.daemon_given) {
251 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
254 if (conf.logfile_given)
255 logfile = open_log(conf.logfile_arg);
262 free(user_list_file);
263 user_list_file = NULL;
267 static void setup_signal_handling(void)
271 signal_pipe = para_signal_init();
272 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
273 ret += para_install_sighandler(SIGINT);
274 ret += para_install_sighandler(SIGTERM);
275 ret += para_install_sighandler(SIGHUP);
276 ret += para_install_sighandler(SIGCHLD);
277 ret += para_install_sighandler(SIGUSR1);
278 signal(SIGPIPE, SIG_IGN);
280 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
285 static void init_selector(void)
289 mmd->selector_change = -1; /* no change nec., set to new num by com_chs */
290 if (!conf.selector_given)
292 for (i = 0; selectors[i].name; i++) {
293 if (strcmp(selectors[i].name, conf.selector_arg))
295 PARA_NOTICE_LOG("initializing %s audio file selector\n",
297 ret = selectors[i].init(&selectors[i]);
299 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
302 mmd->selector_num = i;
305 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
307 mmd->selector_num = 0;
308 selectors[0].init(&selectors[0]); /* always successful */
311 static unsigned init_network(void)
313 int fd, ret = init_tcp_socket(conf.port_arg);
318 ret = mark_fd_nonblock(fd);
323 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
327 static void init_random_seed(void)
331 size_t len = sizeof(unsigned int);
333 fd = open("/dev/urandom", O_RDONLY);
337 if (read(fd, &seed, len) != len)
346 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
351 uint32_t afs_socket_cookie;
352 static int afs_socket;
355 static void init_afs(void)
357 int ret, afs_server_socket[2];
359 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
362 afs_socket_cookie = para_random((uint32_t)-1);
366 if (!afs_pid) /* child (afs) */
367 afs_init(afs_socket_cookie, afs_server_socket[1]);
368 close(afs_server_socket[1]);
369 afs_socket = afs_server_socket[0];
370 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket,
371 (unsigned) afs_socket_cookie);
375 static unsigned do_inits(int argc, char **argv)
377 /* connector's address information */
381 /* parse command line options */
382 server_cmdline_parser(argc, argv, &conf);
383 HANDLE_VERSION_FLAG("server", conf);
384 para_drop_privileges(conf.user_arg, conf.group_arg);
385 /* parse config file, open log and set defaults */
387 log_welcome("para_server", conf.loglevel_arg);
388 shm_init(); /* init mmd struct */
389 server_uptime(UPTIME_SET); /* reset server uptime */
390 init_user_list(user_list_file);
392 if (conf.daemon_given)
395 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
396 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
398 mmd->server_pid = getpid();
399 setup_signal_handling();
402 /* init network socket */
403 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
404 sockfd = init_network();
405 PARA_NOTICE_LOG("%s", "init complete\n");
409 static void change_selector(void)
411 int ret, old = mmd->selector_num, new = mmd->selector_change;
413 selectors[old].shutdown();
414 ret = selectors[new].init(&selectors[new]);
415 mmd->selector_change = -1; /* reset */
417 mmd->selector_num = new;
421 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret));
422 selectors[0].init(&selectors[0]);
423 mmd->selector_num = 0;
427 * called when server gets SIGHUP or when client invokes hup command.
429 static void handle_sighup(void)
431 PARA_NOTICE_LOG("%s", "SIGHUP\n");
432 close_log(logfile); /* gets reopened if necessary by parse_config */
434 parse_config(1); /* reopens log */
435 mmd->selector_change = mmd->selector_num; /* do not change selector.. */
436 change_selector(); /* .. just reload */
437 init_user_list(user_list_file); /* reload user list */
440 static void status_refresh(void)
442 static int prev_uptime = -1, prev_events = -1;
443 int uptime = server_uptime(UPTIME_GET), ret = 1;
445 if (prev_events != mmd->events)
447 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
449 if (uptime / 60 != prev_uptime / 60)
453 prev_uptime = uptime;
454 prev_events = mmd->events;
455 mmd->vss_status_flags = mmd->new_vss_status_flags;
457 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
458 mmd->events, mmd->audio_format);
464 * the main function of para_server
466 * \param argc usual argument count
467 * \param argv usual argument vector
469 * \return EXIT_SUCCESS or EXIT_FAILURE
472 int main(int argc, char *argv[])
474 /* listen on sock_fd, new connection on new_fd */
476 struct sockaddr_in their_addr;
477 int i, max_fileno, ret;
480 struct timeval *timeout;
483 sockfd = do_inits(argc, argv);
488 /* check socket and signal pipe in any case */
489 para_fd_set(sockfd, &rfds, &max_fileno);
490 para_fd_set(signal_pipe, &rfds, &max_fileno);
491 timeout = vss_preselect();
493 for (i = 0; senders[i].name; i++) {
494 if (senders[i].status != SENDER_ON)
496 if (!senders[i].pre_select)
498 senders[i].pre_select( &max_fileno, &rfds, &wfds);
500 if (selectors[mmd->selector_num].pre_select) {
501 ret = selectors[mmd->selector_num].pre_select(&rfds, &wfds);
502 max_fileno = PARA_MAX(max_fileno, ret);
505 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
507 if (mmd->selector_change >= 0)
509 if (selectors[mmd->selector_num].post_select)
510 selectors[mmd->selector_num].post_select(&rfds, &wfds);
513 for (i = 0; senders[i].name; i++) {
514 if (senders[i].status != SENDER_ON)
516 if (!senders[i].post_select)
518 senders[i].post_select(&rfds, &wfds);
522 if (FD_ISSET(signal_pipe, &rfds)) {
525 sig = para_next_signal();
532 pid = para_reap_child();
537 PARA_EMERG_LOG("fatal: afs died\n");
541 /* die on sigint/sigterm. Kill all children too. */
544 PARA_EMERG_LOG("terminating on signal %d\n", sig);
547 selectors[mmd->selector_num].shutdown();
548 mutex_destroy(mmd_mutex);
550 shm_destroy(mmd_shm_id);
555 if (mmd->sender_cmd_data.cmd_num >= 0) {
556 int num = mmd->sender_cmd_data.cmd_num,
557 s = mmd->sender_cmd_data.sender_num;
559 if (senders[s].client_cmds[num])
560 senders[s].client_cmds[num](&mmd->sender_cmd_data);
561 mmd->sender_cmd_data.cmd_num = -1;
563 if (!FD_ISSET(sockfd, &rfds))
566 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
569 PARA_INFO_LOG("got connection from %s, forking\n",
570 inet_ntoa(their_addr.sin_addr));
572 mmd->active_connections++;
576 PARA_CRIT_LOG("%s", "fork failed\n");
581 /* parent keeps accepting connections */
584 alarm(ALARM_TIMEOUT);
586 close(sockfd); /* child doesn't need the listener */
588 * put info on who we are serving into argv[0] to make
589 * client ip visible in top/ps
591 for (i = argc - 1; i >= 0; i--)
592 memset(argv[i], 0, strlen(argv[i]));
593 sprintf(argv[0], "para_server (serving %s)",
594 inet_ntoa(their_addr.sin_addr));
595 return handle_connect(new_fd, &their_addr);