Merge branch 't/test_man'
[paraslash.git] / server.c
index e963da7760465af02bcceec58730be2be4be84fd..fa19ce4225111e33ec7f192935f455c6cd5b2d76 100644 (file)
--- a/server.c
+++ b/server.c
@@ -1,74 +1,48 @@
 /*
- * Copyright (C) 1997-2013 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1997-2014 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file server.c Paraslash's main server. */
 
-
 /**
- * \mainpage Paraslash API Reference
- *
- * Starting points for getting an overview:
+ * \mainpage Main data structures:
  *
- *
- *     - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
- *       \ref audioc.c, \ref afh.c, \ref play.c,
  *     - Server: \ref server_command, \ref sender,
- *     - Audio file selector: \ref audio_format_handler, \ref afs_table,
- *     - Client: \ref receiver, \ref receiver_node, \ref filter,
- *       \ref filter_node, \ref writer_node.
- *
+ *     - Audio file selector: \ref afs_info, \ref afs_table,
+ *     - Audio format handler: \ref audio_format_handler, \ref afh_info
+ *     - Receivers/filters/writers: \ref receiver, \ref receiver_node,
+ *       \ref filter, \ref filter_node, \ref writer_node, \ref writer.
  *
- * The gory details, listed by topic:
+ * Selected APIs:
  *
- *     - Audio format handlers: \ref send_common.c \ref mp3_afh.c,
- *       \ref ogg_afh.c, \ref aac_afh.c, \ref wma_afh.c, \ref spx_afh.c
- *     - Decoders: \ref mp3dec_filter.c, \ref oggdec_filter.c,
- *       \ref aacdec_filter.c, \ref wmadec_filter.c, spxdec_filter.c,
- *       \ref flacdec_filter.c,
- *     - Volume normalizer: \ref compress_filter.c,
- *     - Output: \ref alsa_write.c, \ref osx_write.c, \ref oss_write.c,
- *     - http: \ref http_recv.c, \ref http_send.c,
- *     - udp: \ref udp_recv.c, \ref udp_send.c,
- *     - dccp: \ref dccp_recv.c, \ref dccp_send.c,
- *     - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
- *     - Afs structures: \ref afs_table, \ref audio_file_data,
- *       \ref afs_info \ref afh_info,
- *     - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
- *       \ref attribute.c, \ref score.c,
- *     - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
- *
- * Lower levels:
- *
- *     - Scheduling: \ref sched.c, \ref sched.h,
- *     - Networking: \ref net.c,
- *     - File descriptors: \ref fd.c,
- *     - Signals: \ref signal.c,
- *     - Daemons: \ref daemon.c,
- *     - Strings: \ref string.c, \ref string.h,
+ *     - Scheduling: \ref sched.h,
+ *     - Buffer trees: \ref buffer_tree.h,
+ *     - Sideband API: \ref sideband.h,
+ *     - Crypto: \ref crypt.h, \ref crypt_backend.h,
+ *     - Error subsystem: \ref error.h, \ref error2.c,
+ *     - Inter process communication: \ref ipc.h,
+ *     - Forward error correction: \ref fec.h,
+ *     - Daemons: \ref daemon.h,
+ *     - Mixer API: \ref mix.h,
+ *     - Interactive sessions: \ref interactive.h,
+ *     - File descriptors: \ref fd.h,
+ *     - Signals: \ref signal.h,
+ *     - Networking: \ref net.h,
  *     - Time: \ref time.c,
- *     - Spawning processes: \ref exec.c,
- *     - Inter process communication: \ref ipc.c,
- *     - Blob tables: \ref blob.c,
- *     - The error subsystem: \ref error.h.
- *     - Access control for paraslash senders: \ref acl.c, \ref acl.h.
- *     - Internal crypto API: \ref crypt.h.
- *     - interactive sessions (libreadline): \ref interactive.c.
- *
- * Low-level data structures:
- *
- *     - Doubly linked lists: \ref list.h,
- *     - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
- *     - openssl: \ref crypt.c
- *     - libgcrypt: \ref gcrypt.c
- *     - Forward error correction: \ref fec.c.
+ *     - Doubly linked lists: \ref list.h.
  */
 
+#include <netinet/in.h>
+#include <sys/socket.h>
 #include <signal.h>
 #include <regex.h>
 #include <osl.h>
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <netdb.h>
 
 #include "para.h"
 #include "error.h"
@@ -91,6 +65,7 @@
 #include "signal.h"
 #include "user_list.h"
 #include "color.h"
+#include "ggo.h"
 #include "version.h"
 
 __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
@@ -236,13 +211,13 @@ void parse_config_or_die(int override)
                        .print_errors = !conf.daemon_given
                };
                server_cmdline_parser_config_file(cf, &conf, &params);
+               daemon_set_loglevel(conf.loglevel_arg);
                conf.daemon_given = tmp;
        }
        if (conf.logfile_given) {
                daemon_set_logfile(conf.logfile_arg);
                daemon_open_log_or_die();
        }
-       daemon_set_loglevel(conf.loglevel_arg);
        init_colors_or_die();
        daemon_set_flag(DF_LOG_PID);
        daemon_set_flag(DF_LOG_LL);
@@ -338,7 +313,7 @@ static void init_signal_task(void)
                *st = &signal_task_struct;
 
        st->task.pre_select = signal_pre_select;
-       st->task.new_post_select = signal_post_select;
+       st->task.post_select = signal_post_select;
        sprintf(st->task.status, "signal task");
 
        PARA_NOTICE_LOG("setting up signal handling\n");
@@ -390,6 +365,9 @@ static int command_post_select(struct sched *s, struct task *t)
                goto out;
        }
        if (child_pid) {
+               /* avoid problems with non-fork-safe PRNGs */
+               unsigned char buf[16];
+               get_random_bytes_or_die(buf, sizeof(buf));
                close(new_fd);
                /* parent keeps accepting connections */
                return 0;
@@ -422,7 +400,7 @@ static void init_server_command_task(int argc, char **argv)
 
        PARA_NOTICE_LOG("initializing tcp command socket\n");
        sct->task.pre_select = command_pre_select;
-       sct->task.new_post_select = command_post_select;
+       sct->task.post_select = command_post_select;
        sct->argc = argc;
        sct->argv = argv;
        ret = para_listen_simple(IPPROTO_TCP, conf.port_arg);
@@ -473,6 +451,15 @@ static int init_afs(int argc, char **argv)
        return afs_server_socket[0];
 }
 
+__noreturn static void print_help_and_die(void)
+{
+       struct ggo_help h = DEFINE_GGO_HELP(server);
+       bool d = conf.detailed_help_given;
+
+       ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
+       exit(0);
+}
+
 static void server_init(int argc, char **argv)
 {
        struct server_cmdline_parser_params params = {
@@ -488,7 +475,10 @@ static void server_init(int argc, char **argv)
        init_random_seed_or_die();
        /* parse command line options */
        server_cmdline_parser_ext(argc, argv, &conf, &params);
-       HANDLE_VERSION_FLAG("server", conf);
+       daemon_set_loglevel(conf.loglevel_arg);
+       version_handle_flag("server", conf.version_given);
+       if (conf.help_given || conf.detailed_help_given)
+               print_help_and_die();
        drop_privileges_or_die(conf.user_arg, conf.group_arg);
        /* parse config file, open log and set defaults */
        parse_config_or_die(0);