From bb899fb1a0c279a2890ff30d11bf7aec50fb92ed Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 3 Oct 2021 21:52:02 +0200 Subject: [PATCH] Hide implementation of para_fd_set(). This preparatory patch for replacing select() renames para_fd_set() to sched_fd_set(), moves it to sched.c and makes it static. All users are modified to call either of the two new public functions sched_monitor_{read,write}fd() which take a pointer to struct sched rather than an fd set pointer. --- afs.c | 6 +++--- alsa_write.c | 2 +- audioc.c | 2 +- audiod.c | 2 +- client_common.c | 8 ++++---- dccp_recv.c | 2 +- dccp_send.c | 2 +- fd.c | 28 ---------------------------- fd.h | 1 - file_write.c | 2 +- grab_client.c | 2 +- gui.c | 8 ++++---- http_recv.c | 4 ++-- http_send.c | 6 +++--- interactive.c | 4 ++-- oss_write.c | 2 +- play.c | 2 +- sched.c | 42 ++++++++++++++++++++++++++++++++++++++++++ sched.h | 2 ++ server.c | 2 +- signal.h | 4 ++-- stdin.c | 2 +- stdout.c | 2 +- sync_filter.c | 2 +- udp_recv.c | 2 +- vss.c | 4 ++-- 26 files changed, 80 insertions(+), 65 deletions(-) diff --git a/afs.c b/afs.c index 9f32c8aa..99a53b68 100644 --- a/afs.c +++ b/afs.c @@ -767,10 +767,10 @@ static void command_pre_select(struct sched *s, void *context) struct command_task *ct = context; struct afs_client *client; - para_fd_set(server_socket, &s->rfds, &s->max_fileno); - para_fd_set(ct->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(server_socket, s); + sched_monitor_readfd(ct->fd, s); list_for_each_entry(client, &afs_client_list, node) - para_fd_set(client->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(client->fd, s); } /** diff --git a/alsa_write.c b/alsa_write.c index 1e61a17f..19223344 100644 --- a/alsa_write.c +++ b/alsa_write.c @@ -230,7 +230,7 @@ static void alsa_write_pre_select(struct sched *s, void *context) return; } pad->poll_fd = pfd.fd; - para_fd_set(pfd.fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(pfd.fd, s); } static void alsa_close(struct writer_node *wn) diff --git a/audioc.c b/audioc.c index 45688acb..248f6fb6 100644 --- a/audioc.c +++ b/audioc.c @@ -150,7 +150,7 @@ static void audioc_pre_select(struct sched *s, void *context) if (ret < 0) sched_min_delay(s); - para_fd_set(at->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(at->fd, s); } static int audioc_post_select(struct sched *s, void *context) diff --git a/audiod.c b/audiod.c index 12de0886..1dc70e5e 100644 --- a/audiod.c +++ b/audiod.c @@ -1078,7 +1078,7 @@ static int signal_post_select(struct sched *s, void *context) static void command_pre_select(struct sched *s, void *context) { struct command_task *ct = context; - para_fd_set(ct->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(ct->fd, s); } static int command_post_select(struct sched *s, void *context) diff --git a/client_common.c b/client_common.c index 7a353098..fdd04e5a 100644 --- a/client_common.c +++ b/client_common.c @@ -68,13 +68,13 @@ static void client_pre_select(struct sched *s, void *context) case CL_CONNECTED: case CL_SENT_AUTH: case CL_SENT_CH_RESPONSE: - para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(ct->scc.fd, s); return; case CL_RECEIVED_WELCOME: case CL_RECEIVED_PROCEED: case CL_RECEIVED_CHALLENGE: - para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(ct->scc.fd, s); return; case CL_SENDING: @@ -83,7 +83,7 @@ static void client_pre_select(struct sched *s, void *context) if (ret < 0) sched_min_delay(s); else if (ret > 0) - para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(ct->scc.fd, s); } __attribute__ ((fallthrough)); case CL_EXECUTING: @@ -92,7 +92,7 @@ static void client_pre_select(struct sched *s, void *context) if (ret < 0) sched_min_delay(s); else if (ret > 0) - para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(ct->scc.fd, s); } return; } diff --git a/dccp_recv.c b/dccp_recv.c index 1773ffe7..9a17269d 100644 --- a/dccp_recv.c +++ b/dccp_recv.c @@ -115,7 +115,7 @@ static void dccp_recv_pre_select(struct sched *s, void *context) if (generic_recv_pre_select(s, rn) <= 0) return; - para_fd_set(rn->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(rn->fd, s); } static int dccp_recv_post_select(__a_unused struct sched *s, void *context) diff --git a/dccp_send.c b/dccp_send.c index 47d6b408..5c274237 100644 --- a/dccp_send.c +++ b/dccp_send.c @@ -42,7 +42,7 @@ static void dccp_pre_select(struct sched *s) FOR_EACH_LISTEN_FD(n, dss) if (dss->listen_fds[n] >= 0) - para_fd_set(dss->listen_fds[n], &s->rfds, &s->max_fileno); + sched_monitor_readfd(dss->listen_fds[n], s); } /** diff --git a/fd.c b/fd.c index f7e0e42b..b8d1062d 100644 --- a/fd.c +++ b/fd.c @@ -376,34 +376,6 @@ __must_check int mark_fd_nonblocking(int fd) return 1; } -/** - * Set a file descriptor in a fd_set. - * - * \param fd The file descriptor to be set. - * \param fds The file descriptor set. - * \param max_fileno Highest-numbered file descriptor. - * - * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon - * return, \a max_fileno contains the maximum of the old_value and \a fd. - * - * \sa \ref para_select. -*/ -void para_fd_set(int fd, fd_set *fds, int *max_fileno) -{ - assert(fd >= 0 && fd < FD_SETSIZE); -#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); -} - /** * Paraslash's wrapper for mmap. * diff --git a/fd.h b/fd.h index 37d3ace9..ea6307b2 100644 --- a/fd.h +++ b/fd.h @@ -9,7 +9,6 @@ bool file_exists(const char *); int para_select(int n, fd_set *readfds, fd_set *writefds, int timeout); __must_check int mark_fd_nonblocking(int fd); __must_check int mark_fd_blocking(int fd); -void para_fd_set(int fd, fd_set *fds, int *max_fileno); int para_mmap(size_t length, int prot, int flags, int fd, void *map); int para_open(const char *path, int flags, mode_t mode); int para_mkdir(const char *path, mode_t mode); diff --git a/file_write.c b/file_write.c index d512df05..86c4e8ea 100644 --- a/file_write.c +++ b/file_write.c @@ -79,7 +79,7 @@ static void file_write_pre_select(struct sched *s, void *context) return; if (ret < 0 || !pfwd) return sched_min_delay(s); - para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(pfwd->fd, s); } static void file_write_close(struct writer_node *wn) diff --git a/grab_client.c b/grab_client.c index 83706493..04d90169 100644 --- a/grab_client.c +++ b/grab_client.c @@ -98,7 +98,7 @@ static void gc_pre_select(struct sched *s, void *context) return; if (ret < 0) sched_min_delay(s); - para_fd_set(gc->fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(gc->fd, s); } /* diff --git a/gui.c b/gui.c index 686fda31..fc7bfaf4 100644 --- a/gui.c +++ b/gui.c @@ -614,7 +614,7 @@ static void status_pre_select(struct sched *s, void *context) struct status_task *st = context; if (st->fd >= 0) - para_fd_set(st->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(st->fd, s); if (task_get_notification(st->task) < 0) return sched_min_delay(s); if (st->fd < 0) @@ -935,9 +935,9 @@ static void exec_pre_select(struct sched *s, void *context) { struct exec_task *et = context; if (exec_fds[0] >= 0) - para_fd_set(exec_fds[0], &s->rfds, &s->max_fileno); + sched_monitor_readfd(exec_fds[0], s); if (exec_fds[1] >= 0) - para_fd_set(exec_fds[1], &s->rfds, &s->max_fileno); + sched_monitor_readfd(exec_fds[1], s); if (task_get_notification(et->task) < 0) sched_min_delay(s); } @@ -995,7 +995,7 @@ static int exec_post_select(__a_unused struct sched *s, void *context) static void input_pre_select(struct sched *s, __a_unused void *context) { if (exec_status() != EXEC_XCMD) - para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno); + sched_monitor_readfd(STDIN_FILENO, s); if (window_update_needed()) sched_min_delay(s); } diff --git a/http_recv.c b/http_recv.c index ac942b4f..94e24d1b 100644 --- a/http_recv.c +++ b/http_recv.c @@ -64,9 +64,9 @@ static void http_recv_pre_select(struct sched *s, void *context) if (generic_recv_pre_select(s, rn) <= 0) return; if (phd->status == HTTP_CONNECTED) - para_fd_set(rn->fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(rn->fd, s); else - para_fd_set(rn->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(rn->fd, s); } /* diff --git a/http_send.c b/http_send.c index 6d026c71..59fe2efa 100644 --- a/http_send.c +++ b/http_send.c @@ -204,15 +204,15 @@ static void http_pre_select(struct sched *s) FOR_EACH_LISTEN_FD(n, hss) { if (hss->listen_fds[n] < 0) continue; - para_fd_set(hss->listen_fds[n], &s->rfds, &s->max_fileno); + sched_monitor_readfd(hss->listen_fds[n], s); } list_for_each_entry_safe(sc, tmp, &hss->client_list, node) { struct private_http_sender_data *phsd = sc->private_data; if (phsd->status == HTTP_CONNECTED) /* need to recv get request */ - para_fd_set(sc->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(sc->fd, s); if (phsd->status == HTTP_GOT_GET_REQUEST || phsd->status == HTTP_INVALID_GET_REQUEST) - para_fd_set(sc->fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(sc->fd, s); } } diff --git a/interactive.c b/interactive.c index 3af90a68..729a0dd5 100644 --- a/interactive.c +++ b/interactive.c @@ -377,7 +377,7 @@ static void i9e_pre_select(struct sched *s, __a_unused void *context) return; } if (ret > 0) - para_fd_set(i9ep->ici->fds[1], &s->wfds, &s->max_fileno); + sched_monitor_writefd(i9ep->ici->fds[1], s); } /* * fd[0] might have been reset to blocking mode if our job was moved to @@ -388,7 +388,7 @@ static void i9e_pre_select(struct sched *s, __a_unused void *context) if (ret < 0) PARA_WARNING_LOG("set to nonblock failed: (fd0 %d, %s)\n", i9ep->ici->fds[0], para_strerror(-ret)); - para_fd_set(i9ep->ici->fds[0], &s->rfds, &s->max_fileno); + sched_monitor_readfd(i9ep->ici->fds[0], s); } static void update_winsize(void) diff --git a/oss_write.c b/oss_write.c index 75ad98b9..794f4242 100644 --- a/oss_write.c +++ b/oss_write.c @@ -71,7 +71,7 @@ static void oss_pre_select(struct sched *s, void *context) return; if (ret < 0 || !powd) return sched_min_delay(s); - para_fd_set(powd->fd, &s->wfds, &s->max_fileno); + sched_monitor_writefd(powd->fd, s); } static void oss_close(struct writer_node *wn) diff --git a/play.c b/play.c index 02fd27b3..feb0f997 100644 --- a/play.c +++ b/play.c @@ -1168,7 +1168,7 @@ static void play_pre_select(struct sched *s, __a_unused void *context) { char state; - para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno); + sched_monitor_readfd(STDIN_FILENO, s); state = get_playback_state(); if (state == 'R' || state == 'F' || state == 'X') return sched_min_delay(s); diff --git a/sched.c b/sched.c index 8deb7f38..4ca040f4 100644 --- a/sched.c +++ b/sched.c @@ -451,3 +451,45 @@ int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s) sched_request_timeout(&diff, s); return 1; } + +static void sched_fd_set(int fd, fd_set *fds, int *max_fileno) +{ + assert(fd >= 0 && fd < FD_SETSIZE); +#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); +} + +/** + * Instruct the scheduler to monitor an fd for readiness for reading. + * + * \param fd The file descriptor. + * \param s The scheduler. + * + * \sa \ref sched_monitor_writefd(). + */ +void sched_monitor_readfd(int fd, struct sched *s) +{ + sched_fd_set(fd, &s->rfds, &s->max_fileno); +} + +/** + * Instruct the scheduler to monitor an fd for readiness for writing. + * + * \param fd The file descriptor. + * \param s The scheduler. + * + * \sa \ref sched_monitor_readfd(). + */ +void sched_monitor_writefd(int fd, struct sched *s) +{ + sched_fd_set(fd, &s->wfds, &s->max_fileno); +} diff --git a/sched.h b/sched.h index e0ca0c01..d9e85244 100644 --- a/sched.h +++ b/sched.h @@ -80,6 +80,8 @@ void sched_request_timeout(struct timeval *to, struct sched *s); void sched_request_timeout_ms(long unsigned ms, struct sched *s); int sched_request_barrier(struct timeval *barrier, struct sched *s); int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s); +void sched_monitor_readfd(int fd, struct sched *s); +void sched_monitor_writefd(int fd, struct sched *s); static inline bool sched_read_ok(int fd, const struct sched *s) { diff --git a/server.c b/server.c index ca00d0e9..91fb9dff 100644 --- a/server.c +++ b/server.c @@ -326,7 +326,7 @@ static void command_pre_select(struct sched *s, void *context) struct server_command_task *sct = context; for (n = 0; n < sct->num_listen_fds; n++) - para_fd_set(sct->listen_fds[n], &s->rfds, &s->max_fileno); + sched_monitor_readfd(sct->listen_fds[n], s); } static int command_task_accept(unsigned listen_idx, struct sched *s, diff --git a/signal.h b/signal.h index 2f3422e2..35221024 100644 --- a/signal.h +++ b/signal.h @@ -15,7 +15,7 @@ struct signal_task { /** * A generic pre-select method for signal tasks. * - * \param s Passed to para_fd_set(). + * \param s Used to watch the signal fd for reading. * \param context Signal task pointer. * * This convenience helper is called from several programs which need to handle @@ -31,7 +31,7 @@ struct signal_task { _static_inline_ void signal_pre_select(struct sched *s, void *context) { struct signal_task *st = context; - para_fd_set(st->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(st->fd, s); } struct signal_task *signal_init_or_die(void); diff --git a/stdin.c b/stdin.c index eea78b3e..5f9259f7 100644 --- a/stdin.c +++ b/stdin.c @@ -28,7 +28,7 @@ static void stdin_pre_select(struct sched *s, void *context) if (ret <= 0) return; if (btr_pool_unused(sit->btrp) > 0) - return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno); + return sched_monitor_readfd(STDIN_FILENO, s); sched_request_timeout_ms(100, s); } diff --git a/stdout.c b/stdout.c index 71c45f69..4af40984 100644 --- a/stdout.c +++ b/stdout.c @@ -18,7 +18,7 @@ static void stdout_pre_select(struct sched *s, void *context) ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF); if (ret > 0) - para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno); + sched_monitor_writefd(STDOUT_FILENO, s); else if (ret < 0) sched_min_delay(s); } diff --git a/sync_filter.c b/sync_filter.c index 3877c13d..ec5bb273 100644 --- a/sync_filter.c +++ b/sync_filter.c @@ -261,7 +261,7 @@ static void sync_pre_select(struct sched *s, void *context) ret = btr_node_status(fn->btrn, 0, BTR_NT_INTERNAL); if (ret < 0) return sched_min_delay(s); - para_fd_set(ctx->listen_fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(ctx->listen_fd, s); if (ret == 0) return; if (ctx->timeout.tv_sec == 0) { /* must ping buddies */ diff --git a/udp_recv.c b/udp_recv.c index cc6e2049..94ad4512 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -29,7 +29,7 @@ static void udp_recv_pre_select(struct sched *s, void *context) if (generic_recv_pre_select(s, rn) <= 0) return; - para_fd_set(rn->fd, &s->rfds, &s->max_fileno); + sched_monitor_readfd(rn->fd, s); } static int udp_check_eof(size_t sz, struct iovec iov[2]) diff --git a/vss.c b/vss.c index d2fbfb34..fce5281b 100644 --- a/vss.c +++ b/vss.c @@ -899,10 +899,10 @@ static void vss_pre_select(struct sched *s, void *context) if (need_to_request_new_audio_file(vsst)) { PARA_DEBUG_LOG("ready and playing, but no audio file\n"); - para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno); + sched_monitor_writefd(vsst->afs_socket, s); vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE; } else - para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno); + sched_monitor_readfd(vsst->afs_socket, s); FOR_EACH_SENDER(i) { if (!senders[i]->pre_select) continue; -- 2.39.2