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 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"
45 #include "user_list.h"
47 /** define the array of error lists needed by para_server */
50 /** shut down non-authorized connections after that many seconds */
51 #define ALARM_TIMEOUT 10
53 /* these are exported to afs.c. command.c and to all selectors */
54 struct misc_meta_data
*mmd
;
55 /** the configuration of para_server
57 * It also contains the options for all audio file selectors and all supported
60 struct server_args_info conf
;
61 char *user_list_file
= NULL
;
62 extern void dccp_send_init(struct sender
*);
63 extern void http_send_init(struct sender
*);
64 extern void ortp_send_init(struct sender
*);
66 /* TODO: This is better handled by autoconf */
67 /** the list of supported audio file selectors */
68 struct audio_file_selector selectors
[] = {
71 .init
= random_selector_init
,
72 .update_audio_file
= NULL
,
76 .init
= playlist_selector_init
,
77 .update_audio_file
= NULL
,
84 .init
= mysql_selector_init
,
85 .update_audio_file
= NULL
,
95 /** the list of supported senders */
96 struct sender senders
[] = {
99 .init
= http_send_init
,
103 .init
= dccp_send_init
,
108 .init
= ortp_send_init
,
117 /* global variables for server-internal use */
118 static FILE *logfile
;
119 static int mmd_mutex
, mmd_shm_id
;
120 static int signal_pipe
;
123 * para_server's log function
125 * \param ll the log level
126 * \param fmt the format string describing the log message
128 void para_log(int ll
, const char* fmt
,...)
134 char str
[MAXLINE
] = "";
137 if (ll
< conf
.loglevel_arg
)
139 outfd
= logfile
? logfile
: stderr
;
142 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
143 fprintf(outfd
, "%s ", str
);
144 if (conf
.loglevel_arg
<= INFO
)
145 fprintf(outfd
, "%i: ", ll
);
147 if (conf
.loglevel_arg
<= INFO
)
148 fprintf(outfd
, "(%d) ", mypid
);
150 vfprintf(outfd
, fmt
, argp
);
155 * setup shared memory area and get mutex for locking
157 static void shm_init(void)
160 int ret
= shm_new(sizeof(struct misc_meta_data
));
165 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
176 mmd
->selector_num
= 0;
178 mmd
->num_commands
= 0;
180 mmd
->num_connects
= 0;
181 mmd
->active_connections
= 0;
182 strcpy(mmd
->filename
, "(none)");
183 mmd
->audio_format
= -1;
184 mmd
->afs_status_flags
= AFS_NEXT
;
185 mmd
->new_afs_status_flags
= AFS_NEXT
;
186 mmd
->sender_cmd_data
.cmd_num
= -1;
189 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret
));
194 * lock the shared memory area containing the mmd struct
196 * \sa semop(2), struct misc_meta_data
200 mutex_lock(mmd_mutex
);
204 * unlock the shared memory area containing the mmd struct
206 * \sa semop(2), struct misc_meta_data
209 void mmd_unlock(void)
211 mutex_unlock(mmd_mutex
);
214 static void parse_config(int override
)
216 char *home
= para_homedir();
221 if (conf
.config_file_given
)
222 cf
= conf
.config_file_arg
;
224 cf
= make_message("%s/.paraslash/server.conf", home
);
225 free(user_list_file
);
226 if (!conf
.user_list_given
)
227 user_list_file
= make_message("%s/.paraslash/server.users", home
);
229 user_list_file
= para_strdup(conf
.user_list_arg
);
230 ret
= stat(cf
, &statbuf
);
231 if (ret
&& conf
.config_file_given
) {
233 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
237 int tmp
= conf
.daemon_given
;
238 server_cmdline_parser_configfile(cf
, &conf
, override
, 0, 0);
239 conf
.daemon_given
= tmp
;
242 if (!conf
.logfile_given
&& conf
.daemon_given
) {
244 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
247 if (conf
.logfile_given
)
248 logfile
= open_log(conf
.logfile_arg
);
255 free(user_list_file
);
256 user_list_file
= NULL
;
260 static void setup_signal_handling(void)
264 signal_pipe
= para_signal_init();
265 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
266 ret
+= para_install_sighandler(SIGINT
);
267 ret
+= para_install_sighandler(SIGTERM
);
268 ret
+= para_install_sighandler(SIGHUP
);
269 ret
+= para_install_sighandler(SIGCHLD
);
270 ret
+= para_install_sighandler(SIGUSR1
);
271 signal(SIGPIPE
, SIG_IGN
);
273 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
278 static void init_selector(void)
282 mmd
->selector_change
= -1; /* no change nec., set to new num by com_chs */
283 if (!conf
.selector_given
)
285 for (i
= 0; selectors
[i
].name
; i
++) {
286 if (strcmp(selectors
[i
].name
, conf
.selector_arg
))
288 PARA_NOTICE_LOG("initializing %s audio file selector\n",
290 ret
= selectors
[i
].init(&selectors
[i
]);
292 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret
));
295 mmd
->selector_num
= i
;
298 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
300 mmd
->selector_num
= 0;
301 selectors
[0].init(&selectors
[0]); /* always successful */
304 static unsigned init_network(void)
306 int fd
, ret
= init_tcp_socket(conf
.port_arg
);
311 ret
= mark_fd_nonblock(fd
);
316 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
320 static void init_random_seed(void)
322 int fd
, ret
= -1, len
= sizeof(unsigned int);
325 fd
= open("/dev/urandom", O_RDONLY
);
329 if (read(fd
, &seed
, len
) != len
)
338 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
343 static unsigned do_inits(int argc
, char **argv
)
345 /* connector's address information */
349 /* parse command line options */
350 server_cmdline_parser(argc
, argv
, &conf
);
351 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
352 /* parse config file, open log and set defaults */
354 log_welcome("para_server", conf
.loglevel_arg
);
355 shm_init(); /* init mmd struct */
356 server_uptime(UPTIME_SET
); /* reset server uptime */
357 init_user_list(user_list_file
);
359 if (conf
.daemon_given
)
362 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
363 /* audio file sender */
365 mmd
->server_pid
= getpid();
366 setup_signal_handling();
368 /* init network socket */
369 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
370 sockfd
= init_network();
371 PARA_NOTICE_LOG("%s", "init complete\n");
375 static void change_selector(void)
377 int ret
, old
= mmd
->selector_num
, new = mmd
->selector_change
;
379 selectors
[old
].shutdown();
380 ret
= selectors
[new].init(&selectors
[new]);
381 mmd
->selector_change
= -1; /* reset */
383 mmd
->selector_num
= new;
387 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret
));
388 selectors
[0].init(&selectors
[0]);
389 mmd
->selector_num
= 0;
393 * called when server gets SIGHUP or when client invokes hup command.
395 static void handle_sighup(void)
397 PARA_NOTICE_LOG("%s", "SIGHUP\n");
398 close_log(logfile
); /* gets reopened if necessary by parse_config */
400 parse_config(1); /* reopens log */
401 mmd
->selector_change
= mmd
->selector_num
; /* do not change selector.. */
402 change_selector(); /* .. just reload */
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_afs_status_flags
!= mmd
->afs_status_flags
)
414 if (uptime
/ 60 != prev_uptime
/ 60)
418 prev_uptime
= uptime
;
419 prev_events
= mmd
->events
;
420 mmd
->afs_status_flags
= mmd
->new_afs_status_flags
;
422 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
423 mmd
->events
, mmd
->audio_format
);
431 int main(int argc
, char *argv
[])
433 /* listen on sock_fd, new connection on new_fd */
435 struct sockaddr_in their_addr
;
436 int i
, max_fileno
, ret
;
439 struct timeval
*timeout
;
442 sockfd
= do_inits(argc
, argv
);
447 /* check socket and signal pipe in any case */
448 para_fd_set(sockfd
, &rfds
, &max_fileno
);
449 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
450 timeout
= afs_preselect();
452 for (i
= 0; senders
[i
].name
; i
++) {
453 if (senders
[i
].status
!= SENDER_ON
)
455 if (!senders
[i
].pre_select
)
457 senders
[i
].pre_select( &max_fileno
, &rfds
, &wfds
);
459 if (selectors
[mmd
->selector_num
].pre_select
) {
460 ret
= selectors
[mmd
->selector_num
].pre_select(&rfds
, &wfds
);
461 max_fileno
= PARA_MAX(max_fileno
, ret
);
464 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, timeout
);
466 if (mmd
->selector_change
>= 0)
468 if (selectors
[mmd
->selector_num
].post_select
)
469 selectors
[mmd
->selector_num
].post_select(&rfds
, &wfds
);
472 for (i
= 0; senders
[i
].name
; i
++) {
473 if (senders
[i
].status
!= SENDER_ON
)
475 if (!senders
[i
].post_select
)
477 senders
[i
].post_select(&rfds
, &wfds
);
481 if (FD_ISSET(signal_pipe
, &rfds
)) {
483 sig
= para_next_signal();
489 para_reap_children();
491 /* die on sigint/sigterm. Kill all children too. */
494 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
496 selectors
[mmd
->selector_num
].shutdown();
497 mutex_destroy(mmd_mutex
);
499 shm_destroy(mmd_shm_id
);
503 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
504 int num
= mmd
->sender_cmd_data
.cmd_num
,
505 s
= mmd
->sender_cmd_data
.sender_num
;
507 if (senders
[s
].client_cmds
[num
])
508 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
509 mmd
->sender_cmd_data
.cmd_num
= -1;
511 if (!FD_ISSET(sockfd
, &rfds
))
514 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
517 PARA_INFO_LOG("got connection from %s, forking\n",
518 inet_ntoa(their_addr
.sin_addr
));
520 mmd
->active_connections
++;
524 PARA_CRIT_LOG("%s", "fork failed\n");
529 /* parent keeps accepting connections */
532 alarm(ALARM_TIMEOUT
);
534 close(sockfd
); /* child doesn't need the listener */
536 * put info on who we are serving into argv[0] to make
537 * client ip visible in top/ps
539 for (i
= argc
- 1; i
>= 0; i
--)
540 memset(argv
[i
], 0, strlen(argv
[i
]));
541 sprintf(argv
[0], "para_server (serving %s)",
542 inet_ntoa(their_addr
.sin_addr
));
543 return handle_connect(new_fd
, &their_addr
);