btr: Rename btr_bytes_pending() and make it public.
[paraslash.git] / server.c
1 /*
2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file server.c Paraslash's main server. */
8
9
10 /**
11 * \mainpage Paraslash API Reference
12 *
13 * Starting points for getting an overview:
14 *
15 *
16 * - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
17 * \ref audioc.c, \ref afh.c
18 * - Server: \ref server_command, \ref sender,
19 * - Audio file selector: \ref audio_format_handler, \ref mood, \ref afs_table,
20 * - Client: \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
21 *
22 *
23 * The gory details, listed by topic:
24 *
25 * - Audio format handlers: \ref send_common.c \ref mp3_afh.c,
26 * \ref ogg_afh.c, \ref aac_afh.c, \ref wma_afh.c,
27 * - Decoders: \ref mp3dec_filter.c, \ref oggdec_filter.c,
28 * \ref aacdec_filter.c, \ref wmadec_filter.c,
29 * - Volume normalizer: \ref compress_filter.c,
30 * - Output: \ref alsa_write.c, \ref osx_write.c, \ref oss_write.c,
31 * - http: \ref http_recv.c, \ref http_send.c,
32 * - udp: \ref udp_recv.c, \ref udp_send.c,
33 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
34 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
35 * - Afs structures: \ref afs_table, \ref audio_file_data,
36 * \ref afs_info \ref afh_info,
37 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
38 * \ref attribute.c, \ref score.c,
39 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
40 *
41 * Lower levels:
42 *
43 * - Scheduling: \ref sched.c, \ref sched.h,
44 * - Networking: \ref net.c,
45 * - File descriptors: \ref fd.c,
46 * - Signals: \ref signal.c,
47 * - Daemons: \ref daemon.c,
48 * - Strings: \ref string.c, \ref string.h,
49 * - Time: \ref time.c,
50 * - Spawning processes: \ref exec.c,
51 * - Inter process communication: \ref ipc.c,
52 * - Blob tables: \ref blob.c,
53 * - The error subssystem: \ref error.h.
54 * - Access control for paraslash senders: \ref acl.c, \ref acl.h.
55 *
56 * Low-level data structures:
57 *
58 * - Doubly linked lists: \ref list.h,
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.
62 * - Forward error correction: \ref fec.c.
63 */
64
65 #include <signal.h>
66 #include <dirent.h>
67 #include <sys/time.h>
68 #include <openssl/rc4.h>
69 #include <regex.h>
70 #include <osl.h>
71
72 #include "para.h"
73 #include "error.h"
74 #include "crypt.h"
75 #include "server.cmdline.h"
76 #include "afh.h"
77 #include "string.h"
78 #include "afs.h"
79 #include "server.h"
80 #include "vss.h"
81 #include "config.h"
82 #include "close_on_fork.h"
83 #include "list.h"
84 #include "send.h"
85 #include "net.h"
86 #include "daemon.h"
87 #include "ipc.h"
88 #include "fd.h"
89 #include "sched.h"
90 #include "signal.h"
91 #include "user_list.h"
92 #include "color.h"
93
94 /** Define the array of error lists needed by para_server. */
95 INIT_SERVER_ERRLISTS;
96
97 /** Shut down non-authorized connections after that many seconds. */
98 #define ALARM_TIMEOUT 10
99
100 /**
101 * Pointer to shared memory area for communication between para_server
102 * and its children. Exported to vss.c. command.c and to afs.
103 */
104 struct misc_meta_data *mmd;
105
106 /**
107 * The configuration of para_server
108 *
109 * It also contains the options for the audio file selector, audio format
110 * handler and all supported senders.
111 */
112 struct server_args_info conf;
113
114 /** A random value used in child context for authentication. */
115 uint32_t afs_socket_cookie;
116
117 /** The mutex protecting the shared memory area containing the mmd struct. */
118 int mmd_mutex;
119
120 /** The file containing user information (public key, permissions). */
121 static char *user_list_file = NULL;
122
123
124 /** The task responsible for server command handling. */
125 struct server_command_task {
126 /** TCP port on which para_server listens for connections. */
127 int listen_fd;
128 /** Copied from para_server's main function. */
129 int argc;
130 /** Argument vector passed to para_server's main function. */
131 char **argv;
132 /** The command task structure for scheduling. */
133 struct task task;
134 };
135
136 static int want_colors(void)
137 {
138 if (conf.color_arg == color_arg_no)
139 return 0;
140 if (conf.color_arg == color_arg_yes)
141 return 1;
142 if (conf.logfile_given)
143 return 0;
144 return isatty(STDERR_FILENO);
145 }
146
147 static void init_colors_or_die(void)
148 {
149 int ret, i;
150
151 if (!want_colors())
152 return;
153 daemon_set_flag(DF_COLOR_LOG);
154 daemon_set_default_log_colors();
155 for (i = 0; i < conf.log_color_given; i++) {
156 ret = daemon_set_log_color(conf.log_color_arg[i]);
157 if (ret < 0)
158 exit(EXIT_FAILURE);
159 }
160 }
161
162 /*
163 * setup shared memory area and get mutex for locking
164 */
165 static void init_ipc_or_die(void)
166 {
167 void *shm;
168 int shmid, ret = shm_new(sizeof(struct misc_meta_data));
169
170 if (ret < 0)
171 goto err_out;
172 shmid = ret;
173 ret = shm_attach(shmid, ATTACH_RW, &shm);
174 shm_destroy(shmid);
175 if (ret < 0)
176 goto err_out;
177 mmd = shm;
178
179 ret = mutex_new();
180 if (ret < 0)
181 goto err_out;
182 mmd_mutex = ret;
183
184 mmd->num_played = 0;
185 mmd->num_commands = 0;
186 mmd->events = 0;
187 mmd->num_connects = 0;
188 mmd->active_connections = 0;
189 mmd->vss_status_flags = VSS_NEXT;
190 mmd->new_vss_status_flags = VSS_NEXT;
191 return;
192 err_out:
193 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
194 exit(EXIT_FAILURE);
195 }
196
197 /**
198 * (Re-)read the server configuration files.
199 *
200 * \param override Passed to gengetopt to activate the override feature.
201 *
202 * This function also re-opens the logfile and sets the global \a
203 * user_list_file variable.
204 */
205 void parse_config_or_die(int override)
206 {
207 char *home = para_homedir();
208 int ret;
209 char *cf;
210
211 daemon_close_log();
212 if (conf.config_file_given)
213 cf = para_strdup(conf.config_file_arg);
214 else
215 cf = make_message("%s/.paraslash/server.conf", home);
216 free(user_list_file);
217 if (!conf.user_list_given)
218 user_list_file = make_message("%s/.paraslash/server.users", home);
219 else
220 user_list_file = para_strdup(conf.user_list_arg);
221 ret = file_exists(cf);
222 if (conf.config_file_given && !ret) {
223 ret = -1;
224 PARA_EMERG_LOG("can not read config file %s\n", cf);
225 goto out;
226 }
227 if (ret) {
228 int tmp = conf.daemon_given;
229 struct server_cmdline_parser_params params = {
230 .override = override,
231 .initialize = 0,
232 .check_required = 1,
233 .check_ambiguity = 0,
234 .print_errors = !conf.daemon_given
235 };
236 server_cmdline_parser_config_file(cf, &conf, &params);
237 conf.daemon_given = tmp;
238 }
239 if (conf.logfile_given) {
240 daemon_set_logfile(conf.logfile_arg);
241 daemon_open_log_or_die();
242 }
243 daemon_set_loglevel(conf.loglevel_arg);
244 init_colors_or_die();
245 daemon_set_flag(DF_LOG_PID);
246 daemon_set_flag(DF_LOG_LL);
247 daemon_set_flag(DF_LOG_TIME);
248 if (conf.log_timing_given)
249 daemon_set_flag(DF_LOG_TIMING);
250 ret = 1;
251 out:
252 free(cf);
253 free(home);
254 if (ret > 0)
255 return;
256 free(user_list_file);
257 user_list_file = NULL;
258 exit(EXIT_FAILURE);
259 }
260
261 static void signal_pre_select(struct sched *s, struct task *t)
262 {
263 struct signal_task *st = container_of(t, struct signal_task, task);
264 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
265 }
266
267 /*
268 * called when server gets SIGHUP or when client invokes hup command.
269 */
270 static void handle_sighup(void)
271 {
272 PARA_NOTICE_LOG("SIGHUP\n");
273 parse_config_or_die(1); /* reopens log */
274 init_user_list(user_list_file); /* reload user list */
275 if (mmd->afs_pid)
276 kill(mmd->afs_pid, SIGHUP);
277 }
278
279 static void signal_post_select(struct sched *s, struct task *t)
280 {
281 struct signal_task *st = container_of(t, struct signal_task, task);
282
283 if (!FD_ISSET(st->fd, &s->rfds))
284 return;
285
286 st->signum = para_next_signal();
287 switch (st->signum) {
288 case SIGHUP:
289 handle_sighup();
290 break;
291 case SIGCHLD:
292 for (;;) {
293 pid_t pid;
294 int ret = para_reap_child(&pid);
295 if (ret <= 0)
296 break;
297 if (pid != mmd->afs_pid)
298 continue;
299 PARA_EMERG_LOG("fatal: afs died\n");
300 kill(0, SIGTERM);
301 goto cleanup;
302 }
303 break;
304 /* die on sigint/sigterm. Kill all children too. */
305 case SIGINT:
306 case SIGTERM:
307 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
308 kill(0, SIGTERM);
309 /*
310 * We must wait for afs because afs catches SIGINT/SIGTERM.
311 * Before reacting to the signal, afs might want to use the
312 * shared memory area and the mmd mutex. If we destroy this
313 * mutex too early and afs tries to lock the shared memory
314 * area, the call to mutex_lock() will fail and terminate the
315 * afs process. This leads to dirty osl tables.
316 *
317 * There's no such problem with the other children of the
318 * server process (the command handlers) as these reset their
319 * SIGINT/SIGTERM handlers to the default action, i.e. these
320 * processes get killed immediately by the above kill().
321 */
322 PARA_INFO_LOG("waiting for afs (pid %d) to die\n",
323 (int)mmd->afs_pid);
324 waitpid(mmd->afs_pid, NULL, 0);
325 cleanup:
326 free(mmd->afd.afhi.chunk_table);
327 close_listed_fds();
328 mutex_destroy(mmd_mutex);
329 shm_detach(mmd);
330 exit(EXIT_FAILURE);
331 }
332 }
333
334 static void init_signal_task(void)
335 {
336 static struct signal_task signal_task_struct,
337 *st = &signal_task_struct;
338
339 st->task.pre_select = signal_pre_select;
340 st->task.post_select = signal_post_select;
341 sprintf(st->task.status, "signal task");
342
343 PARA_NOTICE_LOG("setting up signal handling\n");
344 st->fd = para_signal_init(); /* always successful */
345 para_install_sighandler(SIGINT);
346 para_install_sighandler(SIGTERM);
347 para_install_sighandler(SIGHUP);
348 para_install_sighandler(SIGCHLD);
349 para_sigaction(SIGPIPE, SIG_IGN);
350 add_close_on_fork_list(st->fd);
351 register_task(&st->task);
352 }
353
354 static void command_pre_select(struct sched *s, struct task *t)
355 {
356 struct server_command_task *sct = container_of(t, struct server_command_task, task);
357 para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
358 }
359
360 static void command_post_select(struct sched *s, struct task *t)
361 {
362 struct server_command_task *sct = container_of(t, struct server_command_task, task);
363
364 int new_fd, ret, i;
365 char *peer_name;
366 pid_t child_pid;
367 uint32_t *chunk_table;
368
369 if (!FD_ISSET(sct->listen_fd, &s->rfds))
370 return;
371 ret = para_accept(sct->listen_fd, NULL, 0);
372 if (ret < 0)
373 goto out;
374 new_fd = ret;
375 peer_name = remote_name(new_fd);
376 PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
377 mmd->num_connects++;
378 mmd->active_connections++;
379 /*
380 * The chunk table is a pointer located in the mmd struct that points
381 * to dynamically allocated memory, i.e. it must be freed by the parent
382 * and the child. However, as the mmd struct is in a shared memory
383 * area, there's no guarantee that after the fork this pointer is still
384 * valid in child context. As it is not used in the child anyway, we
385 * save it to a local variable before the fork and free the memory via
386 * that copy in the child directly after the fork.
387 */
388 chunk_table = mmd->afd.afhi.chunk_table;
389 child_pid = fork();
390 if (child_pid < 0) {
391 ret = -ERRNO_TO_PARA_ERROR(errno);
392 goto out;
393 }
394 if (child_pid) {
395 close(new_fd);
396 /* parent keeps accepting connections */
397 return;
398 }
399 /* mmd might already have changed at this point */
400 free(chunk_table);
401 alarm(ALARM_TIMEOUT);
402 close_listed_fds();
403 para_signal_shutdown();
404 /*
405 * put info on who we are serving into argv[0] to make
406 * client ip visible in top/ps
407 */
408 for (i = sct->argc - 1; i >= 0; i--)
409 memset(sct->argv[i], 0, strlen(sct->argv[i]));
410 sprintf(sct->argv[0], "para_server (serving %s)", peer_name);
411 return handle_connect(new_fd, peer_name);
412 out:
413 if (ret < 0)
414 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
415 }
416
417 static void init_server_command_task(int argc, char **argv)
418 {
419 int ret;
420 static struct server_command_task server_command_task_struct,
421 *sct = &server_command_task_struct;
422
423 PARA_NOTICE_LOG("initializing tcp command socket\n");
424 sct->task.pre_select = command_pre_select;
425 sct->task.post_select = command_post_select;
426 sct->argc = argc;
427 sct->argv = argv;
428 ret = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.port_arg);
429 if (ret < 0)
430 goto err;
431 sct->listen_fd = ret;
432 ret = mark_fd_nonblocking(sct->listen_fd);
433 if (ret < 0)
434 goto err;
435 add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */
436 register_task(&sct->task);
437 return;
438 err:
439 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
440 exit(EXIT_FAILURE);
441 }
442
443 static int init_afs(void)
444 {
445 int ret, afs_server_socket[2];
446 pid_t afs_pid;
447
448 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
449 if (ret < 0)
450 exit(EXIT_FAILURE);
451 get_random_bytes_or_die((unsigned char *)&afs_socket_cookie,
452 sizeof(afs_socket_cookie));
453 afs_pid = fork();
454 if (afs_pid < 0)
455 exit(EXIT_FAILURE);
456 if (afs_pid == 0) { /* child (afs) */
457 close(afs_server_socket[0]);
458 afs_init(afs_socket_cookie, afs_server_socket[1]);
459 }
460 mmd->afs_pid = afs_pid;
461 close(afs_server_socket[1]);
462 ret = mark_fd_nonblocking(afs_server_socket[0]);
463 if (ret < 0)
464 exit(EXIT_FAILURE);
465 add_close_on_fork_list(afs_server_socket[0]);
466 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
467 afs_server_socket[0], (unsigned) afs_socket_cookie);
468 return afs_server_socket[0];
469 }
470
471 __noreturn static void tmp_sigchld_handler(__a_unused int s)
472 {
473 PARA_EMERG_LOG("caught early SIGCHLD\n");
474 exit(EXIT_FAILURE);
475 }
476
477 static void server_init(int argc, char **argv)
478 {
479 struct server_cmdline_parser_params params = {
480 .override = 0,
481 .initialize = 1,
482 .check_required = 0,
483 .check_ambiguity = 0,
484 .print_errors = 1
485 };
486 int afs_socket;
487
488 valid_fd_012();
489 init_random_seed_or_die();
490 /* parse command line options */
491 server_cmdline_parser_ext(argc, argv, &conf, &params);
492 HANDLE_VERSION_FLAG("server", conf);
493 drop_privileges_or_die(conf.user_arg, conf.group_arg);
494 /* parse config file, open log and set defaults */
495 parse_config_or_die(0);
496 log_welcome("para_server");
497 init_ipc_or_die(); /* init mmd struct and mmd->lock */
498 /* make sure, the global now pointer is uptodate */
499 gettimeofday(now, NULL);
500 server_uptime(UPTIME_SET); /* reset server uptime */
501 init_user_list(user_list_file);
502 /* become daemon */
503 if (conf.daemon_given)
504 daemonize();
505 PARA_NOTICE_LOG("initializing audio format handlers\n");
506 afh_init();
507
508 /*
509 * Although afs uses its own signal handling we must ignore SIGUSR1
510 * _before_ the afs child process gets born by init_afs() below. It's
511 * racy to do this in the child because the parent might send SIGUSR1
512 * before the child gets a chance to ignore this signal -- only the
513 * good die young.
514 */
515 para_sigaction(SIGUSR1, SIG_IGN);
516 /*
517 * We have to install a SIGCHLD handler before the afs process is being
518 * forked off. Otherwise, para_server does not notice if afs dies before
519 * the SIGCHLD handler has been installed by init_signal_task() below.
520 */
521 para_sigaction(SIGCHLD, tmp_sigchld_handler);
522 PARA_NOTICE_LOG("initializing the audio file selector\n");
523 afs_socket = init_afs();
524 init_signal_task();
525 PARA_NOTICE_LOG("initializing virtual streaming system\n");
526 init_vss_task(afs_socket);
527 init_server_command_task(argc, argv);
528 PARA_NOTICE_LOG("server init complete\n");
529 }
530
531 static void status_refresh(void)
532 {
533 static int prev_uptime = -1, prev_events = -1;
534 int uptime = server_uptime(UPTIME_GET), ret = 1;
535
536 if (prev_events != mmd->events)
537 goto out;
538 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
539 goto out_inc_events;
540 if (uptime / 60 != prev_uptime / 60)
541 goto out_inc_events;
542 return;
543 out_inc_events:
544 mmd->events++;
545 out:
546 prev_uptime = uptime;
547 prev_events = mmd->events;
548 mmd->vss_status_flags = mmd->new_vss_status_flags;
549 if (ret) {
550 PARA_DEBUG_LOG("%d events, forcing status update\n",
551 mmd->events);
552 killpg(0, SIGUSR1);
553 }
554 }
555
556 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
557 struct timeval *timeout_tv)
558 {
559 int ret;
560
561 status_refresh();
562 mutex_unlock(mmd_mutex);
563 ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
564 mutex_lock(mmd_mutex);
565 return ret;
566 }
567
568 /**
569 * The main function of para_server.
570 *
571 * \param argc Usual argument count.
572 * \param argv Usual argument vector.
573 *
574 * \return EXIT_SUCCESS or EXIT_FAILURE.
575 */
576 int main(int argc, char *argv[])
577 {
578 int ret;
579 static struct sched s = {
580 .default_timeout = {
581 .tv_sec = 1,
582 .tv_usec = 0
583 },
584 .select_function = server_select
585 };
586 server_init(argc, argv);
587 mutex_lock(mmd_mutex);
588 ret = schedule(&s);
589 if (ret < 0) {
590 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
591 exit(EXIT_FAILURE);
592 }
593 exit(EXIT_SUCCESS);
594 }