]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - audiod.c
improved version of split_args()
[paraslash.git] / audiod.c
index 9708f4fa363f4ffa6d3540fc7cb27c79917f76ca..7596c5c54939e9178d186a49e21a1f5f395c93c6 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -268,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);
@@ -821,6 +811,7 @@ static void check_stat_line(char *line)
        struct timeval tv;
        char *tmp;
 
+       PARA_INFO_LOG("line: %s\n", line);
        if (!line)
                return;
        itemnum = stat_line_valid(line);
@@ -961,9 +952,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) {
@@ -985,12 +976,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)
@@ -1120,7 +1108,7 @@ out:
 
 static int init_stream_io(void)
 {
-       int i, ret, receiver_num;
+       int i, ret, receiver_num, nf;
        char *cmd;
 
        for (i = 0; i < conf.stream_write_cmd_given; i++) {
@@ -1169,9 +1157,11 @@ static int init_stream_io(void)
        free(cmd);
        /* filters */
        filter_init(filters);
+       nf = MAX(2,  conf.filter_given) + 1;
+       PARA_INFO_LOG("allocating space for %d filters\n", nf);
        FOR_EACH_AUDIO_FORMAT(i) {
-               afi[i].filter_conf = para_malloc((conf.filter_given + 1) * sizeof(char *));
-               afi[i].filters = para_malloc((conf.filter_given + 1) * sizeof(struct filter *));
+               afi[i].filter_conf = para_malloc(nf * sizeof(char *));
+               afi[i].filters = para_malloc(nf * sizeof(struct filter *));
        }
        if (!conf.no_default_filters_given)
                return setup_default_filters();
@@ -1458,12 +1448,12 @@ static int handle_connect(void)
        ret = check_perms(&c);
        if (ret < 0)
                goto out;
-       argc = split_args(buf, &argv, '\n');
-       PARA_INFO_LOG("argv[0]: %s\n", argv[0]);
+       argc = split_args(buf, &argv, "\n");
+       PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
        for (i = 0; cmds[i].name; i++) {
                if (strcmp(cmds[i].name, argv[0]))
                        continue;
-               ret = cmds[i].handler(clifd, argc + 1, argv);
+               ret = cmds[i].handler(clifd, argc, argv);
                goto out;
        }
        ret = -E_INVALID_AUDIOD_CMD; /* cmd not found */
@@ -1521,9 +1511,10 @@ static int open_stat_pipe(void)
        return ret;
 }
 
-static int audiod_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];
@@ -1534,10 +1525,8 @@ static int audiod_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)
 {
@@ -1577,15 +1566,14 @@ repeat:
        /* always check signal pipe and the local socket */
        FD_SET(signal_pipe, &rfds);
        max_fileno = signal_pipe;
-       FD_SET(audiod_socket, &rfds);
-       max_fileno = MAX(max_fileno, audiod_socket);
+       para_fd_set(audiod_socket, &rfds, &max_fileno);
 
        if (audiod_status != AUDIOD_ON)
                kill_all_decoders();
        else if (playing)
                start_current_receiver();
 
-       max_fileno = MAX(max_fileno, set_stream_fds(&wfds));
+       set_stream_fds(&wfds, &max_fileno);
        /* status pipe */
        if (stat_pipe >= 0 && audiod_status == AUDIOD_OFF)
                close_stat_pipe();
@@ -1594,16 +1582,12 @@ 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);
-       }
+       if (stat_pipe >= 0 && audiod_status != AUDIOD_OFF)
+               para_fd_set(stat_pipe, &rfds, &max_fileno);
        /* local socket */
        tv.tv_sec = 0;
        tv.tv_usec = 200 * 1000;
-       ret = audiod_pre_select(&rfds, &wfds, &tv);
-       max_fileno = MAX(max_fileno, ret);
-
+       audiod_pre_select(&rfds, &wfds, &tv, &max_fileno);
        ret = para_select(max_fileno + 1, &rfds, &wfds, &tv);
        if (ret < 0)
                goto repeat;
@@ -1662,22 +1646,24 @@ int __noreturn main(int argc, char *argv[])
        char *cf;
        int i;
 
+       fprintf(stderr, "argc: %d\n", argc);
        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);