]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
introduce para_select()
authorAndre <maan@p133.(none)>
Fri, 7 Apr 2006 12:28:09 +0000 (14:28 +0200)
committerAndre <maan@p133.(none)>
Fri, 7 Apr 2006 12:28:09 +0000 (14:28 +0200)
A simple wrapper for select(2) that checks for EINTR and restarts the
select call in this case.

13 files changed:
Makefile.in
audioc.c
audiod.c
configure.ac
fd.c
fd.h
grab_client.c
gui.c
play.c
recv.c
sdl_gui.c
server.c
stat.c

index 2c64d19dd692f262ebebe52044679ca033c43062..d424c5388ffc0a8baaefd2714d34a6faa9468c06 100644 (file)
@@ -109,8 +109,8 @@ dbadm_objs = dbadm.o exec.o close_on_fork.o string.o
 fade_objs = fade.cmdline.o fade.o exec.o close_on_fork.o string.o fd.o
 krell_objs = krell.o string.o
 slider_objs = slider.o string.o
-audioc_objs = audioc.cmdline.o audioc.o string.o net.o
-play_objs = play.cmdline.o play.o time.o
+audioc_objs = audioc.cmdline.o audioc.o string.o net.o fd.o
+play_objs = play.cmdline.o play.o time.o fd.o
 
 *.o: para.h config.h gcc-compat.h
 
index 968f84b6fcd3c6960b2dbf4e2009297e8fd1028c..93e91178907dd71a2fcb9761b64da9852e42be35 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -22,6 +22,7 @@
 #include "para.h"
 #include "net.h"
 #include "string.h"
+#include "fd.h"
 
 struct gengetopt_args_info conf;
 char *tmpfifo;
@@ -130,7 +131,7 @@ int main(int argc, char *argv[])
                ret = -E_OVERRUN;
                if (max_fileno < 0)
                        goto out;
-               ret = select(max_fileno + 1, &rfd, &wfd, NULL, NULL);
+               ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
                if (ret < 0) {
                        ret = -E_SELECT;
                        goto out;
index 581cec154bb80493ae007b59a2d7b29d7b84c3dd..9e82b13a782580be39ffe04d9b4f5521cfe9b097 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -1604,14 +1604,11 @@ repeat:
        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)) {
index 19c2ae43831625644b7100b8778bb8450bd01a37..f6b8733eb06e9f84f7e4810934b0acd3b89168d0 100644 (file)
@@ -58,7 +58,8 @@ AC_CHECK_LIB([menu], [new_menu], [extras="$extras para_dbadm"],
 
 
 recv_cmdline_objs="recv.cmdline http_recv.cmdline dccp_recv.cmdline"
-recv_errlist_objs="http_recv recv_common recv time string net dccp_recv dccp"
+recv_errlist_objs="http_recv recv_common recv time string net dccp_recv
+       dccp fd"
 recv_ldflags=""
 
 filter_cmdline_objs="filter.cmdline compress_filter.cmdline"
@@ -73,7 +74,8 @@ audiod_ldflags=""
 
 server_cmdline_objs="server.cmdline"
 server_errlist_objs="server mp3 afs command net string signal random_selector
-       time daemon stat crypt http_send db close_on_fork playlist_selector ipc dccp dccp_send"
+       time daemon stat crypt http_send db close_on_fork playlist_selector
+       ipc dccp dccp_send fd"
 server_ldflags=""
 
 ########################################################################### ssl
diff --git a/fd.c b/fd.c
index 5db30b63fd9ed62148b8d04390ec8456826337a3..b5310f8128c85563bf2f99f30e833eb59455f6e1 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -12,3 +12,16 @@ int file_exists(const char *fn)
 
        return !stat(fn, &statbuf);
 }
+
+int para_select(int n, fd_set *readfds, fd_set *writefds,
+               struct timeval *timeout)
+{
+       int ret, err;
+       do {
+               ret = select(n, readfds, writefds, NULL, timeout);
+               err = errno;
+       } while (ret < 0 && errno == EINTR);
+       if (ret < 0)
+               PARA_CRIT_LOG("select error (%s)\n", strerror(err));
+       return ret;
+}
diff --git a/fd.h b/fd.h
index 30ab0852eacf20ee4feaeec6a65f600dd446f744..2db28f761c06f9c62378cefb3a6f1d88ed8a556a 100644 (file)
--- a/fd.h
+++ b/fd.h
@@ -19,3 +19,6 @@
 /** \file fd.h file handling functions */
 
 int file_exists(const char *);
+
+int para_select(int n, fd_set *readfds, fd_set *writefds,
+               struct timeval *timeout);
index 2dfb295b8ce87fcb5f165bcfd4b3c25f3bdb5b50..6769ee522ef3dfcd7dac66fb0c299c4ee0e37ef7 100644 (file)
@@ -32,6 +32,7 @@
 #include "audiod.h"
 #include "error.h"
 #include "string.h"
+#include "fd.h"
 
 /** grab clients that are not yet attached to a filter node */
 struct list_head inactive_grab_client_list;
@@ -56,12 +57,10 @@ static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
 
 //     PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
        fd_set wfds;
-       do {
-               FD_ZERO(&wfds);
-               FD_SET(gc->fd, &wfds);
-               ret = select(gc->fd + 1, NULL, &wfds, NULL, &tv);
-       } while (ret == EAGAIN || ret == EINTR);
-       if (ret != 1) {
+       FD_ZERO(&wfds);
+       FD_SET(gc->fd, &wfds);
+       ret = para_select(gc->fd + 1, NULL, &wfds, &tv);
+       if (ret <= 0) {
                if (gc->mode == GRAB_PEDANTIC)
                        return -E_PEDANTIC_GRAB;
                if (gc->mode == GRAB_SLOPPY)
diff --git a/gui.c b/gui.c
index f63e99bd2c2e3363351f37a0b1b6afe591dd7ca5..de0e9c54a2729c4d597b8521989c233bf8340bb1 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -898,17 +898,16 @@ repeat:
        }
        if (curses_active)
                FD_SET(STDIN_FILENO, &rfds);
-       ret = select(max_fileno + 1, &rfds, NULL, NULL, &tv);
+       ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
 //     PARA_DEBUG_LOG("select returned %d\n", ret);
-
+       if (ret <= 0)
+               goto check_return; /* skip fd checks */
        /* signals */
        if (FD_ISSET(signal_pipe, &rfds)) {
                int sig_nr = para_next_signal();
                if (sig_nr > 0)
                        handle_signal(sig_nr);
        }
-       if (ret <= 0)
-               goto check_return; /* skip fd checks */
        /* read command pipe if ready */
        if (command_pipe >= 0 && mode == COMMAND_MODE &&
                        FD_ISSET(command_pipe, &rfds)) {
diff --git a/play.c b/play.c
index 689747e900b6636672d6fc8d06679829fd272189..fd8c9b28a8deed548be8e2344ca909c131dbf9b4 100644 (file)
--- a/play.c
+++ b/play.c
@@ -25,6 +25,7 @@
 #define WAV_HEADER_LEN 44
 #include <sys/time.h> /* gettimeofday */
 #include "para.h"
+#include "fd.h"
 #include "play.cmdline.h"
 #include <alsa/asoundlib.h>
 
@@ -66,6 +67,15 @@ static size_t bytes_per_frame;
 static struct timeval *start_time;
 static struct gengetopt_args_info conf;
 
+void para_log(__unused int ll, const char* fmt,...)
+{
+       va_list argp;
+
+       va_start(argp, fmt);
+       vfprintf(stderr, fmt, argp);
+       va_end(argp);
+}
+
 /*
  * read_wav_header - read WAV_HEADER_LEN bytes from stdin to audio buffer
  *
@@ -114,7 +124,7 @@ static void set_alsa_params(void)
        err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
        if (err < 0 || !buffer_time)
                EXIT(E_GET_BUFFER_TIME);
-       fprintf(stderr, "buffer time: %d\n", buffer_time);
+       PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
        if (snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
                        &buffer_time, 0) < 0)
                EXIT(E_SET_BUFFER_TIME);
@@ -122,7 +132,7 @@ static void set_alsa_params(void)
                EXIT(E_HW_PARAMS);
        snd_pcm_hw_params_get_period_size(hwparams, &chunk_size, 0);
        snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
-       fprintf(stderr, "buffer size: %lu, period_size: %lu\n", buffer_size, chunk_size);
+       PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size, chunk_size);
        if (chunk_size == buffer_size)
                EXIT(E_BAD_PERIOD);
        snd_pcm_sw_params_current(handle, swparams);
@@ -163,7 +173,7 @@ static snd_pcm_sframes_t pcm_write(u_char *data, size_t count)
                /* write interleaved frames */
                r = snd_pcm_writei(handle, data, count);
                if (r < 0)
-                       fprintf(stderr, "write error: %s\n", snd_strerror(r));
+                       PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
                if (r == -EAGAIN || (r >= 0 && r < count))
                        snd_pcm_wait(handle, 1);
                else if (r == -EPIPE)
@@ -209,9 +219,8 @@ static int start_time_in_future(struct timeval *diff)
  */
 static void do_initial_delay(struct timeval *delay)
 {
-//     fprintf(stderr, "sleeping %lums\n", tv2ms(delay));
        do
-               select(1, NULL, NULL, NULL, delay);
+               para_select(1, NULL, NULL, delay);
        while (start_time_in_future(delay));
 }
 
@@ -248,16 +257,13 @@ again:
        }
        p = audiobuf;
        while (loaded >= chunk_bytes) {
-//                     fprintf(stderr, "write (loaded = %d)\n", loaded);
                ret = pcm_write(p, chunk_size) * bytes_per_frame;
                p += ret;
                written += ret;
                loaded -= ret;
        }
-       if (loaded && p != audiobuf) {
-//                     fprintf(stderr, "memcpy: %d@%d\n", loaded, p - audiobuf);
+       if (loaded && p != audiobuf)
                memcpy(audiobuf, p, loaded);
-       }
 read:
        ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded);
        if (ret < 0)
@@ -282,8 +288,6 @@ static size_t check_wave(void)
                return WAV_HEADER_LEN;
        conf.channels_arg = (unsigned) a[22];
        conf.sample_rate_arg = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
-//     fprintf(stderr, "channels: %d, rate: %d\n", conf.channels_arg,
-//             conf.sample_rate_arg);
        return 0;
 }
 
@@ -301,7 +305,6 @@ int main(int argc, char *argv[])
                        EXIT(E_SYNTAX);
                start_time = &tv;
        }
-//     fprintf(stderr, "argc=%d, argv[1]=%s\n",argc,  argv[1]);
        snd_pcm_info_alloca(&info);
        if (snd_output_stdio_attach(&log, stderr, 0) < 0)
                EXIT(E_LOG);
diff --git a/recv.c b/recv.c
index fd82bc0fa6d998e51072284aefe1dc5d79dd3663..ba1cfd20bba03651387e1bb1826baa0592ab2b46 100644 (file)
--- a/recv.c
+++ b/recv.c
@@ -19,6 +19,7 @@
 
 #include "recv.h"
 #include "recv.cmdline.h"
+#include "fd.h"
 #include "error.h"
 
 struct gengetopt_args_info conf;
@@ -87,10 +88,8 @@ recv:
        max = MAX(max, ret);
 
        PARA_DEBUG_LOG("timeout: %lums\n", tv2ms(&timeout));
-       ret = select(max + 1, &rfds, &wfds, NULL, &timeout);
+       ret = para_select(max + 1, &rfds, &wfds, &timeout);
        if (ret < 0) {
-               if (errno == EINTR || errno == EAGAIN)
-                       goto recv;
                ret = -E_RECV_SELECT;
                goto out;
        }
index d0b83f09a3cf8a9b81b2f2e5f78d3465be3d998d..a1381c2f202b0f7e9fd5c2e1003203129ea60fcf 100644 (file)
--- a/sdl_gui.c
+++ b/sdl_gui.c
@@ -699,13 +699,11 @@ static int draw_status(int pipe)
        tv.tv_usec = 3000000;
        FD_ZERO(&rfds);
        FD_SET(pipe, &rfds);
-       ret = select(pipe + 1, &rfds, NULL, NULL, &tv);
-//     printf("select returned %d\n", ret);
+       ret = para_select(pipe + 1, &rfds, NULL, &tv);
        if (ret <= 0)
                return 0;
        if (read_audiod_pipe(pipe, update_status) > 0)
                return 1;
-//     clear_all_items();
        free(stat_items[SI_STATUS_BAR].content);
        stat_items[SI_STATUS_BAR].content =
                para_strdup("audiod not running!?\n");
index 8f20763fb44ed01327514b92837dceb8b0f66021..2f66dc4da17833b2ac32464746645e978d8752c8 100644 (file)
--- a/server.c
+++ b/server.c
@@ -41,6 +41,7 @@
 #include "daemon.h"
 #include "string.h"
 #include "ipc.h"
+#include "fd.h"
 
 /** define the array of error lists needed by para_server */
 INIT_SERVER_ERRLISTS;
@@ -436,7 +437,7 @@ int main(int argc, char *argv[])
        /* listen on sock_fd, new connection on new_fd */
        int sockfd, new_fd;
        struct sockaddr_in their_addr;
-       int err, i, max_fileno, ret;
+       int i, max_fileno, ret;
        pid_t chld_pid;
        fd_set rfds, wfds;
        struct timeval *timeout;
@@ -469,21 +470,14 @@ repeat:
                max_fileno = MAX(max_fileno, ret);
        }
        mmd_unlock();
-//     PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
-       ret = select(max_fileno + 1, &rfds, &wfds, NULL, timeout);
-       err = errno;
-       //PARA_DEBUG_LOG("%s: select returned  %i\n", __func__, ret);
+       ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
        mmd_lock();
        if (mmd->selector_change >= 0)
                change_selector();
        if (selectors[mmd->selector_num].post_select)
                selectors[mmd->selector_num].post_select(&rfds, &wfds);
-       if (ret < 0 && err == EINTR)
-               goto repeat;
-       if (ret < 0) {
-               PARA_CRIT_LOG("select error (%s)\n", strerror(err));
+       if (ret < 0)
                goto repeat;
-       }
        for (i = 0; senders[i].name; i++) {
                if (senders[i].status != SENDER_ON)
                        continue;
diff --git a/stat.c b/stat.c
index e3773df6403fb72d0c5163efa19c2f0ea43f7392..7346f097e61e9933759563d64af7e7b2dedd3eb6 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -25,6 +25,7 @@
 #include "list.h"
 #include "error.h"
 #include "string.h"
+#include "fd.h"
 
 /** the maximal number of simultaneous connections */
 #define MAX_STAT_CLIENTS 50
@@ -161,10 +162,8 @@ void stat_client_write(char *msg, int itemnum)
                FD_ZERO(&wfds);
                FD_SET(fd, &wfds);
 //             PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
-               do
-                       ret = select(fd + 1, NULL, &wfds, NULL, &tv);
-               while (ret < 0 && errno == EINTR);
-               if (ret) {
+               ret = para_select(fd + 1, NULL, &wfds, &tv);
+               if (ret > 0) {
                        ret = write(fd, msg, len);
                        PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
                        if (ret == len )