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
18 #include <sys/types.h>
22 #include "server.cmdline.h"
23 #include "afs_common.h"
28 #include "close_on_fork.h"
39 #include "user_list.h"
42 /** define the array of error lists needed by para_server */
45 /** shut down non-authorized connections after that many seconds */
46 #define ALARM_TIMEOUT 10
49 * pointer to shared memory area for communication between para_server
50 * and its children. exported to vss.c. command.c and to all selectors.
52 struct misc_meta_data
*mmd
;
55 * the configuration of para_server
57 * It also contains the options for all audio file selectors, audio format handler
58 * and all supported senders.
60 struct server_args_info conf
;
62 /** the file containing user information (public key, permissions) */
63 char *user_list_file
= NULL
;
65 extern void dccp_send_init(struct sender
*);
66 extern void http_send_init(struct sender
*);
67 extern void ortp_send_init(struct sender
*);
69 /* TODO: This is better handled by autoconf */
70 /** the list of supported audio file selectors */
71 struct audio_file_selector selectors
[] = {
74 .init
= random_selector_init
,
75 .update_audio_file
= NULL
,
79 .init
= playlist_selector_init
,
80 .update_audio_file
= NULL
,
87 .init
= mysql_selector_init
,
88 .update_audio_file
= NULL
,
98 /** the list of supported senders */
99 struct sender senders
[] = {
102 .init
= http_send_init
,
106 .init
= dccp_send_init
,
111 .init
= ortp_send_init
,
120 /* global variables for server-internal use */
121 static FILE *logfile
;
122 static int mmd_mutex
, mmd_shm_id
;
123 static int signal_pipe
;
126 * para_server's log function
128 * \param ll the log level
129 * \param fmt the format string describing the log message
131 void para_log(int ll
, const char* fmt
,...)
137 char str
[MAXLINE
] = "";
140 if (ll
< conf
.loglevel_arg
)
142 outfd
= logfile
? logfile
: stderr
;
145 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
146 fprintf(outfd
, "%s ", str
);
147 if (conf
.loglevel_arg
<= INFO
)
148 fprintf(outfd
, "%i: ", ll
);
150 if (conf
.loglevel_arg
<= INFO
)
151 fprintf(outfd
, "(%d) ", mypid
);
153 vfprintf(outfd
, fmt
, argp
);
158 * setup shared memory area and get mutex for locking
160 static void shm_init(void)
163 int ret
= shm_new(sizeof(struct misc_meta_data
));
168 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
179 mmd
->selector_num
= 0;
181 mmd
->num_commands
= 0;
183 mmd
->num_connects
= 0;
184 mmd
->active_connections
= 0;
185 strcpy(mmd
->filename
, "(none)");
186 mmd
->audio_format
= -1;
187 mmd
->vss_status_flags
= VSS_NEXT
;
188 mmd
->new_vss_status_flags
= VSS_NEXT
;
189 mmd
->sender_cmd_data
.cmd_num
= -1;
192 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret
));
197 * lock the shared memory area containing the mmd struct
199 * \sa semop(2), struct misc_meta_data
203 mutex_lock(mmd_mutex
);
207 * unlock the shared memory area containing the mmd struct
209 * \sa semop(2), struct misc_meta_data
212 void mmd_unlock(void)
214 mutex_unlock(mmd_mutex
);
217 static void parse_config(int override
)
219 char *home
= para_homedir();
224 if (conf
.config_file_given
)
225 cf
= conf
.config_file_arg
;
227 cf
= make_message("%s/.paraslash/server.conf", home
);
228 free(user_list_file
);
229 if (!conf
.user_list_given
)
230 user_list_file
= make_message("%s/.paraslash/server.users", home
);
232 user_list_file
= para_strdup(conf
.user_list_arg
);
233 ret
= stat(cf
, &statbuf
);
234 if (ret
&& conf
.config_file_given
) {
236 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
240 int tmp
= conf
.daemon_given
;
241 struct server_cmdline_parser_params params
= {
242 .override
= override
,
247 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
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)
333 size_t len
= sizeof(unsigned int);
335 fd
= open("/dev/urandom", O_RDONLY
);
339 if (read(fd
, &seed
, len
) != len
)
348 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
353 uint32_t afs_socket_cookie
;
354 static int afs_socket
;
357 static void init_afs(void)
359 int ret
, afs_server_socket
[2];
361 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
364 afs_socket_cookie
= para_random((uint32_t)-1);
368 if (!afs_pid
) { /* child (afs) */
369 close(afs_server_socket
[0]);
370 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
372 close(afs_server_socket
[1]);
373 afs_socket
= afs_server_socket
[0];
374 ret
= mark_fd_nonblock(afs_socket
);
377 add_close_on_fork_list(afs_socket
);
378 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket
,
379 (unsigned) afs_socket_cookie
);
383 static unsigned do_inits(int argc
, char **argv
)
385 /* connector's address information */
389 /* parse command line options */
390 server_cmdline_parser(argc
, argv
, &conf
);
391 HANDLE_VERSION_FLAG("server", conf
);
392 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
393 /* parse config file, open log and set defaults */
395 log_welcome("para_server", conf
.loglevel_arg
);
396 shm_init(); /* init mmd struct */
397 server_uptime(UPTIME_SET
); /* reset server uptime */
398 init_user_list(user_list_file
);
400 if (conf
.daemon_given
)
403 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
404 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
406 mmd
->server_pid
= getpid();
407 setup_signal_handling();
410 /* init network socket */
411 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
412 sockfd
= init_network();
413 PARA_NOTICE_LOG("%s", "init complete\n");
417 static void change_selector(void)
419 int ret
, old
= mmd
->selector_num
, new = mmd
->selector_change
;
421 selectors
[old
].shutdown();
422 ret
= selectors
[new].init(&selectors
[new]);
423 mmd
->selector_change
= -1; /* reset */
425 mmd
->selector_num
= new;
429 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret
));
430 selectors
[0].init(&selectors
[0]);
431 mmd
->selector_num
= 0;
435 * called when server gets SIGHUP or when client invokes hup command.
437 static void handle_sighup(void)
439 PARA_NOTICE_LOG("%s", "SIGHUP\n");
440 close_log(logfile
); /* gets reopened if necessary by parse_config */
442 parse_config(1); /* reopens log */
443 mmd
->selector_change
= mmd
->selector_num
; /* do not change selector.. */
444 change_selector(); /* .. just reload */
445 init_user_list(user_list_file
); /* reload user list */
448 static void status_refresh(void)
450 static int prev_uptime
= -1, prev_events
= -1;
451 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
453 if (prev_events
!= mmd
->events
)
455 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
457 if (uptime
/ 60 != prev_uptime
/ 60)
461 prev_uptime
= uptime
;
462 prev_events
= mmd
->events
;
463 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
465 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
466 mmd
->events
, mmd
->audio_format
);
472 * the main function of para_server
474 * \param argc usual argument count
475 * \param argv usual argument vector
477 * \return EXIT_SUCCESS or EXIT_FAILURE
480 int main(int argc
, char *argv
[])
482 /* listen on sock_fd, new connection on new_fd */
484 struct sockaddr_in their_addr
;
485 int i
, max_fileno
, ret
;
488 struct timeval
*timeout
;
491 sockfd
= do_inits(argc
, argv
);
496 /* check socket and signal pipe in any case */
497 para_fd_set(sockfd
, &rfds
, &max_fileno
);
498 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
499 timeout
= vss_preselect();
501 for (i
= 0; senders
[i
].name
; i
++) {
502 if (senders
[i
].status
!= SENDER_ON
)
504 if (!senders
[i
].pre_select
)
506 senders
[i
].pre_select( &max_fileno
, &rfds
, &wfds
);
508 if (selectors
[mmd
->selector_num
].pre_select
) {
509 ret
= selectors
[mmd
->selector_num
].pre_select(&rfds
, &wfds
);
510 max_fileno
= PARA_MAX(max_fileno
, ret
);
513 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, timeout
);
515 if (mmd
->selector_change
>= 0)
517 if (selectors
[mmd
->selector_num
].post_select
)
518 selectors
[mmd
->selector_num
].post_select(&rfds
, &wfds
);
521 for (i
= 0; senders
[i
].name
; i
++) {
522 if (senders
[i
].status
!= SENDER_ON
)
524 if (!senders
[i
].post_select
)
526 senders
[i
].post_select(&rfds
, &wfds
);
530 if (FD_ISSET(signal_pipe
, &rfds
)) {
533 sig
= para_next_signal();
540 pid
= para_reap_child();
545 PARA_EMERG_LOG("fatal: afs died\n");
549 /* die on sigint/sigterm. Kill all children too. */
552 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
555 selectors
[mmd
->selector_num
].shutdown();
556 mutex_destroy(mmd_mutex
);
558 shm_destroy(mmd_shm_id
);
563 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
564 int num
= mmd
->sender_cmd_data
.cmd_num
,
565 s
= mmd
->sender_cmd_data
.sender_num
;
567 if (senders
[s
].client_cmds
[num
])
568 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
569 mmd
->sender_cmd_data
.cmd_num
= -1;
571 if (!FD_ISSET(sockfd
, &rfds
))
574 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
577 PARA_INFO_LOG("got connection from %s, forking\n",
578 inet_ntoa(their_addr
.sin_addr
));
580 mmd
->active_connections
++;
584 PARA_CRIT_LOG("%s", "fork failed\n");
589 /* parent keeps accepting connections */
592 alarm(ALARM_TIMEOUT
);
594 close(sockfd
); /* child doesn't need the listener */
596 * put info on who we are serving into argv[0] to make
597 * client ip visible in top/ps
599 for (i
= argc
- 1; i
>= 0; i
--)
600 memset(argv
[i
], 0, strlen(argv
[i
]));
601 sprintf(argv
[0], "para_server (serving %s)",
602 inet_ntoa(their_addr
.sin_addr
));
603 return handle_connect(new_fd
, &their_addr
);