NEWS update.
[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  *
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 #include <stdbool.h>
72
73 #include "para.h"
74 #include "error.h"
75 #include "crypt.h"
76 #include "server.cmdline.h"
77 #include "afh.h"
78 #include "string.h"
79 #include "afs.h"
80 #include "server.h"
81 #include "list.h"
82 #include "send.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 "sched.h"
91 #include "signal.h"
92 #include "user_list.h"
93 #include "color.h"
94
95 /** Define the array of error lists needed by para_server. */
96 INIT_SERVER_ERRLISTS;
97
98 /** Shut down non-authorized connections after that many seconds. */
99 #define ALARM_TIMEOUT 10
100
101 /**
102  * Pointer to shared memory area for communication between para_server
103  * and its children. Exported to vss.c. command.c and to afs.
104  */
105 struct misc_meta_data *mmd;
106
107 /**
108  * The configuration of para_server
109  *
110  * It also contains the options for the audio file selector, audio format
111  * handler and all supported senders.
112  */
113 struct server_args_info conf;
114
115 /** A random value used in child context for authentication. */
116 uint32_t afs_socket_cookie;
117
118 /** The mutex protecting the shared memory area containing the mmd struct. */
119 int mmd_mutex;
120
121 /** The file containing user information (public key, permissions). */
122 static char *user_list_file = NULL;
123
124
125 /** The task responsible for server command handling. */
126 struct server_command_task {
127         /** TCP port on which para_server listens for connections. */
128         int listen_fd;
129         /** Copied from para_server's main function. */
130         int argc;
131         /** Argument vector passed to para_server's main function. */
132         char **argv;
133         /** The command task structure for scheduling. */
134         struct task task;
135 };
136
137 static int want_colors(void)
138 {
139         if (conf.color_arg == color_arg_no)
140                 return 0;
141         if (conf.color_arg == color_arg_yes)
142                 return 1;
143         if (conf.logfile_given)
144                 return 0;
145         return isatty(STDERR_FILENO);
146 }
147
148 static void init_colors_or_die(void)
149 {
150         int ret, i;
151
152         if (!want_colors())
153                 return;
154         daemon_set_flag(DF_COLOR_LOG);
155         daemon_set_default_log_colors();
156         for (i = 0; i < conf.log_color_given; i++) {
157                 ret = daemon_set_log_color(conf.log_color_arg[i]);
158                 if (ret < 0)
159                         exit(EXIT_FAILURE);
160         }
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(&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         register_task(&sct->task);
433         return;
434 err:
435         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
436         exit(EXIT_FAILURE);
437 }
438
439 static int init_afs(void)
440 {
441         int ret, afs_server_socket[2];
442         pid_t afs_pid;
443
444         ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
445         if (ret < 0)
446                 exit(EXIT_FAILURE);
447         get_random_bytes_or_die((unsigned char *)&afs_socket_cookie,
448                 sizeof(afs_socket_cookie));
449         afs_pid = fork();
450         if (afs_pid < 0)
451                 exit(EXIT_FAILURE);
452         if (afs_pid == 0) { /* child (afs) */
453                 close(afs_server_socket[0]);
454                 afs_init(afs_socket_cookie, afs_server_socket[1]);
455         }
456         mmd->afs_pid = afs_pid;
457         close(afs_server_socket[1]);
458         ret = mark_fd_nonblocking(afs_server_socket[0]);
459         if (ret < 0)
460                 exit(EXIT_FAILURE);
461         add_close_on_fork_list(afs_server_socket[0]);
462         PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
463                 afs_server_socket[0], (unsigned) afs_socket_cookie);
464         return afs_server_socket[0];
465 }
466
467 __noreturn static void tmp_sigchld_handler(__a_unused int s)
468 {
469         PARA_EMERG_LOG("caught early SIGCHLD\n");
470         exit(EXIT_FAILURE);
471 }
472
473 static void server_init(int argc, char **argv)
474 {
475         struct server_cmdline_parser_params params = {
476                 .override = 0,
477                 .initialize = 1,
478                 .check_required = 0,
479                 .check_ambiguity = 0,
480                 .print_errors = 1
481         };
482         int afs_socket;
483
484         valid_fd_012();
485         init_random_seed_or_die();
486         /* parse command line options */
487         server_cmdline_parser_ext(argc, argv, &conf, &params);
488         HANDLE_VERSION_FLAG("server", conf);
489         drop_privileges_or_die(conf.user_arg, conf.group_arg);
490         /* parse config file, open log and set defaults */
491         parse_config_or_die(0);
492         log_welcome("para_server");
493         init_ipc_or_die(); /* init mmd struct and mmd->lock */
494         /* make sure, the global now pointer is uptodate */
495         gettimeofday(now, NULL);
496         server_uptime(UPTIME_SET); /* reset server uptime */
497         init_user_list(user_list_file);
498         /* become daemon */
499         if (conf.daemon_given)
500                 daemonize();
501         PARA_NOTICE_LOG("initializing audio format handlers\n");
502         afh_init();
503
504         /*
505          * Although afs uses its own signal handling we must ignore SIGUSR1
506          * _before_ the afs child process gets born by init_afs() below.  It's
507          * racy to do this in the child because the parent might send SIGUSR1
508          * before the child gets a chance to ignore this signal -- only the
509          * good die young.
510          */
511         para_sigaction(SIGUSR1, SIG_IGN);
512         /*
513          * We have to install a SIGCHLD handler before the afs process is being
514          * forked off. Otherwise, para_server does not notice if afs dies before
515          * the SIGCHLD handler has been installed by init_signal_task() below.
516          */
517         para_sigaction(SIGCHLD, tmp_sigchld_handler);
518         PARA_NOTICE_LOG("initializing the audio file selector\n");
519         afs_socket = init_afs();
520         init_signal_task();
521         PARA_NOTICE_LOG("initializing virtual streaming system\n");
522         init_vss_task(afs_socket);
523         init_server_command_task(argc, argv);
524         PARA_NOTICE_LOG("server init complete\n");
525 }
526
527 static void status_refresh(void)
528 {
529         static int prev_uptime = -1, prev_events = -1;
530         int uptime = server_uptime(UPTIME_GET), ret = 1;
531
532         if (prev_events != mmd->events)
533                 goto out;
534         if (mmd->new_vss_status_flags != mmd->vss_status_flags)
535                 goto out_inc_events;
536         if (uptime / 60 != prev_uptime / 60)
537                 goto out_inc_events;
538         return;
539 out_inc_events:
540         mmd->events++;
541 out:
542         prev_uptime = uptime;
543         prev_events = mmd->events;
544         mmd->vss_status_flags = mmd->new_vss_status_flags;
545         if (ret) {
546                 PARA_DEBUG_LOG("%d events, forcing status update\n",
547                         mmd->events);
548                 killpg(0, SIGUSR1);
549         }
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         static struct sched s = {
576                 .default_timeout = {
577                         .tv_sec = 1,
578                         .tv_usec = 0
579                 },
580                 .select_function = server_select
581         };
582         server_init(argc, argv);
583         mutex_lock(mmd_mutex);
584         ret = schedule(&s);
585         if (ret < 0) {
586                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
587                 exit(EXIT_FAILURE);
588         }
589         exit(EXIT_SUCCESS);
590 }