From 6969c9fa8293a44daf7ba5a0426573fcd71c1953 Mon Sep 17 00:00:00 2001 From: Andre Date: Tue, 13 Jun 2006 11:41:28 +0200 Subject: [PATCH] mark all fds used for select() as non-blocking This shouldn't matter much, but there _are_ (rare) situations where an fd is marked ready for reading, but a subsequent read will block nevertheless. It's never wrong to use non-blocking fds, so just do it. This patch also includes a check in para_fd_set() which spots blocking fds as they are added to the fd sets. This check is commented out for performance reasons but can easily be activated. --- audiod.c | 2 ++ dccp.c | 1 + dccp_recv.c | 1 + fd.c | 10 ++++++++++ http_recv.c | 6 ++++-- stdin.c | 1 + stdout.c | 1 + 7 files changed, 20 insertions(+), 2 deletions(-) diff --git a/audiod.c b/audiod.c index 52f4ff4c..90290639 100644 --- a/audiod.c +++ b/audiod.c @@ -882,6 +882,7 @@ static int audiod_get_socket(void) exit(EXIT_FAILURE); /* do not unlink socket */ } add_close_on_fork_list(fd); + mark_fd_nonblock(fd); return fd; } @@ -894,6 +895,7 @@ static int open_stat_pipe(void) ret = fd[1]; PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret); add_close_on_fork_list(ret); + mark_fd_nonblock(ret); } else clean_exit(EXIT_FAILURE, "failed to open status pipe"); return ret; diff --git a/dccp.c b/dccp.c index 147e4630..fa48976e 100644 --- a/dccp.c +++ b/dccp.c @@ -26,6 +26,7 @@ #include "para.h" #include "error.h" #include "dccp.h" +#include "fd.h" /** \cond some magic dccp constants */ #define SOL_DCCP 269 diff --git a/dccp_recv.c b/dccp_recv.c index 335ad2e6..943b8283 100644 --- a/dccp_recv.c +++ b/dccp_recv.c @@ -98,6 +98,7 @@ static int dccp_recv_open(struct receiver_node *rn) ret = -E_DCCP_CONNECT; if (connect(pdd->fd, ai->ai_addr, ai->ai_addrlen) < 0) goto err_out; + mark_fd_nonblock(pdd->fd); return 1; err_out: dccp_recv_close(rn); diff --git a/fd.c b/fd.c index d1e0412d..b9130093 100644 --- a/fd.c +++ b/fd.c @@ -100,10 +100,20 @@ int mark_fd_nonblock(int fd) */ void para_fd_set(int fd, fd_set *fds, int *max_fileno) { + if (fd < 0 || fd >= FD_SETSIZE) { PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd); exit(EXIT_FAILURE); } +#if 0 + { + int flags = fcntl(fd, F_GETFL); + if (!(flags & O_NONBLOCK)) { + PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd); + exit(EXIT_FAILURE); + } + } +#endif FD_SET(fd, fds); *max_fileno = PARA_MAX(*max_fileno, fd); } diff --git a/http_recv.c b/http_recv.c index 3d72c7c7..09654167 100644 --- a/http_recv.c +++ b/http_recv.c @@ -189,9 +189,10 @@ static int http_recv_open(struct receiver_node *rn) if (!ret < 0) goto err_out; /* get new socket */ - ret = -E_SOCKET; - if ((phd->fd = get_socket()) < 0) + ret = get_socket(); + if (ret < 0) goto err_out; + phd->fd = ret; /* init their_addr */ init_sockaddr(&their_addr, conf->port_arg, he); /* connect */ @@ -200,6 +201,7 @@ static int http_recv_open(struct receiver_node *rn) ret = para_connect(phd->fd, &their_addr); if (ret < 0) goto err_out; + mark_fd_nonblock(phd->fd); phd->status = HTTP_CONNECTED; return 1; err_out: diff --git a/stdin.c b/stdin.c index cb13250e..8b18c2e2 100644 --- a/stdin.c +++ b/stdin.c @@ -105,5 +105,6 @@ void stdin_set_defaults(struct stdin_task *sit) sit->task.post_select = stdin_post_select; sit->task.event_handler = stdin_default_event_handler; sit->task.private_data = sit; + mark_fd_nonblock(STDIN_FILENO); sprintf(sit->task.status, "stdin reader"); } diff --git a/stdout.c b/stdout.c index 1c1435a3..2e86fe96 100644 --- a/stdout.c +++ b/stdout.c @@ -107,5 +107,6 @@ void stdout_set_defaults(struct stdout_task *sot) sot->task.post_select = stdout_post_select; sot->task.event_handler = stdout_default_event_handler; sot->eof = 0; + mark_fd_nonblock(STDOUT_FILENO); sprintf(sot->task.status, "stdout writer"); } -- 2.39.2