1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
//
// SPDX-License-Identifier: BSD-2-Clause
#include <common/time.h>
#include <sys/select.h>
#include <wasi/api.h>
#include <errno.h>
int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
fd_set *restrict errorfds, const struct timespec *restrict timeout,
const sigset_t *sigmask) {
// Negative file descriptor upperbound.
if (nfds < 0) {
errno = EINVAL;
return -1;
}
// This implementation does not support polling for exceptional
// conditions, such as out-of-band data on TCP sockets.
if (errorfds != NULL && errorfds->__nfds > 0) {
errno = ENOSYS;
return -1;
}
// Replace NULL pointers by the empty set.
fd_set empty;
FD_ZERO(&empty);
if (readfds == NULL)
readfds = ∅
if (writefds == NULL)
writefds = ∅
// Determine the maximum number of events.
size_t maxevents = readfds->__nfds + writefds->__nfds + 1;
__wasi_subscription_t subscriptions[maxevents];
size_t nsubscriptions = 0;
// Convert the readfds set.
for (size_t i = 0; i < readfds->__nfds; ++i) {
int fd = readfds->__fds[i];
if (fd < nfds) {
__wasi_subscription_t *subscription = &subscriptions[nsubscriptions++];
*subscription = (__wasi_subscription_t){
.userdata = fd,
.u.tag = __WASI_EVENTTYPE_FD_READ,
.u.u.fd_read.file_descriptor = fd,
};
}
}
// Convert the writefds set.
for (size_t i = 0; i < writefds->__nfds; ++i) {
int fd = writefds->__fds[i];
if (fd < nfds) {
__wasi_subscription_t *subscription = &subscriptions[nsubscriptions++];
*subscription = (__wasi_subscription_t){
.userdata = fd,
.u.tag = __WASI_EVENTTYPE_FD_WRITE,
.u.u.fd_write.file_descriptor = fd,
};
}
}
// Create extra event for the timeout.
if (timeout != NULL) {
__wasi_subscription_t *subscription = &subscriptions[nsubscriptions++];
*subscription = (__wasi_subscription_t){
.u.tag = __WASI_EVENTTYPE_CLOCK,
.u.u.clock.id = __WASI_CLOCKID_REALTIME,
};
if (!timespec_to_timestamp_clamp(timeout, &subscription->u.u.clock.timeout)) {
errno = EINVAL;
return -1;
}
}
// Execute poll().
size_t nevents;
__wasi_event_t events[nsubscriptions];
__wasi_errno_t error =
__wasi_poll_oneoff(subscriptions, events, nsubscriptions, &nevents);
if (error != 0) {
// WASI's poll requires at least one subscription, or else it returns
// `EINVAL`. Since a `pselect` with nothing to wait for is valid in POSIX,
// return `ENOTSUP` to indicate that we don't support that case.
//
// Wasm has no signal handling, so if none of the user-provided `pollfd`
// elements, nor the timeout, led us to producing even one subscription
// to wait for, there would be no way for the poll to wake up. WASI
// returns `EINVAL` in this case, but for users of `poll`, `ENOTSUP` is
// more likely to be understood.
if (nsubscriptions == 0)
errno = ENOTSUP;
else
errno = error;
return -1;
}
// Test for EBADF.
for (size_t i = 0; i < nevents; ++i) {
const __wasi_event_t *event = &events[i];
if ((event->type == __WASI_EVENTTYPE_FD_READ ||
event->type == __WASI_EVENTTYPE_FD_WRITE) &&
event->error == __WASI_ERRNO_BADF) {
errno = EBADF;
return -1;
}
}
// Clear and set entries in the result sets.
FD_ZERO(readfds);
FD_ZERO(writefds);
for (size_t i = 0; i < nevents; ++i) {
const __wasi_event_t *event = &events[i];
if (event->type == __WASI_EVENTTYPE_FD_READ) {
readfds->__fds[readfds->__nfds++] = event->userdata;
} else if (event->type == __WASI_EVENTTYPE_FD_WRITE) {
writefds->__fds[writefds->__nfds++] = event->userdata;
}
}
return readfds->__nfds + writefds->__nfds;
}
|