para_server/para_audiod: new option --group
[paraslash.git] / audiod.c
index 56d022fac1c3293ef303f819515941261c8f1777..7029ea9116f9c115a5c4d5d41599fcd414ea930e 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -34,6 +34,7 @@
 #include "net.h"
 #include "daemon.h"
 #include "string.h"
+#include "fd.h"
 
 /** define the array of error lists needed by para_audiod */
 INIT_AUDIOD_ERRLISTS;
@@ -267,17 +268,7 @@ void para_log(int ll, const char* fmt,...)
 
        if (ll < conf.loglevel_arg)
                return;
-       if (!logfile && conf.logfile_given)
-               logfile = open_log(conf.logfile_arg);
-       if (!logfile && conf.daemon_given)
-               return;
-       if (!logfile) {
-               if (ll < WARNING)
-                       outfd = stdout;
-               else
-                       outfd = stderr;
-       } else
-               outfd = logfile;
+       outfd = logfile? logfile : stderr;
        time(&t1);
        tm = localtime(&t1);
        strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
@@ -720,7 +711,7 @@ static void start_stream_writer(int slot_num)
        s->write_fd = fds[0];
        add_close_on_fork_list(s->write_fd);
        /* we write to this fd in do_select, so we need non-blocking */
-       fcntl(s->write_fd, F_SETFL, O_NONBLOCK);
+       mark_fd_nonblock(s->write_fd);
        gettimeofday(&s->wstime, NULL);
        current_decoder = slot_num;
        activate_inactive_grab_clients(slot_num, s->format, &s->fci->filters);
@@ -960,9 +951,9 @@ static void close_decoder_if_idle(int slot_num)
        clear_slot(slot_num);
 }
 
-static int set_stream_fds(fd_set *wfds)
+static void set_stream_fds(fd_set *wfds, int *max_fileno)
 {
-       int i, max_fileno = -1;
+       int i;
 
        check_timeouts();
        FOR_EACH_SLOT(i) {
@@ -984,12 +975,9 @@ static int set_stream_fds(fd_set *wfds)
                        continue;
                if (!get_loaded_bytes(i))
                        continue;
-               FD_SET(s->write_fd, wfds);
+               para_fd_set(s->write_fd, wfds, max_fileno);
                s->wcheck = 1;
-               max_fileno = MAX(s->write_fd, max_fileno);
        }
-//     PARA_INFO_LOG("return %d\n", max_fileno);
-       return max_fileno;
 }
 
 static int write_audio_data(int slot_num)
@@ -1520,9 +1508,10 @@ static int open_stat_pipe(void)
        return ret;
 }
 
-static int pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
+static void audiod_pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv,
+               int *max_fileno)
 {
-       int i, ret, max = -1;
+       int i, ret;
 
        FOR_EACH_SLOT(i) {
                struct slot_info *s = &slot[i];
@@ -1533,10 +1522,8 @@ static int pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
                a = &afi[s->format];
                ret = a->receiver->pre_select(rn, rfds, wfds, tv);
 //             PARA_NOTICE_LOG("%s preselect: %d\n", a->receiver->name, ret);
-               max = MAX(max, ret);
+               *max_fileno = MAX(*max_fileno, ret);
        }
-       return max;
-
 }
 static void audiod_post_select(int select_ret, fd_set *rfds, fd_set *wfds)
 {
@@ -1571,15 +1558,20 @@ static void __noreturn audiod_mainloop(void)
        char status_buf[STRINGSIZE] = "";
        struct timeval tv;
 repeat:
-       FD_ZERO(&rfds);
        FD_ZERO(&wfds);
-       max_fileno = 0;
+       FD_ZERO(&rfds);
+       /* always check signal pipe and the local socket */
+       FD_SET(signal_pipe, &rfds);
+       max_fileno = signal_pipe;
+       para_fd_set(audiod_socket, &rfds, &max_fileno);
+
        if (audiod_status != AUDIOD_ON)
                kill_all_decoders();
        else if (playing)
                start_current_receiver();
-       max_fileno = set_stream_fds(&wfds);
-       /* stat pipe (read) */
+
+       set_stream_fds(&wfds, &max_fileno);
+       /* status pipe */
        if (stat_pipe >= 0 && audiod_status == AUDIOD_OFF)
                close_stat_pipe();
        if (stat_pipe < 0 && audiod_status != AUDIOD_OFF) {
@@ -1587,30 +1579,17 @@ repeat:
                sbo = 0;
                status_buf[0] = '\0';
        }
-       if (stat_pipe >= 0 && audiod_status != AUDIOD_OFF) {
-               FD_SET(stat_pipe, &rfds);
-               max_fileno = MAX(max_fileno, stat_pipe);
-       }
-       /* always check signal pipe */
-       FD_SET(signal_pipe, &rfds);
-       max_fileno = MAX(max_fileno, signal_pipe);
+       if (stat_pipe >= 0 && audiod_status != AUDIOD_OFF)
+               para_fd_set(stat_pipe, &rfds, &max_fileno);
        /* local socket */
-       if (audiod_socket < 0)
-               audiod_get_socket(); /* doesn't return on errors */
-       FD_SET(audiod_socket, &rfds);
-       max_fileno = MAX(max_fileno, audiod_socket);
        tv.tv_sec = 0;
        tv.tv_usec = 200 * 1000;
-       ret = pre_select(&rfds, &wfds, &tv);
-       max_fileno = MAX(max_fileno, ret);
-       ret = select(max_fileno + 1, &rfds, &wfds, NULL, &tv);
-       if (ret < 0 && errno != EINTR)
-               PARA_ERROR_LOG("select returned %d (%s)\n", ret,
-                       strerror(errno));
-       if (audiod_status != AUDIOD_OFF)
-               audiod_status_dump();
+       audiod_pre_select(&rfds, &wfds, &tv, &max_fileno);
+       ret = para_select(max_fileno + 1, &rfds, &wfds, &tv);
        if (ret < 0)
                goto repeat;
+       if (audiod_status != AUDIOD_OFF)
+               audiod_status_dump();
        audiod_post_select(ret, &rfds, &wfds);
        /* read status pipe */
        if (stat_pipe >=0 && FD_ISSET(stat_pipe, &rfds)) {
@@ -1667,19 +1646,20 @@ int __noreturn main(int argc, char *argv[])
        valid_fd_012();
        hostname = para_hostname();
        cmdline_parser(argc, argv, &conf);
-       para_drop_privileges(conf.user_arg);
+       para_drop_privileges(conf.user_arg, conf.group_arg);
        cf = configfile_exists();
        if (cf) {
                if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
-                       fprintf(stderr, "parse error in config file\n");
+                       PARA_EMERG_LOG("%s", "parse error in config file\n");
                        exit(EXIT_FAILURE);
                }
        }
+       if (conf.logfile_given)
+               logfile = open_log(conf.logfile_arg);
        log_welcome("para_audiod", conf.loglevel_arg);
        i = init_stream_io();
        if (i < 0) {
-               fprintf(stderr, "init stream io error: %s\n",
-                       PARA_STRERROR(-i));
+               PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i));
                exit(EXIT_FAILURE);
        }
        server_uptime(UPTIME_SET);
@@ -1690,5 +1670,6 @@ int __noreturn main(int argc, char *argv[])
        setup_signal_handling();
        if (conf.daemon_given)
                daemon_init();
+       audiod_get_socket(); /* doesn't return on errors */
        audiod_mainloop();
 }