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
19 #include <sys/types.h>
23 #include "server.cmdline.h"
24 #include "afs_common.h"
29 #include "close_on_fork.h"
40 #include "user_list.h"
43 /** define the array of error lists needed by para_server */
46 /** shut down non-authorized connections after that many seconds */
47 #define ALARM_TIMEOUT 10
50 * pointer to shared memory area for communication between para_server
51 * and its children. exported to vss.c. command.c and to all selectors.
53 struct misc_meta_data
*mmd
;
56 * the configuration of para_server
58 * It also contains the options for all audio file selectors, audio format handler
59 * and all supported senders.
61 struct server_args_info conf
;
63 /** the file containing user information (public key, permissions) */
64 char *user_list_file
= NULL
;
66 extern void dccp_send_init(struct sender
*);
67 extern void http_send_init(struct sender
*);
68 extern void ortp_send_init(struct sender
*);
70 /* TODO: This is better handled by autoconf */
71 /** the list of supported audio file selectors */
72 struct audio_file_selector selectors
[] = {
75 .init
= random_selector_init
,
76 .update_audio_file
= NULL
,
80 .init
= playlist_selector_init
,
81 .update_audio_file
= NULL
,
88 .init
= mysql_selector_init
,
89 .update_audio_file
= NULL
,
99 /** the list of supported senders */
100 struct sender senders
[] = {
103 .init
= http_send_init
,
107 .init
= dccp_send_init
,
112 .init
= ortp_send_init
,
121 /* global variables for server-internal use */
122 static FILE *logfile
;
123 static int mmd_mutex
, mmd_shm_id
;
124 static int signal_pipe
;
127 * para_server's log function
129 * \param ll the log level
130 * \param fmt the format string describing the log message
132 void para_log(int ll
, const char* fmt
,...)
138 char str
[MAXLINE
] = "";
141 if (ll
< conf
.loglevel_arg
)
143 outfd
= logfile
? logfile
: stderr
;
146 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
147 fprintf(outfd
, "%s ", str
);
148 if (conf
.loglevel_arg
<= INFO
)
149 fprintf(outfd
, "%i: ", ll
);
151 if (conf
.loglevel_arg
<= INFO
)
152 fprintf(outfd
, "(%d) ", mypid
);
154 vfprintf(outfd
, fmt
, argp
);
159 * setup shared memory area and get mutex for locking
161 static void shm_init(void)
164 int ret
= shm_new(sizeof(struct misc_meta_data
));
169 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
180 mmd
->selector_num
= 0;
182 mmd
->num_commands
= 0;
184 mmd
->num_connects
= 0;
185 mmd
->active_connections
= 0;
186 strcpy(mmd
->filename
, "(none)");
187 mmd
->audio_format
= -1;
188 mmd
->vss_status_flags
= VSS_NEXT
;
189 mmd
->new_vss_status_flags
= VSS_NEXT
;
190 mmd
->sender_cmd_data
.cmd_num
= -1;
193 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret
));
198 * lock the shared memory area containing the mmd struct
200 * \sa semop(2), struct misc_meta_data
204 mutex_lock(mmd_mutex
);
208 * unlock the shared memory area containing the mmd struct
210 * \sa semop(2), struct misc_meta_data
213 void mmd_unlock(void)
215 mutex_unlock(mmd_mutex
);
218 static void parse_config(int override
)
220 char *home
= para_homedir();
225 if (conf
.config_file_given
)
226 cf
= conf
.config_file_arg
;
228 cf
= make_message("%s/.paraslash/server.conf", home
);
229 free(user_list_file
);
230 if (!conf
.user_list_given
)
231 user_list_file
= make_message("%s/.paraslash/server.users", home
);
233 user_list_file
= para_strdup(conf
.user_list_arg
);
234 ret
= stat(cf
, &statbuf
);
235 if (ret
&& conf
.config_file_given
) {
237 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
241 int tmp
= conf
.daemon_given
;
242 struct server_cmdline_parser_params params
= {
243 .override
= override
,
248 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
249 conf
.daemon_given
= tmp
;
252 if (!conf
.logfile_given
&& conf
.daemon_given
) {
254 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
257 if (conf
.logfile_given
)
258 logfile
= open_log(conf
.logfile_arg
);
265 free(user_list_file
);
266 user_list_file
= NULL
;
270 static void setup_signal_handling(void)
274 signal_pipe
= para_signal_init();
275 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
276 ret
+= para_install_sighandler(SIGINT
);
277 ret
+= para_install_sighandler(SIGTERM
);
278 ret
+= para_install_sighandler(SIGHUP
);
279 ret
+= para_install_sighandler(SIGCHLD
);
280 ret
+= para_install_sighandler(SIGUSR1
);
281 signal(SIGPIPE
, SIG_IGN
);
283 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
288 static void init_selector(void)
292 mmd
->selector_change
= -1; /* no change nec., set to new num by com_chs */
293 if (!conf
.selector_given
)
295 for (i
= 0; selectors
[i
].name
; i
++) {
296 if (strcmp(selectors
[i
].name
, conf
.selector_arg
))
298 PARA_NOTICE_LOG("initializing %s audio file selector\n",
300 ret
= selectors
[i
].init(&selectors
[i
]);
302 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret
));
305 mmd
->selector_num
= i
;
308 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
310 mmd
->selector_num
= 0;
311 selectors
[0].init(&selectors
[0]); /* always successful */
314 static unsigned init_network(void)
316 int fd
, ret
= init_tcp_socket(conf
.port_arg
);
321 ret
= mark_fd_nonblock(fd
);
326 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
330 static void init_random_seed(void)
334 size_t len
= sizeof(unsigned int);
336 fd
= open("/dev/urandom", O_RDONLY
);
340 if (read(fd
, &seed
, len
) != len
)
349 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
354 uint32_t afs_socket_cookie
;
355 static int afs_socket
;
356 static pid_t afs_pid
;
358 static void init_afs(void)
360 int ret
, afs_server_socket
[2];
362 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
365 afs_socket_cookie
= para_random((uint32_t)-1);
369 if (!afs_pid
) { /* child (afs) */
370 close(afs_server_socket
[0]);
371 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
373 close(afs_server_socket
[1]);
374 afs_socket
= afs_server_socket
[0];
375 ret
= mark_fd_nonblock(afs_socket
);
378 add_close_on_fork_list(afs_socket
);
379 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket
,
380 (unsigned) afs_socket_cookie
);
384 static unsigned do_inits(int argc
, char **argv
)
386 /* connector's address information */
390 /* parse command line options */
391 server_cmdline_parser(argc
, argv
, &conf
);
392 HANDLE_VERSION_FLAG("server", conf
);
393 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
394 /* parse config file, open log and set defaults */
396 log_welcome("para_server", conf
.loglevel_arg
);
397 shm_init(); /* init mmd struct */
398 server_uptime(UPTIME_SET
); /* reset server uptime */
399 init_user_list(user_list_file
);
401 if (conf
.daemon_given
)
404 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
405 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
407 mmd
->server_pid
= getpid();
408 setup_signal_handling();
411 /* init network socket */
412 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
413 sockfd
= init_network();
414 PARA_NOTICE_LOG("%s", "init complete\n");
418 static void change_selector(void)
420 int ret
, old
= mmd
->selector_num
, new = mmd
->selector_change
;
422 selectors
[old
].shutdown();
423 ret
= selectors
[new].init(&selectors
[new]);
424 mmd
->selector_change
= -1; /* reset */
426 mmd
->selector_num
= new;
430 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret
));
431 selectors
[0].init(&selectors
[0]);
432 mmd
->selector_num
= 0;
436 * called when server gets SIGHUP or when client invokes hup command.
438 static void handle_sighup(void)
440 PARA_NOTICE_LOG("%s", "SIGHUP\n");
441 close_log(logfile
); /* gets reopened if necessary by parse_config */
443 parse_config(1); /* reopens log */
444 mmd
->selector_change
= mmd
->selector_num
; /* do not change selector.. */
445 change_selector(); /* .. just reload */
446 init_user_list(user_list_file
); /* reload user list */
448 kill(afs_pid
, SIGHUP
);
451 static void status_refresh(void)
453 static int prev_uptime
= -1, prev_events
= -1;
454 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
456 if (prev_events
!= mmd
->events
)
458 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
460 if (uptime
/ 60 != prev_uptime
/ 60)
464 prev_uptime
= uptime
;
465 prev_events
= mmd
->events
;
466 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
468 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
469 mmd
->events
, mmd
->audio_format
);
475 * the main function of para_server
477 * \param argc usual argument count
478 * \param argv usual argument vector
480 * \return EXIT_SUCCESS or EXIT_FAILURE
483 int main(int argc
, char *argv
[])
485 /* listen on sock_fd, new connection on new_fd */
487 struct sockaddr_in their_addr
;
488 int i
, max_fileno
, ret
;
491 struct timeval
*timeout
;
494 sockfd
= do_inits(argc
, argv
);
499 /* check socket and signal pipe in any case */
500 para_fd_set(sockfd
, &rfds
, &max_fileno
);
501 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
502 timeout
= vss_preselect();
504 for (i
= 0; senders
[i
].name
; i
++) {
505 if (senders
[i
].status
!= SENDER_ON
)
507 if (!senders
[i
].pre_select
)
509 senders
[i
].pre_select( &max_fileno
, &rfds
, &wfds
);
511 if (selectors
[mmd
->selector_num
].pre_select
) {
512 ret
= selectors
[mmd
->selector_num
].pre_select(&rfds
, &wfds
);
513 max_fileno
= PARA_MAX(max_fileno
, ret
);
516 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, timeout
);
518 if (mmd
->selector_change
>= 0)
520 if (selectors
[mmd
->selector_num
].post_select
)
521 selectors
[mmd
->selector_num
].post_select(&rfds
, &wfds
);
524 for (i
= 0; senders
[i
].name
; i
++) {
525 if (senders
[i
].status
!= SENDER_ON
)
527 if (!senders
[i
].post_select
)
529 senders
[i
].post_select(&rfds
, &wfds
);
533 if (FD_ISSET(signal_pipe
, &rfds
)) {
536 sig
= para_next_signal();
543 ret
= para_reap_child(&pid
);
548 PARA_EMERG_LOG("fatal: afs died\n");
552 /* die on sigint/sigterm. Kill all children too. */
555 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
558 selectors
[mmd
->selector_num
].shutdown();
559 mutex_destroy(mmd_mutex
);
561 shm_destroy(mmd_shm_id
);
566 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
567 int num
= mmd
->sender_cmd_data
.cmd_num
,
568 s
= mmd
->sender_cmd_data
.sender_num
;
570 if (senders
[s
].client_cmds
[num
])
571 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
572 mmd
->sender_cmd_data
.cmd_num
= -1;
574 if (!FD_ISSET(sockfd
, &rfds
))
577 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
580 PARA_INFO_LOG("got connection from %s, forking\n",
581 inet_ntoa(their_addr
.sin_addr
));
583 mmd
->active_connections
++;
587 PARA_CRIT_LOG("%s", "fork failed\n");
592 /* parent keeps accepting connections */
595 alarm(ALARM_TIMEOUT
);
597 close(sockfd
); /* child doesn't need the listener */
599 * put info on who we are serving into argv[0] to make
600 * client ip visible in top/ps
602 for (i
= argc
- 1; i
>= 0; i
--)
603 memset(argv
[i
], 0, strlen(argv
[i
]));
604 sprintf(argv
[0], "para_server (serving %s)",
605 inet_ntoa(their_addr
.sin_addr
));
606 return handle_connect(new_fd
, &their_addr
);