feat(select): poll/select and fd_set

This commit is contained in:
2026-09-05 21:15:07 -04:00
parent 5af4197ee9
commit ddcf8f39be
7 changed files with 671 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
#ifndef VLIBC_POLL_H
#define VLIBC_POLL_H
/*
* vlibc — <poll.h>.
*
* Poll multiple file descriptors for readiness. Every declaration here is a
* thin pass-through to the kernel: poll() blocks per its millisecond
* timeout, ppoll() takes a struct timespec and an optional signal mask
* directly, and both return the count of ready descriptors (0 on timeout,
* -1 with errno set on error).
*
* Level 1 (onlyposix): poll, nfds_t, struct pollfd, the POLL* event bits.
* Level 2 (muslmimic): ppoll (Linux-specific; POSIX has only poll).
*
* struct pollfd and the POLL* values mirror the kernel ABI (x86_64
* asm-generic/poll.h) exactly: the wrappers pass them to SYS_ppoll
* unmodified. POLLERR, POLLHUP and POLLNVAL are report-only — they can
* appear in revents even when not requested in events.
*
* The signal mask type ppoll takes is declared under the shared guard
* below; <signal.h> is its canonical POSIX home and will own the full sig*
* API.
*/
#include <vlibc/features.h>
#include <sys/types.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Requestable event bits (poll()'s events field). */
#define POLLIN 0x001 /* readable */
#define POLLPRI 0x002 /* urgent readable (out-of-band) */
#define POLLOUT 0x004 /* writable */
/* Report-only event bits (poll()'s revents field). */
#define POLLERR 0x008 /* error condition */
#define POLLHUP 0x010 /* hung up */
#define POLLNVAL 0x020 /* invalid fd */
/* The number of struct pollfd entries poll()/ppoll() watch. */
typedef unsigned long nfds_t;
/*
* One descriptor watched by poll()/ppoll(). fd is the descriptor (negative
* to ignore), events the requested bits (POLLIN/POLLOUT), revents the bits
* the kernel reports (the requested bits plus any POLLERR/POLLHUP/POLLNVAL).
*/
struct pollfd
{
int fd;
short events;
short revents;
};
/*
* Wait for readiness on the first nfds entries of fds, blocking up to
* timeout milliseconds (timeout < 0 blocks indefinitely, 0 never blocks).
* Return the number of entries with a nonzero revents, 0 on timeout, or -1
* with errno set on error.
*/
int
poll(struct pollfd *fds, nfds_t nfds, int timeout);
#if VLIBC_LEVEL_GE(2)
/* Level 2 (muslmimic): Linux-specific. */
#ifndef VLIBC_SIGSET_T_DEFINED
#define VLIBC_SIGSET_T_DEFINED
/*
* Signal mask type: a single 64-bit word — the x86_64 Linux sigset_t (see
* setjmp.h). <signal.h> is the canonical POSIX home for sigset_t and builds
* the sig* API on this same layout; the typedef is repeated in
* <sys/select.h> under this guard so the two headers stay consistent.
*/
typedef unsigned long sigset_t;
#endif
/*
* Like poll(), but the timeout is a struct timespec and, when sigmask is
* not NULL, the given signal mask is atomically installed for the duration
* of the wait (the previous mask is restored before returning).
* Linux-specific.
*/
int
ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask);
#endif /* VLIBC_LEVEL_GE(2) */
#ifdef __cplusplus
}
#endif
#endif /* VLIBC_POLL_H */