introduce mark_fd_nonblock()
[paraslash.git] / audiod.c
index adfcbf41f767bb489ed6d84eefb740321bb6041a..9708f4fa363f4ffa6d3540fc7cb27c79917f76ca 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;
@@ -720,7 +721,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);
@@ -1348,7 +1349,7 @@ static int com_grab(int fd, int argc, char **argv)
        struct grab_client *gc;
        struct filter_node *fn;
        int i, err;
-       char *help;
+       char *msg;
 
        PARA_INFO_LOG("argc: %d, argv[0]: %s, optind: %d\n", argc, argv[0], optind);
        gc = grab_client_new(fd, argc, argv, &err);
@@ -1360,17 +1361,22 @@ static int com_grab(int fd, int argc, char **argv)
                activate_grab_client(gc, fn);
        return 1;
 err_out:
-       if (err != -E_GC_HELP_GIVEN)
+       if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
                return err;
-       help = make_message("%s\n\n", grab_client_args_info_usage);
-       for (i = 0; grab_client_args_info_help[i]; i++) {
-               char *tmp = make_message("%s%s\n", help,
-                       grab_client_args_info_help[i]);
-               free(help);
-               help = tmp;
-       }
-       err = client_write(fd, help);
-       free(help);
+       if (err == -E_GC_HELP_GIVEN) {
+               msg = make_message("%s\n\n", grab_client_args_info_usage);
+               for (i = 0; grab_client_args_info_help[i]; i++) {
+                       char *tmp = make_message("%s%s\n", msg,
+                               grab_client_args_info_help[i]);
+                       free(msg);
+                       msg = tmp;
+               }
+       } else
+               msg = make_message("%s %s\n",
+                       GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
+                       GRAB_CLIENT_CMDLINE_PARSER_VERSION);
+       err = client_write(fd, msg);
+       free(msg);
        if (err < 0)
                return err;
        close(fd);
@@ -1515,7 +1521,7 @@ static int open_stat_pipe(void)
        return ret;
 }
 
-static int pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
+static int audiod_pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
 {
        int i, ret, max = -1;
 
@@ -1566,15 +1572,21 @@ 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;
+       FD_SET(audiod_socket, &rfds);
+       max_fileno = MAX(max_fileno, audiod_socket);
+
        if (audiod_status != AUDIOD_ON)
                kill_all_decoders();
        else if (playing)
                start_current_receiver();
-       max_fileno = set_stream_fds(&wfds);
-       /* stat pipe (read) */
+
+       max_fileno = MAX(max_fileno, set_stream_fds(&wfds));
+       /* status pipe */
        if (stat_pipe >= 0 && audiod_status == AUDIOD_OFF)
                close_stat_pipe();
        if (stat_pipe < 0 && audiod_status != AUDIOD_OFF) {
@@ -1586,26 +1598,17 @@ repeat:
                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);
        /* 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);
+       ret = audiod_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();
+
+       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)) {
@@ -1685,5 +1688,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();
 }