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"
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 server_cmdline_parser_configfile(cf, &conf, override, 0, 0);
236 conf.daemon_given = tmp;
239 if (!conf.logfile_given && conf.daemon_given) {
241 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
244 if (conf.logfile_given)
245 logfile = open_log(conf.logfile_arg);
252 free(user_list_file);
253 user_list_file = NULL;
257 static void setup_signal_handling(void)
261 signal_pipe = para_signal_init();
262 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
263 ret += para_install_sighandler(SIGINT);
264 ret += para_install_sighandler(SIGTERM);
265 ret += para_install_sighandler(SIGHUP);
266 ret += para_install_sighandler(SIGCHLD);
267 ret += para_install_sighandler(SIGUSR1);
268 signal(SIGPIPE, SIG_IGN);
270 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
275 static void init_selector(void)
279 mmd->selector_change = -1; /* no change nec., set to new num by com_chs */
280 if (!conf.selector_given)
282 for (i = 0; selectors[i].name; i++) {
283 if (strcmp(selectors[i].name, conf.selector_arg))
285 PARA_NOTICE_LOG("initializing %s audio file selector\n",
287 ret = selectors[i].init(&selectors[i]);
289 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
292 mmd->selector_num = i;
295 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
297 mmd->selector_num = 0;
298 selectors[0].init(&selectors[0]); /* always successful */
301 static unsigned init_network(void)
303 int fd, ret = init_tcp_socket(conf.port_arg);
308 ret = mark_fd_nonblock(fd);
313 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
317 static void init_random_seed(void)
321 size_t len = sizeof(unsigned int);
323 fd = open("/dev/urandom", O_RDONLY);
327 if (read(fd, &seed, len) != len)
336 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
341 static unsigned do_inits(int argc, char **argv)
343 /* connector's address information */
347 /* parse command line options */
348 server_cmdline_parser(argc, argv, &conf);
349 HANDLE_VERSION_FLAG("server", conf);
350 para_drop_privileges(conf.user_arg, conf.group_arg);
351 /* parse config file, open log and set defaults */
353 log_welcome("para_server", conf.loglevel_arg);
354 shm_init(); /* init mmd struct */
355 server_uptime(UPTIME_SET); /* reset server uptime */
356 init_user_list(user_list_file);
358 if (conf.daemon_given)
361 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
362 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
364 mmd->server_pid = getpid();
365 setup_signal_handling();
367 /* init network socket */
368 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
369 sockfd = init_network();
370 PARA_NOTICE_LOG("%s", "init complete\n");
374 static void change_selector(void)
376 int ret, old = mmd->selector_num, new = mmd->selector_change;
378 selectors[old].shutdown();
379 ret = selectors[new].init(&selectors[new]);
380 mmd->selector_change = -1; /* reset */
382 mmd->selector_num = new;
386 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret));
387 selectors[0].init(&selectors[0]);
388 mmd->selector_num = 0;
392 * called when server gets SIGHUP or when client invokes hup command.
394 static void handle_sighup(void)
396 PARA_NOTICE_LOG("%s", "SIGHUP\n");
397 close_log(logfile); /* gets reopened if necessary by parse_config */
399 parse_config(1); /* reopens log */
400 mmd->selector_change = mmd->selector_num; /* do not change selector.. */
401 change_selector(); /* .. just reload */
402 init_user_list(user_list_file); /* reload user list */
405 static void status_refresh(void)
407 static int prev_uptime = -1, prev_events = -1;
408 int uptime = server_uptime(UPTIME_GET), ret = 1;
410 if (prev_events != mmd->events)
412 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
414 if (uptime / 60 != prev_uptime / 60)
418 prev_uptime = uptime;
419 prev_events = mmd->events;
420 mmd->vss_status_flags = mmd->new_vss_status_flags;
422 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
423 mmd->events, mmd->audio_format);
429 * the main function of para_server
431 * \param argc usual argument count
432 * \param argv usual argument vector
434 * \return EXIT_SUCCESS or EXIT_FAILURE
437 int main(int argc, char *argv[])
439 /* listen on sock_fd, new connection on new_fd */
441 struct sockaddr_in their_addr;
442 int i, max_fileno, ret;
445 struct timeval *timeout;
448 sockfd = do_inits(argc, argv);
453 /* check socket and signal pipe in any case */
454 para_fd_set(sockfd, &rfds, &max_fileno);
455 para_fd_set(signal_pipe, &rfds, &max_fileno);
456 timeout = vss_preselect();
458 for (i = 0; senders[i].name; i++) {
459 if (senders[i].status != SENDER_ON)
461 if (!senders[i].pre_select)
463 senders[i].pre_select( &max_fileno, &rfds, &wfds);
465 if (selectors[mmd->selector_num].pre_select) {
466 ret = selectors[mmd->selector_num].pre_select(&rfds, &wfds);
467 max_fileno = PARA_MAX(max_fileno, ret);
470 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
472 if (mmd->selector_change >= 0)
474 if (selectors[mmd->selector_num].post_select)
475 selectors[mmd->selector_num].post_select(&rfds, &wfds);
478 for (i = 0; senders[i].name; i++) {
479 if (senders[i].status != SENDER_ON)
481 if (!senders[i].post_select)
483 senders[i].post_select(&rfds, &wfds);
487 if (FD_ISSET(signal_pipe, &rfds)) {
489 sig = para_next_signal();
495 para_reap_children();
497 /* die on sigint/sigterm. Kill all children too. */
500 PARA_EMERG_LOG("terminating on signal %d\n", sig);
502 selectors[mmd->selector_num].shutdown();
503 mutex_destroy(mmd_mutex);
505 shm_destroy(mmd_shm_id);
509 if (mmd->sender_cmd_data.cmd_num >= 0) {
510 int num = mmd->sender_cmd_data.cmd_num,
511 s = mmd->sender_cmd_data.sender_num;
513 if (senders[s].client_cmds[num])
514 senders[s].client_cmds[num](&mmd->sender_cmd_data);
515 mmd->sender_cmd_data.cmd_num = -1;
517 if (!FD_ISSET(sockfd, &rfds))
520 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
523 PARA_INFO_LOG("got connection from %s, forking\n",
524 inet_ntoa(their_addr.sin_addr));
526 mmd->active_connections++;
530 PARA_CRIT_LOG("%s", "fork failed\n");
535 /* parent keeps accepting connections */
538 alarm(ALARM_TIMEOUT);
540 close(sockfd); /* child doesn't need the listener */
542 * put info on who we are serving into argv[0] to make
543 * client ip visible in top/ps
545 for (i = argc - 1; i >= 0; i--)
546 memset(argv[i], 0, strlen(argv[i]));
547 sprintf(argv[0], "para_server (serving %s)",
548 inet_ntoa(their_addr.sin_addr));
549 return handle_connect(new_fd, &their_addr);