2020-11-28 20:01:51 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-11-28 20:01:51 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-11-28 20:01:51 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-11-28 20:01:51 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/assert.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-08-15 22:18:37 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/calls/struct/timeval.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/limits.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/sock/select.h"
|
|
|
|
#include "libc/sock/sock.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/sock/struct/pollfd.h"
|
|
|
|
#include "libc/sock/struct/pollfd.internal.h"
|
2023-06-10 16:15:19 +00:00
|
|
|
#include "libc/stdckdint.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/sysv/consts/poll.h"
|
2021-08-26 22:59:55 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2023-05-10 06:35:10 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
int sys_select_nt(int nfds, fd_set *readfds, fd_set *writefds,
|
2022-08-15 22:18:37 +00:00
|
|
|
fd_set *exceptfds, struct timeval *timeout,
|
|
|
|
const sigset_t *sigmask) {
|
2022-04-15 06:39:48 +00:00
|
|
|
int i, pfds, events, fdcount;
|
2020-11-28 20:01:51 +00:00
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
// convert bitsets to pollfd
|
2023-09-21 14:30:39 +00:00
|
|
|
struct pollfd fds[64];
|
2022-04-15 06:39:48 +00:00
|
|
|
for (pfds = i = 0; i < nfds; ++i) {
|
|
|
|
events = 0;
|
|
|
|
if (readfds && FD_ISSET(i, readfds)) events |= POLLIN;
|
|
|
|
if (writefds && FD_ISSET(i, writefds)) events |= POLLOUT;
|
|
|
|
if (exceptfds && FD_ISSET(i, exceptfds)) events |= POLLERR;
|
|
|
|
if (events) {
|
|
|
|
if (pfds < ARRAYLEN(fds)) {
|
|
|
|
fds[pfds].fd = i;
|
|
|
|
fds[pfds].events = events;
|
|
|
|
fds[pfds].revents = 0;
|
|
|
|
pfds += 1;
|
|
|
|
} else {
|
|
|
|
return enomem();
|
2020-11-28 20:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
// convert the wait time to a word
|
2023-09-21 14:30:39 +00:00
|
|
|
uint32_t millis;
|
2023-09-07 23:03:19 +00:00
|
|
|
if (!timeout) {
|
2022-04-15 06:39:48 +00:00
|
|
|
millis = -1;
|
2023-09-07 23:03:19 +00:00
|
|
|
} else {
|
2023-09-21 14:30:39 +00:00
|
|
|
int64_t ms = timeval_tomillis(*timeout);
|
|
|
|
if (ms < 0 || ms > UINT32_MAX) {
|
|
|
|
millis = -1u;
|
|
|
|
} else {
|
|
|
|
millis = ms;
|
|
|
|
}
|
2020-11-28 20:01:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
// call our nt poll implementation
|
2022-08-15 22:18:37 +00:00
|
|
|
fdcount = sys_poll_nt(fds, pfds, &millis, sigmask);
|
2023-09-21 14:30:39 +00:00
|
|
|
unassert(fdcount < 64);
|
|
|
|
if (fdcount < 0) return -1;
|
2022-04-15 06:39:48 +00:00
|
|
|
|
|
|
|
// convert pollfd back to bitsets
|
|
|
|
if (readfds) FD_ZERO(readfds);
|
|
|
|
if (writefds) FD_ZERO(writefds);
|
|
|
|
if (exceptfds) FD_ZERO(exceptfds);
|
2023-09-21 14:30:39 +00:00
|
|
|
int bits = 0;
|
|
|
|
for (i = 0; i < pfds; ++i) {
|
|
|
|
if (fds[i].revents & POLLIN) {
|
|
|
|
if (readfds) {
|
|
|
|
FD_SET(fds[i].fd, readfds);
|
|
|
|
++bits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fds[i].revents & POLLOUT) {
|
|
|
|
if (writefds) {
|
|
|
|
FD_SET(fds[i].fd, writefds);
|
|
|
|
++bits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fds[i].revents & (POLLERR | POLLNVAL)) {
|
|
|
|
if (exceptfds) {
|
|
|
|
FD_SET(fds[i].fd, exceptfds);
|
|
|
|
++bits;
|
|
|
|
}
|
|
|
|
}
|
2020-11-28 20:01:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
// store remaining time back in caller's timeval
|
|
|
|
if (timeout) {
|
2023-09-07 23:03:19 +00:00
|
|
|
*timeout = timeval_frommillis(millis);
|
2021-08-26 22:59:55 +00:00
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
return bits;
|
2020-11-28 20:01:51 +00:00
|
|
|
}
|
2023-05-10 06:35:10 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|