-
Notifications
You must be signed in to change notification settings - Fork 1
/
fd.c
838 lines (713 loc) · 18.7 KB
/
fd.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//===-- fd.c --------------------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define _LARGEFILE64_SOURCE
#include "fd.h"
#include "sockets.h"
#include "pipes.h"
#include "buffers.h"
#include "files.h"
#include "symfs.h"
#include "misc.h"
#include "klee/klee.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#ifndef __FreeBSD__
#include <sys/vfs.h>
#endif
#include <dirent.h>
#include <sys/select.h>
#include <unistd.h>
// Forward declaration
static int _dup(int oldfd, int startfd);
// For multiplexing multiple syscalls into a single _read/_write op. set
#define _IO_TYPE_SCATTER_GATHER 0x1
#define _IO_TYPE_POSITIONAL 0x2
////////////////////////////////////////////////////////////////////////////////
// Internal routines
////////////////////////////////////////////////////////////////////////////////
/* Returns pointer to the file entry for a valid fd */
fd_entry_t *__get_fd(int fd) {
if (fd>=0 && fd<MAX_FDS) {
fd_entry_t *f = &__exe_env.fds[fd];
if (f->attr & eOpen)
return f;
}
return NULL;
}
mode_t umask(mode_t mask) {
mode_t r = __exe_env.umask;
__exe_env.umask = mask & 0777;
return r;
}
int __fd_allocate() {
int fd;
for (fd = 0; fd < MAX_FDS; ++fd) {
if (!(__exe_env.fds[fd].attr & eOpen)) {
fd_entry_t *f = &__exe_env.fds[fd];
assert(f->attr == 0 && !f->io_object);
f->attr |= eOpen;
break;
}
}
return fd;
}
// note: this does not free io_object
void __fd_clear(int fd) {
fd_entry_t *f = __get_fd(fd);
assert(f != NULL);
memset(f, 0, sizeof(*f));
}
static int _is_blocking(int fd, int event) {
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
return 0;
}
switch (event) {
case EVENT_READ:
if ((fde->io_object->flags & O_ACCMODE) == O_WRONLY) {
return 0;
}
break;
case EVENT_WRITE:
if ((fde->io_object->flags & O_ACCMODE) == O_RDONLY) {
return 0;
}
break;
default:
assert(0 && "invalid event");
}
if (fde->attr & eIsFile) {
return _is_blocking_file((file_t*)fde->io_object, event);
} else if (fde->attr & eIsPIPE) {
return _is_blocking_pipe((pipe_end_t*)fde->io_object, event);
} else if (fde->attr & eIsSocket) {
return _is_blocking_socket((socket_t*)fde->io_object, event);
} else {
assert(0 && "invalid fd");
}
}
/*
* Normal read/write
*/
// input fd is guaranteed to be valid
static ssize_t _clean_read(int fd, void *buf, size_t count, off64_t offset) {
static int n_calls = 0;
n_calls++;
fd_entry_t *fde = &__exe_env.fds[fd];
if (offset >= 0 && !(fde->attr & eIsFile)) {
errno = EINVAL;
return -1;
}
if (buf == NULL) {
errno = EFAULT;
return -1;
}
if (count == 0)
return 0;
if (__sym_fs.max_failures && *__sym_fs.read_fail == n_calls) {
__sym_fs.max_failures--;
errno = EIO;
return -1;
}
posix_debug_msg("Reading %lu bytes of data from FD %d\n", count, fd);
if (fde->attr & eIsFile) {
return _read_file((file_t *)fde->io_object, buf, count, offset);
} else if (fde->attr & eIsPIPE) {
return _read_pipe((pipe_end_t *)fde->io_object, buf, count);
} else if (fde->attr & eIsSocket) {
return _read_socket((socket_t *)fde->io_object, buf, count);
} else {
assert(0 && "Invalid file descriptor");
return 0;
}
}
// input fd is guaranteed to be valid
static ssize_t _clean_write(int fd, const void *buf, size_t count, off_t offset) {
static int n_calls = 0;
n_calls++;
fd_entry_t *fde = &__exe_env.fds[fd];
if (offset >= 0 && !(fde->attr & eIsFile)) {
errno = EINVAL;
return -1;
}
if (__sym_fs.max_failures && *__sym_fs.write_fail == n_calls) {
__sym_fs.max_failures--;
errno = EIO;
return -1;
}
if (fde->attr & eIsFile) {
return _write_file((file_t*)fde->io_object, buf, count, offset);
} else if (fde->attr & eIsPIPE) {
return _write_pipe((pipe_end_t*)fde->io_object, buf, count);
} else if (fde->attr & eIsSocket) {
return _write_socket((socket_t*)fde->io_object, buf, count);
} else {
assert(0 && "Invalid file descriptor");
return 0;
}
}
/*
* Vectorized scatter read / gather write
*/
ssize_t _scatter_read(int fd, const struct iovec *iov, int iovcnt) {
size_t count = 0;
int i;
for (i = 0; i < iovcnt; i++) {
if (iov[i].iov_len == 0)
continue;
if (count > 0 && _is_blocking(fd, EVENT_READ))
return count;
ssize_t res = _clean_read(fd, iov[i].iov_base, iov[i].iov_len, -1);
if (res == -1) {
assert(count == 0);
return res;
}
count += res;
if ((size_t)res < iov[i].iov_len)
break;
}
return count;
}
ssize_t _gather_write(int fd, const struct iovec *iov, int iovcnt) {
size_t count = 0;
int i;
for (i = 0; i < iovcnt; i++) {
if (iov[i].iov_len == 0)
continue;
// If we have something written, but now we blocked, we just return
// what we have
if (count > 0 && _is_blocking(fd, EVENT_WRITE))
return count;
ssize_t res = _clean_write(fd, iov[i].iov_base, iov[i].iov_len, -1);
if (res == -1) {
assert(count == 0);
return res;
}
count += res;
if ((size_t)res < iov[i].iov_len)
break;
}
return count;
}
////////////////////////////////////////////////////////////////////////////////
// FD specific POSIX routines
////////////////////////////////////////////////////////////////////////////////
int close(int fd) {
static int n_calls = 0;
fd_entry_t *fde;
n_calls++;
fde = __get_fd(fd);
if (!fde) {
errno = EBADF;
return -1;
}
posix_debug_msg("Attempting to close %d, attr %#x\n", fd, fde->attr);
if (__sym_fs.max_failures && *__sym_fs.close_fail == n_calls) {
__sym_fs.max_failures--;
errno = EIO;
return -1;
}
if (fde->io_object->refcount > 1) {
// Just clear this FD
fde->io_object->refcount--;
__fd_clear(fd);
return 0;
}
int res;
// Check the type of the file descriptor
if (fde->attr & eIsFile) {
if (fd >= 0 && fd <= 2) {
// special fds, statically initialized, should not be freed
res = 0;
} else {
res = _close_file((file_t *)(fde->io_object));
}
} else if (fde->attr & eIsPIPE) {
res = _close_pipe((pipe_end_t*)fde->io_object);
} else if (fde->attr & eIsSocket) {
res = _close_socket((socket_t*)fde->io_object);
} else {
assert(0 && "Invalid file descriptor");
return -1;
}
if (res == 0) {
__fd_clear(fd);
}
return res;
}
/*
* Read related POSIX API
*/
static ssize_t _read(int fd, int type, ...) {
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
errno = EBADF;
return -1;
}
// Check for permissions
if ((fde->io_object->flags & O_ACCMODE) == O_WRONLY) {
posix_debug_msg("Permission error (flags: %o)\n", fde->io_object->flags);
errno = EBADF;
return -1;
}
if (INJECT_FAULT(read, EINTR)) {
return -1;
}
if (fde->attr & eIsFile) {
if (INJECT_FAULT(read, EIO)) {
return -1;
}
} else if (fde->attr & eIsSocket) {
socket_t *sock = (socket_t*)fde->io_object;
if (sock->status == SOCK_STATUS_CONNECTED &&
INJECT_FAULT(read, ECONNRESET)) {
return -1;
}
}
// Now perform the real thing
if (type == _IO_TYPE_SCATTER_GATHER) {
va_list ap;
va_start(ap, type);
struct iovec *iov = va_arg(ap, struct iovec*);
int iovcnt = va_arg(ap, int);
va_end(ap);
return _scatter_read(fd, iov, iovcnt);
} else {
va_list ap;
va_start(ap, type);
void *buf = va_arg(ap, void*);
size_t count = va_arg(ap, size_t);
off64_t offset = -1;
if (type == _IO_TYPE_POSITIONAL)
offset = va_arg(ap, off64_t);
va_end(ap);
return _clean_read(fd, buf, count, offset);
}
}
DEFINE_MODEL(ssize_t, read, int fd, void *buf, size_t count) {
return _read(fd, 0, buf, count);
}
DEFINE_MODEL(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) {
return _read(fd, _IO_TYPE_SCATTER_GATHER, iov, iovcnt);
}
DEFINE_MODEL(ssize_t, pread64, int fd, void *buf, size_t count, off64_t offset) {
return _read(fd, _IO_TYPE_POSITIONAL, buf, count, offset);
}
/*
* Write related POSIX API
*/
static ssize_t _write(int fd, int type, ...) {
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
errno = EBADF;
return -1;
}
// Check for permissions
if ((fde->io_object->flags & O_ACCMODE) == O_RDONLY) {
errno = EBADF;
return -1;
}
if (INJECT_FAULT(write, EINTR)) {
return -1;
}
if (fde->attr & eIsFile) {
if (INJECT_FAULT(write, EIO, EFBIG, ENOSPC)) {
return -1;
}
} else if (fde->attr & eIsSocket) {
socket_t *sock = (socket_t*)fde->io_object;
if (sock->status == SOCK_STATUS_CONNECTED &&
INJECT_FAULT(write, ECONNRESET, ENOMEM)) {
return -1;
}
}
if (type == _IO_TYPE_SCATTER_GATHER) {
va_list ap;
va_start(ap, type);
struct iovec *iov = va_arg(ap, struct iovec*);
int iovcnt = va_arg(ap, int);
va_end(ap);
return _gather_write(fd, iov, iovcnt);
} else {
va_list ap;
va_start(ap, type);
void *buf = va_arg(ap, void*);
size_t count = va_arg(ap, size_t);
off_t offset = -1;
if (type == _IO_TYPE_POSITIONAL)
offset = va_arg(ap, off_t);
va_end(ap);
return _clean_write(fd, buf, count, offset);
}
}
DEFINE_MODEL(ssize_t, write, int fd, const void *buf, size_t count) {
return _write(fd, 0, buf, count);
}
DEFINE_MODEL(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
return _write(fd, _IO_TYPE_SCATTER_GATHER, iov, iovcnt);
}
DEFINE_MODEL(ssize_t, pwrite, int fd, const void *buf, size_t count, off_t offset) {
return _write(fd, _IO_TYPE_POSITIONAL, buf, count, offset);
}
int __fd_fstat(int fd, struct stat64 *buf) {
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
errno = EBADF;
return -1;
}
if (fde->attr & eIsFile) {
return _stat_file((file_t*)fde->io_object, buf);
} else if (fde->attr & eIsPIPE) {
return _stat_pipe((pipe_end_t*)fde->io_object, buf);
} else if (fde->attr & eIsSocket) {
return _stat_socket((socket_t*)fde->io_object, buf);
} else {
assert(0 && "Invalid file descriptor");
}
}
#if __WORDSIZE == 64
int ioctl(int fd, unsigned long int request, ...) {
#else
int ioctl(int fd, unsigned long request, ...) {
#endif
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
errno = EBADF;
return -1;
}
va_list ap;
va_start(ap, request);
char *argp = va_arg(ap, char*);
va_end(ap);
if (fde->attr & eIsFile) {
return _ioctl_file((file_t*)fde->io_object, request, argp);
} else if (fde->attr & eIsSocket) {
return _ioctl_socket((socket_t*)fde->io_object, request, argp);
} else {
klee_warning("ioctl operation not supported on file descriptor");
errno = EINVAL;
return -1;
}
}
int fcntl(int fd, int cmd, ...) {
fd_entry_t *fde = __get_fd(fd);
va_list ap;
unsigned arg; /* 32 bit assumption (int/ptr) */
if (!fde) {
errno = EBADF;
return -1;
}
#ifdef F_GETSIG
if (cmd==F_GETFD || cmd==F_GETFL || cmd==F_GETOWN || cmd==F_GETSIG ||
cmd==F_GETLEASE || cmd==F_NOTIFY) {
#else
if (cmd==F_GETFD || cmd==F_GETFL || cmd==F_GETOWN) {
#endif
arg = 0;
} else {
va_start(ap, cmd);
arg = va_arg(ap, int);
va_end(ap);
}
// This is the side path, do syscall if fnctl a concrete file
if (fde->attr & eIsFile) {
file_t *file = (file_t *)(fde->io_object);
if (_file_is_concrete(file)) {
return syscall(__NR_fcntl, file->concrete_fd, cmd, arg);
}
}
switch (cmd) {
case F_GETFD: {
int flags = 0;
if (fde->attr & eCloseOnExec)
flags |= FD_CLOEXEC;
return flags;
}
case F_SETFD: {
fde->attr &= ~eCloseOnExec;
if (arg & FD_CLOEXEC)
fde->attr |= eCloseOnExec;
return 0;
}
case F_GETFL: {
return fde->io_object->flags;
}
case F_SETFL: {
if (arg & (O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME)) {
klee_warning("unsupported fnctl flags");
errno = EINVAL;
return -1;
}
// This is from cloud9 and I do not understand why doing double check
// here. (Double check unsupported fnctl flags)
fde->io_object->flags |=
(arg & (O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK));
return 0;
}
case F_DUPFD:
case F_DUPFD_CLOEXEC: {
int res = _dup(fd, arg);
if (res < -1) {
return res;
}
if (cmd == F_DUPFD_CLOEXEC) {
__get_fd(res)->attr |= FD_CLOEXEC;
}
return res;
}
default:
klee_warning("symbolic file, ignoring (EINVAL)");
errno = EINVAL;
return -1;
}
}
// dup, dup2, dup3
DEFINE_MODEL(int, dup3, int oldfd, int newfd, int flags) {
fd_entry_t *fde = __get_fd(oldfd);
if (!fde) {
errno = EBADF;
return -1;
}
if (newfd >= MAX_FDS) {
errno = EBADF;
return -1;
}
if (newfd == oldfd) {
errno = EINVAL;
return -1;
}
if (INJECT_FAULT(dup, EMFILE, EINTR)) {
return -1;
}
fd_entry_t *newfde = __get_fd(newfd);
if (newfde) {
CALL_MODEL(close, newfd);
}
__exe_env.fds[newfd] = __exe_env.fds[oldfd];
newfde = __get_fd(newfd);
newfde->io_object->refcount++;
if (flags & O_CLOEXEC) {
newfde->attr |= eCloseOnExec;
} else {
newfde->attr &= ~eCloseOnExec;
}
posix_debug_msg("New duplicate of %d: %d, newattr %#x\n", oldfd, newfd,
newfde->attr);
return newfd;
}
DEFINE_MODEL(int, dup2, int oldfd, int newfd) {
fd_entry_t *fde = __get_fd(oldfd);
if (!fde) {
errno = EBADF;
return -1;
}
if (newfd >= MAX_FDS) {
errno = EBADF;
return -1;
}
if (newfd == oldfd)
return 0;
return CALL_MODEL(dup3, oldfd, newfd, 0);
}
static int _dup(int oldfd, int startfd) {
fd_entry_t *fde = __get_fd(oldfd);
if (!fde) {
errno = EBADF;
return -1;
}
if (startfd < 0 || startfd >= MAX_FDS) {
errno = EBADF;
return -1;
}
// Find the lowest unused file descriptor
int fd;
for (fd = startfd; fd < MAX_FDS; fd++) {
if (!(__exe_env.fds[fd].attr & eOpen))
break;
}
if (fd == MAX_FDS) {
errno = EMFILE;
return -1;
}
return CALL_MODEL(dup2, oldfd, fd);
}
DEFINE_MODEL(int, dup, int oldfd) {
return _dup(oldfd, 0);
}
// select() ////////////////////////////////////////////////////////////////////
static int _validate_fd_set(int nfds, fd_set *fds) {
int res = 0;
int fd;
for (fd = 0; fd < nfds; fd++) {
if (!_FD_ISSET(fd, fds))
continue;
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
klee_warning("unallocated FD");
return -1;
}
res++;
}
return res;
}
static int _register_events(int fd, wlist_id_t wlist, int events) {
fd_entry_t *fde = __get_fd(fd);
assert(fde);
if (fde->attr & eIsPIPE) {
return _register_events_pipe((pipe_end_t*)fde->io_object, wlist, events);
} else if (fde->attr & eIsSocket) {
return _register_events_socket((socket_t*)fde->io_object, wlist, events);
} else {
assert(0 && "invalid fd");
}
}
static void _deregister_events(int fd, wlist_id_t wlist, int events) {
fd_entry_t *fde = __get_fd(fd);
if (!fde) {
// Silently exit
return;
}
if (fde->attr & eIsPIPE) {
_deregister_events_pipe((pipe_end_t*)fde->io_object, wlist, events);
} else if (fde->attr & eIsSocket) {
_deregister_events_socket((socket_t*)fde->io_object, wlist, events);
} else {
assert(0 && "invalid fd");
}
}
// XXX Maybe we should break this into more pieces?
DEFINE_MODEL(int, select, int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout) {
if (nfds < 0 || nfds > FD_SETSIZE) {
errno = EINVAL;
return -1;
}
int totalfds = 0;
static fd_set out_readfds;
static fd_set out_writefds;
// Compute the minimum size of the FD set
int setsize = ((nfds / NFDBITS) + ((nfds % NFDBITS) ? 1 : 0)) * (NFDBITS / 8);
// Validating the fds
if (readfds) {
int res = _validate_fd_set(nfds, readfds);
if (res == -1) {
errno = EBADF;
return -1;
}
totalfds += res;
_FD_ZERO(&out_readfds);
}
if (writefds) {
int res = _validate_fd_set(nfds, writefds);
if (res == -1) {
errno = EBADF;
return -1;
}
totalfds += res;
_FD_ZERO(&out_writefds);
}
if (exceptfds) {
int res = _validate_fd_set(nfds, exceptfds);
if (res == -1) {
errno = EBADF;
return -1;
}
totalfds += res;
}
if (INJECT_FAULT(select, ENOMEM)) {
return -1;
}
if (timeout != NULL && totalfds == 0) {
klee_warning("simulating timeout");
// We just return timeout
if (timeout->tv_sec != 0 || timeout->tv_usec != 0)
_yield_sleep(timeout->tv_sec, timeout->tv_usec);
return 0;
}
// No out_exceptfds here. This means that the thread will hang if select()
// is called with FDs only in exceptfds.
wlist_id_t wlist = 0;
int res = 0;
do {
int fd;
// First check to see if we have anything available
for (fd = 0; fd < nfds; fd++) {
if (readfds && _FD_ISSET(fd, readfds) && !_is_blocking(fd, EVENT_READ)) {
_FD_SET(fd, &out_readfds);
res++;
}
if (writefds && _FD_ISSET(fd, writefds) && !_is_blocking(fd, EVENT_WRITE)) {
_FD_SET(fd, &out_writefds);
res++;
}
}
if (res > 0)
break;
// Nope, bad luck...
// We wait until at least one FD becomes non-blocking
// In particular, if all FD blocked, then all of them would be
// valid FDs (none of them closed in the mean time)
if (wlist == 0)
wlist = klee_get_wlist();
int fail = 0;
// Register ourselves to the relevant FDs
for (fd = 0; fd < nfds; fd++) {
int events = 0;
if (readfds && _FD_ISSET(fd, readfds)) {
events |= EVENT_READ;
}
if (writefds && _FD_ISSET(fd, writefds)) {
events |= EVENT_WRITE;
}
if (events != 0) {
if (_register_events(fd, wlist, events) == -1) {
fail = 1;
break;
}
}
}
if (!fail)
__thread_sleep(wlist);
// Now deregister, in order to avoid useless notifications
for (fd = 0; fd < nfds; fd++) {
int events = 0;
if (readfds && _FD_ISSET(fd, readfds)) {
events |= EVENT_READ;
}
if (writefds && _FD_ISSET(fd, writefds)) {
events |= EVENT_WRITE;
}
if (events != 0) {
_deregister_events(fd, wlist, events);
}
}
if (fail) {
errno = ENOMEM;
return -1;
}
} while (1);
if (readfds) {
memcpy(readfds, &out_readfds, setsize);
}
if (writefds) {
memcpy(writefds, &out_writefds, setsize);
}
if (exceptfds) {
memset(exceptfds, 0, setsize);
}
return res;
}