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"
25 #include "close_on_fork.h"
34 #include "user_list.h"
36 /** define the array of error lists needed by para_server */
39 /** shut down non-authorized connections after that many seconds */
40 #define ALARM_TIMEOUT 10
43 * pointer to shared memory area for communication between para_server
44 * and its children. exported to vss.c. command.c and to all selectors.
46 struct misc_meta_data *mmd;
49 * the configuration of para_server
51 * It also contains the options for all audio file selectors, audio format handler
52 * and all supported senders.
54 struct server_args_info conf;
56 /** the file containing user information (public key, permissions) */
57 char *user_list_file = NULL;
59 extern void dccp_send_init(struct sender *);
60 extern void http_send_init(struct sender *);
61 extern void ortp_send_init(struct sender *);
63 /* TODO: This is better handled by autoconf */
64 /** the list of supported audio file selectors */
65 struct audio_file_selector selectors[] = {
68 .init = random_selector_init,
69 .update_audio_file = NULL,
73 .init = playlist_selector_init,
74 .update_audio_file = NULL,
81 .init = mysql_selector_init,
82 .update_audio_file = NULL,
92 /** the list of supported senders */
93 struct sender senders[] = {
96 .init = http_send_init,
100 .init = dccp_send_init,
105 .init = ortp_send_init,
114 /* global variables for server-internal use */
115 static FILE *logfile;
116 static int mmd_mutex, mmd_shm_id;
117 static int signal_pipe;
120 * para_server's log function
122 * \param ll the log level
123 * \param fmt the format string describing the log message
125 void para_log(int ll, const char* fmt,...)
131 char str[MAXLINE] = "";
134 if (ll < conf.loglevel_arg)
136 outfd = logfile? logfile : stderr;
139 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
140 fprintf(outfd, "%s ", str);
141 if (conf.loglevel_arg <= INFO)
142 fprintf(outfd, "%i: ", ll);
144 if (conf.loglevel_arg <= INFO)
145 fprintf(outfd, "(%d) ", mypid);
147 vfprintf(outfd, fmt, argp);
152 * setup shared memory area and get mutex for locking
154 static void shm_init(void)
157 int ret = shm_new(sizeof(struct misc_meta_data));
162 ret = shm_attach(ret, ATTACH_RW, &shm);
173 mmd->selector_num = 0;
175 mmd->num_commands = 0;
177 mmd->num_connects = 0;
178 mmd->active_connections = 0;
179 strcpy(mmd->filename, "(none)");
180 mmd->audio_format = -1;
181 mmd->vss_status_flags = VSS_NEXT;
182 mmd->new_vss_status_flags = VSS_NEXT;
183 mmd->sender_cmd_data.cmd_num = -1;
186 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
191 * lock the shared memory area containing the mmd struct
193 * \sa semop(2), struct misc_meta_data
197 mutex_lock(mmd_mutex);
201 * unlock the shared memory area containing the mmd struct
203 * \sa semop(2), struct misc_meta_data
206 void mmd_unlock(void)
208 mutex_unlock(mmd_mutex);
211 static void parse_config(int override)
213 char *home = para_homedir();
218 if (conf.config_file_given)
219 cf = conf.config_file_arg;
221 cf = make_message("%s/.paraslash/server.conf", home);
222 free(user_list_file);
223 if (!conf.user_list_given)
224 user_list_file = make_message("%s/.paraslash/server.users", home);
226 user_list_file = para_strdup(conf.user_list_arg);
227 ret = stat(cf, &statbuf);
228 if (ret && conf.config_file_given) {
230 PARA_EMERG_LOG("can not stat config file %s\n", cf);
234 int tmp = conf.daemon_given;
235 struct server_cmdline_parser_params params = {
236 .override = override,
241 server_cmdline_parser_config_file(cf, &conf, ¶ms);
242 conf.daemon_given = tmp;
245 if (!conf.logfile_given && conf.daemon_given) {
247 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
250 if (conf.logfile_given)
251 logfile = open_log(conf.logfile_arg);
258 free(user_list_file);
259 user_list_file = NULL;
263 static void setup_signal_handling(void)
267 signal_pipe = para_signal_init();
268 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
269 ret += para_install_sighandler(SIGINT);
270 ret += para_install_sighandler(SIGTERM);
271 ret += para_install_sighandler(SIGHUP);
272 ret += para_install_sighandler(SIGCHLD);
273 ret += para_install_sighandler(SIGUSR1);
274 signal(SIGPIPE, SIG_IGN);
276 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
281 static void init_selector(void)
285 mmd->selector_change = -1; /* no change nec., set to new num by com_chs */
286 if (!conf.selector_given)
288 for (i = 0; selectors[i].name; i++) {
289 if (strcmp(selectors[i].name, conf.selector_arg))
291 PARA_NOTICE_LOG("initializing %s audio file selector\n",
293 ret = selectors[i].init(&selectors[i]);
295 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
298 mmd->selector_num = i;
301 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
303 mmd->selector_num = 0;
304 selectors[0].init(&selectors[0]); /* always successful */
307 static unsigned init_network(void)
309 int fd, ret = init_tcp_socket(conf.port_arg);
314 ret = mark_fd_nonblock(fd);
319 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
323 static void init_random_seed(void)
327 size_t len = sizeof(unsigned int);
329 fd = open("/dev/urandom", O_RDONLY);
333 if (read(fd, &seed, len) != len)
342 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
347 static unsigned do_inits(int argc, char **argv)
349 /* connector's address information */
353 /* parse command line options */
354 server_cmdline_parser(argc, argv, &conf);
355 HANDLE_VERSION_FLAG("server", conf);
356 para_drop_privileges(conf.user_arg, conf.group_arg);
357 /* parse config file, open log and set defaults */
359 log_welcome("para_server", conf.loglevel_arg);
360 shm_init(); /* init mmd struct */
361 server_uptime(UPTIME_SET); /* reset server uptime */
362 init_user_list(user_list_file);
364 if (conf.daemon_given)
367 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
368 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
370 mmd->server_pid = getpid();
371 setup_signal_handling();
373 /* init network socket */
374 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
375 sockfd = init_network();
376 PARA_NOTICE_LOG("%s", "init complete\n");
380 static void change_selector(void)
382 int ret, old = mmd->selector_num, new = mmd->selector_change;
384 selectors[old].shutdown();
385 ret = selectors[new].init(&selectors[new]);
386 mmd->selector_change = -1; /* reset */
388 mmd->selector_num = new;
392 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret));
393 selectors[0].init(&selectors[0]);
394 mmd->selector_num = 0;
398 * called when server gets SIGHUP or when client invokes hup command.
400 static void handle_sighup(void)
402 PARA_NOTICE_LOG("%s", "SIGHUP\n");
403 close_log(logfile); /* gets reopened if necessary by parse_config */
405 parse_config(1); /* reopens log */
406 mmd->selector_change = mmd->selector_num; /* do not change selector.. */
407 change_selector(); /* .. just reload */
408 init_user_list(user_list_file); /* reload user list */
411 static void status_refresh(void)
413 static int prev_uptime = -1, prev_events = -1;
414 int uptime = server_uptime(UPTIME_GET), ret = 1;
416 if (prev_events != mmd->events)
418 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
420 if (uptime / 60 != prev_uptime / 60)
424 prev_uptime = uptime;
425 prev_events = mmd->events;
426 mmd->vss_status_flags = mmd->new_vss_status_flags;
428 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
429 mmd->events, mmd->audio_format);
435 * the main function of para_server
437 * \param argc usual argument count
438 * \param argv usual argument vector
440 * \return EXIT_SUCCESS or EXIT_FAILURE
443 int main(int argc, char *argv[])
445 /* listen on sock_fd, new connection on new_fd */
447 struct sockaddr_in their_addr;
448 int i, max_fileno, ret;
451 struct timeval *timeout;
454 sockfd = do_inits(argc, argv);
459 /* check socket and signal pipe in any case */
460 para_fd_set(sockfd, &rfds, &max_fileno);
461 para_fd_set(signal_pipe, &rfds, &max_fileno);
462 timeout = vss_preselect();
464 for (i = 0; senders[i].name; i++) {
465 if (senders[i].status != SENDER_ON)
467 if (!senders[i].pre_select)
469 senders[i].pre_select( &max_fileno, &rfds, &wfds);
471 if (selectors[mmd->selector_num].pre_select) {
472 ret = selectors[mmd->selector_num].pre_select(&rfds, &wfds);
473 max_fileno = PARA_MAX(max_fileno, ret);
476 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
478 if (mmd->selector_change >= 0)
480 if (selectors[mmd->selector_num].post_select)
481 selectors[mmd->selector_num].post_select(&rfds, &wfds);
484 for (i = 0; senders[i].name; i++) {
485 if (senders[i].status != SENDER_ON)
487 if (!senders[i].post_select)
489 senders[i].post_select(&rfds, &wfds);
493 if (FD_ISSET(signal_pipe, &rfds)) {
495 sig = para_next_signal();
501 para_reap_children();
503 /* die on sigint/sigterm. Kill all children too. */
506 PARA_EMERG_LOG("terminating on signal %d\n", sig);
508 selectors[mmd->selector_num].shutdown();
509 mutex_destroy(mmd_mutex);
511 shm_destroy(mmd_shm_id);
515 if (mmd->sender_cmd_data.cmd_num >= 0) {
516 int num = mmd->sender_cmd_data.cmd_num,
517 s = mmd->sender_cmd_data.sender_num;
519 if (senders[s].client_cmds[num])
520 senders[s].client_cmds[num](&mmd->sender_cmd_data);
521 mmd->sender_cmd_data.cmd_num = -1;
523 if (!FD_ISSET(sockfd, &rfds))
526 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
529 PARA_INFO_LOG("got connection from %s, forking\n",
530 inet_ntoa(their_addr.sin_addr));
532 mmd->active_connections++;
536 PARA_CRIT_LOG("%s", "fork failed\n");
541 /* parent keeps accepting connections */
544 alarm(ALARM_TIMEOUT);
546 close(sockfd); /* child doesn't need the listener */
548 * put info on who we are serving into argv[0] to make
549 * client ip visible in top/ps
551 for (i = argc - 1; i >= 0; i--)
552 memset(argv[i], 0, strlen(argv[i]));
553 sprintf(argv[0], "para_server (serving %s)",
554 inet_ntoa(their_addr.sin_addr));
555 return handle_connect(new_fd, &their_addr);