recv.c: Make rn_event_handler() static
[paraslash.git] / server.c
index 5616273..6195266 100644 (file)
--- a/server.c
+++ b/server.c
@@ -42,6 +42,8 @@
 #include "string.h"
 #include "ipc.h"
 #include "fd.h"
+#include "signal.h"
+#include "user_list.h"
 
 /** define the array of error lists needed by para_server */
 INIT_SERVER_ERRLISTS;
@@ -56,8 +58,8 @@ struct misc_meta_data *mmd;
  * It also contains the options for all audio file selectors and all supported
  * senders.
 */
-struct gengetopt_args_info conf;
-char *user_list = NULL;
+struct server_args_info conf;
+char *user_list_file = NULL;
 extern void dccp_send_init(struct sender *);
 extern void http_send_init(struct sender *);
 extern void ortp_send_init(struct sender *);
@@ -221,11 +223,11 @@ static void parse_config(int override)
                cf = conf.config_file_arg;
        else
                cf = make_message("%s/.paraslash/server.conf", home);
-       free(user_list);
+       free(user_list_file);
        if (!conf.user_list_given)
-               user_list = make_message("%s/.paraslash/server.users", home);
+               user_list_file = make_message("%s/.paraslash/server.users", home);
        else
-               user_list = para_strdup(conf.user_list_arg);
+               user_list_file = para_strdup(conf.user_list_arg);
        ret = stat(cf, &statbuf);
        if (ret && conf.config_file_given) {
                ret = -1;
@@ -234,7 +236,7 @@ static void parse_config(int override)
        }
        if (!ret) {
                int tmp = conf.daemon_given;
-               cmdline_parser_configfile(cf, &conf, override, 0, 0);
+               server_cmdline_parser_configfile(cf, &conf, override, 0, 0);
                conf.daemon_given = tmp;
        }
        /* logfile */
@@ -251,8 +253,8 @@ out:
        free(home);
        if (ret > 0)
                return;
-       free(user_list);
-       user_list = NULL;
+       free(user_list_file);
+       user_list_file = NULL;
        exit(EXIT_FAILURE);
 }
 
@@ -302,11 +304,18 @@ random:
 
 static unsigned init_network(void)
 {
-       int sockfd = init_tcp_socket(conf.port_arg);
+       int fd, ret = init_tcp_socket(conf.port_arg);
 
-       if (sockfd < 0)
-               exit(EXIT_FAILURE);
-       return sockfd;
+       if (ret < 0)
+               goto err;
+       fd = ret;
+       ret = mark_fd_nonblock(fd);
+       if (ret < 0)
+               goto err;
+       return fd;
+err:
+       PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
+       exit(EXIT_FAILURE);
 }
 
 static void init_random_seed(void)
@@ -339,13 +348,14 @@ static unsigned do_inits(int argc, char **argv)
 
        init_random_seed();
        /* parse command line options */
-       cmdline_parser(argc, argv, &conf);
+       server_cmdline_parser(argc, argv, &conf);
        para_drop_privileges(conf.user_arg, conf.group_arg);
        /* parse config file, open log and set defaults */
        parse_config(0);
        log_welcome("para_server", conf.loglevel_arg);
        shm_init(); /* init mmd struct */
        server_uptime(UPTIME_SET); /* reset server uptime */
+       init_user_list(user_list_file);
        /* become daemon */
        if (conf.daemon_given)
                daemon_init();
@@ -359,10 +369,6 @@ static unsigned do_inits(int argc, char **argv)
        /* init network socket */
        PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
        sockfd = init_network();
-       if (conf.autoplay_given) {
-               mmd->afs_status_flags |= AFS_PLAYING;
-               mmd->new_afs_status_flags |= AFS_PLAYING;
-       }
        PARA_NOTICE_LOG("%s", "init complete\n");
        return sockfd;
 }
@@ -395,6 +401,7 @@ static void handle_sighup(void)
        parse_config(1); /* reopens log */
        mmd->selector_change = mmd->selector_num; /* do not change selector.. */
        change_selector(); /* .. just reload */
+       init_user_list(user_list_file); /* reload user list */
 }
 
 static void status_refresh(void)
@@ -449,10 +456,7 @@ repeat:
                        continue;
                if (!senders[i].pre_select)
                        continue;
-               senders[i].pre_select(mmd->audio_format >= 0?
-                       &afl[mmd->audio_format] : NULL,
-                       &max_fileno,
-                       &rfds, &wfds);
+               senders[i].pre_select( &max_fileno, &rfds, &wfds);
        }
        if (selectors[mmd->selector_num].pre_select) {
                ret = selectors[mmd->selector_num].pre_select(&rfds, &wfds);
@@ -472,9 +476,7 @@ repeat:
                        continue;
                if (!senders[i].post_select)
                        continue;
-               senders[i].post_select(mmd->audio_format >= 0?
-                       &afl[mmd->audio_format] : NULL,
-                       &rfds, &wfds);
+               senders[i].post_select(&rfds, &wfds);
        }
        afs_send_chunk();
        status_refresh();