2 * Copyright (C) 1997-2006 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 dbtool, \ref sender,
25 * \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
31 #include "server.cmdline.h"
36 #include "close_on_fork.h"
44 /** define the array of error lists needed by para_server */
47 /** shut down non-authorized connections after that many seconds */
48 #define ALARM_TIMEOUT 10
50 /* these are exported to afs/command/dbtool */
51 struct misc_meta_data *mmd;
52 /** the configuration of para_server
54 * It also contains the options for all database tools and all supported
57 struct gengetopt_args_info conf;
58 char *user_list = 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 *);
62 extern struct audio_format afl[];
64 /** the list of supported database tools */
65 struct dbtool dblist[] = {
68 .init = random_dbtool_init,
69 .update_audio_file = NULL,
73 .init = plm_dbtool_init,
74 .update_audio_file = NULL,
81 .init = mysql_dbtool_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, char* fmt,...)
131 char str[MAXLINE] = "";
134 if (ll < conf.loglevel_arg)
143 if (conf.daemon_given && !logfile)
147 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
148 fprintf(outfd, "%s ", str);
149 if (conf.loglevel_arg <= INFO)
150 fprintf(outfd, "%i: ", ll);
152 if (conf.loglevel_arg <= INFO)
153 fprintf(outfd, "(%d) ", mypid);
155 vfprintf(outfd, fmt, argp);
160 * setup shared memory area and get mutex for locking
162 static void shm_init(void)
165 int ret = shm_new(sizeof(struct misc_meta_data));
170 ret = shm_attach(ret, ATTACH_RW, &shm);
183 mmd->num_commands = 0;
185 mmd->num_connects = 0;
186 mmd->active_connections = 0;
187 strcpy(mmd->filename, "(none)");
188 mmd->audio_format = -1;
189 mmd->afs_status_flags = AFS_NEXT;
190 mmd->new_afs_status_flags = AFS_NEXT;
191 mmd->sender_cmd_data.cmd_num = -1;
194 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
199 * lock the shared memory area containing the mmd struct
201 * \sa semop(2), struct misc_meta_data
205 mutex_lock(mmd_mutex);
209 * unlock the shared memory area containing the mmd struct
211 * \sa semop(2), struct misc_meta_data
214 void mmd_unlock(void)
216 mutex_unlock(mmd_mutex);
219 static void parse_config(int override)
221 char *home = para_homedir();
226 if (conf.config_file_given)
227 cf = conf.config_file_arg;
229 cf = make_message("%s/.paraslash/server.conf", home);
231 if (!conf.user_list_given)
232 user_list = make_message("%s/.paraslash/server.users", home);
234 user_list = para_strdup(conf.user_list_arg);
235 ret = stat(cf, &statbuf);
236 if (ret && conf.config_file_given) {
238 PARA_EMERG_LOG("can not stat config file %s\n", cf);
242 int tmp = conf.daemon_given;
243 cmdline_parser_configfile(cf, &conf, override, 0, 0);
244 conf.daemon_given = tmp;
247 if (!conf.logfile_given && conf.daemon_given) {
249 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
252 if (conf.logfile_given)
253 logfile = open_log(conf.logfile_arg);
265 static void setup_signal_handling(void)
269 signal_pipe = para_signal_init();
270 // fcntl(signal_pipe, F_SETFL, O_NONBLOCK);
271 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
272 ret += para_install_sighandler(SIGINT);
273 ret += para_install_sighandler(SIGTERM);
274 ret += para_install_sighandler(SIGHUP);
275 ret += para_install_sighandler(SIGCHLD);
276 ret += para_install_sighandler(SIGUSR1);
277 signal(SIGPIPE, SIG_IGN);
279 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
284 static void init_dbtool(void)
288 mmd->dbt_change = -1; /* no change nec., set to new dbt num by com_cdt */
289 if (!conf.dbtool_given)
291 for (i = 0; dblist[i].name; i++) {
292 if (strcmp(dblist[i].name, conf.dbtool_arg))
294 PARA_NOTICE_LOG("initializing %s database tool\n",
296 ret = dblist[i].init(&dblist[i]);
298 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
304 PARA_WARNING_LOG("%s", "falling back to the random dbtool\n");
307 dblist[0].init(&dblist[0]); /* always successful */
310 static unsigned init_network(void)
312 int sockfd = init_tcp_socket(conf.port_arg);
319 static void init_random_seed(void)
321 int fd, ret = -1, len = sizeof(unsigned int);
324 fd = open("/dev/urandom", O_RDONLY);
328 if (read(fd, &seed, len) != len)
337 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
342 static unsigned do_inits(int argc, char **argv)
344 /* connector's address information */
348 /* parse command line options */
349 cmdline_parser(argc, argv, &conf);
350 para_drop_privileges(conf.user_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 */
357 if (conf.daemon_given)
360 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
361 /* audio file sender */
363 mmd->server_pid = getpid();
364 setup_signal_handling();
366 /* init network socket */
367 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
368 sockfd = init_network();
369 if (conf.autoplay_given) {
370 mmd->afs_status_flags |= AFS_PLAYING;
371 mmd->new_afs_status_flags |= AFS_PLAYING;
373 PARA_NOTICE_LOG("%s", "init complete\n");
377 static void handle_dbt_change(void)
379 int ret, old = mmd->dbt_num, new = mmd->dbt_change;
381 dblist[old].shutdown();
382 ret = dblist[new].init(&dblist[new]);
383 mmd->dbt_change = -1; /* reset */
389 PARA_ERROR_LOG("%s -- switching to the random dbtool\n", PARA_STRERROR(-ret));
390 dblist[0].init(&dblist[0]);
395 * called when server gets SIGHUP or when client invokes hup command.
397 static void handle_sighup(void)
399 PARA_NOTICE_LOG("%s", "SIGHUP\n");
400 close_log(logfile); /* gets reopened if necessary by parse_config */
402 parse_config(1); /* reopens log */
403 mmd->dbt_change = mmd->dbt_num; /* do not change dbtool */
404 handle_dbt_change(); /* force reloading dbtool */
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_afs_status_flags != mmd->afs_status_flags)
416 if (uptime / 60 != prev_uptime / 60)
420 prev_uptime = uptime;
421 prev_events = mmd->events;
422 mmd->afs_status_flags = mmd->new_afs_status_flags;
424 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
425 mmd->events, mmd->audio_format);
433 int main(int argc, char *argv[])
435 /* listen on sock_fd, new connection on new_fd */
437 struct sockaddr_in their_addr;
438 int err, i, max_fileno, ret;
441 struct timeval *timeout;
444 sockfd = do_inits(argc, argv);
446 /* check socket and signal pipe in any case */
449 FD_SET(sockfd, &rfds);
451 FD_SET(signal_pipe, &rfds);
452 max_fileno = MAX(max_fileno, signal_pipe);
454 timeout = afs_preselect();
456 for (i = 0; senders[i].name; i++) {
457 if (senders[i].status != SENDER_ON)
459 if (!senders[i].pre_select)
461 senders[i].pre_select(mmd->audio_format >= 0?
462 &afl[mmd->audio_format] : NULL,
466 if (dblist[mmd->dbt_num].pre_select) {
467 ret = dblist[mmd->dbt_num].pre_select(&rfds, &wfds);
468 max_fileno = MAX(max_fileno, ret);
471 // PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
472 ret = select(max_fileno + 1, &rfds, &wfds, NULL, timeout);
474 //PARA_DEBUG_LOG("%s: select returned %i\n", __func__, ret);
476 if (mmd->dbt_change >= 0)
478 if (dblist[mmd->dbt_num].post_select)
479 dblist[mmd->dbt_num].post_select(&rfds, &wfds);
480 if (ret < 0 && err == EINTR)
483 PARA_CRIT_LOG("select error (%s)\n", strerror(err));
486 for (i = 0; senders[i].name; i++) {
487 if (senders[i].status != SENDER_ON)
489 if (!senders[i].post_select)
491 senders[i].post_select(mmd->audio_format >= 0?
492 &afl[mmd->audio_format] : NULL,
499 if (FD_ISSET(signal_pipe, &rfds)) {
501 sig = para_next_signal();
507 para_reap_children();
509 /* die on sigint/sigterm. Kill all children too. */
512 PARA_EMERG_LOG("terminating on signal %d\n", sig);
514 dblist[mmd->dbt_num].shutdown();
515 mutex_destroy(mmd_mutex);
517 shm_destroy(mmd_shm_id);
521 if (mmd->sender_cmd_data.cmd_num >= 0) {
522 int num = mmd->sender_cmd_data.cmd_num,
523 s = mmd->sender_cmd_data.sender_num;
525 if (senders[s].client_cmds[num])
526 senders[s].client_cmds[num](&mmd->sender_cmd_data);
527 mmd->sender_cmd_data.cmd_num = -1;
529 if (!FD_ISSET(sockfd, &rfds))
532 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
535 PARA_INFO_LOG("got connection from %s, forking\n",
536 inet_ntoa(their_addr.sin_addr));
538 mmd->active_connections++;
542 PARA_CRIT_LOG("%s", "fork failed\n");
547 /* parent keeps accepting connections */
550 alarm(ALARM_TIMEOUT);
552 close(sockfd); /* child doesn't need the listener */
554 * put info on who we are serving into argv[0] to make
555 * client ip visible in top/ps
557 for (i = argc - 1; i >= 0; i--)
558 memset(argv[i], 0, strlen(argv[i]));
559 sprintf(argv[0], "para_server (serving %s)",
560 inet_ntoa(their_addr.sin_addr));
561 return handle_connect(new_fd, &their_addr);