-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.c
1045 lines (812 loc) · 27.9 KB
/
operations.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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
#define _GNU_SOURCE
#include "operations.h"
#include "macros.h"
#include "reactor-internal.h"
#include "stack.h"
#include <assert.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ucontext.h>
#ifdef HAVE_MEMCHECK_H
#include <valgrind/memcheck.h>
#endif
int
iou_accept(reactor_t * reactor, int fd, struct sockaddr *addr, socklen_t *addrlen) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_accept(sqe, fd, addr, addrlen, 0);
reactor_promise(reactor, sqe);
return reactor->result;
}
void
iou_barrier(reactor_t * reactor) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_nop(sqe);
io_uring_sqe_set_flags(sqe, IOSQE_IO_DRAIN);
reactor_future_fake(reactor, sqe);
}
void
iou_cancel_fd_all(reactor_t * reactor, int fd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_cancel_fd(sqe, fd, IORING_ASYNC_CANCEL_ALL);
reactor_future_fake(reactor, sqe);
}
void
iou_cancel_fd_any(reactor_t * reactor, int fd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_cancel_fd(sqe, fd, 0);
reactor_future_fake(reactor, sqe);
}
int
iou_close(reactor_t * reactor, int fd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_close(sqe, fd);
reactor_promise(reactor, sqe);
return reactor->result;
}
void
iou_close_fast(reactor_t * reactor, int fd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_close(sqe, fd);
reactor_future_fake(reactor, sqe);
}
int
iou_connect(reactor_t * reactor, int sockfd, const struct sockaddr *addr, socklen_t addrlen, const struct timespec delta) {
assert(reactor);
switch (timespec_when(normalize_timespec(delta))) {
case -1: {
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_connect(sqe, sockfd, addr, addrlen);
reactor_promise(reactor, sqe);
break;
}
case 0: {
reactor_reserve_sqes(reactor, 2);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_connect(sqe, sockfd, addr, addrlen);
reactor_promise_nonchalant(reactor, sqe);
break;
}
case 1: {
struct timespec when = reify_timespec(delta);
reactor_reserve_sqes(reactor, 2);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_connect(sqe, sockfd, addr, addrlen);
reactor_promise_impatient(reactor, sqe, when);
break;
}
default: abort();
}
return reactor->result;
}
int
iou_epoll_add(reactor_t * reactor, int epfd, int fd, struct epoll_event *event) {
return iou_epoll_ctl(reactor, epfd, EPOLL_CTL_ADD, fd, event);
}
int
iou_epoll_ctl(reactor_t * reactor, int epfd, int op, int fd, struct epoll_event *event) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_epoll_ctl(sqe, epfd, fd, op, event);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_epoll_del(reactor_t * reactor, int epfd, int fd) {
return iou_epoll_ctl(reactor, epfd, EPOLL_CTL_DEL, fd, NULL);
}
int
iou_epoll_mod(reactor_t * reactor, int epfd, int fd, struct epoll_event *event) {
return iou_epoll_ctl(reactor, epfd, EPOLL_CTL_MOD, fd, event);
}
int
iou_epoll_set(reactor_t * reactor, int epfd, int fd, struct epoll_event *event) {
int result = iou_epoll_mod(reactor, epfd, fd, event);
if (result == -ENOENT)
result = iou_epoll_add(reactor, epfd, fd, event);
return result;
}
int
iou_exchange(reactor_t * reactor, const char *oldpath, const char *newpath) {
return iou_exchangeat(reactor, AT_FDCWD, oldpath, newpath);
}
int
iou_exchangeat(reactor_t * reactor, int dirfd, const char *oldpath, const char *newpath) {
return iou_renameat(reactor, dirfd, oldpath, dirfd, newpath, RENAME_EXCHANGE);
}
bool
iou_exists(reactor_t * reactor, const char *pathname) {
struct statx buf;
return !iou_statxat(reactor, AT_FDCWD, pathname, 0, STATX_ALL, &buf);
}
int
iou_fadvise(reactor_t * reactor, int fd, off_t offset, off_t len, int advice) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_fadvise(sqe, fd, offset, len, advice);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_fallocate(reactor_t * reactor, int fd, int mode, off_t offset, off_t len) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_fallocate(sqe, fd, mode, offset, len);
reactor_promise(reactor, sqe);
return reactor->result;
}
ssize_t
iou_fd_size(reactor_t * reactor, int fd) {
struct statx buf;
int result = iou_statxat(reactor, fd, "", AT_EMPTY_PATH, STATX_SIZE, &buf);
if (result < 0)
return result;
return buf.stx_size;
}
int
iou_fdatasync(reactor_t * reactor, int fd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_fsync(sqe, fd, IORING_FSYNC_DATASYNC);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_fgetxattr(reactor_t * reactor, int fd, const char *name, void *value, size_t size) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_fgetxattr(sqe, fd, name, value, size);
reactor_promise(reactor, sqe);
return reactor->result;
}
void
iou_flush(reactor_t * reactor) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_nop(sqe);
io_uring_sqe_set_flags(sqe, IOSQE_IO_DRAIN);
reactor_promise(reactor, sqe);
}
int
iou_fsetxattr(reactor_t * reactor, int fd, const char *name, const void *value, size_t size, int flags) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_fsetxattr(sqe, fd, name, value, size, flags);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_fsync(reactor_t * reactor, int fd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_fsync(sqe, fd, 0);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_getxattr(reactor_t * reactor, const char *path, const char *name, void *value, size_t size) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_getxattr(sqe, name, value, path, size);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_link(reactor_t * reactor, const char *oldpath, const char *newpath) {
return iou_linkat(reactor, AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
}
int iou_linkat(reactor_t * reactor, int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
assert(reactor);
if (!oldpath || !*oldpath)
flags |= AT_EMPTY_PATH;
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_linkat(sqe, olddirfd, oldpath, newdirfd, newpath, flags);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_madvise(reactor_t * reactor, void *addr, size_t len, int advice) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_madvise(sqe, addr, len, advice);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_mkdir(reactor_t * reactor, const char *pathname, mode_t mode) {
return iou_mkdirat(reactor, AT_FDCWD, pathname, mode);
}
int
iou_mkdirat(reactor_t * reactor, int dirfd, const char *pathname, mode_t mode) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_mkdirat(sqe, dirfd, pathname, mode);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_open(reactor_t * reactor, const char *pathname, int flags, mode_t mode) {
return iou_openat(reactor, AT_FDCWD, pathname, flags, mode);
}
int
iou_openat(reactor_t * reactor, int dirfd, const char *pathname, int flags, mode_t mode) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_openat(sqe, dirfd, pathname, flags, mode);
reactor_promise(reactor, sqe);
return reactor->result;
}
static bool
iou_poll_mask(reactor_t * reactor, int fd, unsigned mask, const struct timespec delta) {
assert(reactor);
switch (timespec_when(normalize_timespec(delta))) {
case -1: {
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_poll_add(sqe, fd, mask);
reactor_promise(reactor, sqe);
break;
}
case 0: {
reactor_reserve_sqes(reactor, 2);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_poll_add(sqe, fd, mask);
reactor_promise_nonchalant(reactor, sqe);
break;
}
case 1: {
struct timespec when = reify_timespec(delta);
reactor_reserve_sqes(reactor, 2);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_poll_add(sqe, fd, mask);
reactor_promise_impatient(reactor, sqe, when);
break;
}
default: abort();
}
return reactor->result > 0 && !!(reactor->result & mask);
}
bool iou_poll_hup(reactor_t * reactor, int fd, const struct timespec delta) { return iou_poll_mask(reactor, fd, POLLRDHUP, delta); }
bool iou_poll_in(reactor_t * reactor, int fd, const struct timespec delta) { return iou_poll_mask(reactor, fd, POLLIN, delta); }
bool iou_poll_out(reactor_t * reactor, int fd, const struct timespec delta) { return iou_poll_mask(reactor, fd, POLLOUT, delta); }
ssize_t
iou_pread(reactor_t * reactor, int fildes, void *buf, size_t nbytes, off_t offset) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_read(sqe, fildes, buf, nbytes, offset);
reactor_promise(reactor, sqe);
#ifdef HAVE_MEMCHECK_H
if (RUNNING_ON_VALGRIND) {
ssize_t bytes = reactor->result;
assert(bytes <= nbytes);
if (bytes > 0 && nbytes <= bytes) {
VALGRIND_MAKE_MEM_DEFINED(buf, nbytes);
} else if (bytes > 0) {
assert(bytes < nbytes);
VALGRIND_MAKE_MEM_DEFINED(buf, bytes);
VALGRIND_MAKE_MEM_UNDEFINED(buf + bytes, nbytes - bytes);
bytes = 0;
} else {
VALGRIND_MAKE_MEM_UNDEFINED(buf, nbytes);
}
}
#endif
return reactor->result;
}
ssize_t
iou_preadv(reactor_t * reactor, int fildes, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_readv2(sqe, fildes, iov, iovcnt, offset, flags);
reactor_promise(reactor, sqe);
#ifdef HAVE_MEMCHECK_H
if (RUNNING_ON_VALGRIND) {
ssize_t bytes = reactor->result;
for (int i = 0 ; i < iovcnt ; ++i) {
const void * buf = iov[i].iov_base;
size_t nbytes = iov[i].iov_len;
if (bytes > 0 && nbytes <= bytes) {
VALGRIND_MAKE_MEM_DEFINED(buf, nbytes);
bytes -= nbytes;
} else if (bytes > 0) {
assert(bytes < nbytes);
VALGRIND_MAKE_MEM_DEFINED(buf, bytes);
VALGRIND_MAKE_MEM_UNDEFINED(buf + bytes, nbytes - bytes);
bytes = 0;
} else {
VALGRIND_MAKE_MEM_UNDEFINED(buf, nbytes);
}
}
}
#endif
return reactor->result;
}
int
iou_printf(reactor_t * reactor, int fd, const char *format, ...) {
int result;
va_list args;
va_start(args, format);
result = iou_vprintf(reactor, fd, format, args);
va_end(args);
return result;
}
ssize_t
iou_pwrite(reactor_t * reactor, int fildes, const void *buf, size_t nbytes, off_t offset) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_write(sqe, fildes, buf, nbytes, offset);
reactor_promise(reactor, sqe);
return reactor->result;
}
ssize_t
iou_pwritev(reactor_t * reactor, int fildes, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_writev2(sqe, fildes, iov, iovcnt, offset, flags);
reactor_promise(reactor, sqe);
return reactor->result;
}
ssize_t
iou_read(reactor_t * reactor, int fildes, void *buf, size_t nbytes) {
return iou_pread(reactor, fildes, buf, nbytes, -1);
}
ssize_t
iou_recv(reactor_t * reactor, int socket, void *buffer, size_t length, int flags) {
return iou_recvfrom(reactor, socket, buffer, length, flags, NULL, 0);
}
ssize_t
iou_recvfrom(reactor_t * reactor, int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t address_len) {
assert(reactor);
struct iovec iov = {
.iov_base = buffer,
.iov_len = length
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_name = address,
.msg_namelen = address_len,
};
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_recvmsg(sqe, socket, &msg, flags);
reactor_promise(reactor, sqe);
#ifdef HAVE_MEMCHECK_H
if (RUNNING_ON_VALGRIND) {
ssize_t bytes = reactor->result;
if (bytes > 0 && length <= bytes) {
VALGRIND_MAKE_MEM_DEFINED(buffer, length);
} else if (bytes > 0) {
assert(bytes < length);
VALGRIND_MAKE_MEM_DEFINED(buffer, bytes);
VALGRIND_MAKE_MEM_UNDEFINED(buffer + bytes, length - bytes);
bytes = 0;
} else {
VALGRIND_MAKE_MEM_UNDEFINED(buffer, length);
}
}
#endif
return reactor->result;
}
int
iou_rename(reactor_t * reactor, const char *oldpath, const char *newpath) {
return iou_renameat(reactor, AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
}
int
iou_rename_noreplace(reactor_t * reactor, const char *oldpath, const char *newpath) {
return iou_renameat(reactor, AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_NOREPLACE);
}
int
iou_renameat(reactor_t * reactor, int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_renameat(sqe, olddirfd, oldpath, newdirfd, newpath, flags);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_rmdir(reactor_t * reactor, const char *pathname) {
return iou_rmdirat(reactor, AT_FDCWD, pathname);
}
iou_semaphore_t
iou_semaphore_get(reactor_t * reactor, uint64_t value) {
return (iou_semaphore_t)eventfd(value, EFD_CLOEXEC | EFD_SEMAPHORE);
}
int
iou_semaphore_wait(reactor_t * reactor , iou_semaphore_t semaphore, const struct timespec delta) {
assert(reactor);
int efd = (int)semaphore;
uint64_t value = 0;
switch (timespec_when(normalize_timespec(delta))) {
case -1: {
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_read(sqe, efd, &value, sizeof value, -1);
reactor_promise(reactor, sqe);
return reactor->result == sizeof value ? value : reactor->result;
}
case 0: {
struct iovec iov = {
.iov_base = &value,
.iov_len = sizeof value,
};
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_readv2(sqe, efd, &iov, 1, -1, RWF_NOWAIT);
reactor_promise(reactor, sqe);
return reactor->result == sizeof value ? value :
reactor->result == -EAGAIN ? 0 :
reactor->result;
}
case 1: {
struct timespec when = reify_timespec(delta);
reactor_reserve_sqes(reactor, 2);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_read(sqe, efd, &value, sizeof value, -1);
reactor_promise_impatient(reactor, sqe, when);
return reactor->result == sizeof value ? value :
reactor->result == -ECANCELED ? 0 :
reactor->result;
}
default: abort();
}
}
static const uint64_t value = 1;
void iou_semaphore_post(reactor_t * reactor, iou_semaphore_t semaphore) {
assert(reactor);
int efd = (int)semaphore;
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_write(sqe, efd, &value, sizeof value, -1);
reactor_future_fake(reactor, sqe);
}
void iou_semaphore_put(reactor_t * reactor, iou_semaphore_t semaphore) {
assert(reactor);
int efd = (int)semaphore;
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_close(sqe, efd);
reactor_future_fake(reactor, sqe);
}
int
iou_rmdirat(reactor_t * reactor, int dirfd, const char *pathname) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_unlinkat(sqe, dirfd, pathname, AT_REMOVEDIR);
reactor_promise(reactor, sqe);
return reactor->result;
}
ssize_t
iou_send(reactor_t * reactor, int socket, const void *buffer, size_t length, int flags) {
return iou_sendto(reactor, socket, buffer, length, flags, NULL, 0);
}
ssize_t
iou_sendto(reactor_t * reactor, int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len) {
assert(reactor);
struct iovec iov = {
.iov_base = (void*)message,
.iov_len = length
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_name = (void*)dest_addr,
.msg_namelen = dest_len,
};
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_sendmsg(sqe, socket, &msg, flags);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_setxattr(reactor_t * reactor, const char *path, const char *name, const void *value, size_t size, int flags) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_setxattr(sqe, name, value, path, size, flags);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_shutdown(reactor_t * reactor, int sockfd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_shutdown(sqe, sockfd, SHUT_RDWR);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_shutdown_read(reactor_t * reactor, int sockfd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_shutdown(sqe, sockfd, SHUT_RD);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_shutdown_write(reactor_t * reactor, int sockfd) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_shutdown(sqe, sockfd, SHUT_WR);
reactor_promise(reactor, sqe);
return reactor->result;
}
struct timespec
iou_sleep(reactor_t * reactor, const struct timespec delta) {
assert(reactor);
struct timespec now;
TRY(clock_gettime, CLOCK_BOOTTIME, &now);
const struct timespec when = normalize_timespec((struct timespec){
.tv_sec = now.tv_sec + delta.tv_sec,
.tv_nsec = now.tv_nsec + delta.tv_nsec,
});
if (iou_sleep_absolute(reactor, when))
return (struct timespec){ .tv_sec = 0, .tv_nsec = 0 };
TRY(clock_gettime, CLOCK_BOOTTIME, &now);
const struct timespec remaining = normalize_timespec((struct timespec){
.tv_sec = when.tv_sec - now.tv_sec,
.tv_nsec = when.tv_nsec - now.tv_nsec,
});
if (remaining.tv_sec < 0)
return (struct timespec){ .tv_sec = 0, .tv_nsec = 0 };
return remaining;
}
bool
iou_sleep_absolute(reactor_t * reactor, const struct timespec when) {
assert(reactor);
const struct timespec ts = normalize_timespec(when);
struct __kernel_timespec kts = {
.tv_sec = ts.tv_sec,
.tv_nsec = ts.tv_nsec,
};
while (true) {
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_timeout(sqe, &kts, 0, 0
| IORING_TIMEOUT_ABS
| IORING_TIMEOUT_BOOTTIME
);
reactor_promise(reactor, sqe);
switch (reactor->result) {
case 0: continue;
case -ETIME: return true;
default: return false;
}
}
}
int
iou_socket(reactor_t * reactor, int domain, int type, int protocol) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_socket(sqe, domain, type, protocol, 0);
reactor_promise(reactor, sqe);
return reactor->result;
}
static void
_iou_spawn_trampoline(sigjmp_buf *buf, pid_t *pid, const posix_spawnattr_t *attrs, int stdin_fd, int stdout_fd, const char *command, const char *argv[]) {
posix_spawn_file_actions_t file_actions;
TRY(posix_spawn_file_actions_init, &file_actions);
if (stdin_fd >= 0) {
TRY(posix_spawn_file_actions_adddup2, &file_actions, stdin_fd, STDIN_FILENO);
} else {
TRY(posix_spawn_file_actions_addclose, &file_actions, STDIN_FILENO);
}
if (stdout_fd >= 0) {
TRY(posix_spawn_file_actions_adddup2, &file_actions, stdout_fd, STDOUT_FILENO);
} else {
TRY(posix_spawn_file_actions_addopen, &file_actions, STDOUT_FILENO, "/dev/null", O_WRONLY|O_APPEND, 0666);
}
TRY(posix_spawnp, pid, command, &file_actions, attrs, (char * const *)argv, NULL);
TRY(posix_spawn_file_actions_destroy, &file_actions);
siglongjmp(*buf, true);
}
pid_t
iou__spawn(reactor_t * reactor, const posix_spawnattr_t *attrs, int to_fd, int from_fd, const char *command, ...) {
va_list argv;
va_start(argv, command);
pid_t pid = iou_spawnv(reactor, attrs, to_fd, from_fd, command, argv);
va_end(argv);
return pid;
}
static inline size_t
va_list_size(va_list va) {
size_t c = 0;
va_list copy;
va_copy(copy, va);
while (va_arg(copy, void*)) ++c;
va_end(copy);
return c;
}
pid_t
iou_spawnv(reactor_t * reactor, const posix_spawnattr_t *attrs, int to_fd, int from_fd, const char *command, va_list args) {
assert(reactor);
stack_t stack = reactor->stack;
if (attrs)
if (!(attrs = stack_memcpy(&stack, attrs, sizeof(*attrs), alignof(*attrs))))
return -ENOMEM;
size_t argc = 1 + va_list_size(args);
const char **argv = stack_array(&stack, const char*, argc + 1);
if (!argv)
return -ENOMEM;
if (!(argv[0] = stack_strcpy(&stack, command)))
return -ENOMEM;
for (int i = 1 ; i < argc; ++i)
if (!(argv[i] = stack_strcpy(&stack, va_arg(args, char*))))
return -ENOMEM;
assert(NULL == va_arg(args, char*));
argv[argc] = NULL;
pid_t pid = -1;
sigjmp_buf todo;
if (!sigsetjmp(todo, false)) {
ucontext_t uc;
TRY(getcontext, &uc);
uc.uc_stack = stack;
uc.uc_link = NULL;
makecontext(&uc, (void(*)())_iou_spawn_trampoline, 7,
&todo,
&pid, attrs,
to_fd, from_fd,
command, argv);
setcontext(&uc);
}
return pid;
}
ssize_t
iou_splice(reactor_t * reactor, int fd_in, int fd_out, size_t len) {
return iou_splice_offset(reactor, fd_in, NULL, fd_out, NULL, len);
}
ssize_t
iou_splice_all(reactor_t * reactor, int fd_in, int fd_out, size_t len) {
ssize_t bytes = 0;
while (bytes < len) {
ssize_t n = iou_splice(reactor, fd_in, fd_out, len - bytes);
if (n < 0)
return n;
bytes += n;
}
return bytes;
}
ssize_t
iou_splice_offset(reactor_t * reactor, int fd_in, off_t *off_in, int fd_out, off_t *off_out, size_t len) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_splice(sqe, fd_in, off_in ? *off_in : -1, fd_out, off_out ? *off_out : -1, len, SPLICE_F_MORE | SPLICE_F_MOVE);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_statx(reactor_t * reactor, const char *pathname, struct statx *statxbuf) {
return iou_statxat(reactor, AT_FDCWD, pathname, 0, STATX_BASIC_STATS, statxbuf);
}
int
iou_statxat(reactor_t * reactor, int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_statx(sqe, dirfd, pathname, flags | AT_EMPTY_PATH, mask, statxbuf);
reactor_promise(reactor, sqe);
#ifdef HAVE_MEMCHECK_H
if (RUNNING_ON_VALGRIND) {
if (0 == reactor->result)
VALGRIND_MAKE_MEM_DEFINED(statxbuf, sizeof *statxbuf);
else
VALGRIND_MAKE_MEM_UNDEFINED(statxbuf, sizeof *statxbuf);
}
#endif
return reactor->result;
}
int
iou_statxfd(reactor_t * reactor, int fd, struct statx *statxbuf) {
return iou_statxat(reactor, fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, statxbuf);
}
int
iou_symlink(reactor_t * reactor, const char *path1, const char *path2) {
return iou_symlinkat(reactor, path1, AT_FDCWD, path2);
}
int
iou_symlinkat(reactor_t * reactor, const char *path1, int fd, const char *path2) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_symlinkat(sqe, path1, fd, path2);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_sync_file_range(reactor_t * reactor, int fd, off_t offset, off_t nbytes, bool wait) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
const unsigned int flags_no_wait = SYNC_FILE_RANGE_WRITE;
const unsigned int flags_yes_wait = SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER;
io_uring_prep_sync_file_range(sqe, fd, offset, nbytes, wait ? flags_yes_wait : flags_no_wait);
reactor_promise(reactor, sqe);
return reactor->result;
}
ssize_t
iou_tee(reactor_t * reactor, int fd_in, int fd_out, size_t len) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_tee(sqe, fd_in, fd_out, len, SPLICE_F_MORE);
reactor_promise(reactor, sqe);
return reactor->result;
}
int
iou_unlink(reactor_t * reactor, const char *pathname) {
return iou_unlinkat(reactor, AT_FDCWD, pathname);
}
int
iou_unlinkat(reactor_t * reactor, int dirfd, const char *pathname) {
assert(reactor);
struct io_uring_sqe * sqe = reactor_sqe(reactor);
io_uring_prep_unlinkat(sqe, dirfd, pathname, 0);
reactor_promise(reactor, sqe);
return reactor->result;
}
typedef struct _iou_vprintf_cookie_s {
reactor_t * reactor;
int fd;
} _iou_vprintf_cookie_t;
static
ssize_t _iou_vprintf_write(void *_cookie, const char *buf, size_t size) {
_iou_vprintf_cookie_t *cookie = (_iou_vprintf_cookie_t*)_cookie;
int n = iou_write(cookie->reactor, cookie->fd, buf, size);
return n < 0 ? 0 : n;
}
static const cookie_io_functions_t _iou_vprintf_cookie_io_functions = {
.write = _iou_vprintf_write,
};
int
iou_vprintf(reactor_t * reactor, int fd, const char *format, va_list args) {
assert(reactor);
va_list copy;
va_copy(copy, args);
char buffer[256];
int result = vsnprintf(buffer, sizeof buffer, format, args);
if (result >= sizeof buffer) {
_iou_vprintf_cookie_t cookie = {
.reactor = reactor,
.fd = fd,
};
FILE *file = fopencookie(&cookie, "w", _iou_vprintf_cookie_io_functions);
result = vfprintf(file, format, copy);
fflush(file);
fclose(file);
} else if (result > 0) {
result = iou_write(reactor, fd, buffer, result);