Implement colored logging for para_server.
[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 fsck.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, \ref ogg_afh.c, \ref aac_afh.c,
26 * - Decoders: \ref mp3dec_filter.c, \ref oggdec_filter.c, \ref aacdec_filter.c,
27 * - Volume normalizer: \ref compress_filter.c,
28 * - Output: \ref alsa_write.c, \ref osx_write.c,
29 * - http: \ref http_recv.c, \ref http_send.c,
30 * - udp: \ref udp_recv.c, \ref udp_send.c,
31 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
32 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
33 * - Afs structures: \ref afs_table, \ref audio_file_data,
34 * \ref afs_info \ref afh_info,
35 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
36 * \ref attribute.c, \ref score.c,
37 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
38 *
39 * Lower levels:
40 *
41 * - Scheduling: \ref sched.c, \ref sched.h,
42 * - Networking: \ref net.c,
43 * - File descriptors: \ref fd.c,
44 * - Signals: \ref signal.c,
45 * - Daemons: \ref daemon.c,
46 * - Strings: \ref string.c, \ref string.h,
47 * - Time: \ref time.c,
48 * - Spawning processes: \ref exec.c,
49 * - Inter process communication: \ref ipc.c,
50 * - The object storage layer: \ref osl.c,
51 * - Blob tables: \ref blob.c,
52 * - The error subssystem: \ref error.h.
53 * - Access control for paraslash senders: \ref acl.c, \ref acl.h.
54 *
55 * Low-level data structures:
56 *
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.
62 *
63 */
64
65 #include <signal.h>
66 #include <dirent.h>
67 #include <sys/time.h>
68
69 #include "para.h"
70 #include "error.h"
71 #include "server.cmdline.h"
72 #include "afh.h"
73 #include "string.h"
74 #include "afs.h"
75 #include "server.h"
76 #include "vss.h"
77 #include "config.h"
78 #include "close_on_fork.h"
79 #include "list.h"
80 #include "send.h"
81 #include "net.h"
82 #include "daemon.h"
83 #include "ipc.h"
84 #include "fd.h"
85 #include "sched.h"
86 #include "signal.h"
87 #include "user_list.h"
88 #include "color.h"
89
90 /** Define the array of error lists needed by para_server. */
91 INIT_SERVER_ERRLISTS;
92
93 /** Shut down non-authorized connections after that many seconds. */
94 #define ALARM_TIMEOUT 10
95
96 /**
97 * Pointer to shared memory area for communication between para_server
98 * and its children. Exported to vss.c. command.c and to afs.
99 */
100 struct misc_meta_data *mmd;
101
102 /**
103 * The configuration of para_server
104 *
105 * It also contains the options for the audio file selector, audio format
106 * handler and all supported senders.
107 */
108 struct server_args_info conf;
109
110 /** A random value used in child context for authentication. */
111 uint32_t afs_socket_cookie;
112
113 /** The mutex protecting the shared memory area containing the mmd struct. */
114 int mmd_mutex;
115
116 /* global variables for server-internal use */
117 static FILE *logfile;
118 /** The file containing user information (public key, permissions). */
119 static char *user_list_file = NULL;
120 static int mmd_shm_id;
121
122
123 /** The task responsible for server command handling. */
124 struct server_command_task {
125 /** TCP port on which para_server listens for connections. */
126 int listen_fd;
127 /** Copied from para_server's main function. */
128 int argc;
129 /** Argument vector passed to para_server's main function. */
130 char **argv;
131 /** The command task structure for scheduling. */
132 struct task task;
133 };
134
135 static int want_colors(void)
136 {
137 if (conf.color_arg == color_arg_no)
138 return 0;
139 if (conf.color_arg == color_arg_yes)
140 return 1;
141 if (logfile)
142 return 0;
143 return isatty(STDERR_FILENO);
144 }
145
146 static int get_loglevel_by_name(const char *txt, size_t n)
147 {
148 if (!strncasecmp(txt, "debug", n))
149 return LL_DEBUG;
150 if (!strncasecmp(txt, "info", n))
151 return LL_INFO;
152 if (!strncasecmp(txt, "notice", n))
153 return LL_NOTICE;
154 if (!strncasecmp(txt, "warning", n))
155 return LL_WARNING;
156 if (!strncasecmp(txt, "error", n))
157 return LL_ERROR;
158 if (!strncasecmp(txt, "crit", n))
159 return LL_CRIT;
160 if (!strncasecmp(txt, "emerg", n))
161 return LL_EMERG;
162 return -1;
163 }
164
165 static char log_colors[NUM_LOGLEVELS][COLOR_MAXLEN];
166
167 static void init_colors_or_die(void)
168 {
169 int ret, i;
170 static const char *default_log_colors[NUM_LOGLEVELS] = {
171 [LL_DEBUG] = "normal",
172 [LL_INFO] = "white bold",
173 [LL_NOTICE] = "cyan bold",
174 [LL_WARNING] = "green bold",
175 [LL_ERROR] = "yellow bold",
176 [LL_CRIT] = "magenta bold",
177 [LL_EMERG] = "red bold",
178 };
179
180 for (i = 0; i < NUM_LOGLEVELS; i++) {
181 ret = color_parse(default_log_colors[i], log_colors[i]);
182 assert(ret >= 0);
183 }
184
185 for (i = 0; i < conf.log_color_given; i++) {
186 char *arg = conf.log_color_arg[i], *p = strchr(arg, ':');
187 int ll;
188 if (!p)
189 goto err;
190 ret = get_loglevel_by_name(arg, p - arg);
191 if (ret < 0)
192 goto err;
193 ll = ret;
194 p++;
195 ret = color_parse(p, log_colors[ll]);
196 if (ret < 0)
197 goto err;
198 }
199 return;
200 err:
201 PARA_EMERG_LOG("color syntax error, arg %d (%s)\n", i,
202 conf.log_color_arg[i]);
203 exit(EXIT_FAILURE);
204 }
205
206 /**
207 * Para_server's log function.
208 *
209 * \param ll The log level.
210 * \param fmt The format string describing the log message.
211 */
212 __printf_2_3 void para_log(int ll, const char* fmt,...)
213 {
214 va_list argp;
215 FILE *fp;
216 struct tm *tm;
217 time_t t1;
218 char *color, str[MAXLINE] = "";
219
220 ll = PARA_MIN(ll, NUM_LOGLEVELS - 1);
221 ll = PARA_MAX(ll, LL_DEBUG);
222 if (ll < conf.loglevel_arg)
223 return;
224
225 fp = logfile? logfile : stderr;
226 color = want_colors()? log_colors[ll] : NULL;
227
228 if (color)
229 fprintf(fp, "%s", color);
230 /* date and time */
231 time(&t1);
232 tm = localtime(&t1);
233 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
234 fprintf(fp, "%s ", str);
235 /* loglevel */
236 if (conf.loglevel_arg <= LL_INFO)
237 fprintf(fp, "%i: ", ll);
238 if (conf.loglevel_arg <= LL_INFO) { /* log pid */
239 pid_t mypid = getpid();
240 fprintf(fp, "(%d) ", (int)mypid);
241 }
242 va_start(argp, fmt);
243 vfprintf(fp, fmt, argp);
244 va_end(argp);
245 if (color)
246 fprintf(fp, "%s", COLOR_RESET);
247 }
248
249 /*
250 * setup shared memory area and get mutex for locking
251 */
252 static void init_ipc_or_die(void)
253 {
254 void *shm;
255 int ret = shm_new(sizeof(struct misc_meta_data));
256
257 if (ret < 0)
258 goto err_out;
259
260 ret = shm_attach(ret, ATTACH_RW, &shm);
261 if (ret < 0)
262 goto err_out;
263 mmd = shm;
264 mmd_shm_id = ret;
265
266 ret = mutex_new();
267 if (ret < 0)
268 goto err_out;
269 mmd_mutex = ret;
270
271 mmd->num_played = 0;
272 mmd->num_commands = 0;
273 mmd->events = 0;
274 mmd->num_connects = 0;
275 mmd->active_connections = 0;
276 mmd->vss_status_flags = VSS_NEXT;
277 mmd->new_vss_status_flags = VSS_NEXT;
278 return;
279 err_out:
280 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
281 exit(EXIT_FAILURE);
282 }
283
284 /**
285 * (Re-)read the server configuration files.
286 *
287 * \param override Passed to gengetopt to activate the override feature.
288 *
289 * This function also re-opens the logfile and sets the global \a
290 * user_list_file variable.
291 */
292 void parse_config_or_die(int override)
293 {
294 char *home = para_homedir();
295 struct stat statbuf;
296 int ret;
297 char *cf;
298
299 close_log(logfile);
300 logfile = NULL;
301 if (conf.config_file_given)
302 cf = para_strdup(conf.config_file_arg);
303 else
304 cf = make_message("%s/.paraslash/server.conf", home);
305 free(user_list_file);
306 if (!conf.user_list_given)
307 user_list_file = make_message("%s/.paraslash/server.users", home);
308 else
309 user_list_file = para_strdup(conf.user_list_arg);
310 ret = stat(cf, &statbuf);
311 if (ret && conf.config_file_given) {
312 ret = -1;
313 PARA_EMERG_LOG("can not stat config file %s\n", cf);
314 goto out;
315 }
316 if (!ret) {
317 int tmp = conf.daemon_given;
318 struct server_cmdline_parser_params params = {
319 .override = override,
320 .initialize = 0,
321 .check_required = 1,
322 .check_ambiguity = 0,
323 .print_errors = 1
324 };
325 server_cmdline_parser_config_file(cf, &conf, &params);
326 conf.daemon_given = tmp;
327 }
328 if (conf.logfile_given)
329 logfile = open_log(conf.logfile_arg);
330 if (want_colors())
331 init_colors_or_die();
332 ret = 1;
333 out:
334 free(cf);
335 free(home);
336 if (ret > 0)
337 return;
338 free(user_list_file);
339 user_list_file = NULL;
340 exit(EXIT_FAILURE);
341 }
342
343 static void signal_pre_select(struct sched *s, struct task *t)
344 {
345 struct signal_task *st = container_of(t, struct signal_task, task);
346 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
347 }
348
349 /*
350 * called when server gets SIGHUP or when client invokes hup command.
351 */
352 static void handle_sighup(void)
353 {
354 PARA_NOTICE_LOG("SIGHUP\n");
355 parse_config_or_die(1); /* reopens log */
356 init_user_list(user_list_file); /* reload user list */
357 if (mmd->afs_pid)
358 kill(mmd->afs_pid, SIGHUP);
359 }
360
361 static void signal_post_select(struct sched *s, struct task *t)
362 {
363 struct signal_task *st = container_of(t, struct signal_task, task);
364
365 if (!FD_ISSET(st->fd, &s->rfds))
366 return;
367
368 st->signum = para_next_signal();
369 switch (st->signum) {
370 case SIGHUP:
371 handle_sighup();
372 break;
373 case SIGCHLD:
374 for (;;) {
375 pid_t pid;
376 int ret = para_reap_child(&pid);
377 if (ret <= 0)
378 break;
379 if (pid != mmd->afs_pid)
380 continue;
381 PARA_EMERG_LOG("fatal: afs died\n");
382 goto genocide;
383 }
384 break;
385 /* die on sigint/sigterm. Kill all children too. */
386 case SIGINT:
387 case SIGTERM:
388 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
389 genocide:
390 kill(0, SIGTERM);
391 mutex_destroy(mmd_mutex);
392 shm_detach(mmd);
393 shm_destroy(mmd_shm_id);
394
395 exit(EXIT_FAILURE);
396 }
397 }
398
399 static void init_signal_task(void)
400 {
401 static struct signal_task signal_task_struct,
402 *st = &signal_task_struct;
403
404 st->task.pre_select = signal_pre_select;
405 st->task.post_select = signal_post_select;
406 sprintf(st->task.status, "signal task");
407
408 st->fd = para_signal_init(); /* always successful */
409
410 PARA_NOTICE_LOG("setting up signal handlers\n");
411 if (para_install_sighandler(SIGINT) < 0)
412 goto err;
413 if (para_install_sighandler(SIGTERM) < 0)
414 goto err;
415 if (para_install_sighandler(SIGHUP) < 0)
416 goto err;
417 if (para_install_sighandler(SIGCHLD) < 0)
418 goto err;
419 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
420 goto err;
421 if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
422 goto err;
423 add_close_on_fork_list(st->fd);
424 register_task(&st->task);
425 return;
426 err:
427 PARA_EMERG_LOG("could not install signal handlers\n");
428 exit(EXIT_FAILURE);
429 }
430
431 static void command_pre_select(struct sched *s, struct task *t)
432 {
433 struct server_command_task *sct = container_of(t, struct server_command_task, task);
434 para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
435 }
436
437 static void command_post_select(struct sched *s, struct task *t)
438 {
439 struct server_command_task *sct = container_of(t, struct server_command_task, task);
440
441 int new_fd, ret, i;
442 char *peer_name;
443 pid_t child_pid;
444
445 if (!FD_ISSET(sct->listen_fd, &s->rfds))
446 return;
447 ret = para_accept(sct->listen_fd, NULL, 0);
448 if (ret < 0)
449 goto out;
450 new_fd = ret;
451 peer_name = remote_name(new_fd);
452 PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
453 mmd->num_connects++;
454 mmd->active_connections++;
455 random();
456 child_pid = fork();
457 if (child_pid < 0) {
458 ret = -ERRNO_TO_PARA_ERROR(errno);
459 goto out;
460 }
461 if (child_pid) {
462 close(new_fd);
463 /* parent keeps accepting connections */
464 return;
465 }
466 alarm(ALARM_TIMEOUT);
467 close_listed_fds();
468 para_signal_shutdown();
469 /*
470 * put info on who we are serving into argv[0] to make
471 * client ip visible in top/ps
472 */
473 for (i = sct->argc - 1; i >= 0; i--)
474 memset(sct->argv[i], 0, strlen(sct->argv[i]));
475 sprintf(sct->argv[0], "para_server (serving %s)", peer_name);
476 return handle_connect(new_fd, peer_name);
477 out:
478 if (ret < 0)
479 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
480 }
481
482 static void init_server_command_task(int argc, char **argv)
483 {
484 int ret;
485 static struct server_command_task server_command_task_struct,
486 *sct = &server_command_task_struct;
487
488 PARA_NOTICE_LOG("initializing tcp command socket\n");
489 sct->task.pre_select = command_pre_select;
490 sct->task.post_select = command_post_select;
491 sct->argc = argc;
492 sct->argv = argv;
493 ret = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.port_arg);
494 if (ret < 0)
495 goto err;
496 sct->listen_fd = ret;
497 ret = mark_fd_nonblocking(sct->listen_fd);
498 if (ret < 0)
499 goto err;
500 add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */
501 register_task(&sct->task);
502 return;
503 err:
504 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
505 exit(EXIT_FAILURE);
506 }
507
508 static void init_random_seed(void)
509 {
510 unsigned int seed;
511 int fd, ret = para_open("/dev/urandom", O_RDONLY, 0);
512
513 if (ret < 0)
514 goto err;
515 fd = ret;
516 ret = read(fd, &seed, sizeof(seed));
517 if (ret < 0) {
518 ret = -ERRNO_TO_PARA_ERROR(errno);
519 goto out;
520 }
521 if (ret != sizeof(seed)) {
522 ret = -ERRNO_TO_PARA_ERROR(EIO);
523 goto out;
524 }
525 srandom(seed);
526 ret = 1;
527 out:
528 close(fd);
529 if (ret >= 0)
530 return;
531 err:
532 PARA_EMERG_LOG("can not seed pseudo random number generator: %s\n",
533 para_strerror(-ret));
534 exit(EXIT_FAILURE);
535 }
536
537 static int init_afs(void)
538 {
539 int ret, afs_server_socket[2];
540
541 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
542 if (ret < 0)
543 exit(EXIT_FAILURE);
544 afs_socket_cookie = para_random((uint32_t)-1);
545 mmd->afs_pid = fork();
546 if (mmd->afs_pid < 0)
547 exit(EXIT_FAILURE);
548 if (!mmd->afs_pid) { /* child (afs) */
549 close(afs_server_socket[0]);
550 afs_init(afs_socket_cookie, afs_server_socket[1]);
551 }
552 close(afs_server_socket[1]);
553 ret = mark_fd_nonblocking(afs_server_socket[0]);
554 if (ret < 0)
555 exit(EXIT_FAILURE);
556 add_close_on_fork_list(afs_server_socket[0]);
557 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
558 afs_server_socket[0], (unsigned) afs_socket_cookie);
559 return afs_server_socket[0];
560 }
561
562 static void server_init(int argc, char **argv)
563 {
564 struct server_cmdline_parser_params params = {
565 .override = 0,
566 .initialize = 1,
567 .check_required = 0,
568 .check_ambiguity = 0,
569 .print_errors = 1
570 };
571 int afs_socket;
572
573 valid_fd_012();
574 init_random_seed();
575 /* parse command line options */
576 server_cmdline_parser_ext(argc, argv, &conf, &params);
577 HANDLE_VERSION_FLAG("server", conf);
578 drop_privileges_or_die(conf.user_arg, conf.group_arg);
579 /* parse config file, open log and set defaults */
580 parse_config_or_die(0);
581 log_welcome("para_server", conf.loglevel_arg);
582 init_ipc_or_die(); /* init mmd struct and mmd->lock */
583 /* make sure, the global now pointer is uptodate */
584 gettimeofday(now, NULL);
585 server_uptime(UPTIME_SET); /* reset server uptime */
586 init_user_list(user_list_file);
587 /* become daemon */
588 if (conf.daemon_given)
589 daemon_init();
590 PARA_NOTICE_LOG("initializing audio format handlers\n");
591 afh_init();
592 PARA_NOTICE_LOG("initializing the audio file selector\n");
593 afs_socket = init_afs();
594 init_signal_task();
595 PARA_NOTICE_LOG("initializing virtual streaming system\n");
596 init_vss_task(afs_socket);
597 init_server_command_task(argc, argv);
598 PARA_NOTICE_LOG("server init complete\n");
599 }
600
601 static void status_refresh(void)
602 {
603 static int prev_uptime = -1, prev_events = -1;
604 int uptime = server_uptime(UPTIME_GET), ret = 1;
605
606 if (prev_events != mmd->events)
607 goto out;
608 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
609 goto out_inc_events;
610 if (uptime / 60 != prev_uptime / 60)
611 goto out_inc_events;
612 return;
613 out_inc_events:
614 mmd->events++;
615 out:
616 prev_uptime = uptime;
617 prev_events = mmd->events;
618 mmd->vss_status_flags = mmd->new_vss_status_flags;
619 if (ret) {
620 PARA_DEBUG_LOG("%d events, forcing status update\n",
621 mmd->events);
622 killpg(0, SIGUSR1);
623 }
624 }
625
626 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
627 struct timeval *timeout_tv)
628 {
629 int ret;
630
631 status_refresh();
632 mutex_unlock(mmd_mutex);
633 ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
634 mutex_lock(mmd_mutex);
635 return ret;
636 }
637
638 /**
639 * The main function of para_server.
640 *
641 * \param argc Usual argument count.
642 * \param argv Usual argument vector.
643 *
644 * \return EXIT_SUCCESS or EXIT_FAILURE.
645 */
646 int main(int argc, char *argv[])
647 {
648 int ret;
649 static struct sched s = {
650 .default_timeout = {
651 .tv_sec = 1,
652 .tv_usec = 0
653 },
654 .select_function = server_select
655 };
656 server_init(argc, argv);
657 mutex_lock(mmd_mutex);
658 ret = schedule(&s);
659 if (ret < 0) {
660 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
661 exit(EXIT_FAILURE);
662 }
663 exit(EXIT_SUCCESS);
664 }