This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
shim_poll.c
327 lines (269 loc) · 10.8 KB
/
shim_poll.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* Implementation of system calls "poll", "ppoll", "select" and "pselect6".
*/
#include <errno.h>
#include <linux/fcntl.h>
#include "pal.h"
#include "pal_error.h"
#include "shim_fs.h"
#include "shim_handle.h"
#include "shim_internal.h"
#include "shim_lock.h"
#include "shim_signal.h"
#include "shim_table.h"
#include "shim_thread.h"
#include "shim_utils.h"
typedef unsigned long __fd_mask;
#ifndef __NFDBITS
#define __NFDBITS (8 * (int)sizeof(__fd_mask))
#endif
#ifndef __FDS_BITS
#define __FDS_BITS(set) ((set)->fds_bits)
#endif
#define __FD_ZERO(set) \
do { \
unsigned int i; \
fd_set* arr = (set); \
for (i = 0; i < sizeof(fd_set) / sizeof(__fd_mask); i++) \
__FDS_BITS(arr)[i] = 0; \
} while (0)
#define __FD_ELT(d) ((d) / __NFDBITS)
#define __FD_MASK(d) ((__fd_mask)1 << ((d) % __NFDBITS))
#define __FD_SET(d, set) ((void)(__FDS_BITS(set)[__FD_ELT(d)] |= __FD_MASK(d)))
#define __FD_CLR(d, set) ((void)(__FDS_BITS(set)[__FD_ELT(d)] &= ~__FD_MASK(d)))
#define __FD_ISSET(d, set) ((__FDS_BITS(set)[__FD_ELT(d)] & __FD_MASK(d)) != 0)
#define POLL_NOTIMEOUT ((uint64_t)-1)
static long _shim_do_poll(struct pollfd* fds, nfds_t nfds, int timeout_ms) {
if ((uint64_t)nfds > get_rlimit_cur(RLIMIT_NOFILE))
return -EINVAL;
struct shim_handle_map* map = get_cur_thread()->handle_map;
uint64_t timeout_us = timeout_ms < 0 ? POLL_NOTIMEOUT : timeout_ms * 1000ULL;
/* nfds is the upper limit for actual number of handles */
PAL_HANDLE* pals = malloc(nfds * sizeof(PAL_HANDLE));
if (!pals)
return -ENOMEM;
/* for bookkeeping, need to have a mapping FD -> {shim handle, index-in-pals} */
struct fds_mapping_t {
struct shim_handle* hdl; /* NULL if no mapping (handle is not used in polling) */
nfds_t idx; /* index from fds array to pals array */
};
struct fds_mapping_t* fds_mapping = malloc(nfds * sizeof(struct fds_mapping_t));
if (!fds_mapping) {
free(pals);
return -ENOMEM;
}
/* allocate one memory region to hold two PAL_FLG arrays: events and revents */
PAL_FLG* pal_events = malloc(nfds * sizeof(PAL_FLG) * 2);
if (!pal_events) {
free(pals);
free(fds_mapping);
return -ENOMEM;
}
PAL_FLG* ret_events = pal_events + nfds;
nfds_t pal_cnt = 0;
nfds_t nrevents = 0;
lock(&map->lock);
/* collect PAL handles that correspond to user-supplied FDs (only those that can be polled) */
for (nfds_t i = 0; i < nfds; i++) {
fds[i].revents = 0;
fds_mapping[i].hdl = NULL;
if (fds[i].fd < 0) {
/* FD is negative, must be ignored */
continue;
}
struct shim_handle* hdl = __get_fd_handle(fds[i].fd, NULL, map);
if (!hdl || !hdl->fs || !hdl->fs->fs_ops) {
/* The corresponding handle doesn't exist or doesn't provide FS-like semantics; do not
* include it in handles-to-poll array but notify user about invalid request. */
fds[i].revents = POLLNVAL;
nrevents++;
continue;
}
if (hdl->type == TYPE_FILE || hdl->type == TYPE_DEV || hdl->type == TYPE_STR) {
/* Files, devs and strings are special cases: their poll is emulated at LibOS level; do
* not include them in handles-to-poll array but instead use handle-specific
* callback.
*
* TODO: we probably should use the poll() callback in all cases. */
int shim_events = 0;
if ((fds[i].events & (POLLIN | POLLRDNORM)) && (hdl->acc_mode & MAY_READ))
shim_events |= FS_POLL_RD;
if ((fds[i].events & (POLLOUT | POLLWRNORM)) && (hdl->acc_mode & MAY_WRITE))
shim_events |= FS_POLL_WR;
int shim_revents = hdl->fs->fs_ops->poll(hdl, shim_events);
fds[i].revents = 0;
if (shim_revents & FS_POLL_RD)
fds[i].revents |= fds[i].events & (POLLIN | POLLRDNORM);
if (shim_revents & FS_POLL_WR)
fds[i].revents |= fds[i].events & (POLLOUT | POLLWRNORM);
if (fds[i].revents)
nrevents++;
continue;
}
if (!hdl->pal_handle) {
fds[i].revents = POLLNVAL;
nrevents++;
continue;
}
PAL_FLG allowed_events = 0;
if ((fds[i].events & (POLLIN | POLLRDNORM)) && (hdl->acc_mode & MAY_READ))
allowed_events |= PAL_WAIT_READ;
if ((fds[i].events & (POLLOUT | POLLWRNORM)) && (hdl->acc_mode & MAY_WRITE))
allowed_events |= PAL_WAIT_WRITE;
if ((fds[i].events & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) && !allowed_events) {
/* If user requested read/write events but they are not allowed on this handle, ignore
* this handle (but note that user may only be interested in errors, and this is a valid
* request). */
continue;
}
get_handle(hdl);
fds_mapping[i].hdl = hdl;
fds_mapping[i].idx = pal_cnt;
pals[pal_cnt] = hdl->pal_handle;
pal_events[pal_cnt] = allowed_events;
ret_events[pal_cnt] = 0;
pal_cnt++;
}
unlock(&map->lock);
bool polled = false;
long error = 0;
if (pal_cnt) {
error = DkStreamsWaitEvents(pal_cnt, pals, pal_events, ret_events, timeout_us);
polled = error == 0;
error = pal_to_unix_errno(error);
}
for (nfds_t i = 0; i < nfds; i++) {
if (!fds_mapping[i].hdl)
continue;
/* update fds.revents, but only if something was actually polled */
if (polled) {
fds[i].revents = 0;
if (ret_events[fds_mapping[i].idx] & PAL_WAIT_ERROR)
fds[i].revents |= POLLERR | POLLHUP;
if (ret_events[fds_mapping[i].idx] & PAL_WAIT_READ)
fds[i].revents |= fds[i].events & (POLLIN | POLLRDNORM);
if (ret_events[fds_mapping[i].idx] & PAL_WAIT_WRITE)
fds[i].revents |= fds[i].events & (POLLOUT | POLLWRNORM);
if (fds[i].revents)
nrevents++;
}
put_handle(fds_mapping[i].hdl);
}
free(pals);
free(pal_events);
free(fds_mapping);
if (error == -EAGAIN) {
/* `poll` returns 0 on timeout. */
error = 0;
} else if (error == -EINTR) {
/* `poll`, `ppoll`, `select` and `pselect` are not restarted after being interrupted by
* a signal handler. */
error = -ERESTARTNOHAND;
}
return nrevents ? (long)nrevents : error;
}
long shim_do_poll(struct pollfd* fds, nfds_t nfds, int timeout_ms) {
if (!is_user_memory_writable(fds, sizeof(*fds) * nfds))
return -EFAULT;
return _shim_do_poll(fds, nfds, timeout_ms);
}
long shim_do_ppoll(struct pollfd* fds, int nfds, struct timespec* tsp, const __sigset_t* sigmask,
size_t sigsetsize) {
__UNUSED(sigmask);
__UNUSED(sigsetsize);
uint64_t timeout_ms = tsp ? tsp->tv_sec * 1000ULL + tsp->tv_nsec / 1000000 : POLL_NOTIMEOUT;
return shim_do_poll(fds, nfds, timeout_ms);
}
long shim_do_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds,
struct __kernel_timeval* tsv) {
if (tsv && (tsv->tv_sec < 0 || tsv->tv_usec < 0))
return -EINVAL;
if (nfds < 0 || (uint64_t)nfds > get_rlimit_cur(RLIMIT_NOFILE))
return -EINVAL;
if (!nfds) {
if (!tsv)
return shim_do_pause();
/* special case of select(0, ..., tsv) used for sleep */
return do_nanosleep(tsv->tv_sec * TIME_US_IN_S + tsv->tv_usec, NULL);
}
if (nfds < __NFDBITS) {
/* interesting corner case: Linux always checks at least 64 first FDs */
nfds = __NFDBITS;
}
/* nfds is the upper limit for actual number of fds for poll */
struct pollfd* fds_poll = malloc(nfds * sizeof(struct pollfd));
if (!fds_poll)
return -ENOMEM;
/* populate array of pollfd's based on user-supplied readfds & writefds */
nfds_t nfds_poll = 0;
for (int fd = 0; fd < nfds; fd++) {
short events = 0;
if (readfds && __FD_ISSET(fd, readfds))
events |= POLLIN;
if (writefds && __FD_ISSET(fd, writefds))
events |= POLLOUT;
if (!events)
continue;
fds_poll[nfds_poll].fd = fd;
fds_poll[nfds_poll].events = events;
fds_poll[nfds_poll].revents = 0;
nfds_poll++;
}
/* select()/pselect() return -EBADF if invalid FD was given by user in readfds/writefds;
* note that poll()/ppoll() don't have this error code, so we return this code only here */
struct shim_handle_map* map = get_cur_thread()->handle_map;
lock(&map->lock);
for (nfds_t i = 0; i < nfds_poll; i++) {
struct shim_handle* hdl = __get_fd_handle(fds_poll[i].fd, NULL, map);
if (!hdl || !hdl->fs || !hdl->fs->fs_ops) {
/* the corresponding handle doesn't exist or doesn't provide FS-like semantics */
free(fds_poll);
unlock(&map->lock);
return -EBADF;
}
}
unlock(&map->lock);
uint64_t timeout_ms = tsv ? tsv->tv_sec * 1000ULL + tsv->tv_usec / 1000 : POLL_NOTIMEOUT;
long ret = _shim_do_poll(fds_poll, nfds_poll, timeout_ms);
if (ret < 0) {
free(fds_poll);
return ret;
}
/* modify readfds, writefds, and errorfds in-place with returned events */
if (readfds)
__FD_ZERO(readfds);
if (writefds)
__FD_ZERO(writefds);
if (errorfds)
__FD_ZERO(errorfds);
ret = 0;
for (nfds_t i = 0; i < nfds_poll; i++) {
if (readfds && (fds_poll[i].revents & POLLIN)) {
__FD_SET(fds_poll[i].fd, readfds);
ret++;
}
if (writefds && (fds_poll[i].revents & POLLOUT)) {
__FD_SET(fds_poll[i].fd, writefds);
ret++;
}
if (errorfds && (fds_poll[i].revents & POLLERR)) {
__FD_SET(fds_poll[i].fd, errorfds);
ret++;
}
}
free(fds_poll);
return ret;
}
long shim_do_pselect6(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds,
const struct __kernel_timespec* tsp, const __sigset_t* sigmask) {
__UNUSED(sigmask);
if (tsp) {
struct __kernel_timeval tsv;
tsv.tv_sec = tsp->tv_sec;
tsv.tv_usec = tsp->tv_nsec / 1000;
return shim_do_select(nfds, readfds, writefds, errorfds, &tsv);
}
return shim_do_select(nfds, readfds, writefds, errorfds, NULL);
}