forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 11
/
poll.c
455 lines (396 loc) · 13.3 KB
/
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "kernel/task.h"
#include <string.h>
#include <poll.h>
#include <fcntl.h>
#include <limits.h>
#include "misc.h"
#include "util/list.h"
#include "kernel/errno.h"
#include "kernel/fs.h"
#include "fs/fd.h"
#include "fs/poll.h"
#include "fs/real.h"
#include "fs/sockrestart.h"
#if defined(__linux__)
#include <sys/epoll.h>
#define HAVE_EPOLL 1
#elif defined(__APPLE__)
#include <sys/event.h>
#define HAVE_KQUEUE 1
#endif
static int real_poll_init(struct real_poll *real);
static void real_poll_close(struct real_poll *real);
struct real_poll_event {
#if HAVE_EPOLL
struct epoll_event real;
#elif HAVE_KQUEUE
struct kevent real;
#endif
};
static void *rpe_data(struct real_poll_event *rpe);
static int rpe_events(struct real_poll_event *rpe);
static int real_poll_wait(struct real_poll *real, struct real_poll_event *events, int max, struct timespec *timeout);
static int real_poll_update(struct real_poll *real, int fd, int types, void *data);
// lock order: fd, then poll
struct poll *poll_create(void) {
struct poll *poll = malloc(sizeof(struct poll));
if (poll == NULL)
return ERR_PTR(_ENOMEM);
int err = real_poll_init(&poll->real);
if (err < 0)
return ERR_PTR(errno_map());
poll->waiters = 0;
poll->notify_pipe[0] = -1;
poll->notify_pipe[1] = -1;
list_init(&poll->poll_fds);
list_init(&poll->pollfd_freelist);
lock_init(&poll->lock, "poll_create\0");
return poll;
}
static inline bool poll_fd_is_real(struct poll_fd *pollfd) {
return pollfd->fd->ops->poll == realfs_poll;
}
// does not do its own locking
static struct poll_fd *poll_find_fd(struct poll *poll, struct fd *fd) {
struct poll_fd *poll_fd, *tmp;
list_for_each_entry_safe(&poll->poll_fds, poll_fd, tmp, fds) {
if (poll_fd->fd == fd)
return poll_fd;
}
return NULL;
}
// See comment on pollfd_freelist for context
static void poll_fd_free(struct poll_fd *poll_fd) {
struct poll *poll = poll_fd->poll;
memset(poll_fd, 0xba, sizeof(*poll_fd));
poll_fd->poll = NULL; // used to mark it as free
list_add(&poll->pollfd_freelist, &poll_fd->fds);
}
bool poll_has_fd(struct poll *poll, struct fd *fd) {
return poll_find_fd(poll, fd) != NULL;
}
int poll_add_fd(struct poll *poll, struct fd *fd, int types, union poll_fd_info info) {
int err;
lock(&fd->poll_lock, 0);
lock(&poll->lock, 0);
struct poll_fd *poll_fd;
if (!list_empty(&poll->pollfd_freelist)) {
poll_fd = list_first_entry(&poll->pollfd_freelist, struct poll_fd, fds);
list_remove(&poll_fd->fds);
} else {
poll_fd = malloc(sizeof(struct poll_fd));
if (poll_fd == NULL) {
err = _ENOMEM;
goto out;
}
}
poll_fd->fd = fd;
poll_fd->poll = poll;
poll_fd->types = types;
poll_fd->info = info;
poll_fd->triggered_types = 0;
if (poll_fd_is_real(poll_fd)) {
err = real_poll_update(&poll->real, fd->real_fd, types, poll_fd);
if (err < 0) {
free(poll_fd);
err = errno_map();
goto out;
}
}
list_add(&fd->poll_fds, &poll_fd->polls);
list_add(&poll->poll_fds, &poll_fd->fds);
err = 0;
out:
unlock(&poll->lock);
unlock(&fd->poll_lock);
return err;
}
int poll_del_fd(struct poll *poll, struct fd *fd) {
int err;
lock(&fd->poll_lock, 0);
lock(&poll->lock, 0);
struct poll_fd *poll_fd = poll_find_fd(poll, fd);
if (poll_fd == NULL) {
err = _ENOENT;
goto out;
}
if (poll_fd_is_real(poll_fd)) {
err = real_poll_update(&poll->real, fd->real_fd, 0, poll_fd);
if (err < 0) {
err = errno_map();
goto out;
}
}
list_remove(&poll_fd->polls);
list_remove(&poll_fd->fds);
poll_fd_free(poll_fd);
err = 0;
out:
unlock(&poll->lock);
unlock(&fd->poll_lock);
return err;
}
int poll_mod_fd(struct poll *poll, struct fd *fd, int types, union poll_fd_info info) {
int err;
lock(&fd->poll_lock, 0);
lock(&poll->lock, 0);
struct poll_fd *poll_fd = poll_find_fd(poll, fd);
if (poll_fd == NULL) {
err = _ENOENT;
goto out;
}
if (poll_fd_is_real(poll_fd)) {
err = real_poll_update(&poll->real, fd->real_fd, types, poll_fd);
if (err < 0) {
err = errno_map();
goto out;
}
}
poll_fd->types = types;
poll_fd->info = info;
poll_fd->triggered_types &= types;
err = 0;
out:
unlock(&poll->lock);
unlock(&fd->poll_lock);
return err;
}
void poll_cleanup_fd(struct fd *fd) {
lock(&fd->poll_lock, 0);
struct poll_fd *poll_fd, *tmp;
list_for_each_entry_safe(&fd->poll_fds, poll_fd, tmp, polls) {
lock(&poll_fd->poll->lock, 0);
if (poll_fd_is_real(poll_fd))
real_poll_update(&poll_fd->poll->real, fd->real_fd, 0, poll_fd);
list_remove(&poll_fd->polls);
list_remove(&poll_fd->fds);
unlock(&poll_fd->poll->lock);
poll_fd_free(poll_fd);
}
unlock(&fd->poll_lock);
}
void poll_wakeup(struct fd *fd, int events) {
struct poll_fd *poll_fd;
lock(&fd->poll_lock, 0);
list_for_each_entry(&fd->poll_fds, poll_fd, polls) {
struct poll *poll = poll_fd->poll;
lock(&poll->lock,0);
if (poll_fd->types & POLL_EDGETRIGGERED)
poll_fd->triggered_types &= ~events;
if (poll->notify_pipe[1] != -1)
write(poll->notify_pipe[1], "", 1);
unlock(&poll->lock);
// oneshot?
}
unlock(&fd->poll_lock);
}
int poll_wait(struct poll *poll_, poll_callback_t callback, void *context, struct timespec *timeout) {
lock(&poll_->lock, 0);
// acquire the pipe
if (poll_->waiters++ == 0) {
assert(poll_->notify_pipe[0] == -1 && poll_->notify_pipe[1] == -1);
if (pipe(poll_->notify_pipe) < 0) {
unlock(&poll_->lock);
return errno_map();
}
fcntl(poll_->notify_pipe[0], F_SETFL, O_NONBLOCK);
fcntl(poll_->notify_pipe[1], F_SETFL, O_NONBLOCK);
real_poll_update(&poll_->real, poll_->notify_pipe[0], POLL_READ, NULL);
}
// TODO this is pretty broken with regards to timeouts
int res = 0;
while (true) {
// check if any fds are ready
struct poll_fd *poll_fd, *tmp;
list_for_each_entry_safe(&poll_->poll_fds, poll_fd, tmp, fds) {
struct fd *fd = poll_fd->fd;
int poll_types = 0;
if (fd->ops->poll)
poll_types = fd->ops->poll(fd);
poll_types &= poll_fd->types | POLL_HUP | POLL_ERR;
if (poll_fd->types & POLL_EDGETRIGGERED) {
poll_types &= ~poll_fd->triggered_types;
}
if (poll_types) {
if (callback(context, poll_types, poll_fd->info) == 1)
res++;
// The real poll does not actually get the FDs set as oneshot.
// But this loop is done while holding the lock, so only one
// thread can get each oneshot event. This doesn't solve the
// thundering herd problem at all, but at least the semantics
// are right. I'll just leave that as a TODO.
if (poll_fd->types & POLL_ONESHOT) {
list_remove(&poll_fd->polls);
list_remove(&poll_fd->fds);
if (poll_fd_is_real(poll_fd)) {
real_poll_update(&poll_->real, fd->real_fd, 0, NULL);
}
free(poll_fd);
}
if (poll_fd->types & POLL_EDGETRIGGERED) {
poll_fd->triggered_types |= poll_types;
}
}
}
if (res > 0)
break;
lock(¤t->sighand->lock,0);
bool signal_pending = !!(current->pending & ~current->blocked);
unlock(¤t->sighand->lock);
if (signal_pending) {
res = _EINTR;
break;
}
// wait for a ready notification
list_for_each_entry(&poll_->poll_fds, poll_fd, fds) {
sockrestart_begin_listen_wait(poll_fd->fd);
}
int err;
struct real_poll_event e[4];
do {
unlock(&poll_->lock);
err = real_poll_wait(&poll_->real, e, sizeof(e)/sizeof(e[0]), timeout);
lock(&poll_->lock, 0);
} while (sockrestart_should_restart_listen_wait(1) && errno == EINTR);
list_for_each_entry(&poll_->poll_fds, poll_fd, fds) {
sockrestart_end_listen_wait(poll_fd->fd);
}
if (err < 0) {
res = errno_map();
break;
}
if (err == 0) {
// timed out and still nobody is ready
break;
}
// dead with any edge-triggered notifications
for (int i = 0; i < err; i++) {
struct poll_fd *triggered_poll_fd = rpe_data(&e[i]);
if (triggered_poll_fd != NULL && triggered_poll_fd->poll != NULL &&
triggered_poll_fd->types & POLL_EDGETRIGGERED) {
triggered_poll_fd->triggered_types &= ~rpe_events(&e[i]);
}
}
char fuck;
if (read(poll_->notify_pipe[0], &fuck, 1) < 0 && errno != EAGAIN) {
res = errno_map();
break;
}
}
// release the pipe
if (--poll_->waiters == 0) {
close(poll_->notify_pipe[0]);
close(poll_->notify_pipe[1]);
poll_->notify_pipe[0] = -1;
poll_->notify_pipe[1] = -1;
}
unlock(&poll_->lock);
return res;
}
void poll_destroy(struct poll *poll) {
struct poll_fd *poll_fd;
struct poll_fd *tmp;
int fug = current->reference.count; // Debugging. Xcode 15.1 can't 'decode' 'current' or any of its components. :-(
while(task_ref_cnt_get(current, 0) > 2) {
nanosleep(&lock_pause, NULL);
}
list_for_each_entry_safe(&poll->poll_fds, poll_fd, tmp, fds) {
lock(&poll_fd->fd->poll_lock, 0);
list_remove(&poll_fd->polls);
list_remove(&poll_fd->fds);
unlock(&poll_fd->fd->poll_lock);
free(poll_fd);
}
while(task_ref_cnt_get(current, 0) > 1) {
nanosleep(&lock_pause, NULL);
}
list_for_each_entry_safe(&poll->pollfd_freelist, poll_fd, tmp, fds) {
while(task_ref_cnt_get(current, 0)) {
nanosleep(&lock_pause, NULL);
}
list_remove(&poll_fd->fds);
free(poll_fd);
}
real_poll_close(&poll->real);
free(poll);
}
// Platform-specific real_poll implementations
#if HAVE_EPOLL
static int real_poll_init(struct real_poll *real) {
real->fd = epoll_create1(0);
if (real->fd < 0)
return -1;
return 0;
}
static int real_poll_wait(struct real_poll *real, struct real_poll_event *events, int max, struct timespec *timeout) {
int timeout_millis = -1;
if (timeout != NULL)
timeout_millis = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000;
return epoll_wait(real->fd, (struct epoll_event *) events, max, timeout_millis);
}
static int real_poll_update(struct real_poll *real, int fd, int types, void *data) {
types &= ~EPOLLONESHOT;
if (types == 0)
return epoll_ctl(real->fd, EPOLL_CTL_DEL, fd, NULL);
struct epoll_event epevent = {.events = types, .data.ptr = data};
int err = epoll_ctl(real->fd, EPOLL_CTL_MOD, fd, &epevent);
if (err < 0 && errno == ENOENT)
err = epoll_ctl(real->fd, EPOLL_CTL_ADD, fd, &epevent);
return err;
}
static void *rpe_data(struct real_poll_event *rpe) {
return rpe->real.data.ptr;
}
static int rpe_events(struct real_poll_event *rpe) {
return rpe->real.events;
}
#elif HAVE_KQUEUE
static int real_poll_init(struct real_poll *real) {
real->fd = kqueue();
if (real->fd < 0)
return -1;
return 0;
}
static int real_poll_update(struct real_poll *real, int fd, int types, void *data) {
struct kevent e[3] = {
{.filter = EVFILT_READ, .flags = types & (POLL_READ | POLL_HUP) ? EV_ADD : EV_DELETE},
{.filter = EVFILT_WRITE, .flags = types & POLL_WRITE ? EV_ADD : EV_DELETE},
{.filter = EVFILT_EXCEPT, .flags = types & POLL_ERR ? EV_ADD : EV_DELETE},
};
if (!(types & POLL_READ) && types & POLL_HUP) {
// Set the low water mark really high so we'll only get woken up on a hangup
e[0].fflags = NOTE_LOWAT;
e[0].data = INT_MAX;
}
for (int i = 0; i < 3; i++) {
e[i].ident = fd;
e[i].udata = data;
e[i].flags |= EV_RECEIPT;
if (types & POLL_EDGETRIGGERED)
e[i].flags |= EV_CLEAR;
}
return kevent(real->fd, e, 3, e, 3, NULL);
}
static int real_poll_wait(struct real_poll *real, struct real_poll_event *events, int max, struct timespec *timeout) {//mkemke
return kevent(real->fd, NULL, 0, (struct kevent *) events, max, timeout);
}
static void *rpe_data(struct real_poll_event *rpe) {
return rpe->real.udata;
}
static int rpe_events(struct real_poll_event *rpe) {
if (rpe->real.filter == EVFILT_READ) {
int events = 0;
if (rpe->real.data > 0)
events |= POLL_READ;
if (rpe->real.flags & EV_EOF)
events |= POLL_HUP;
return events;
}
if (rpe->real.filter == EVFILT_WRITE) return POLL_WRITE;
if (rpe->real.filter == EVFILT_EXCEPT) return POLL_ERR;
return 0;
}
#endif
static void real_poll_close(struct real_poll *real) {
close(real->fd);
}