2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file server.c Paraslash's main server */
22 /** \mainpage Paraslash API Reference
24 * Good starting points for reading are probably \ref audio_file_selector,
25 * \ref sender, \ref receiver, \ref receiver_node, \ref filter, \ref
32 #include "server.cmdline.h"
37 #include "close_on_fork.h"
46 #include "user_list.h"
48 /** define the array of error lists needed by para_server */
51 /** shut down non-authorized connections after that many seconds */
52 #define ALARM_TIMEOUT 10
55 * pointer to shared memory area for communication between para_server
56 * and its children. exported to vss.c. command.c and to all selectors.
58 struct misc_meta_data *mmd;
61 * the configuration of para_server
63 * It also contains the options for all audio file selectors, audio format handler
64 * and all supported senders.
66 struct server_args_info conf;
68 /** the file containing user information (public key, permissions) */
69 char *user_list_file = NULL;
71 extern void dccp_send_init(struct sender *);
72 extern void http_send_init(struct sender *);
73 extern void ortp_send_init(struct sender *);
75 /* TODO: This is better handled by autoconf */
76 /** the list of supported audio file selectors */
77 struct audio_file_selector selectors[] = {
80 .init = random_selector_init,
81 .update_audio_file = NULL,
85 .init = playlist_selector_init,
86 .update_audio_file = NULL,
93 .init = mysql_selector_init,
94 .update_audio_file = NULL,
104 /** the list of supported senders */
105 struct sender senders[] = {
108 .init = http_send_init,
112 .init = dccp_send_init,
117 .init = ortp_send_init,
126 /* global variables for server-internal use */
127 static FILE *logfile;
128 static int mmd_mutex, mmd_shm_id;
129 static int signal_pipe;
132 * para_server's log function
134 * \param ll the log level
135 * \param fmt the format string describing the log message
137 void para_log(int ll, const char* fmt,...)
143 char str[MAXLINE] = "";
146 if (ll < conf.loglevel_arg)
148 outfd = logfile? logfile : stderr;
151 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
152 fprintf(outfd, "%s ", str);
153 if (conf.loglevel_arg <= INFO)
154 fprintf(outfd, "%i: ", ll);
156 if (conf.loglevel_arg <= INFO)
157 fprintf(outfd, "(%d) ", mypid);
159 vfprintf(outfd, fmt, argp);
164 * setup shared memory area and get mutex for locking
166 static void shm_init(void)
169 int ret = shm_new(sizeof(struct misc_meta_data));
174 ret = shm_attach(ret, ATTACH_RW, &shm);
185 mmd->selector_num = 0;
187 mmd->num_commands = 0;
189 mmd->num_connects = 0;
190 mmd->active_connections = 0;
191 strcpy(mmd->filename, "(none)");
192 mmd->audio_format = -1;
193 mmd->vss_status_flags = VSS_NEXT;
194 mmd->new_vss_status_flags = VSS_NEXT;
195 mmd->sender_cmd_data.cmd_num = -1;
198 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
203 * lock the shared memory area containing the mmd struct
205 * \sa semop(2), struct misc_meta_data
209 mutex_lock(mmd_mutex);
213 * unlock the shared memory area containing the mmd struct
215 * \sa semop(2), struct misc_meta_data
218 void mmd_unlock(void)
220 mutex_unlock(mmd_mutex);
223 static void parse_config(int override)
225 char *home = para_homedir();
230 if (conf.config_file_given)
231 cf = conf.config_file_arg;
233 cf = make_message("%s/.paraslash/server.conf", home);
234 free(user_list_file);
235 if (!conf.user_list_given)
236 user_list_file = make_message("%s/.paraslash/server.users", home);
238 user_list_file = para_strdup(conf.user_list_arg);
239 ret = stat(cf, &statbuf);
240 if (ret && conf.config_file_given) {
242 PARA_EMERG_LOG("can not stat config file %s\n", cf);
246 int tmp = conf.daemon_given;
247 server_cmdline_parser_configfile(cf, &conf, override, 0, 0);
248 conf.daemon_given = tmp;
251 if (!conf.logfile_given && conf.daemon_given) {
253 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
256 if (conf.logfile_given)
257 logfile = open_log(conf.logfile_arg);
264 free(user_list_file);
265 user_list_file = NULL;
269 static void setup_signal_handling(void)
273 signal_pipe = para_signal_init();
274 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
275 ret += para_install_sighandler(SIGINT);
276 ret += para_install_sighandler(SIGTERM);
277 ret += para_install_sighandler(SIGHUP);
278 ret += para_install_sighandler(SIGCHLD);
279 ret += para_install_sighandler(SIGUSR1);
280 signal(SIGPIPE, SIG_IGN);
282 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
287 static void init_selector(void)
291 mmd->selector_change = -1; /* no change nec., set to new num by com_chs */
292 if (!conf.selector_given)
294 for (i = 0; selectors[i].name; i++) {
295 if (strcmp(selectors[i].name, conf.selector_arg))
297 PARA_NOTICE_LOG("initializing %s audio file selector\n",
299 ret = selectors[i].init(&selectors[i]);
301 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
304 mmd->selector_num = i;
307 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
309 mmd->selector_num = 0;
310 selectors[0].init(&selectors[0]); /* always successful */
313 static unsigned init_network(void)
315 int fd, ret = init_tcp_socket(conf.port_arg);
320 ret = mark_fd_nonblock(fd);
325 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
329 static void init_random_seed(void)
331 int fd, ret = -1, len = sizeof(unsigned int);
334 fd = open("/dev/urandom", O_RDONLY);
338 if (read(fd, &seed, len) != len)
347 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
352 static unsigned do_inits(int argc, char **argv)
354 /* connector's address information */
358 /* parse command line options */
359 server_cmdline_parser(argc, argv, &conf);
360 HANDLE_VERSION_FLAG("server", conf);
361 para_drop_privileges(conf.user_arg, conf.group_arg);
362 /* parse config file, open log and set defaults */
364 log_welcome("para_server", conf.loglevel_arg);
365 shm_init(); /* init mmd struct */
366 server_uptime(UPTIME_SET); /* reset server uptime */
367 init_user_list(user_list_file);
369 if (conf.daemon_given)
372 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
373 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
375 mmd->server_pid = getpid();
376 setup_signal_handling();
378 /* init network socket */
379 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
380 sockfd = init_network();
381 PARA_NOTICE_LOG("%s", "init complete\n");
385 static void change_selector(void)
387 int ret, old = mmd->selector_num, new = mmd->selector_change;
389 selectors[old].shutdown();
390 ret = selectors[new].init(&selectors[new]);
391 mmd->selector_change = -1; /* reset */
393 mmd->selector_num = new;
397 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret));
398 selectors[0].init(&selectors[0]);
399 mmd->selector_num = 0;
403 * called when server gets SIGHUP or when client invokes hup command.
405 static void handle_sighup(void)
407 PARA_NOTICE_LOG("%s", "SIGHUP\n");
408 close_log(logfile); /* gets reopened if necessary by parse_config */
410 parse_config(1); /* reopens log */
411 mmd->selector_change = mmd->selector_num; /* do not change selector.. */
412 change_selector(); /* .. just reload */
413 init_user_list(user_list_file); /* reload user list */
416 static void status_refresh(void)
418 static int prev_uptime = -1, prev_events = -1;
419 int uptime = server_uptime(UPTIME_GET), ret = 1;
421 if (prev_events != mmd->events)
423 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
425 if (uptime / 60 != prev_uptime / 60)
429 prev_uptime = uptime;
430 prev_events = mmd->events;
431 mmd->vss_status_flags = mmd->new_vss_status_flags;
433 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
434 mmd->events, mmd->audio_format);
440 * the main function of para_server
442 * \param argc usual argument count
443 * \param argv usual argument vector
445 * \return EXIT_SUCCESS or EXIT_FAILURE
448 int main(int argc, char *argv[])
450 /* listen on sock_fd, new connection on new_fd */
452 struct sockaddr_in their_addr;
453 int i, max_fileno, ret;
456 struct timeval *timeout;
459 sockfd = do_inits(argc, argv);
464 /* check socket and signal pipe in any case */
465 para_fd_set(sockfd, &rfds, &max_fileno);
466 para_fd_set(signal_pipe, &rfds, &max_fileno);
467 timeout = vss_preselect();
469 for (i = 0; senders[i].name; i++) {
470 if (senders[i].status != SENDER_ON)
472 if (!senders[i].pre_select)
474 senders[i].pre_select( &max_fileno, &rfds, &wfds);
476 if (selectors[mmd->selector_num].pre_select) {
477 ret = selectors[mmd->selector_num].pre_select(&rfds, &wfds);
478 max_fileno = PARA_MAX(max_fileno, ret);
481 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
483 if (mmd->selector_change >= 0)
485 if (selectors[mmd->selector_num].post_select)
486 selectors[mmd->selector_num].post_select(&rfds, &wfds);
489 for (i = 0; senders[i].name; i++) {
490 if (senders[i].status != SENDER_ON)
492 if (!senders[i].post_select)
494 senders[i].post_select(&rfds, &wfds);
498 if (FD_ISSET(signal_pipe, &rfds)) {
500 sig = para_next_signal();
506 para_reap_children();
508 /* die on sigint/sigterm. Kill all children too. */
511 PARA_EMERG_LOG("terminating on signal %d\n", sig);
513 selectors[mmd->selector_num].shutdown();
514 mutex_destroy(mmd_mutex);
516 shm_destroy(mmd_shm_id);
520 if (mmd->sender_cmd_data.cmd_num >= 0) {
521 int num = mmd->sender_cmd_data.cmd_num,
522 s = mmd->sender_cmd_data.sender_num;
524 if (senders[s].client_cmds[num])
525 senders[s].client_cmds[num](&mmd->sender_cmd_data);
526 mmd->sender_cmd_data.cmd_num = -1;
528 if (!FD_ISSET(sockfd, &rfds))
531 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
534 PARA_INFO_LOG("got connection from %s, forking\n",
535 inet_ntoa(their_addr.sin_addr));
537 mmd->active_connections++;
541 PARA_CRIT_LOG("%s", "fork failed\n");
546 /* parent keeps accepting connections */
549 alarm(ALARM_TIMEOUT);
551 close(sockfd); /* child doesn't need the listener */
553 * put info on who we are serving into argv[0] to make
554 * client ip visible in top/ps
556 for (i = argc - 1; i >= 0; i--)
557 memset(argv[i], 0, strlen(argv[i]));
558 sprintf(argv[0], "para_server (serving %s)",
559 inet_ntoa(their_addr.sin_addr));
560 return handle_connect(new_fd, &their_addr);