@amritk/nish-aarch64-linux 0.46.0 → 0.48.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.
Files changed (46) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +29 -18
  3. package/package.json +1 -1
  4. package/runtime/gfx/README.md +55 -0
  5. package/runtime/gfx/nish-gfx.c +1063 -0
  6. package/runtime/gfx/nish-gfx.h +221 -0
  7. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  8. package/runtime/nish.d.ts +22 -5
  9. package/runtime/nish.h +1 -0
  10. package/runtime/nish.mjs +1 -0
  11. package/runtime/runtime-bytes.c +167 -7
  12. package/runtime/runtime-net.c +122 -52
  13. package/runtime/runtime-uring.c +150 -69
  14. package/runtime/sqlite/nish-sqlite.c +166 -39
  15. package/scripts/build.sh +88 -3
  16. package/std/README.md +2 -2
  17. package/std/base64.ts +28 -30
  18. package/std/compress/deflate.ts +1371 -704
  19. package/std/crypto/aes.ts +81 -45
  20. package/std/crypto/bcrypt-batch.ts +23 -4
  21. package/std/crypto/bcrypt.ts +36 -11
  22. package/std/crypto/chacha20poly1305.ts +93 -52
  23. package/std/crypto/x25519.ts +7 -4
  24. package/std/host/game.ts +52 -20
  25. package/std/host/ring.ts +115 -31
  26. package/std/host/text.ts +9 -3
  27. package/std/json.ts +83 -0
  28. package/std/math.ts +161 -131
  29. package/std/net/handler.ts +24 -6
  30. package/std/net/http-server.ts +358 -141
  31. package/std/net/http1-server.ts +44 -13
  32. package/std/net/prefork.ts +1 -2
  33. package/std/net/static.ts +7 -2
  34. package/std/random.ts +34 -51
  35. package/std/raster.ts +377 -113
  36. package/std/regex.ts +46 -15
  37. package/std/release.ts +19 -13
  38. package/std/sqlite.ts +380 -16
  39. package/std/synth.ts +20 -3
  40. package/std/temporal.ts +740 -363
  41. package/std/term-io.ts +81 -33
  42. package/std/term-pixels.ts +287 -112
  43. package/std/term-probe.ts +7 -3
  44. package/std/term-rate.ts +142 -66
  45. package/std/term.ts +782 -406
  46. package/std/testing.ts +14 -7
@@ -76,6 +76,7 @@ typedef int nish_net_unused;
76
76
  #if defined(__linux__)
77
77
  #include <linux/filter.h>
78
78
  #include <netinet/udp.h>
79
+ #include <poll.h>
79
80
  #include <signal.h>
80
81
  #include <stdio.h>
81
82
  #include <sys/epoll.h>
@@ -242,15 +243,27 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
242
243
 
243
244
  A handle is still an `i32`. Below 2^20 it is a descriptor, which is why
244
245
  `nish_net_raise_nofile` stops the soft limit at 2^20 on Linux. At or above
245
- it, `2^20 + ring * 2^20 + field` names a slot: `ring` is a global ring id,
246
- 64 per listener (`NR_RINGS`), and `field` is `slot + slots * (gen % G)`,
247
- with `G = 2^20 / slots` the generations a slot's handles cycle through. A
248
- close moves the slot's generation on, so the handle a program closed
249
- answers -9 at once and does not reach the next connection in that slot
250
- until the slot has been reused G times (1,024 at a limit of 1,024; never
251
- fewer than 64). At most `NR_SETS` ring listeners exist in a process, which
252
- keeps every handle below 2^31; their ids are not reused, so a closed
253
- listener's handles answer -9 for good.
246
+ it, `2^20 + set * 2^26 + ring * 2^B + field` names a slot: each ring
247
+ listener's set has 2^26 handles, split into 2^(26 - B) rings of 2^B, and
248
+ `field` is `slot + slots * (gen % G)`, with `G = 2^B / slots` the
249
+ generations a slot's handles cycle through. B is the least that is at
250
+ least 20 and leaves G at 64 or more: 20 for a ring of up to 16,384 slots,
251
+ so 64 rings (`NR_RINGS`) a set, and 22 for one of 65,536, so 16. A set
252
+ holds about 2^20 connections either way. A close moves the slot's
253
+ generation on, so the handle a program closed answers -9 at once and does
254
+ not reach the next connection in that slot until the slot has been reused
255
+ G times (1,024 at a limit of 1,024; never fewer than 64; NURING-54). At
256
+ most `NR_SETS` ring listeners exist in a process, which keeps every handle
257
+ below 2^31; their ids are not reused, so a closed listener's handles
258
+ answer -9 for good.
259
+
260
+ A ring holds RLIMIT_NOFILE slots, the most io_uring lets one ring's table
261
+ have, up to 16,384 (`NR_SLOTS_MAX`), or up to 65,536 (`NR_SLOTS_WIDE`) for
262
+ a listener opened with `NET_LISTEN_RING_WIDE`. Each ring is a pass's
263
+ io_uring_enter and its eventfd, so fewer, wider rings cost a busy loop
264
+ less (measured: 3.5 points of one CPU at 250,000 connections); each slot
265
+ costs about 110 bytes a ring up front, so a ring of 65,536 is about 7 MB
266
+ where one of 16,384 is about 1.8 MB, which is why it is asked for.
254
267
 
255
268
  `netShutdown` queues an io_uring SHUTDOWN behind the slot's sends, so the
256
269
  peer reads every byte written before it and then the FIN (NURING-39);
@@ -286,6 +299,7 @@ struct nish_uring_opts {
286
299
  int32_t cq_entries;
287
300
  int32_t buf_count;
288
301
  int32_t buf_size;
302
+ int32_t taskrun_worth;
289
303
  };
290
304
  int32_t nish_uring_probe(void);
291
305
  int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds);
@@ -303,11 +317,11 @@ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t
303
317
  int32_t nish_uring_full(const struct nish_uring_set *s);
304
318
  int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
305
319
  void nish_uring_destroy(struct nish_uring_set *s);
306
- /* runtime-uring.c's counters; the dump below prints six of them. */
320
+ /* runtime-uring.c's counters; the dump below prints seven of them. */
307
321
  struct nish_uring_stats {
308
322
  int64_t rings, live, accepting, slots, taskrun_reentries, overflow_drains, nobufs_rearms, send_requeues;
309
323
  int64_t accept_errors, accept_pauses, bound_violations, max_accepting_rings, direct_waits, sends;
310
- int64_t pool_new, pool_held, pool_cached;
324
+ int64_t pool_new, pool_held, pool_cached, enters;
311
325
  };
312
326
  void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
313
327
 
@@ -323,8 +337,11 @@ void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *s
323
337
  /* A slot's first receive queue, doubled as it fills: small enough for
324
338
  glibc's thread cache, and freed once drained (NURING-38). */
325
339
  #define NR_RECV_FIRST 1024
326
- /* The most slots per ring: a table is the kernel's memory, sized up front. */
340
+ /* The most slots per ring: a table is the kernel's memory, sized up front,
341
+ and so is the set's record of each slot. NET_LISTEN_RING_WIDE asks for
342
+ the larger. */
327
343
  #define NR_SLOTS_MAX 16384
344
+ #define NR_SLOTS_WIDE 65536
328
345
  #define NR_EVENTS 256
329
346
 
330
347
  /* A loop's record of one virtual source, a slot or a ring listener: the loop
@@ -354,14 +371,44 @@ struct nr_set {
354
371
  struct nish_uring_set *u;
355
372
  struct nr_vent v; /* the listener's own entry */
356
373
  int32_t listener, port, slots, gens, dead, accept_err;
374
+ int32_t rbits; /* B above: log2 of each ring's share of the set's 2^26 handles */
357
375
  int32_t accept_once; /* an accept that found memory short, answered by the next tcpAccept */
358
376
  struct nr_conn *conns[NR_RINGS];
359
377
  uint32_t *fifo; /* accepted (ring << 20 | slot), not yet taken */
360
378
  uint32_t fhead, flen, fcap;
361
379
  uint32_t *pend; /* wblocked or lingering slots, checked after each pump */
362
380
  uint32_t npend, pcap;
381
+ int64_t windows, window_waits; /* batch windows, and the ring waits inside them, for the stats line */
382
+ uint8_t **rcache; /* first-size receive queues given back, reused last in, first out */
383
+ uint32_t nrcache;
363
384
  };
364
385
 
386
+ /* Receive queues of NR_RECV_FIRST bytes a set keeps for reuse. Nearly every
387
+ request is one receive into a fresh queue that its read drains and frees,
388
+ and a pass holds more of them than malloc's per-thread cache keeps, so
389
+ kept here they come back hot and cost no allocator call. */
390
+ #define NR_RCACHE 4096
391
+
392
+ /* A queue of NR_RECV_FIRST bytes: a kept one, else malloc's. */
393
+ static uint8_t *nr_rbuf_take(struct nr_set *S) {
394
+ if (S->nrcache > 0) return S->rcache[--S->nrcache];
395
+ return malloc(NR_RECV_FIRST);
396
+ }
397
+
398
+ /* `c`'s queue given back: kept when it is first-size and there is room. */
399
+ static void nr_rbuf_give(struct nr_set *S, struct nr_conn *c) {
400
+ if (c->rbuf && c->rcap == NR_RECV_FIRST) {
401
+ if (!S->rcache) S->rcache = malloc(NR_RCACHE * sizeof *S->rcache);
402
+ if (S->rcache && S->nrcache < NR_RCACHE) {
403
+ S->rcache[S->nrcache++] = c->rbuf;
404
+ c->rbuf = NULL;
405
+ return;
406
+ }
407
+ }
408
+ free(c->rbuf);
409
+ c->rbuf = NULL;
410
+ }
411
+
365
412
  static struct nr_set *nr_sets[NR_SETS];
366
413
  static int32_t nr_nsets;
367
414
  /* `netRingState()`: 0 until a listener opens a set (1) or meets a refusal. */
@@ -436,11 +483,12 @@ static int nr_push(uint32_t **a, uint32_t *n, uint32_t *cap, uint32_t x) {
436
483
  static struct nr_conn *nr_decode(int32_t h, struct nr_set **set, int32_t *ring, int32_t *slot) {
437
484
  if (h < NR_BASE) return NULL;
438
485
  uint32_t x = (uint32_t)h - NR_BASE;
439
- uint32_t g = x >> 20, field = x & (NR_BASE - 1);
440
- uint32_t si = g / NR_RINGS, ri = g % NR_RINGS;
486
+ uint32_t si = x >> 26;
441
487
  if (si >= (uint32_t)nr_nsets) return NULL;
442
488
  struct nr_set *S = nr_sets[si];
443
- if (S->dead || !S->conns[ri]) return NULL;
489
+ uint32_t y = x & ((1u << 26) - 1);
490
+ uint32_t ri = y >> S->rbits, field = y & ((1u << S->rbits) - 1);
491
+ if (ri >= NR_RINGS || S->dead || !S->conns[ri]) return NULL;
444
492
  uint32_t sl = field % (uint32_t)S->slots, gm = field / (uint32_t)S->slots;
445
493
  struct nr_conn *c = &S->conns[ri][sl];
446
494
  if (gm >= (uint32_t)S->gens || !c->open || c->lingering || c->gen % (uint32_t)S->gens != gm) return NULL;
@@ -453,7 +501,7 @@ static struct nr_conn *nr_decode(int32_t h, struct nr_set **set, int32_t *ring,
453
501
  static int32_t nr_encode(const struct nr_set *S, int32_t si, uint32_t ring, uint32_t slot) {
454
502
  const struct nr_conn *c = &S->conns[ring][slot];
455
503
  uint32_t field = slot + (uint32_t)S->slots * (c->gen % (uint32_t)S->gens);
456
- return (int32_t)((uint32_t)NR_BASE + ((uint32_t)si * NR_RINGS + ring) * (uint32_t)NR_BASE + field);
504
+ return (int32_t)((uint32_t)NR_BASE + ((uint32_t)si << 26) + (ring << S->rbits) + field);
457
505
  }
458
506
 
459
507
  /* Ends a slot for good: the set closes it and the slot goes back to the
@@ -539,7 +587,7 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
539
587
  if (c->rlen + n > c->rcap) {
540
588
  uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
541
589
  while (nc < c->rlen + n) nc *= 2;
542
- uint8_t *p = realloc(c->rbuf, nc);
590
+ uint8_t *p = c->rcap == 0 && nc == NR_RECV_FIRST ? nr_rbuf_take(S) : realloc(c->rbuf, nc);
543
591
  if (!p) {
544
592
  c->hup = 1;
545
593
  c->hup_res = -12;
@@ -635,9 +683,11 @@ static void nr_stats_print(const struct nr_set *S, const char *why) {
635
683
  struct nish_uring_stats st;
636
684
  nish_uring_stats(S->u, &st);
637
685
  dprintf(2,
638
- "uring-stats %s port=%d sends=%lld pool_new=%lld pool_held=%lld pool_cached=%lld live=%lld slots=%lld\n",
686
+ "uring-stats %s port=%d sends=%lld pool_new=%lld pool_held=%lld pool_cached=%lld live=%lld slots=%lld "
687
+ "enters=%lld windows=%lld window_waits=%lld\n",
639
688
  why, (int)S->port, (long long)st.sends, (long long)st.pool_new, (long long)st.pool_held,
640
- (long long)st.pool_cached, (long long)st.live, (long long)st.slots);
689
+ (long long)st.pool_cached, (long long)st.live, (long long)st.slots, (long long)st.enters,
690
+ (long long)S->windows, (long long)S->window_waits);
641
691
  }
642
692
 
643
693
  static void nr_pump_all(void) {
@@ -668,14 +718,15 @@ static int nr_refused(int32_t r) { return r == -1 || r == -38 || r == -22 || r =
668
718
  to descriptors (§5.4); a refusal before any set has opened also decides for
669
719
  every later listen, and any other failure, such as -24 or -12, only for
670
720
  this one (NURING-20, NURING-22). */
671
- static int32_t nr_slots(void) {
721
+ static int32_t nr_slots(int wide) {
672
722
  struct rlimit rl;
673
723
  if (getrlimit(RLIMIT_NOFILE, &rl) != 0) return -1;
674
- int32_t n = rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > NR_SLOTS_MAX ? NR_SLOTS_MAX : (int32_t)rl.rlim_cur;
724
+ int32_t cap = wide ? NR_SLOTS_WIDE : NR_SLOTS_MAX;
725
+ int32_t n = rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > (rlim_t)cap ? cap : (int32_t)rl.rlim_cur;
675
726
  return n < 1 ? 1 : n;
676
727
  }
677
728
 
678
- static int32_t nr_open(int32_t fd, int32_t port) {
729
+ static int32_t nr_open(int32_t fd, int32_t port, int wide) {
679
730
  if (nr_state < 0) return 0;
680
731
  /* Under `--sandbox` every set takes the rings made before the filter, so
681
732
  any failure of theirs, a refusal or not, decides for good: no ring can be
@@ -691,9 +742,12 @@ static int32_t nr_open(int32_t fd, int32_t port) {
691
742
  if (nr_nsets >= NR_SETS) return -23;
692
743
  struct nish_uring_opts o;
693
744
  memset(&o, 0, sizeof o);
694
- o.slots = pooled > 0 ? pooled : nr_slots();
745
+ o.slots = pooled > 0 ? pooled : nr_slots(wide);
695
746
  if (o.slots < 0) return 0;
696
- o.rings_max = NR_RINGS;
747
+ /* B above: each ring's handles hold 64 generations of every slot. */
748
+ int32_t rbits = 20;
749
+ while (((int64_t)1 << rbits) < (int64_t)o.slots * 64) rbits++;
750
+ o.rings_max = 1 << (26 - rbits);
697
751
  struct nr_set *S = calloc(1, sizeof *S);
698
752
  if (!S) return 0;
699
753
  int32_t r = nish_uring_open(&S->u, fd, &o);
@@ -709,7 +763,8 @@ static int32_t nr_open(int32_t fd, int32_t port) {
709
763
  S->listener = fd;
710
764
  S->port = port;
711
765
  S->slots = o.slots;
712
- S->gens = NR_BASE / o.slots;
766
+ S->rbits = rbits;
767
+ S->gens = (1 << rbits) / o.slots;
713
768
  S->v.loop = -1;
714
769
  S->v.is_listener = 1;
715
770
  nr_sets[nr_nsets++] = S;
@@ -735,6 +790,10 @@ static int32_t nr_close_listener(struct nr_set *S) {
735
790
  free(S->fifo);
736
791
  free(S->pend);
737
792
  S->fifo = S->pend = NULL;
793
+ for (uint32_t i = 0; i < S->nrcache; i++) free(S->rcache[i]);
794
+ free(S->rcache);
795
+ S->rcache = NULL;
796
+ S->nrcache = 0;
738
797
  S->dead = 1;
739
798
  return close(S->listener) != 0 ? nish_net_fail() : 0;
740
799
  }
@@ -776,9 +835,13 @@ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
776
835
  /* A drained queue is freed and emptied, so an idle slot holds none
777
836
  (NURING-38) and a receive never finds roff at rlen past 0. */
778
837
  if (c->roff == c->rlen) {
779
- free(c->rbuf);
780
- c->rbuf = NULL;
838
+ nr_rbuf_give(S, c);
781
839
  c->rcap = c->roff = c->rlen = 0;
840
+ /* Drained, the slot is not ready, so it leaves the ready list now,
841
+ while its entry is in cache, rather than when the next collect
842
+ walks to it to find that out (one cache miss a request at
843
+ 150,000 connections). */
844
+ if (c->v.listed && !(nr_bits(&c->v) & (c->v.events | 4))) nr_unlist(&c->v);
782
845
  }
783
846
  return (int32_t)n;
784
847
  }
@@ -837,8 +900,7 @@ static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32
837
900
  nr_unregister(&c->v);
838
901
  c->gen++;
839
902
  c->roff = c->rlen = 0;
840
- free(c->rbuf);
841
- c->rbuf = NULL;
903
+ nr_rbuf_give(S, c);
842
904
  c->rcap = 0;
843
905
  int64_t q = nish_uring_queued(S->u, ring, slot);
844
906
  /* A peer that only stopped sending (a FIN) still takes what is queued, so
@@ -1115,12 +1177,19 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
1115
1177
  #endif
1116
1178
 
1117
1179
  #if defined(__linux__)
1118
- /* A batch's second wait: `n` pairs are in `out`, and it waits on the one live
1119
- set's ring, up to `max_wait_ms`, until `min` are ready or `max` fill it. It
1120
- reads no epoll set, so an ordinary descriptor that becomes ready meanwhile
1121
- waits for the next call, at most `max_wait_ms`; on a direct loop the
1122
- ring's poll on `signalFd()` ends the batch as soon as it answers, and the
1123
- next call reports it. */
1180
+ /* A batch's second wait: `n` pairs are in `out`, and it sleeps on the one
1181
+ live set, up to `max_wait_ms`, until `min` are ready or `max` fill it. It
1182
+ sleeps in at most eight slices, each an eighth of `max_wait_ms` rounded
1183
+ up, without entering the ring, and after each slice pumps the set once and
1184
+ collects once, so a window costs at most eight pumps however its
1185
+ completions trickle in: a wait on the ring itself would wake, run its
1186
+ deferred task work and pump once per completion, sends' included. Its
1187
+ `min` is checked at the end of a slice, so a window can outlast the moment
1188
+ `min` became ready by up to one slice. It reads no epoll set, so an
1189
+ ordinary descriptor that becomes ready meanwhile waits for the next call,
1190
+ at most `max_wait_ms`; on a direct loop the slice's poll(2) on
1191
+ `signalFd()` ends the batch as soon as it answers, and the next call
1192
+ reports it. */
1124
1193
  static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int32_t max_wait_ms) {
1125
1194
  struct nr_set *S = NULL;
1126
1195
  for (int32_t i = 0; i < nr_nsets; i++) {
@@ -1138,21 +1207,18 @@ static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int
1138
1207
  int32_t watch = -1;
1139
1208
  int direct = nr_direct(loop, &watch) == S;
1140
1209
  int64_t end = nr_now_ms() + max_wait_ms;
1210
+ int64_t slice = (max_wait_ms + 7) / 8;
1211
+ S->windows++;
1141
1212
  while (n < min && n < max) {
1142
1213
  int64_t left = end - nr_now_ms();
1143
1214
  if (left <= 0) break;
1144
- int rc = direct ? nish_uring_wait_direct(S->u, watch, min - n, (int32_t)left) : -EOPNOTSUPP;
1145
- if (rc == -EOPNOTSUPP) {
1146
- struct nish_uring_event ev[NR_EVENTS];
1147
- int k = nish_uring_wait(S->u, (int32_t)left, ev, NR_EVENTS);
1148
- if (k < 0) break;
1149
- for (int i = 0; i < k; i++) nr_dispatch(S, &ev[i]);
1150
- } else if (rc < 0) {
1151
- break;
1152
- }
1215
+ S->window_waits++;
1216
+ /* poll(2) ignores a negative descriptor, so with no watch this sleeps. */
1217
+ struct pollfd p = {direct ? watch : -1, POLLIN, 0};
1218
+ int woke = poll(&p, 1, (int)(left < slice ? left : slice)) > 0;
1153
1219
  nr_pump(S);
1154
1220
  n += nr_collect_from(loop, out + 2 * n, max - n, 1);
1155
- if (direct && watch >= 0 && nish_uring_poll_events(S->u, 0)) break;
1221
+ if (woke) break;
1156
1222
  }
1157
1223
  return n;
1158
1224
  }
@@ -1167,11 +1233,13 @@ static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int
1167
1233
  has no io_uring, and answers -95. */
1168
1234
  /* WP63 S3: `--sandbox`, before its filter, makes the `n` restricted rings
1169
1235
  every ring listener of this process takes, at the slots `nr_open` would
1170
- give a set, and writes each ring's descriptor to `fds` (64 of them). With
1171
- `n` of 0 it records that no ring may be made. */
1236
+ give a set without `NET_LISTEN_RING_WIDE` (they are made before the
1237
+ program could ask for wider ones, so a sandboxed listener's rings are
1238
+ never wide), and writes each ring's descriptor to `fds` (64 of them).
1239
+ With `n` of 0 it records that no ring may be made. */
1172
1240
  int32_t nish_net_ring_premake(int32_t n, int32_t *fds) {
1173
1241
  #if defined(__linux__)
1174
- int32_t slots = nr_slots();
1242
+ int32_t slots = nr_slots(0);
1175
1243
  return nish_uring_premake(n, slots < 0 ? 1 : slots, fds);
1176
1244
  #else
1177
1245
  (void)n;
@@ -1310,17 +1378,19 @@ int32_t nish_tcp_listen(const nish_str *host, int32_t port, int32_t backlog) {
1310
1378
  with io_uring, `tcpAccept` hands out slot handles rather than descriptors,
1311
1379
  so the listener holds connections past RLIMIT_NOFILE (above). Where rings
1312
1380
  are refused the listener is a plain one and `netRingState()` says why;
1313
- Darwin has none, and the flag does nothing there. Any other bit, or 4
1314
- without 1, is -22. */
1381
+ Darwin has none, and the flag does nothing there. Flag 16, which needs
1382
+ flag 8, makes the listener's rings as wide as RLIMIT_NOFILE allows up to
1383
+ 65,536 slots rather than 16,384 (above). Any other bit, 4 without 1, or
1384
+ 16 without 8, is -22. */
1315
1385
  int32_t nish_tcp_listen_flags(const nish_str *host, int32_t port, int32_t backlog, int32_t flags) {
1316
- if ((uint32_t)flags > 15 || (flags & 5) == 4) return -22;
1386
+ if ((uint32_t)flags > 31 || (flags & 5) == 4 || (flags & 24) == 16) return -22;
1317
1387
  #if !defined(__linux__)
1318
1388
  if (flags & 4) return -95;
1319
1389
  #endif
1320
1390
  int32_t fd = nish_net_bound(host, port, SOCK_STREAM, backlog, flags & 7);
1321
1391
  #if defined(__linux__)
1322
1392
  if (fd >= 0 && (flags & 8)) {
1323
- int32_t r = nr_open(fd, nish_net_local_port(fd));
1393
+ int32_t r = nr_open(fd, nish_net_local_port(fd), (flags & 16) != 0);
1324
1394
  if (r < 0) {
1325
1395
  close(fd);
1326
1396
  return r;