From e341afa6c94215b1ecff2d931ddeca358632c385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Pokusi=C5=84ski?= Date: Tue, 28 Nov 2023 00:11:48 +0100 Subject: [PATCH] [libc] add pselect --- lib/libc/sys/select.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/libc/sys/select.c b/lib/libc/sys/select.c index 7a798e0a0..38a3b53cb 100644 --- a/lib/libc/sys/select.c +++ b/lib/libc/sys/select.c @@ -5,35 +5,22 @@ #include #include #include +#include int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask) { - return 0; -} - -int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, - fd_set *restrict exceptfds, struct timeval *restrict timeout) { int kq; int ret; - struct timespec timeout_ts; struct kevent *events; int nevents = 0; + sigset_t sigs; if (nfds < 0) { errno = EINVAL; return -1; } - if (timeout != NULL) { - if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec >= 1000000) { - errno = EINVAL; - return -1; - } - - tv2ts(timeout, &timeout_ts); - } - kq = kqueue1(O_CLOEXEC); if (kq < 0) return -1; @@ -56,8 +43,11 @@ int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, FD_ZERO(readfds); if (writefds != NULL) FD_ZERO(writefds); - - ret = kevent(kq, events, nevents, events, nevents, timeout == NULL ? NULL : &timeout_ts); + + if (sigmask && sigprocmask(SIG_SETMASK, sigmask, &sigs)) + return -1; + + ret = kevent(kq, events, nevents, events, nevents, timeout); if (ret == -1) goto end; @@ -75,7 +65,26 @@ int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, end: free(events); + if (sigmask && sigprocmask(SIG_SETMASK, &sigs, NULL)) + ret = -1; close_kq: close(kq); return ret; } + +int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, + fd_set *restrict exceptfds, struct timeval *restrict timeout) { + struct timespec timeout_ts; + + if (timeout != NULL) { + if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec >= 1000000) { + errno = EINVAL; + return -1; + } + + tv2ts(timeout, &timeout_ts); + } + + return pselect(nfds, readfds, writefds, exceptfds, + timeout == NULL ? NULL : &timeout_ts , NULL); +}