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>
24 #include "server.cmdline.h"
25 #include "afs_common.h"
32 #include "close_on_fork.h"
41 #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 /** the list of supported senders */
71 struct sender senders
[] = {
74 .init
= http_send_init
,
78 .init
= dccp_send_init
,
83 .init
= ortp_send_init
,
92 /* global variables for server-internal use */
94 static int mmd_mutex
, mmd_shm_id
;
95 static int signal_pipe
;
98 * para_server's log function
100 * \param ll the log level
101 * \param fmt the format string describing the log message
103 void para_log(int ll
, const char* fmt
,...)
109 char str
[MAXLINE
] = "";
112 if (ll
< conf
.loglevel_arg
)
114 outfd
= logfile
? logfile
: stderr
;
117 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
118 fprintf(outfd
, "%s ", str
);
119 if (conf
.loglevel_arg
<= INFO
)
120 fprintf(outfd
, "%i: ", ll
);
122 if (conf
.loglevel_arg
<= INFO
)
123 fprintf(outfd
, "(%d) ", mypid
);
125 vfprintf(outfd
, fmt
, argp
);
130 * setup shared memory area and get mutex for locking
132 static void shm_init(void)
135 int ret
= shm_new(sizeof(struct misc_meta_data
));
140 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
151 mmd
->selector_num
= 0;
153 mmd
->num_commands
= 0;
155 mmd
->num_connects
= 0;
156 mmd
->active_connections
= 0;
157 strcpy(mmd
->filename
, "(none)");
158 mmd
->audio_format
= -1;
159 mmd
->vss_status_flags
= VSS_NEXT
;
160 mmd
->new_vss_status_flags
= VSS_NEXT
;
161 mmd
->sender_cmd_data
.cmd_num
= -1;
164 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret
));
169 * lock the shared memory area containing the mmd struct
171 * \sa semop(2), struct misc_meta_data
175 mutex_lock(mmd_mutex
);
179 * unlock the shared memory area containing the mmd struct
181 * \sa semop(2), struct misc_meta_data
184 void mmd_unlock(void)
186 mutex_unlock(mmd_mutex
);
189 static void parse_config(int override
)
191 char *home
= para_homedir();
196 if (conf
.config_file_given
)
197 cf
= conf
.config_file_arg
;
199 cf
= make_message("%s/.paraslash/server.conf", home
);
200 free(user_list_file
);
201 if (!conf
.user_list_given
)
202 user_list_file
= make_message("%s/.paraslash/server.users", home
);
204 user_list_file
= para_strdup(conf
.user_list_arg
);
205 ret
= stat(cf
, &statbuf
);
206 if (ret
&& conf
.config_file_given
) {
208 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
212 int tmp
= conf
.daemon_given
;
213 struct server_cmdline_parser_params params
= {
214 .override
= override
,
219 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
220 conf
.daemon_given
= tmp
;
223 if (!conf
.logfile_given
&& conf
.daemon_given
) {
225 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
228 if (conf
.logfile_given
)
229 logfile
= open_log(conf
.logfile_arg
);
236 free(user_list_file
);
237 user_list_file
= NULL
;
241 static void setup_signal_handling(void)
245 signal_pipe
= para_signal_init();
246 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
247 ret
+= para_install_sighandler(SIGINT
);
248 ret
+= para_install_sighandler(SIGTERM
);
249 ret
+= para_install_sighandler(SIGHUP
);
250 ret
+= para_install_sighandler(SIGCHLD
);
251 ret
+= para_install_sighandler(SIGUSR1
);
252 signal(SIGPIPE
, SIG_IGN
);
254 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
259 static unsigned init_network(void)
261 int fd
, ret
= init_tcp_socket(conf
.port_arg
);
266 ret
= mark_fd_nonblock(fd
);
271 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
275 static void init_random_seed(void)
279 size_t len
= sizeof(unsigned int);
281 fd
= open("/dev/urandom", O_RDONLY
);
285 if (read(fd
, &seed
, len
) != len
)
294 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
299 uint32_t afs_socket_cookie
;
301 static pid_t afs_pid
;
303 static void init_afs(void)
305 int ret
, afs_server_socket
[2];
307 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
310 afs_socket_cookie
= para_random((uint32_t)-1);
314 if (!afs_pid
) { /* child (afs) */
315 close(afs_server_socket
[0]);
316 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
318 close(afs_server_socket
[1]);
319 afs_socket
= afs_server_socket
[0];
320 ret
= mark_fd_nonblock(afs_socket
);
323 add_close_on_fork_list(afs_socket
);
324 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket
,
325 (unsigned) afs_socket_cookie
);
329 static unsigned do_inits(int argc
, char **argv
)
331 /* connector's address information */
335 /* parse command line options */
336 server_cmdline_parser(argc
, argv
, &conf
);
337 HANDLE_VERSION_FLAG("server", conf
);
338 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
339 /* parse config file, open log and set defaults */
341 log_welcome("para_server", conf
.loglevel_arg
);
342 shm_init(); /* init mmd struct */
343 server_uptime(UPTIME_SET
); /* reset server uptime */
344 init_user_list(user_list_file
);
346 if (conf
.daemon_given
)
349 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
350 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
353 mmd
->server_pid
= getpid();
354 setup_signal_handling();
357 /* init network socket */
358 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
359 sockfd
= init_network();
360 PARA_NOTICE_LOG("%s", "init complete\n");
365 * called when server gets SIGHUP or when client invokes hup command.
367 static void handle_sighup(void)
369 PARA_NOTICE_LOG("%s", "SIGHUP\n");
370 close_log(logfile
); /* gets reopened if necessary by parse_config */
372 parse_config(1); /* reopens log */
373 init_user_list(user_list_file
); /* reload user list */
375 kill(afs_pid
, SIGHUP
);
378 static void status_refresh(void)
380 static int prev_uptime
= -1, prev_events
= -1;
381 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
383 if (prev_events
!= mmd
->events
)
385 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
387 if (uptime
/ 60 != prev_uptime
/ 60)
391 prev_uptime
= uptime
;
392 prev_events
= mmd
->events
;
393 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
395 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
396 mmd
->events
, mmd
->audio_format
);
402 * the main function of para_server
404 * \param argc usual argument count
405 * \param argv usual argument vector
407 * \return EXIT_SUCCESS or EXIT_FAILURE
410 int main(int argc
, char *argv
[])
412 /* listen on sock_fd, new connection on new_fd */
414 struct sockaddr_in their_addr
;
415 int i
, max_fileno
, ret
;
418 struct timeval
*timeout
;
421 sockfd
= do_inits(argc
, argv
);
426 /* check socket and signal pipe in any case */
427 para_fd_set(sockfd
, &rfds
, &max_fileno
);
428 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
429 timeout
= vss_preselect(&rfds
, &wfds
, &max_fileno
);
431 for (i
= 0; senders
[i
].name
; i
++) {
432 if (senders
[i
].status
!= SENDER_ON
)
434 if (!senders
[i
].pre_select
)
436 senders
[i
].pre_select(&max_fileno
, &rfds
, &wfds
);
439 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, timeout
);
441 vss_post_select(&rfds
, &wfds
);
444 for (i
= 0; senders
[i
].name
; i
++) {
445 if (senders
[i
].status
!= SENDER_ON
)
447 if (!senders
[i
].post_select
)
449 senders
[i
].post_select(&rfds
, &wfds
);
453 if (FD_ISSET(signal_pipe
, &rfds
)) {
456 sig
= para_next_signal();
463 ret
= para_reap_child(&pid
);
468 PARA_EMERG_LOG("fatal: afs died\n");
472 /* die on sigint/sigterm. Kill all children too. */
475 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
478 mutex_destroy(mmd_mutex
);
480 shm_destroy(mmd_shm_id
);
485 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
486 int num
= mmd
->sender_cmd_data
.cmd_num
,
487 s
= mmd
->sender_cmd_data
.sender_num
;
489 if (senders
[s
].client_cmds
[num
])
490 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
491 mmd
->sender_cmd_data
.cmd_num
= -1;
493 if (!FD_ISSET(sockfd
, &rfds
))
496 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
499 PARA_INFO_LOG("got connection from %s, forking\n",
500 inet_ntoa(their_addr
.sin_addr
));
502 mmd
->active_connections
++;
506 PARA_CRIT_LOG("%s", "fork failed\n");
511 /* parent keeps accepting connections */
514 alarm(ALARM_TIMEOUT
);
516 close(sockfd
); /* child doesn't need the listener */
518 * put info on who we are serving into argv[0] to make
519 * client ip visible in top/ps
521 for (i
= argc
- 1; i
>= 0; i
--)
522 memset(argv
[i
], 0, strlen(argv
[i
]));
523 sprintf(argv
[0], "para_server (serving %s)",
524 inet_ntoa(their_addr
.sin_addr
));
525 return handle_connect(new_fd
, &their_addr
);