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"
37 #include <sys/mman.h> /* MAP_SHARED, PROT_READ, PROT_WRITE */
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 http_send_init(struct sender *);
60 extern void ortp_send_init(struct sender *);
61 extern struct audio_format afl[];
63 /** the list of supported database tools */
64 struct dbtool dblist[] = {
67 .init = dopey_dbtool_init,
68 .update_audio_file = NULL,
75 .init = mysql_dbtool_init,
76 .update_audio_file = NULL,
86 /** the list of supported senders */
87 struct sender senders[] = {
90 .init = http_send_init,
95 .init = ortp_send_init,
104 /* global variables for server-internal use */
105 static FILE *logfile;
106 static int mmd_semid;
107 static int signal_pipe;
110 * para_server's log function
112 * \param ll the log level
113 * \param fmt the format string describing the log message
115 void para_log(int ll, char* fmt,...)
121 char str[MAXLINE] = "";
124 if (ll < conf.loglevel_arg)
133 if (conf.daemon_given && !logfile)
137 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
138 fprintf(outfd, "%s ", str);
139 if (conf.loglevel_arg <= INFO)
140 fprintf(outfd, "%i: ", ll);
142 if (conf.loglevel_arg <= INFO)
143 fprintf(outfd, "(%d) ", mypid);
145 vfprintf(outfd, fmt, argp);
151 * setup shared memory area and get semaphore for locking
153 static void shm_init(void)
158 if ((fd = open("/dev/zero", O_RDWR)) < 0) {
159 PARA_EMERG_LOG("%s", "failed to open /dev/zero\n");
162 if ((area = mmap(0, sizeof(struct misc_meta_data),
163 PROT_READ | PROT_WRITE,
164 MAP_SHARED, fd, 0)) == (caddr_t) - 1) {
165 PARA_EMERG_LOG("%s", "mmap error\n");
168 close(fd); /* we dont need /dev/zero anymore */
169 mmd = (struct misc_meta_data *)area;
173 mmd->num_commands = 0;
175 mmd->num_connects = 0;
176 mmd->active_connections = 0;
177 strcpy(mmd->filename, "(none)");
178 mmd->audio_format = -1;
179 mmd->afs_status_flags = AFS_NEXT;
180 mmd->new_afs_status_flags = AFS_NEXT;
181 mmd->sender_cmd_data.cmd_num = -1;
183 mmd_semid = semget(42, 1, IPC_CREAT | 0666);
184 if (mmd_semid == -1) {
185 PARA_EMERG_LOG("%s", "semget failed\n");
190 static void do_semop(struct sembuf *sops, int num)
192 if (semop(mmd_semid, sops, num) >= 0)
194 PARA_WARNING_LOG("semop failed (%s), retrying\n", strerror(errno));
195 while (semop(mmd_semid, sops, num) < 0)
200 * lock the shared memory area containing the mmd struct
202 * \sa semop(2), struct misc_meta_data
206 struct sembuf sops[2] = {
222 * unlock the shared memory area containing the mmd struct
224 * \sa semop(2), struct misc_meta_data
226 void mmd_unlock(void)
228 struct sembuf sops[1] = {
238 static void parse_config(int override)
240 char *home = para_homedir();
245 if (conf.config_file_given)
246 cf = conf.config_file_arg;
248 cf = make_message("%s/.paraslash/server.conf", home);
250 if (!conf.user_list_given)
251 user_list = make_message("%s/.paraslash/server.users", home);
253 user_list = para_strdup(conf.user_list_arg);
254 ret = stat(cf, &statbuf);
255 if (ret && conf.config_file_given) {
257 PARA_EMERG_LOG("can not stat config file %s\n", cf);
261 int tmp = conf.daemon_given;
262 cmdline_parser_configfile(cf, &conf, override, 0, 0);
263 conf.daemon_given = tmp;
266 if (!conf.logfile_given && conf.daemon_given) {
268 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
271 if (conf.logfile_given)
272 logfile = open_log(conf.logfile_arg);
284 static void setup_signal_handling(void)
288 signal_pipe = para_signal_init();
289 // fcntl(signal_pipe, F_SETFL, O_NONBLOCK);
290 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
291 ret += para_install_sighandler(SIGINT);
292 ret += para_install_sighandler(SIGTERM);
293 ret += para_install_sighandler(SIGHUP);
294 ret += para_install_sighandler(SIGCHLD);
295 signal(SIGPIPE, SIG_IGN);
296 signal(SIGUSR1, SIG_IGN);
298 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
303 static void init_dbtool(void)
307 mmd->dbt_change = -1; /* no change nec., set to new dbt num by com_cdt */
310 if (conf.dbtool_given) {
311 for (i = 0; dblist[i].name; i++) {
312 if (strcmp(dblist[i].name, conf.dbtool_arg))
314 PARA_NOTICE_LOG("initializing %s database tool\n",
316 if (dblist[i].init(&dblist[i]) < 0) {
317 PARA_WARNING_LOG("init %s failed",
324 PARA_WARNING_LOG("%s", "no such dbtool, switching to dopey\n");
327 /* use the first dbtool that works
328 * (assuming that dopey always works)
330 for (i = 1; dblist[i].name; i++) {
331 int ret = dblist[i].init(&dblist[i]);
333 PARA_INFO_LOG("initialized %s\n", dblist[i].name);
337 PARA_CRIT_LOG("%s init failed: %s\n", dblist[i].name,
338 PARA_STRERROR(-ret));
342 dblist[0].init(&dblist[0]); /* always successful */
345 static unsigned init_network(void)
347 int sockfd = init_tcp_socket(conf.port_arg);
354 static void init_random_seed(void)
356 int fd, ret = -1, len = sizeof(unsigned int);
359 fd = open("/dev/random", O_RDONLY);
363 if (read(fd, &seed, len) != len)
372 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
377 static unsigned do_inits(int argc, char **argv)
379 /* connector's address information */
383 /* parse command line options */
384 cmdline_parser(argc, argv, &conf);
385 para_drop_privileges(conf.user_arg);
386 /* parse config file, open log and set defaults */
388 log_welcome("para_server", conf.loglevel_arg);
389 shm_init(); /* init mmd struct */
390 server_uptime(UPTIME_SET); /* reset server uptime */
392 if (conf.daemon_given)
395 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
396 /* audio file sender */
398 mmd->server_pid = getpid();
399 setup_signal_handling();
401 /* init network socket */
402 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
403 sockfd = init_network();
404 if (conf.autoplay_given) {
405 mmd->afs_status_flags |= AFS_PLAYING;
406 mmd->new_afs_status_flags |= AFS_PLAYING;
408 PARA_NOTICE_LOG("%s", "init complete\n");
412 static void handle_dbt_change(void)
414 int ret, old = mmd->dbt_num, new = mmd->dbt_change;
416 dblist[old].shutdown();
417 ret = dblist[new].init(&dblist[new]);
418 mmd->dbt_change = -1; /* reset */
424 PARA_ERROR_LOG("%s -- switching to dopey\n", PARA_STRERROR(-ret));
425 dblist[0].init(&dblist[0]);
430 * called when server gets SIGHUP or when client invokes hup command.
432 static void handle_sighup(void)
434 PARA_NOTICE_LOG("%s", "SIGHUP\n");
435 close_log(logfile); /* gets reopened if necessary by parse_config */
437 parse_config(1); /* reopens log */
438 mmd->dbt_change = mmd->dbt_num; /* do not change dbtool */
439 handle_dbt_change(); /* force reloading dbtool */
442 static void status_refresh(void)
444 static int prev_uptime = -1, prev_events = -1;
445 int uptime = server_uptime(UPTIME_GET), ret = 1;
447 if (prev_events != mmd->events)
449 if (mmd->new_afs_status_flags != mmd->afs_status_flags)
451 if (uptime / 60 != prev_uptime / 60)
455 prev_uptime = uptime;
456 prev_events = mmd->events;
457 mmd->afs_status_flags = mmd->new_afs_status_flags;
459 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
460 mmd->events, mmd->audio_format);
468 int main(int argc, char *argv[])
470 /* listen on sock_fd, new connection on new_fd */
472 struct sockaddr_in their_addr;
473 int err, i, max_fileno, ret;
476 struct timeval *timeout;
479 sockfd = do_inits(argc, argv);
481 /* check socket and signal pipe in any case */
484 FD_SET(sockfd, &rfds);
486 FD_SET(signal_pipe, &rfds);
487 max_fileno = MAX(max_fileno, signal_pipe);
489 timeout = afs_preselect();
491 for (i = 0; senders[i].name; i++) {
492 if (senders[i].status != SENDER_ON)
494 if (!senders[i].pre_select)
496 senders[i].pre_select(mmd->audio_format >= 0?
497 &afl[mmd->audio_format] : NULL,
502 // PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
503 ret = select(max_fileno + 1, &rfds, &wfds, NULL, timeout);
505 //PARA_DEBUG_LOG("%s: select returned %i\n", __func__, ret);
507 if (mmd->dbt_change >= 0)
509 if (ret < 0 && err == EINTR)
512 PARA_CRIT_LOG("select error (%s)\n", strerror(err));
515 for (i = 0; senders[i].name; i++) {
516 if (senders[i].status != SENDER_ON)
518 if (!senders[i].post_select)
520 senders[i].post_select(mmd->audio_format >= 0?
521 &afl[mmd->audio_format] : NULL,
528 if (FD_ISSET(signal_pipe, &rfds)) {
530 sig = para_next_signal();
536 para_reap_children();
538 /* die on sigint/sigterm. Kill all children too. */
541 PARA_EMERG_LOG("terminating on signal %d\n", sig);
546 if (mmd->sender_cmd_data.cmd_num >= 0) {
547 int num = mmd->sender_cmd_data.cmd_num,
548 s = mmd->sender_cmd_data.sender_num;
550 if (senders[s].client_cmds[num])
551 senders[s].client_cmds[num](&mmd->sender_cmd_data);
552 mmd->sender_cmd_data.cmd_num = -1;
554 if (!FD_ISSET(sockfd, &rfds))
557 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
560 PARA_INFO_LOG("got connection from %s, forking\n",
561 inet_ntoa(their_addr.sin_addr));
563 mmd->active_connections++;
567 PARA_CRIT_LOG("%s", "fork failed\n");
572 /* parent keeps accepting connections */
575 alarm(ALARM_TIMEOUT);
577 close(sockfd); /* child doesn't need the listener */
579 * put info on who we are serving into argv[0] to make
580 * client ip visible in top/ps
582 for (i = argc - 1; i >= 0; i--)
583 memset(argv[i], 0, strlen(argv[i]));
584 sprintf(argv[0], "para_server (serving %s)",
585 inet_ntoa(their_addr.sin_addr));
586 return handle_connect(new_fd, &their_addr);