@amritk/nish-x86_64-linux 0.40.1 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,7 +66,7 @@ typedef int nish_net_unused;
66
66
  #include <netinet/tcp.h>
67
67
  #include <stddef.h>
68
68
  #include <stdint.h>
69
- #include <stddef.h>
69
+ #include <stdlib.h>
70
70
  #include <string.h>
71
71
  #include <sys/resource.h>
72
72
  #include <sys/socket.h>
@@ -77,6 +77,7 @@ typedef int nish_net_unused;
77
77
  #include <linux/filter.h>
78
78
  #include <netinet/udp.h>
79
79
  #include <sys/epoll.h>
80
+ #include <time.h>
80
81
  /* glibc before 2.29 names neither; the numbers are the kernel's ABI. */
81
82
  #ifndef UDP_SEGMENT
82
83
  #define UDP_SEGMENT 103
@@ -201,6 +202,9 @@ static void nish_net_raise_nofile(void) {
201
202
  rlim_t want = r.rlim_max;
202
203
  #if !defined(__linux__)
203
204
  if (want > (rlim_t)OPEN_MAX) want = (rlim_t)OPEN_MAX;
205
+ #else
206
+ /* Every descriptor stays below 2^20, where slot handles begin. */
207
+ if (want > (rlim_t)(1 << 20)) want = (rlim_t)(1 << 20);
204
208
  #endif
205
209
  if (r.rlim_cur >= want) return;
206
210
  r.rlim_cur = want;
@@ -228,9 +232,752 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
228
232
  return 0;
229
233
  }
230
234
 
235
+ /* Slot handles (WP63 S2, docs/wp63-iouring-net.md §5). A listener opened
236
+ with `tcpListenFlags` flag 8 on Linux accepts into an io_uring set
237
+ (runtime-uring.c) rather than into descriptors, so a connection costs a
238
+ slot in a ring's file table instead of an entry in the process's: a set of
239
+ N rings holds N times RLIMIT_NOFILE connections in N + 1 descriptors.
240
+
241
+ A handle is still an `i32`. Below 2^20 it is a descriptor, which is why
242
+ `nish_net_raise_nofile` stops the soft limit at 2^20 on Linux. At or above
243
+ it, `2^20 + ring * 2^20 + field` names a slot: `ring` is a global ring id,
244
+ 64 per listener (`NR_RINGS`), and `field` is `slot + slots * (gen % G)`,
245
+ with `G = 2^20 / slots` the generations a slot's handles cycle through. A
246
+ close moves the slot's generation on, so the handle a program closed
247
+ answers -9 at once and does not reach the next connection in that slot
248
+ until the slot has been reused G times (1,024 at a limit of 1,024; never
249
+ fewer than 64). At most `NR_SETS` ring listeners exist in a process, which
250
+ keeps every handle below 2^31; their ids are not reused, so a closed
251
+ listener's handles answer -9 for good.
252
+
253
+ Every call that takes a handle and is not one of the slot paths here
254
+ (`sendFd`, `netShutdown`, `connectResult`, `peerCredentials`, ...) passes it
255
+ to the kernel as a descriptor, which no process holds at 2^20 or above, so
256
+ it answers -9 (EBADF) with nothing to check.
257
+
258
+ Readiness keeps the descriptor contract: a read with nothing queued, a write
259
+ to a full send area and an accept with nothing accepted answer -11, and the
260
+ loop reports a slot level-triggered, in the order it became ready. The set
261
+ receives on its own (a multishot recv per slot), so what arrives is copied
262
+ into the slot's receive queue at once and the ring's buffer goes back. A
263
+ write is copied into the set's send queue; `NR_AREA` bounds what one slot
264
+ may hold there (NURING-2), and a full area is -11 until the kernel has sent
265
+ enough. A full set, every ring's table full and no ring left to make, is
266
+ -23 from `tcpAccept` (NURING-6): the connection waits in the backlog until a
267
+ close frees a slot. */
268
+ #if defined(__linux__)
269
+ struct nish_uring_set;
270
+ struct nish_uring_event {
271
+ int32_t kind;
272
+ int32_t ring;
273
+ int32_t slot;
274
+ int32_t res;
275
+ const uint8_t *data;
276
+ };
277
+ struct nish_uring_opts {
278
+ int32_t slots;
279
+ int32_t rings_max;
280
+ int32_t sq_entries;
281
+ int32_t cq_entries;
282
+ int32_t buf_count;
283
+ int32_t buf_size;
284
+ };
285
+ int32_t nish_uring_probe(void);
286
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds);
287
+ int32_t nish_uring_pooled(void);
288
+ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts);
289
+ int32_t nish_uring_fd(const struct nish_uring_set *s);
290
+ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
291
+ int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
292
+ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
293
+ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
294
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
295
+ int32_t nish_uring_full(const struct nish_uring_set *s);
296
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
297
+ void nish_uring_destroy(struct nish_uring_set *s);
298
+
299
+ #define NR_BASE (1 << 20)
300
+ #define NR_RINGS 64
301
+ #define NR_SETS 31
302
+ /* The most bytes one slot may have queued and not yet sent (NURING-2), and
303
+ the receive queue at which the slot's receive is held (NURING-9). */
304
+ #define NR_AREA (256 * 1024)
305
+ /* What a held slot's queue may still take from the kernel before the cancel
306
+ lands; past it the connection ends with -105 (ENOBUFS). */
307
+ #define NR_RECV_MAX (16 * 1024 * 1024)
308
+ /* The most slots per ring: a table is the kernel's memory, sized up front. */
309
+ #define NR_SLOTS_MAX 16384
310
+ #define NR_EVENTS 256
311
+
312
+ /* A loop's record of one virtual source, a slot or a ring listener: the loop
313
+ it is in (-1 for none), what it watches and its token, and its place on
314
+ the list of sources that are ready, kept in the order they became so. */
315
+ struct nr_vent {
316
+ int32_t loop, events, token;
317
+ uint8_t listed, is_listener;
318
+ struct nr_vent *prev, *next;
319
+ };
320
+
321
+ /* One slot's connection. `gen` counts the slot's closes; `lingering` is a
322
+ close the program made while bytes were still queued, which ends when they
323
+ are sent, the peer goes or a send fails. */
324
+ struct nr_conn {
325
+ struct nr_vent v;
326
+ uint32_t gen;
327
+ uint8_t open, lingering, hup, wblocked, rheld;
328
+ int32_t hup_res, werr;
329
+ uint8_t *rbuf;
330
+ uint32_t rlen, roff, rcap;
331
+ };
332
+
333
+ struct nr_set {
334
+ struct nish_uring_set *u;
335
+ struct nr_vent v; /* the listener's own entry */
336
+ int32_t listener, port, slots, gens, dead, accept_err;
337
+ int32_t accept_once; /* an accept that found memory short, answered by the next tcpAccept */
338
+ struct nr_conn *conns[NR_RINGS];
339
+ uint32_t *fifo; /* accepted (ring << 20 | slot), not yet taken */
340
+ uint32_t fhead, flen, fcap;
341
+ uint32_t *pend; /* wblocked or lingering slots, checked after each pump */
342
+ uint32_t npend, pcap;
343
+ };
344
+
345
+ static struct nr_set *nr_sets[NR_SETS];
346
+ static int32_t nr_nsets;
347
+ /* `netRingState()`: 0 until a listener opens a set (1) or meets a refusal. */
348
+ static int32_t nr_state;
349
+ static struct nr_vent *nr_head, *nr_tail;
350
+
351
+ static void nr_unlist(struct nr_vent *v) {
352
+ if (!v->listed) return;
353
+ if (v->prev) v->prev->next = v->next; else nr_head = v->next;
354
+ if (v->next) v->next->prev = v->prev; else nr_tail = v->prev;
355
+ v->prev = v->next = NULL;
356
+ v->listed = 0;
357
+ }
358
+
359
+ static void nr_append(struct nr_vent *v) {
360
+ v->prev = nr_tail;
361
+ v->next = NULL;
362
+ if (nr_tail) nr_tail->next = v; else nr_head = v;
363
+ nr_tail = v;
364
+ v->listed = 1;
365
+ }
366
+
367
+ static struct nr_conn *nr_conn_of(struct nr_vent *v) { return (struct nr_conn *)(void *)v; }
368
+ static struct nr_set *nr_set_of(struct nr_vent *v) {
369
+ return (struct nr_set *)(void *)((char *)v - offsetof(struct nr_set, v));
370
+ }
371
+
372
+ /* Whether the slot's connection has failed: a receive's error or a send's.
373
+ A peer's FIN alone (`hup_res` 0) is not a failure. */
374
+ static int nr_failed(const struct nr_conn *c) {
375
+ return c->werr || (c->hup && c->hup_res < 0);
376
+ }
377
+
378
+ /* The readiness bits a source holds now: 1 readable, 2 writable, 4 an error. */
379
+ static int32_t nr_bits(struct nr_vent *v) {
380
+ if (v->is_listener) {
381
+ struct nr_set *S = nr_set_of(v);
382
+ return (S->flen > 0 || S->accept_err || S->accept_once) ? 1 : 0;
383
+ }
384
+ struct nr_conn *c = nr_conn_of(v);
385
+ int32_t b = 0;
386
+ if (c->rlen > c->roff || c->hup || c->werr) b |= 1;
387
+ if (nr_failed(c)) b |= 4;
388
+ else if (!c->wblocked) b |= 2;
389
+ return b;
390
+ }
391
+
392
+ /* Puts a source that has become ready at the tail of the list. */
393
+ static void nr_touch(struct nr_vent *v) {
394
+ if (v->loop >= 0 && !v->listed && (nr_bits(v) & (v->events | 4))) nr_append(v);
395
+ }
396
+
397
+ static void nr_unregister(struct nr_vent *v) {
398
+ if (v->loop < 0) return;
399
+ nr_unlist(v);
400
+ v->loop = -1;
401
+ }
402
+
403
+ static int nr_push(uint32_t **a, uint32_t *n, uint32_t *cap, uint32_t x) {
404
+ if (*n == *cap) {
405
+ uint32_t c = *cap ? *cap * 2 : 64;
406
+ uint32_t *p = realloc(*a, (size_t)c * sizeof *p);
407
+ if (!p) return -12;
408
+ *a = p;
409
+ *cap = c;
410
+ }
411
+ (*a)[(*n)++] = x;
412
+ return 0;
413
+ }
414
+
415
+ /* The connection a handle names, or NULL for a stale or foreign one. */
416
+ static struct nr_conn *nr_decode(int32_t h, struct nr_set **set, int32_t *ring, int32_t *slot) {
417
+ if (h < NR_BASE) return NULL;
418
+ uint32_t x = (uint32_t)h - NR_BASE;
419
+ uint32_t g = x >> 20, field = x & (NR_BASE - 1);
420
+ uint32_t si = g / NR_RINGS, ri = g % NR_RINGS;
421
+ if (si >= (uint32_t)nr_nsets) return NULL;
422
+ struct nr_set *S = nr_sets[si];
423
+ if (S->dead || !S->conns[ri]) return NULL;
424
+ uint32_t sl = field % (uint32_t)S->slots, gm = field / (uint32_t)S->slots;
425
+ struct nr_conn *c = &S->conns[ri][sl];
426
+ if (gm >= (uint32_t)S->gens || !c->open || c->lingering || c->gen % (uint32_t)S->gens != gm) return NULL;
427
+ *set = S;
428
+ *ring = (int32_t)ri;
429
+ *slot = (int32_t)sl;
430
+ return c;
431
+ }
432
+
433
+ static int32_t nr_encode(const struct nr_set *S, int32_t si, uint32_t ring, uint32_t slot) {
434
+ const struct nr_conn *c = &S->conns[ring][slot];
435
+ uint32_t field = slot + (uint32_t)S->slots * (c->gen % (uint32_t)S->gens);
436
+ return (int32_t)((uint32_t)NR_BASE + ((uint32_t)si * NR_RINGS + ring) * (uint32_t)NR_BASE + field);
437
+ }
438
+
439
+ /* Ends a slot for good: the set closes it and the slot goes back to the
440
+ accepting job. 1 when done, 0 when the set had no room and it is retried. */
441
+ static int nr_finish(struct nr_set *S, uint32_t ring, uint32_t slot) {
442
+ struct nr_conn *c = &S->conns[ring][slot];
443
+ if (nish_uring_close(S->u, (int32_t)ring, (int32_t)slot) == -11) return 0;
444
+ c->open = 0;
445
+ c->lingering = 0;
446
+ return 1;
447
+ }
448
+
449
+ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
450
+ if (e->kind == 5) {
451
+ S->accept_err = e->res < 0 ? e->res : -5;
452
+ nr_touch(&S->v);
453
+ return;
454
+ }
455
+ if (e->kind == 6) {
456
+ /* Memory was short for an accept; the set pauses and accepts again. A
457
+ descriptor's accept answers the errno once, and so does tcpAccept. */
458
+ S->accept_once = e->res < 0 ? e->res : -12;
459
+ nr_touch(&S->v);
460
+ return;
461
+ }
462
+ if (e->ring < 0 || e->ring >= NR_RINGS || e->slot < 0 || e->slot >= S->slots) return;
463
+ if (!S->conns[e->ring]) {
464
+ S->conns[e->ring] = calloc((size_t)S->slots, sizeof(struct nr_conn));
465
+ /* With no table to record it in, an accepted slot is closed at once
466
+ rather than left open in the ring for good. */
467
+ if (!S->conns[e->ring]) {
468
+ if (e->kind == 1) (void)nish_uring_close(S->u, e->ring, e->slot);
469
+ return;
470
+ }
471
+ }
472
+ struct nr_conn *c = &S->conns[e->ring][e->slot];
473
+ if (e->kind == 1) {
474
+ uint32_t gen = c->gen;
475
+ uint8_t *buf = c->rbuf;
476
+ uint32_t cap = c->rcap;
477
+ memset(c, 0, sizeof *c);
478
+ c->gen = gen;
479
+ c->rbuf = buf;
480
+ c->rcap = cap;
481
+ c->open = 1;
482
+ c->v.loop = -1;
483
+ uint32_t i = (S->fhead + S->flen) % (S->fcap ? S->fcap : 1);
484
+ if (S->flen == S->fcap) {
485
+ /* Grow the ring buffer, unrolled into the new array from its head. */
486
+ uint32_t nc = S->fcap ? S->fcap * 2 : 64;
487
+ uint32_t *p = malloc((size_t)nc * sizeof *p);
488
+ if (!p) {
489
+ (void)nr_finish(S, (uint32_t)e->ring, (uint32_t)e->slot);
490
+ return;
491
+ }
492
+ for (uint32_t k = 0; k < S->flen; k++) p[k] = S->fifo[(S->fhead + k) % S->fcap];
493
+ free(S->fifo);
494
+ S->fifo = p;
495
+ S->fcap = nc;
496
+ S->fhead = 0;
497
+ i = S->flen;
498
+ }
499
+ S->fifo[i] = (uint32_t)e->ring << 20 | (uint32_t)e->slot;
500
+ S->flen++;
501
+ nr_touch(&S->v);
502
+ return;
503
+ }
504
+ if (!c->open) return;
505
+ if (e->kind == 2) {
506
+ /* Bytes after the slot has hung up, here or by the kernel, are dropped,
507
+ so a read never runs past the failure it reports. */
508
+ if (c->lingering || c->hup || e->res <= 0) return;
509
+ uint32_t n = (uint32_t)e->res;
510
+ if (c->roff == c->rlen) c->roff = c->rlen = 0;
511
+ if ((uint64_t)(c->rlen - c->roff) + n > NR_RECV_MAX) {
512
+ c->hup = 1;
513
+ c->hup_res = -105;
514
+ nr_touch(&c->v);
515
+ return;
516
+ }
517
+ if (c->rlen + n > c->rcap) {
518
+ if (c->roff > 0) {
519
+ memmove(c->rbuf, c->rbuf + c->roff, c->rlen - c->roff);
520
+ c->rlen -= c->roff;
521
+ c->roff = 0;
522
+ }
523
+ if (c->rlen + n > c->rcap) {
524
+ uint32_t nc = c->rcap ? c->rcap : 4096;
525
+ while (nc < c->rlen + n) nc *= 2;
526
+ uint8_t *p = realloc(c->rbuf, nc);
527
+ if (!p) {
528
+ c->hup = 1;
529
+ c->hup_res = -12;
530
+ nr_touch(&c->v);
531
+ return;
532
+ }
533
+ c->rbuf = p;
534
+ c->rcap = nc;
535
+ }
536
+ }
537
+ memcpy(c->rbuf + c->rlen, e->data, n);
538
+ c->rlen += n;
539
+ if (!c->rheld && c->rlen - c->roff >= NR_AREA && nish_uring_hold(S->u, e->ring, e->slot, 1) == 0) c->rheld = 1;
540
+ } else if (e->kind == 3) {
541
+ /* The first hangup is the one a read reports: a later one, such as the
542
+ kernel's after a -105 here, does not overwrite it (NURING-27). */
543
+ if (!c->hup) {
544
+ c->hup = 1;
545
+ c->hup_res = e->res;
546
+ }
547
+ } else if (e->kind == 4) {
548
+ if (!c->werr) c->werr = e->res < 0 ? e->res : -5;
549
+ }
550
+ if (c->lingering && nr_failed(c)) (void)nr_finish(S, (uint32_t)e->ring, (uint32_t)e->slot);
551
+ else nr_touch(&c->v);
552
+ }
553
+
554
+ /* Takes every event the set holds, and hands the set what it has queued.
555
+ Then the slots waiting on their send areas: a write-blocked one whose area
556
+ has room is ready again, and a lingering close whose bytes are gone ends. */
557
+ static void nr_pump(struct nr_set *S) {
558
+ struct nish_uring_event ev[NR_EVENTS];
559
+ for (int round = 0; round < 64; round++) {
560
+ int n = nish_uring_wait(S->u, 0, ev, NR_EVENTS);
561
+ if (n <= 0) break;
562
+ for (int i = 0; i < n; i++) nr_dispatch(S, &ev[i]);
563
+ }
564
+ int full = S->npend > 0 && nish_uring_full(S->u);
565
+ uint32_t keep = 0;
566
+ for (uint32_t i = 0; i < S->npend; i++) {
567
+ uint32_t x = S->pend[i], ring = x >> 20, slot = x & (NR_BASE - 1);
568
+ struct nr_conn *c = &S->conns[ring][slot];
569
+ int64_t q = nish_uring_queued(S->u, (int32_t)ring, (int32_t)slot);
570
+ if (!c->open) continue;
571
+ if (c->lingering) {
572
+ if ((q <= 0 || nr_failed(c) || full) && nr_finish(S, ring, slot)) continue;
573
+ } else if (c->wblocked) {
574
+ if (q >= NR_AREA) {
575
+ S->pend[keep++] = x;
576
+ continue;
577
+ }
578
+ c->wblocked = 0;
579
+ nr_touch(&c->v);
580
+ continue;
581
+ } else {
582
+ continue;
583
+ }
584
+ S->pend[keep++] = x;
585
+ }
586
+ S->npend = keep;
587
+ }
588
+
589
+ static void nr_pump_all(void) {
590
+ for (int32_t i = 0; i < nr_nsets; i++)
591
+ if (!nr_sets[i]->dead) nr_pump(nr_sets[i]);
592
+ }
593
+
594
+ static struct nr_set *nr_listener(int32_t fd) {
595
+ for (int32_t i = 0; i < nr_nsets; i++)
596
+ if (!nr_sets[i]->dead && nr_sets[i]->listener == fd) return nr_sets[i];
597
+ return NULL;
598
+ }
599
+
600
+ /* Whether `r`, from the probe or the first set's open, refuses io_uring for
601
+ the process: EPERM, ENOSYS, EINVAL or EOPNOTSUPP, the answers
602
+ nish_uring_probe keeps (NURING-20). Any other failure, such as EMFILE or
603
+ ENOMEM, may pass, and a later listen asks again. */
604
+ static int nr_refused(int32_t r) { return r == -1 || r == -38 || r == -22 || r == -95; }
605
+
606
+ /* A ring listener for `fd`: 1 when it has one, 0 when it stays on
607
+ descriptors, or -23 when the process has opened every set it may hold.
608
+ Whatever else fails, the probe or this set's open, the listener falls back
609
+ to descriptors (§5.4); a refusal before any set has opened also decides for
610
+ every later listen, and any other failure, such as -24 or -12, only for
611
+ this one (NURING-20, NURING-22). */
612
+ static int32_t nr_slots(void) {
613
+ struct rlimit rl;
614
+ if (getrlimit(RLIMIT_NOFILE, &rl) != 0) return -1;
615
+ int32_t n = rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > NR_SLOTS_MAX ? NR_SLOTS_MAX : (int32_t)rl.rlim_cur;
616
+ return n < 1 ? 1 : n;
617
+ }
618
+
619
+ static int32_t nr_open(int32_t fd, int32_t port) {
620
+ if (nr_state < 0) return 0;
621
+ /* Under `--sandbox` every set takes the rings made before the filter, so
622
+ any failure of theirs, a refusal or not, decides for good: no ring can be
623
+ made again (WP63 S3). */
624
+ int32_t pooled = nish_uring_pooled();
625
+ if (nr_state == 0) {
626
+ int32_t p = nish_uring_probe();
627
+ if (p < 0) {
628
+ if (pooled != 0 || nr_refused(p)) nr_state = p;
629
+ return 0;
630
+ }
631
+ }
632
+ if (nr_nsets >= NR_SETS) return -23;
633
+ struct nish_uring_opts o;
634
+ memset(&o, 0, sizeof o);
635
+ o.slots = pooled > 0 ? pooled : nr_slots();
636
+ if (o.slots < 0) return 0;
637
+ o.rings_max = NR_RINGS;
638
+ struct nr_set *S = calloc(1, sizeof *S);
639
+ if (!S) return 0;
640
+ int32_t r = nish_uring_open(&S->u, fd, &o);
641
+ if (r < 0) {
642
+ free(S);
643
+ if (nr_state == 0 && (pooled != 0 || nr_refused(r))) nr_state = r;
644
+ return 0;
645
+ }
646
+ /* Only an open set decides for rings: a probe that passed before a set
647
+ that ran short leaves the state at 0 (NURING-26). */
648
+ nr_state = 1;
649
+ S->listener = fd;
650
+ S->port = port;
651
+ S->slots = o.slots;
652
+ S->gens = NR_BASE / o.slots;
653
+ S->v.loop = -1;
654
+ S->v.is_listener = 1;
655
+ nr_sets[nr_nsets++] = S;
656
+ return 1;
657
+ }
658
+
659
+ /* Closes a ring listener: the set and every slot in it, then the descriptor.
660
+ The record stays, dead, so its handles answer -9. */
661
+ static int32_t nr_close_listener(struct nr_set *S) {
662
+ nr_unregister(&S->v);
663
+ for (int ri = 0; ri < NR_RINGS; ri++) {
664
+ if (!S->conns[ri]) continue;
665
+ for (int32_t sl = 0; sl < S->slots; sl++) {
666
+ nr_unregister(&S->conns[ri][sl].v);
667
+ free(S->conns[ri][sl].rbuf);
668
+ }
669
+ free(S->conns[ri]);
670
+ S->conns[ri] = NULL;
671
+ }
672
+ nish_uring_destroy(S->u);
673
+ S->u = NULL;
674
+ free(S->fifo);
675
+ free(S->pend);
676
+ S->fifo = S->pend = NULL;
677
+ S->dead = 1;
678
+ return close(S->listener) != 0 ? nish_net_fail() : 0;
679
+ }
680
+
681
+ static int32_t nr_accept(struct nr_set *S, nish_array *peer) {
682
+ int32_t si = 0;
683
+ while (nr_sets[si] != S) si++;
684
+ nr_pump(S);
685
+ while (S->flen > 0) {
686
+ uint32_t x = S->fifo[S->fhead];
687
+ S->fhead = (S->fhead + 1) % S->fcap;
688
+ S->flen--;
689
+ uint32_t ring = x >> 20, slot = x & (NR_BASE - 1);
690
+ if (!S->conns[ring][slot].open) continue;
691
+ /* A slot has no descriptor to ask its peer of, so the address is the
692
+ 18 zeros `recvmsg` gives for one it does not know. */
693
+ memset(peer->data, 0, NISH_ADDR_BYTES);
694
+ return nr_encode(S, si, ring, slot);
695
+ }
696
+ if (S->accept_err) return S->accept_err;
697
+ if (S->accept_once) {
698
+ int32_t r = S->accept_once;
699
+ S->accept_once = 0;
700
+ return r;
701
+ }
702
+ return nish_uring_full(S->u) ? -23 : -11;
703
+ }
704
+
705
+ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, nish_array *buf, int64_t off,
706
+ int64_t len) {
707
+ if (c->rlen == c->roff && !c->hup) nr_pump(S);
708
+ uint32_t have = c->rlen - c->roff;
709
+ if (have > 0) {
710
+ uint32_t n = len < (int64_t)have ? (uint32_t)len : have;
711
+ memcpy(buf->data + off, c->rbuf + c->roff, n);
712
+ c->roff += n;
713
+ /* A held receive goes again once the program has read half the queue. */
714
+ if (c->rheld && c->rlen - c->roff < NR_AREA / 2 && nish_uring_hold(S->u, ring, slot, 0) == 0) c->rheld = 0;
715
+ return (int32_t)n;
716
+ }
717
+ if (c->hup) return c->hup_res < 0 ? c->hup_res : 0;
718
+ /* A failed send ends the read side too, as recv's error would. */
719
+ if (c->werr) return c->werr;
720
+ return len == 0 ? 0 : -11;
721
+ }
722
+
723
+ /* The errno a failed slot answers a write with: its send's, else its
724
+ receive's, such as -104 for a peer's reset. */
725
+ static int32_t nr_fail_code(const struct nr_conn *c) {
726
+ return c->werr ? c->werr : c->hup_res;
727
+ }
728
+
729
+ /* Copies at most what the slot's send area has room for into the set's send
730
+ queue. -11 when it has none, or the connection's errno once it has failed. */
731
+ static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, const char *p, int64_t len) {
732
+ if (nr_failed(c)) return nr_fail_code(c);
733
+ int64_t q = nish_uring_queued(S->u, ring, slot);
734
+ if (q < 0) return -9;
735
+ if (q >= NR_AREA) {
736
+ nr_pump(S);
737
+ if (nr_failed(c)) return nr_fail_code(c);
738
+ q = nish_uring_queued(S->u, ring, slot);
739
+ }
740
+ int64_t room = NR_AREA - q;
741
+ int32_t n = room <= 0 ? 0 : (int32_t)(len < room ? len : room);
742
+ int32_t r = n > 0 ? nish_uring_send(S->u, ring, slot, (const uint8_t *)p, n) : 0;
743
+ if (r < 0) return r;
744
+ if (q + n >= NR_AREA && !c->wblocked) {
745
+ c->wblocked = 1;
746
+ if (nr_push(&S->pend, &S->npend, &S->pcap, (uint32_t)ring << 20 | (uint32_t)slot) != 0) c->wblocked = 0;
747
+ }
748
+ return len > 0 && n == 0 ? -11 : n;
749
+ }
750
+
751
+ static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot) {
752
+ nr_unregister(&c->v);
753
+ c->gen++;
754
+ c->roff = c->rlen = 0;
755
+ free(c->rbuf);
756
+ c->rbuf = NULL;
757
+ c->rcap = 0;
758
+ int64_t q = nish_uring_queued(S->u, ring, slot);
759
+ /* A peer that only stopped sending (a FIN) still takes what is queued, so
760
+ only a failure ends the slot before its bytes are sent. */
761
+ if ((q <= 0 || nr_failed(c)) && nr_finish(S, (uint32_t)ring, (uint32_t)slot)) {
762
+ nr_pump(S);
763
+ return 0;
764
+ }
765
+ c->lingering = 1;
766
+ if (!c->wblocked && nr_push(&S->pend, &S->npend, &S->pcap, (uint32_t)ring << 20 | (uint32_t)slot) != 0) {
767
+ while (!nr_finish(S, (uint32_t)ring, (uint32_t)slot)) nr_pump(S);
768
+ return 0;
769
+ }
770
+ nr_pump(S);
771
+ return 0;
772
+ }
773
+
774
+ /* The loops a virtual source has joined, so a close of any other descriptor
775
+ skips the scan below; past NR_LOOPS of them every close scans. */
776
+ #define NR_LOOPS 64
777
+ static int32_t nr_loops[NR_LOOPS];
778
+ static int32_t nr_nloops, nr_loops_spilled;
779
+
780
+ static void nr_note_loop(int32_t loop) {
781
+ for (int32_t i = 0; i < nr_nloops; i++)
782
+ if (nr_loops[i] == loop) return;
783
+ if (nr_nloops < NR_LOOPS) nr_loops[nr_nloops++] = loop;
784
+ else nr_loops_spilled = 1;
785
+ }
786
+
787
+ /* A closed descriptor may have been a loop: every virtual source in it leaves
788
+ it, as epoll drops its registrations, so a later loop that gets the same
789
+ number reports none of them. */
790
+ static void nr_forget_loop(int32_t fd) {
791
+ int32_t at = -1;
792
+ for (int32_t i = 0; i < nr_nloops; i++)
793
+ if (nr_loops[i] == fd) at = i;
794
+ if (at < 0 && !nr_loops_spilled) return;
795
+ if (at >= 0) nr_loops[at] = nr_loops[--nr_nloops];
796
+ for (int32_t si = 0; si < nr_nsets; si++) {
797
+ struct nr_set *S = nr_sets[si];
798
+ if (S->v.loop == fd) nr_unregister(&S->v);
799
+ for (int32_t ring = 0; ring < NR_RINGS; ring++) {
800
+ if (!S->conns[ring]) continue;
801
+ for (int32_t slot = 0; slot < S->slots; slot++)
802
+ if (S->conns[ring][slot].v.loop == fd) nr_unregister(&S->conns[ring][slot].v);
803
+ }
804
+ }
805
+ }
806
+
807
+ /* The virtual source `fd` names in a loop, or NULL for a descriptor. A stale
808
+ slot handle sets `*stale`. */
809
+ static struct nr_vent *nr_vent_for(int32_t fd, int *stale, struct nr_set **set) {
810
+ *stale = 0;
811
+ if (fd >= NR_BASE) {
812
+ int32_t ring, slot;
813
+ struct nr_conn *c = nr_decode(fd, set, &ring, &slot);
814
+ if (!c) *stale = 1;
815
+ return c ? &c->v : NULL;
816
+ }
817
+ struct nr_set *S = nr_listener(fd);
818
+ *set = S;
819
+ return S ? &S->v : NULL;
820
+ }
821
+
822
+ static int32_t nr_ctl(int32_t loop, int op, struct nr_vent *v, struct nr_set *S, int32_t events, int32_t token) {
823
+ if (op == EPOLL_CTL_ADD) {
824
+ if (v->loop >= 0) return -17;
825
+ /* The set's own epoll descriptor joins the loop, marked so a wake on it
826
+ is taken as the set's, never reported as a token. */
827
+ struct epoll_event e;
828
+ memset(&e, 0, sizeof e);
829
+ e.events = EPOLLIN;
830
+ int32_t si = 0;
831
+ while (nr_sets[si] != S) si++;
832
+ e.data.u64 = (uint64_t)1 << 32 | (uint32_t)si;
833
+ if (epoll_ctl(loop, EPOLL_CTL_ADD, nish_uring_fd(S->u), &e) != 0 && errno != EEXIST) return nish_net_fail();
834
+ v->loop = loop;
835
+ nr_note_loop(loop);
836
+ } else if (v->loop != loop) {
837
+ return -2;
838
+ }
839
+ if (op == EPOLL_CTL_DEL) {
840
+ nr_unregister(v);
841
+ return 0;
842
+ }
843
+ v->events = events;
844
+ v->token = token;
845
+ nr_unlist(v);
846
+ nr_touch(v);
847
+ return 0;
848
+ }
849
+
850
+ /* Writes the loop's ready virtual sources into `out`, at most `max`, in the
851
+ order they became ready. One reported and still ready goes to the tail,
852
+ so a busy source does not starve the rest. */
853
+ static int nr_collect(int32_t loop, int32_t *out, int max) {
854
+ int n = 0;
855
+ struct nr_vent *again = NULL, *again_tail = NULL;
856
+ for (struct nr_vent *v = nr_head, *next; v && n < max; v = next) {
857
+ next = v->next;
858
+ if (v->loop != loop) continue;
859
+ int32_t bits = nr_bits(v) & (v->events | 4);
860
+ nr_unlist(v);
861
+ if (bits == 0) continue;
862
+ out[2 * n] = v->token;
863
+ out[2 * n + 1] = bits;
864
+ n++;
865
+ v->next = NULL;
866
+ if (again_tail) again_tail->next = v; else again = v;
867
+ again_tail = v;
868
+ }
869
+ for (struct nr_vent *v = again, *next; v; v = next) {
870
+ next = v->next;
871
+ nr_append(v);
872
+ }
873
+ return n;
874
+ }
875
+
876
+ static int64_t nr_now_ms(void) {
877
+ struct timespec t;
878
+ clock_gettime(CLOCK_MONOTONIC, &t);
879
+ return (int64_t)t.tv_sec * 1000 + t.tv_nsec / 1000000;
880
+ }
881
+
882
+ /* Whose turn a full wait with room for one gives: 1 the epoll set's. */
883
+ static int nr_turn;
884
+
885
+ /* `pollWait` for a loop that may hold virtual sources: pump every set, take
886
+ what is ready, and sleep on the loop's epoll set, which holds each set's
887
+ descriptor, no longer than the sets allow (NURING-8). A wake by the bound,
888
+ or an EINTR, with nothing to report sleeps again until the caller's timeout
889
+ is spent. */
890
+ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout_ms) {
891
+ int64_t deadline = timeout_ms > 0 ? nr_now_ms() + timeout_ms : 0;
892
+ for (;;) {
893
+ nr_pump_all();
894
+ int n = nr_collect(loop, out, max);
895
+ int32_t t = 0;
896
+ if (n == 0) {
897
+ t = timeout_ms < 0 ? -1 : timeout_ms == 0 ? 0 : (int32_t)(deadline - nr_now_ms());
898
+ if (t < -1 || (timeout_ms > 0 && t < 0)) t = 0;
899
+ for (int32_t i = 0; i < nr_nsets; i++)
900
+ if (!nr_sets[i]->dead) t = nish_uring_sleep_ms(nr_sets[i]->u, t);
901
+ }
902
+ /* Ready slots that fill the batch still leave the epoll set a share of
903
+ it, half, or every other wait with room for one, so a descriptor such
904
+ as `signalFd()` is not starved by them (NURING-18). */
905
+ int room = max - n;
906
+ if (room == 0) room = max > 1 ? max / 2 : (nr_turn ^= 1);
907
+ struct epoll_event e[NR_EVENTS];
908
+ int k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
909
+ int woke = 0;
910
+ /* io_uring's task work interrupts the wait with no signal behind it, so
911
+ EINTR is a wake to pump for, not an answer of 0 before the timeout. */
912
+ if (k < 0 && errno != EINTR) return nish_net_fail();
913
+ if (k < 0) {
914
+ k = 0;
915
+ woke = 1;
916
+ }
917
+ int got_fds = 0;
918
+ for (int i = 0; i < k; i++) got_fds += !(e[i].data.u64 >> 32);
919
+ /* The slots this drops stay listed and ready, and a later wait reports them. */
920
+ if (n + got_fds > max) n = max - got_fds;
921
+ for (int i = 0; i < k; i++) {
922
+ if (e[i].data.u64 >> 32) {
923
+ woke = 1;
924
+ continue;
925
+ }
926
+ uint32_t got = e[i].events;
927
+ out[2 * n] = (int32_t)e[i].data.u32;
928
+ out[2 * n + 1] = ((got & EPOLLIN) ? 1 : 0) | ((got & EPOLLOUT) ? 2 : 0) | ((got & (EPOLLHUP | EPOLLERR)) ? 4 : 0);
929
+ n++;
930
+ }
931
+ if (n > 0) return n;
932
+ if (woke) {
933
+ nr_pump_all();
934
+ n = nr_collect(loop, out, max);
935
+ if (n > 0) return n;
936
+ }
937
+ if (timeout_ms == 0 || (timeout_ms > 0 && nr_now_ms() >= deadline)) return 0;
938
+ }
939
+ }
940
+ #endif
941
+
942
+ /* `netRingState()`: whether `tcpListenFlags` flag 8 gets rings. 0 before a
943
+ listener has asked, 1 when it got them, or the negative errno that refused
944
+ them, after which every listener in the process is a descriptor one: -1
945
+ (EPERM) for a filter or `io_uring_disabled`, -38 for a kernel without
946
+ io_uring, -22 or -95 for one too old for the features the set needs. Darwin
947
+ has no io_uring, and answers -95. */
948
+ /* WP63 S3: `--sandbox`, before its filter, makes the `n` restricted rings
949
+ every ring listener of this process takes, at the slots `nr_open` would
950
+ give a set, and writes each ring's descriptor to `fds` (64 of them). With
951
+ `n` of 0 it records that no ring may be made. */
952
+ int32_t nish_net_ring_premake(int32_t n, int32_t *fds) {
953
+ #if defined(__linux__)
954
+ int32_t slots = nr_slots();
955
+ return nish_uring_premake(n, slots < 0 ? 1 : slots, fds);
956
+ #else
957
+ (void)n;
958
+ (void)fds;
959
+ return -95;
960
+ #endif
961
+ }
962
+
963
+ int32_t nish_net_ring_state(void) {
964
+ #if defined(__linux__)
965
+ return nr_state;
966
+ #else
967
+ return -95;
968
+ #endif
969
+ }
970
+
231
971
  /* `netLocalPort(fd)`: the port the socket is bound to, which is how a
232
972
  program that listened on port 0 learns the one the kernel chose. */
233
973
  int32_t nish_net_local_port(int32_t fd) {
974
+ #if defined(__linux__)
975
+ if (fd >= NR_BASE) {
976
+ struct nr_set *S;
977
+ int32_t ring, slot;
978
+ return nr_decode(fd, &S, &ring, &slot) ? S->port : -9;
979
+ }
980
+ #endif
234
981
  nish_sockaddr s;
235
982
  socklen_t n = sizeof s;
236
983
  if (getsockname(fd, &s.sa, &n) != 0) return nish_net_fail();
@@ -339,13 +1086,28 @@ int32_t nish_tcp_listen(const nish_str *host, int32_t port, int32_t backlog) {
339
1086
  new connection to the group's listener whose index is the CPU that received
340
1087
  it, the index being the order the group's sockets were bound in, and the
341
1088
  kernel falls back to its hash when there is no such listener. Linux alone
342
- has it; elsewhere flag 4 is -95. Any other bit, or 4 without 1, is -22. */
1089
+ has it; elsewhere flag 4 is -95. Flag 8 asks for io_uring slots: on Linux
1090
+ with io_uring, `tcpAccept` hands out slot handles rather than descriptors,
1091
+ so the listener holds connections past RLIMIT_NOFILE (above). Where rings
1092
+ are refused the listener is a plain one and `netRingState()` says why;
1093
+ Darwin has none, and the flag does nothing there. Any other bit, or 4
1094
+ without 1, is -22. */
343
1095
  int32_t nish_tcp_listen_flags(const nish_str *host, int32_t port, int32_t backlog, int32_t flags) {
344
- if ((uint32_t)flags > 7 || (flags & 5) == 4) return -22;
1096
+ if ((uint32_t)flags > 15 || (flags & 5) == 4) return -22;
345
1097
  #if !defined(__linux__)
346
1098
  if (flags & 4) return -95;
347
1099
  #endif
348
- return nish_net_bound(host, port, SOCK_STREAM, backlog, flags);
1100
+ int32_t fd = nish_net_bound(host, port, SOCK_STREAM, backlog, flags & 7);
1101
+ #if defined(__linux__)
1102
+ if (fd >= 0 && (flags & 8)) {
1103
+ int32_t r = nr_open(fd, nish_net_local_port(fd));
1104
+ if (r < 0) {
1105
+ close(fd);
1106
+ return r;
1107
+ }
1108
+ }
1109
+ #endif
1110
+ return fd;
349
1111
  }
350
1112
 
351
1113
  /* A socket address of `n` bytes into the caller's 18-byte form. An IPv4 peer
@@ -387,6 +1149,10 @@ static void nish_net_put(nish_array *out, const nish_sockaddr *s, socklen_t n) {
387
1149
  socket without it. */
388
1150
  int32_t nish_tcp_accept(int32_t fd, nish_array *peer) {
389
1151
  if (peer->len < NISH_ADDR_BYTES) return -22;
1152
+ #if defined(__linux__)
1153
+ struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
1154
+ if (R) return nr_accept(R, peer);
1155
+ #endif
390
1156
  nish_sockaddr s;
391
1157
  socklen_t n = sizeof s;
392
1158
  #if defined(__linux__)
@@ -457,6 +1223,14 @@ int32_t nish_connect_result(int32_t fd) {
457
1223
  The compiled call has already checked `0 <= off <= off + len <=
458
1224
  buf.length`. */
459
1225
  int32_t nish_net_read(int32_t fd, nish_array *buf, int64_t off, int64_t len) {
1226
+ #if defined(__linux__)
1227
+ if (fd >= NR_BASE) {
1228
+ struct nr_set *S;
1229
+ int32_t ring, slot;
1230
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1231
+ return c ? nr_read(S, c, ring, slot, buf, off, len) : -9;
1232
+ }
1233
+ #endif
460
1234
  ssize_t n = recv(fd, buf->data + off, (size_t)len, 0);
461
1235
  return n < 0 ? nish_net_fail() : (int32_t)n;
462
1236
  }
@@ -464,6 +1238,14 @@ int32_t nish_net_read(int32_t fd, nish_array *buf, int64_t off, int64_t len) {
464
1238
  /* `netWrite(fd, buf, off, len)`: at most `len` bytes of `buf` from `off`,
465
1239
  with a peer that has gone answering -32 rather than raising SIGPIPE. */
466
1240
  int32_t nish_net_write(int32_t fd, const nish_array *buf, int64_t off, int64_t len) {
1241
+ #if defined(__linux__)
1242
+ if (fd >= NR_BASE) {
1243
+ struct nr_set *S;
1244
+ int32_t ring, slot;
1245
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1246
+ return c ? nr_send(S, c, ring, slot, buf->data + off, len) : -9;
1247
+ }
1248
+ #endif
467
1249
  ssize_t n = send(fd, buf->data + off, (size_t)len, NISH_NOSIGNAL);
468
1250
  return n < 0 ? nish_net_fail() : (int32_t)n;
469
1251
  }
@@ -479,6 +1261,20 @@ int32_t nish_net_write2(int32_t fd, const nish_array *a, int64_t aoff, int64_t a
479
1261
  struct iovec iov[2];
480
1262
  struct msghdr msg;
481
1263
  if (blen > (int64_t)INT32_MAX - alen) blen = (int64_t)INT32_MAX - alen;
1264
+ #if defined(__linux__)
1265
+ if (fd >= NR_BASE) {
1266
+ struct nr_set *S;
1267
+ int32_t ring, slot;
1268
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1269
+ if (!c) return -9;
1270
+ int32_t n = nr_send(S, c, ring, slot, a->data + aoff, alen);
1271
+ if (n < 0 || n < alen || blen == 0) return n;
1272
+ int32_t m = nr_send(S, c, ring, slot, b->data + boff, blen);
1273
+ /* With nothing of the first range sent, the second's answer is the
1274
+ call's, -11 included, as `sendmsg`'s would be. */
1275
+ return n == 0 ? m : m < 0 ? n : n + m;
1276
+ }
1277
+ #endif
482
1278
  iov[0].iov_base = a->data + aoff;
483
1279
  iov[0].iov_len = (size_t)alen;
484
1280
  iov[1].iov_base = b->data + boff;
@@ -499,7 +1295,20 @@ int32_t nish_net_shutdown(int32_t fd, int32_t how) {
499
1295
 
500
1296
  /* `netClose(fd)`. The descriptor is gone whatever this answers, as `close`
501
1297
  promises on Linux, so a failure is information rather than a retry. */
502
- int32_t nish_net_close(int32_t fd) { return close(fd) != 0 ? nish_net_fail() : 0; }
1298
+ int32_t nish_net_close(int32_t fd) {
1299
+ #if defined(__linux__)
1300
+ if (fd >= NR_BASE) {
1301
+ struct nr_set *S;
1302
+ int32_t ring, slot;
1303
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1304
+ return c ? nr_close(S, c, ring, slot) : -9;
1305
+ }
1306
+ struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
1307
+ if (R) return nr_close_listener(R);
1308
+ if (nr_nsets > 0) nr_forget_loop(fd);
1309
+ #endif
1310
+ return close(fd) != 0 ? nish_net_fail() : 0;
1311
+ }
503
1312
 
504
1313
  /* `netSetOption(fd, option, value)`: one option from a whitelist, named by
505
1314
  the runtime's own id rather than a level and an option number, so a
@@ -509,6 +1318,14 @@ int32_t nish_net_close(int32_t fd) { return close(fd) != 0 ? nish_net_fail() : 0
509
1318
  not keep quick-ACK mode: the kernel leaves it of its own accord, so a
510
1319
  server that wants it on or off sets it again after each response. */
511
1320
  int32_t nish_net_set_option(int32_t fd, int32_t option, int32_t value) {
1321
+ #if defined(__linux__)
1322
+ /* A slot's socket is the ring's; no option reaches it from here. */
1323
+ if (fd >= NR_BASE) {
1324
+ struct nr_set *S;
1325
+ int32_t ring, slot;
1326
+ return nr_decode(fd, &S, &ring, &slot) ? -95 : -9;
1327
+ }
1328
+ #endif
512
1329
  int level = SOL_SOCKET;
513
1330
  int name;
514
1331
  switch (option) {
@@ -752,10 +1569,18 @@ static int nish_poll_change(int32_t loop, int32_t fd, int filter, int flags, int
752
1569
  static int32_t nish_poll_ctl(int32_t loop, int op, int32_t fd, int32_t events, int32_t token) {
753
1570
  if ((uint32_t)events > 3) return -22;
754
1571
  #if defined(__linux__)
1572
+ if (nr_nsets > 0) {
1573
+ int stale;
1574
+ struct nr_set *S;
1575
+ struct nr_vent *v = nr_vent_for(fd, &stale, &S);
1576
+ if (stale) return -9;
1577
+ if (v) return nr_ctl(loop, op, v, S, events, token);
1578
+ }
755
1579
  struct epoll_event e;
756
1580
  memset(&e, 0, sizeof e);
757
1581
  e.events = ((events & 1) ? EPOLLIN : 0) | ((events & 2) ? EPOLLOUT : 0);
758
- e.data.u32 = (uint32_t)token;
1582
+ /* The high word is 0 for a descriptor, and 1 for a ring set's own. */
1583
+ e.data.u64 = (uint32_t)token;
759
1584
  return epoll_ctl(loop, op, fd, &e) != 0 ? nish_net_fail() : 0;
760
1585
  #else
761
1586
  int r, w;
@@ -796,9 +1621,11 @@ int32_t nish_poll_remove(int32_t loop, int32_t fd) { return nish_poll_ctl(loop,
796
1621
  `timeout_ms` milliseconds pass, a negative one never, and write a token and
797
1622
  its events into `ready` per ready descriptor, `ready[2k]` and
798
1623
  `ready[2k + 1]`. The count is at most `ready.length / 2` and at most
799
- `NISH_POLL_CHUNK`. A signal that interrupts the wait answers 0, as a
800
- timeout does, rather than waiting again past the deadline; the program's
801
- loop comes round and, if it watches `signalFd()`, finds the signal ready.
1624
+ `NISH_POLL_CHUNK`. Until the process has opened a ring listener, a signal
1625
+ that interrupts the wait answers 0, as a timeout does, rather than waiting
1626
+ again past the deadline; the program's loop comes round and, if it watches
1627
+ `signalFd()`, finds the signal ready. From then on, for the rest of the
1628
+ process's life, an interrupted wait sleeps again (NURING-14).
802
1629
  A `ready` shorter than 2 is -22. */
803
1630
  int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
804
1631
  uint64_t room = ready->len / 2;
@@ -806,6 +1633,7 @@ int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
806
1633
  int max = room < NISH_POLL_CHUNK ? (int)room : NISH_POLL_CHUNK;
807
1634
  int32_t *out = (int32_t *)ready->data;
808
1635
  #if defined(__linux__)
1636
+ if (nr_nsets > 0) return nr_poll_wait(loop, out, max, timeout_ms);
809
1637
  struct epoll_event e[NISH_POLL_CHUNK];
810
1638
  /* epoll waits forever for any negative timeout, as the language does. */
811
1639
  int n = epoll_wait(loop, e, max, timeout_ms);