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