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. */
11 * \mainpage Paraslash API Reference
13 * Starting points for getting an overview are
17 * - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
18 * \ref audioc.c, \ref fsck.c,
19 * - Server: \ref server_command, \ref sender,
20 * - Audio file selector: \ref audio_format_handler, \ref mood, \ref afs_table,
21 * - Client: \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
24 * The gory details, listed by topic:
26 * - Audio format handlers: \ref mp3_afh.c, \ref ogg_afh.c, \ref aac_afh.c,
27 * - Decoders: \ref mp3dec.c, \ref oggdec.c, \ref aacdec.c,
28 * - Volume normalizer: \ref compress.c,
29 * - Output: \ref alsa_write.c, \ref osx_write.c,
30 * - http: \ref http_recv.c, \ref http_send.c,
31 * - ortp: \ref ortp_recv.c, \ref ortp_send.c,
32 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
33 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
34 * - Afs structures: \ref afs_table, \ref audio_file_data,
35 * \ref afs_info \ref afh_info,
36 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
37 * \ref attribute.c, \ref score.c,
38 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
42 * - Scheduling: \ref sched.c, \ref sched.h,
43 * - Networking: \ref net.c,
44 * - File descriptors: \ref fd.c,
45 * - Signals: \ref signal.c,
46 * - Daemons: \ref daemon.c,
47 * - Strings: \ref string.c, \ref string.h,
48 * - Time: \ref time.c,
49 * - Spawning processes: \ref exec.c,
50 * - Inter process communication: \ref ipc.c,
51 * - The object storage layer: \ref osl.c,
52 * - Blob tables: \ref blob.c,
53 * - The error subssystem: \ref error.h.
55 * Low-level data structures:
57 * - Doubly linked lists: \ref list.h,
58 * - Red-black trees: \ref rbtree.h, \ref rbtree.c,
59 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
60 * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c,
61 * - Crypto: \ref crypt.c.
66 #include <sys/types.h>
71 #include "server.cmdline.h"
78 #include "close_on_fork.h"
87 #include "user_list.h"
89 /** define the array of error lists needed by para_server */
92 /** shut down non-authorized connections after that many seconds */
93 #define ALARM_TIMEOUT 10
96 * Pointer to shared memory area for communication between para_server
97 * and its children. Exported to vss.c. command.c and to afs.
99 struct misc_meta_data
*mmd
;
102 * the configuration of para_server
104 * It also contains the options for the audio file selector, audio format
105 * handler and all supported senders.
107 struct server_args_info conf
;
109 /** the file containing user information (public key, permissions) */
110 char *user_list_file
= NULL
;
112 extern void dccp_send_init(struct sender
*);
113 extern void http_send_init(struct sender
*);
114 extern void ortp_send_init(struct sender
*);
116 /** the list of supported senders */
117 struct sender senders
[] = {
120 .init
= http_send_init
,
124 .init
= dccp_send_init
,
129 .init
= ortp_send_init
,
138 /* global variables for server-internal use */
139 static FILE *logfile
;
140 static int mmd_mutex
, mmd_shm_id
;
141 static int signal_pipe
;
144 * para_server's log function
146 * \param ll the log level
147 * \param fmt the format string describing the log message
149 void para_log(int ll
, const char* fmt
,...)
155 char str
[MAXLINE
] = "";
158 if (ll
< conf
.loglevel_arg
)
160 outfd
= logfile
? logfile
: stderr
;
163 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
164 fprintf(outfd
, "%s ", str
);
165 if (conf
.loglevel_arg
<= INFO
)
166 fprintf(outfd
, "%i: ", ll
);
168 if (conf
.loglevel_arg
<= INFO
)
169 fprintf(outfd
, "(%d) ", (int)mypid
);
171 vfprintf(outfd
, fmt
, argp
);
176 * setup shared memory area and get mutex for locking
178 static void shm_init(void)
181 int ret
= shm_new(sizeof(struct misc_meta_data
));
186 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
198 mmd
->num_commands
= 0;
200 mmd
->num_connects
= 0;
201 mmd
->active_connections
= 0;
202 mmd
->vss_status_flags
= VSS_NEXT
;
203 mmd
->new_vss_status_flags
= VSS_NEXT
;
204 mmd
->sender_cmd_data
.cmd_num
= -1;
207 PARA_EMERG_LOG("%s", para_strerror(-ret
));
212 * lock the shared memory area containing the mmd struct
214 * \sa semop(2), struct misc_meta_data.
218 mutex_lock(mmd_mutex
);
222 * unlock the shared memory area containing the mmd struct
224 * \sa semop(2), struct misc_meta_data
227 void mmd_unlock(void)
229 mutex_unlock(mmd_mutex
);
232 static void parse_config(int override
)
234 char *home
= para_homedir();
239 if (conf
.config_file_given
)
240 cf
= para_strdup(conf
.config_file_arg
);
242 cf
= make_message("%s/.paraslash/server.conf", home
);
243 free(user_list_file
);
244 if (!conf
.user_list_given
)
245 user_list_file
= make_message("%s/.paraslash/server.users", home
);
247 user_list_file
= para_strdup(conf
.user_list_arg
);
248 ret
= stat(cf
, &statbuf
);
249 if (ret
&& conf
.config_file_given
) {
251 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
255 int tmp
= conf
.daemon_given
;
256 struct server_cmdline_parser_params params
= {
257 .override
= override
,
262 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
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
);
279 free(user_list_file
);
280 user_list_file
= NULL
;
284 static void setup_signal_handling(void)
286 signal_pipe
= para_signal_init(); /* always successful */
288 PARA_NOTICE_LOG("setting up signal handlers\n");
289 if (para_install_sighandler(SIGINT
) < 0)
291 if (para_install_sighandler(SIGTERM
) < 0)
293 if (para_install_sighandler(SIGHUP
) < 0)
295 if (para_install_sighandler(SIGCHLD
) < 0)
297 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
299 if (signal(SIGUSR1
, SIG_IGN
) == SIG_ERR
)
301 add_close_on_fork_list(signal_pipe
);
304 PARA_EMERG_LOG("could not install signal handlers\n");
308 static unsigned init_network(void)
310 int fd
, ret
= para_listen(AF_UNSPEC
, IPPROTO_TCP
, conf
.port_arg
);
315 ret
= mark_fd_nonblocking(fd
);
318 add_close_on_fork_list(fd
); /* child doesn't need the listener */
321 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
325 static void init_random_seed(void)
329 size_t len
= sizeof(unsigned int);
331 fd
= open("/dev/urandom", O_RDONLY
);
335 if (read(fd
, &seed
, len
) != len
)
344 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
349 uint32_t afs_socket_cookie
;
351 static pid_t afs_pid
;
353 static void init_afs(void)
355 int ret
, afs_server_socket
[2];
357 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
360 afs_socket_cookie
= para_random((uint32_t)-1);
364 if (!afs_pid
) { /* child (afs) */
365 close(afs_server_socket
[0]);
366 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
368 close(afs_server_socket
[1]);
369 afs_socket
= afs_server_socket
[0];
370 ret
= mark_fd_nonblocking(afs_socket
);
373 add_close_on_fork_list(afs_socket
);
374 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket
,
375 (unsigned) afs_socket_cookie
);
379 static unsigned server_init(int argc
, char **argv
)
381 /* connector's address information */
385 /* parse command line options */
386 server_cmdline_parser(argc
, argv
, &conf
);
387 HANDLE_VERSION_FLAG("server", conf
);
388 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
389 /* parse config file, open log and set defaults */
391 log_welcome("para_server", conf
.loglevel_arg
);
392 shm_init(); /* init mmd struct */
393 server_uptime(UPTIME_SET
); /* reset server uptime */
394 init_user_list(user_list_file
);
396 if (conf
.daemon_given
)
398 PARA_NOTICE_LOG("initializing audio format handlers\n");
400 PARA_NOTICE_LOG("initializing virtual streaming system\n");
402 mmd
->server_pid
= getpid();
403 setup_signal_handling();
404 PARA_NOTICE_LOG("initializing the audio file selector\n");
407 /* init network socket */
408 PARA_NOTICE_LOG("initializing tcp command socket\n");
409 sockfd
= init_network();
410 PARA_NOTICE_LOG("server init complete\n");
415 * called when server gets SIGHUP or when client invokes hup command.
417 static void handle_sighup(void)
419 PARA_NOTICE_LOG("%s", "SIGHUP\n");
420 close_log(logfile
); /* gets reopened if necessary by parse_config */
422 parse_config(1); /* reopens log */
423 init_user_list(user_list_file
); /* reload user list */
425 kill(afs_pid
, SIGHUP
);
428 static void status_refresh(void)
430 static int prev_uptime
= -1, prev_events
= -1;
431 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
433 if (prev_events
!= mmd
->events
)
435 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
437 if (uptime
/ 60 != prev_uptime
/ 60)
441 prev_uptime
= uptime
;
442 prev_events
= mmd
->events
;
443 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
445 PARA_DEBUG_LOG("%d events, forcing status update\n",
452 * the main function of para_server
454 * \param argc usual argument count
455 * \param argv usual argument vector
457 * \return EXIT_SUCCESS or EXIT_FAILURE
460 int main(int argc
, char *argv
[])
462 /* listen on sock_fd, new connection on new_fd */
465 int i
, max_fileno
, ret
;
468 struct timeval
*timeout
;
471 sockfd
= server_init(argc
, argv
);
476 /* check socket and signal pipe in any case */
477 para_fd_set(sockfd
, &rfds
, &max_fileno
);
478 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
479 timeout
= vss_preselect(&rfds
, &wfds
, &max_fileno
);
481 for (i
= 0; senders
[i
].name
; i
++) {
482 if (senders
[i
].status
!= SENDER_ON
)
484 if (!senders
[i
].pre_select
)
486 senders
[i
].pre_select(&max_fileno
, &rfds
, &wfds
);
489 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, timeout
);
491 vss_post_select(&rfds
, &wfds
);
494 for (i
= 0; senders
[i
].name
; i
++) {
495 if (senders
[i
].status
!= SENDER_ON
)
497 if (!senders
[i
].post_select
)
499 senders
[i
].post_select(&rfds
, &wfds
);
503 if (FD_ISSET(signal_pipe
, &rfds
)) {
506 sig
= para_next_signal();
513 ret
= para_reap_child(&pid
);
518 PARA_EMERG_LOG("fatal: afs died\n");
522 /* die on sigint/sigterm. Kill all children too. */
525 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
528 mutex_destroy(mmd_mutex
);
530 shm_destroy(mmd_shm_id
);
535 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
536 int num
= mmd
->sender_cmd_data
.cmd_num
,
537 s
= mmd
->sender_cmd_data
.sender_num
;
539 if (senders
[s
].client_cmds
[num
])
540 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
541 mmd
->sender_cmd_data
.cmd_num
= -1;
543 if (!FD_ISSET(sockfd
, &rfds
))
546 new_fd
= para_accept(sockfd
, NULL
, 0);
549 peer_name
= remote_name(new_fd
);
550 PARA_INFO_LOG("got connection from %s, forking\n", peer_name
);
552 mmd
->active_connections
++;
556 PARA_CRIT_LOG("%s", "fork failed\n");
561 /* parent keeps accepting connections */
564 alarm(ALARM_TIMEOUT
);
566 para_signal_shutdown();
568 * put info on who we are serving into argv[0] to make
569 * client ip visible in top/ps
571 for (i
= argc
- 1; i
>= 0; i
--)
572 memset(argv
[i
], 0, strlen(argv
[i
]));
573 sprintf(argv
[0], "para_server (serving %s)", peer_name
);
574 return handle_connect(new_fd
, peer_name
);