@amritk/nish-x86_64-darwin 0.45.1 → 0.46.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.
- package/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/package.json +1 -1
- package/runtime/nish.d.ts +41 -0
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +13 -13
- package/std/net/handler.ts +42 -7
- package/std/sqlite.ts +171 -0
package/INSTALL.md
CHANGED
|
@@ -194,7 +194,9 @@ The attestation is a Sigstore-signed statement, made by
|
|
|
194
194
|
`SHA256SUMS` was built by `.github/workflows/release.yml` at the release's tag
|
|
195
195
|
on a GitHub-hosted runner. A failed verification refuses the install; with no
|
|
196
196
|
`gh`, a signed-out one or one too old for those options, the check is skipped
|
|
197
|
-
with a note saying so, and the SHA-256 still applies.
|
|
197
|
+
with a note saying so, and the SHA-256 still applies. A `gh` that has a token
|
|
198
|
+
but whose `gh auth status` fails (it asks GitHub's API) is tried three times,
|
|
199
|
+
a second or two apart, before it counts as unusable, and the note quotes it. Set
|
|
198
200
|
`NISH_REQUIRE_ATTESTATION=1` to refuse instead of skipping: the install then
|
|
199
201
|
fails unless `gh` verifies the attestation, and refuses a release of 0.21.0 or
|
|
200
202
|
older, which carries none. The same command by hand, on a tarball you
|
package/bin/nish
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/runtime/nish.d.ts
CHANGED
|
@@ -1998,6 +1998,27 @@ declare function pollRemove(loop: i32, fd: i32): i32;
|
|
|
1998
1998
|
* the next wait), 0 on a timeout, or a negative errno.
|
|
1999
1999
|
*/
|
|
2000
2000
|
declare function pollWait(loop: i32, ready: i32[], timeoutMs: i32): i32;
|
|
2001
|
+
/**
|
|
2002
|
+
* Wait as `pollWait` does, up to `timeoutMs` for the first event, and then up to `maxWaitMs`
|
|
2003
|
+
* more until `minEvents` pairs are ready, so a busy ring loop takes its events in batches. An
|
|
2004
|
+
* idle loop is not woken every `maxWaitMs`: the second wait starts only once an event is in.
|
|
2005
|
+
* The batch counts the slots and listeners of a `NET_LISTEN_RING` listener; a loop with no ring
|
|
2006
|
+
* listener, and Darwin, answer as `pollWait` does. A descriptor that becomes ready during the
|
|
2007
|
+
* second wait, `signalFd()` included, is reported at most `maxWaitMs` late. `minEvents` of 1 or
|
|
2008
|
+
* less, or a `maxWaitMs` of 0 or less, is `pollWait`. Not callable in a `scope()` task or a
|
|
2009
|
+
* parallel body. A call that writes `ready` is refused in a scope's region on an array a task may
|
|
2010
|
+
* read, as `fill` is. Refused at compile time on wasm32 and the WASI profile (NL2404).
|
|
2011
|
+
* Capability: net.
|
|
2012
|
+
*
|
|
2013
|
+
* @param loop the loop from `pollCreate`.
|
|
2014
|
+
* @param ready receives the pairs; shorter than 2 is -22. Its length caps the batch.
|
|
2015
|
+
* @param timeoutMs the most milliseconds to wait for the first event; negative: forever (-1).
|
|
2016
|
+
* @param minEvents the pairs a batch waits for once the first is in.
|
|
2017
|
+
* @param maxWaitMs the most milliseconds the batch waits after the first event.
|
|
2018
|
+
* @returns how many pairs, at most `ready.length / 2` and at most 64, 0 on a timeout, or a
|
|
2019
|
+
* negative errno.
|
|
2020
|
+
*/
|
|
2021
|
+
declare function pollWaitBatch(loop: i32, ready: i32[], timeoutMs: i32, minEvents: i32, maxWaitMs: i32): i32;
|
|
2001
2022
|
/**
|
|
2002
2023
|
* Set one socket option from a whitelist: there is no raw level and option number, so a program
|
|
2003
2024
|
* cannot pass a level, a name and a length through to the kernel. The ids are the runtime's own
|
|
@@ -3453,6 +3474,26 @@ declare module "nish:net" {
|
|
|
3453
3474
|
* for a `ready` shorter than 2.
|
|
3454
3475
|
*/
|
|
3455
3476
|
export function pollWait(loop: i32, ready: i32[], timeoutMs: i32): i32;
|
|
3477
|
+
/**
|
|
3478
|
+
* Wait as `pollWait` does for the first event, and then, on a loop holding
|
|
3479
|
+
* a `NET_LISTEN_RING` listener's slots, up to `maxWaitMs` more until
|
|
3480
|
+
* `minEvents` pairs are ready. An idle loop is not woken every `maxWaitMs`.
|
|
3481
|
+
* A descriptor that becomes ready during the second wait is reported at
|
|
3482
|
+
* most `maxWaitMs` late. A loop with no ring listener, and Darwin, answer
|
|
3483
|
+
* as `pollWait` does.
|
|
3484
|
+
*
|
|
3485
|
+
* @param loop The loop.
|
|
3486
|
+
* @param ready The array the pairs are written into; its length caps the
|
|
3487
|
+
* batch.
|
|
3488
|
+
* @param timeoutMs The most milliseconds to wait for the first event; any
|
|
3489
|
+
* negative number waits forever.
|
|
3490
|
+
* @param minEvents The pairs a batch waits for once the first is in.
|
|
3491
|
+
* @param maxWaitMs The most milliseconds the batch waits after the first
|
|
3492
|
+
* event.
|
|
3493
|
+
* @returns The count of pairs, at most `ready.length / 2` and at most 64,
|
|
3494
|
+
* `0` on a timeout, or `-22` for a `ready` shorter than 2.
|
|
3495
|
+
*/
|
|
3496
|
+
export function pollWaitBatch(loop: i32, ready: i32[], timeoutMs: i32, minEvents: i32, maxWaitMs: i32): i32;
|
|
3456
3497
|
/**
|
|
3457
3498
|
* Set one socket option from a whitelist. There is no way to pass a raw
|
|
3458
3499
|
* level and option number, because that would be an untyped call into the
|
package/runtime/nish.h
CHANGED
|
@@ -793,6 +793,15 @@ double nish_stat_mtime(const nish_str *path);
|
|
|
793
793
|
* or a read that fails. */
|
|
794
794
|
int32_t nish_signal_fd(void);
|
|
795
795
|
int32_t nish_read_signal(int32_t fd);
|
|
796
|
+
/* The runtime's own, with no builtin: the read end `signalFd()` made, or -1
|
|
797
|
+
* while none is made, without making one. A loop's wait asks it to tell the
|
|
798
|
+
* signal descriptor from any other (runtime-net.c, the direct ring wait). */
|
|
799
|
+
int32_t nish_signal_fd_made(void);
|
|
800
|
+
/* Every closer that takes a program's descriptor number calls this first:
|
|
801
|
+
* once the signal read end is closed, `nish_signal_fd_made` answers -1 for
|
|
802
|
+
* good, since its number may be reused (docs/security/net-uring.md,
|
|
803
|
+
* NURING-49). */
|
|
804
|
+
void nish_signal_fd_closing(int32_t fd);
|
|
796
805
|
/* Who owns a path, and whether it runs (docs/security/runtime.md, RT-9; the
|
|
797
806
|
* builtins `lstatOwnerModeSync`, `geteuid` and `isExecutableSync`, #386).
|
|
798
807
|
* `nish_lstat_owner_mode(path)`: `lstat`, so a
|
|
@@ -927,6 +936,7 @@ int32_t nish_poll_add(int32_t loop, int32_t fd, int32_t events, int32_t token);
|
|
|
927
936
|
int32_t nish_poll_modify(int32_t loop, int32_t fd, int32_t events, int32_t token);
|
|
928
937
|
int32_t nish_poll_remove(int32_t loop, int32_t fd);
|
|
929
938
|
int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms);
|
|
939
|
+
int32_t nish_poll_wait_batch(int32_t loop, nish_array *ready, int32_t timeout_ms, int32_t min_events, int32_t max_wait_ms);
|
|
930
940
|
int32_t nish_unix_listen(const nish_str *path, int32_t backlog, int32_t flags);
|
|
931
941
|
int32_t nish_unix_listen_mode(const nish_str *path, int32_t backlog, int32_t flags, int32_t mode);
|
|
932
942
|
int32_t nish_unix_connect(const nish_str *path);
|
package/runtime/nish.mjs
CHANGED
package/runtime/runtime-host.c
CHANGED
|
@@ -212,6 +212,21 @@ int32_t nish_signal_fd(void) {
|
|
|
212
212
|
return (int32_t)(cur >> 32);
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
+
/* Set once a closer (`netClose`, `fileClose`) is handed the read end: from
|
|
216
|
+
then on its number may name any descriptor opened since, so
|
|
217
|
+
`nish_signal_fd_made` answers -1 rather than a number that is no longer the
|
|
218
|
+
signal pipe (docs/security/net-uring.md, NURING-48 and NURING-49). */
|
|
219
|
+
static int32_t nish_signal_closed;
|
|
220
|
+
|
|
221
|
+
int32_t nish_signal_fd_made(void) {
|
|
222
|
+
int64_t cur = __atomic_load_n(&nish_signal_pipe, __ATOMIC_ACQUIRE);
|
|
223
|
+
return cur < 0 || __atomic_load_n(&nish_signal_closed, __ATOMIC_ACQUIRE) ? -1 : (int32_t)(cur >> 32);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
void nish_signal_fd_closing(int32_t fd) {
|
|
227
|
+
if (fd >= 0 && fd == nish_signal_fd_made()) __atomic_store_n(&nish_signal_closed, 1, __ATOMIC_RELEASE);
|
|
228
|
+
}
|
|
229
|
+
|
|
215
230
|
/* Block until a signal's byte arrives and answer it: 15 or 2, or -1 for any
|
|
216
231
|
`fd` that is not the signal descriptor and for a read that fails. */
|
|
217
232
|
int32_t nish_read_signal(int32_t fd) {
|
package/runtime/runtime-net.c
CHANGED
|
@@ -76,6 +76,8 @@ typedef int nish_net_unused;
|
|
|
76
76
|
#if defined(__linux__)
|
|
77
77
|
#include <linux/filter.h>
|
|
78
78
|
#include <netinet/udp.h>
|
|
79
|
+
#include <signal.h>
|
|
80
|
+
#include <stdio.h>
|
|
79
81
|
#include <sys/epoll.h>
|
|
80
82
|
#include <time.h>
|
|
81
83
|
/* glibc before 2.29 names neither; the numbers are the kernel's ABI. */
|
|
@@ -292,6 +294,8 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
292
294
|
int32_t nish_uring_fd(const struct nish_uring_set *s);
|
|
293
295
|
int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
|
|
294
296
|
int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
|
|
297
|
+
int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms);
|
|
298
|
+
int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take);
|
|
295
299
|
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
|
|
296
300
|
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
|
|
297
301
|
int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
|
|
@@ -299,6 +303,13 @@ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t
|
|
|
299
303
|
int32_t nish_uring_full(const struct nish_uring_set *s);
|
|
300
304
|
int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
|
|
301
305
|
void nish_uring_destroy(struct nish_uring_set *s);
|
|
306
|
+
/* runtime-uring.c's counters; the dump below prints six of them. */
|
|
307
|
+
struct nish_uring_stats {
|
|
308
|
+
int64_t rings, live, accepting, slots, taskrun_reentries, overflow_drains, nobufs_rearms, send_requeues;
|
|
309
|
+
int64_t accept_errors, accept_pauses, bound_violations, max_accepting_rings, direct_waits, sends;
|
|
310
|
+
int64_t pool_new, pool_held, pool_cached;
|
|
311
|
+
};
|
|
312
|
+
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
|
|
302
313
|
|
|
303
314
|
#define NR_BASE (1 << 20)
|
|
304
315
|
#define NR_RINGS 64
|
|
@@ -321,6 +332,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
|
|
|
321
332
|
the list of sources that are ready, kept in the order they became so. */
|
|
322
333
|
struct nr_vent {
|
|
323
334
|
int32_t loop, events, token;
|
|
335
|
+
uint32_t seen; /* the `nr_epoch` of the batch that last reported it */
|
|
324
336
|
uint8_t listed, is_listener;
|
|
325
337
|
struct nr_vent *prev, *next;
|
|
326
338
|
};
|
|
@@ -590,9 +602,52 @@ static void nr_pump(struct nr_set *S) {
|
|
|
590
602
|
S->npend = keep;
|
|
591
603
|
}
|
|
592
604
|
|
|
605
|
+
/* The stats dump. With NISH_URING_STATS set to a value other than "" or
|
|
606
|
+
"0" when the first ring listener opens, SIGUSR1 asks for one line per
|
|
607
|
+
open set on stderr, printed by the next pump of the loop, and closing a
|
|
608
|
+
ring listener prints its set's line before the set goes. Without it no
|
|
609
|
+
handler is installed and SIGUSR1 keeps its default action. The handler
|
|
610
|
+
stays installed once set; with no set open, a signal prints nothing.
|
|
611
|
+
signalFd's pipe (runtime-host.c) handles only SIGINT and SIGTERM, so it
|
|
612
|
+
never claims SIGUSR1. */
|
|
613
|
+
static int nr_stats_on = -1; /* -1 until the first open reads the knob */
|
|
614
|
+
static volatile sig_atomic_t nr_stats_asked;
|
|
615
|
+
|
|
616
|
+
static void nr_stats_signal(int sig) {
|
|
617
|
+
(void)sig;
|
|
618
|
+
nr_stats_asked = 1;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
static void nr_stats_setup(void) {
|
|
622
|
+
if (nr_stats_on >= 0) return;
|
|
623
|
+
const char *v = getenv("NISH_URING_STATS");
|
|
624
|
+
nr_stats_on = v && v[0] && !(v[0] == '0' && !v[1]);
|
|
625
|
+
if (!nr_stats_on) return;
|
|
626
|
+
struct sigaction sa;
|
|
627
|
+
memset(&sa, 0, sizeof sa);
|
|
628
|
+
sa.sa_handler = nr_stats_signal;
|
|
629
|
+
sa.sa_flags = SA_RESTART;
|
|
630
|
+
sigemptyset(&sa.sa_mask);
|
|
631
|
+
if (sigaction(SIGUSR1, &sa, NULL) != 0) nr_stats_on = 0;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
static void nr_stats_print(const struct nr_set *S, const char *why) {
|
|
635
|
+
struct nish_uring_stats st;
|
|
636
|
+
nish_uring_stats(S->u, &st);
|
|
637
|
+
dprintf(2,
|
|
638
|
+
"uring-stats %s port=%d sends=%lld pool_new=%lld pool_held=%lld pool_cached=%lld live=%lld slots=%lld\n",
|
|
639
|
+
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);
|
|
641
|
+
}
|
|
642
|
+
|
|
593
643
|
static void nr_pump_all(void) {
|
|
594
644
|
for (int32_t i = 0; i < nr_nsets; i++)
|
|
595
645
|
if (!nr_sets[i]->dead) nr_pump(nr_sets[i]);
|
|
646
|
+
if (nr_stats_asked) {
|
|
647
|
+
nr_stats_asked = 0;
|
|
648
|
+
for (int32_t i = 0; i < nr_nsets; i++)
|
|
649
|
+
if (!nr_sets[i]->dead) nr_stats_print(nr_sets[i], "signal");
|
|
650
|
+
}
|
|
596
651
|
}
|
|
597
652
|
|
|
598
653
|
static struct nr_set *nr_listener(int32_t fd) {
|
|
@@ -650,6 +705,7 @@ static int32_t nr_open(int32_t fd, int32_t port) {
|
|
|
650
705
|
/* Only an open set decides for rings: a probe that passed before a set
|
|
651
706
|
that ran short leaves the state at 0 (NURING-26). */
|
|
652
707
|
nr_state = 1;
|
|
708
|
+
nr_stats_setup();
|
|
653
709
|
S->listener = fd;
|
|
654
710
|
S->port = port;
|
|
655
711
|
S->slots = o.slots;
|
|
@@ -663,6 +719,7 @@ static int32_t nr_open(int32_t fd, int32_t port) {
|
|
|
663
719
|
/* Closes a ring listener: the set and every slot in it, then the descriptor.
|
|
664
720
|
The record stays, dead, so its handles answer -9. */
|
|
665
721
|
static int32_t nr_close_listener(struct nr_set *S) {
|
|
722
|
+
if (nr_stats_on > 0) nr_stats_print(S, "close");
|
|
666
723
|
nr_unregister(&S->v);
|
|
667
724
|
for (int ri = 0; ri < NR_RINGS; ri++) {
|
|
668
725
|
if (!S->conns[ri]) continue;
|
|
@@ -878,17 +935,23 @@ static int32_t nr_ctl(int32_t loop, int op, struct nr_vent *v, struct nr_set *S,
|
|
|
878
935
|
/* Writes the loop's ready virtual sources into `out`, at most `max`, in the
|
|
879
936
|
order they became ready. One reported and still ready goes to the tail,
|
|
880
937
|
so a busy source does not starve the rest. */
|
|
881
|
-
static
|
|
938
|
+
static uint32_t nr_epoch;
|
|
939
|
+
|
|
940
|
+
static int nr_collect_from(int32_t loop, int32_t *out, int max, int skip_seen) {
|
|
882
941
|
int n = 0;
|
|
883
942
|
struct nr_vent *again = NULL, *again_tail = NULL;
|
|
884
943
|
for (struct nr_vent *v = nr_head, *next; v && n < max; v = next) {
|
|
885
944
|
next = v->next;
|
|
886
945
|
if (v->loop != loop) continue;
|
|
946
|
+
/* A batch's second wait passes over what its first already reported,
|
|
947
|
+
which stays listed for the next call. */
|
|
948
|
+
if (skip_seen && v->seen == nr_epoch) continue;
|
|
887
949
|
int32_t bits = nr_bits(v) & (v->events | 4);
|
|
888
950
|
nr_unlist(v);
|
|
889
951
|
if (bits == 0) continue;
|
|
890
952
|
out[2 * n] = v->token;
|
|
891
953
|
out[2 * n + 1] = bits;
|
|
954
|
+
v->seen = nr_epoch;
|
|
892
955
|
n++;
|
|
893
956
|
v->next = NULL;
|
|
894
957
|
if (again_tail) again_tail->next = v; else again = v;
|
|
@@ -901,6 +964,8 @@ static int nr_collect(int32_t loop, int32_t *out, int max) {
|
|
|
901
964
|
return n;
|
|
902
965
|
}
|
|
903
966
|
|
|
967
|
+
static int nr_collect(int32_t loop, int32_t *out, int max) { return nr_collect_from(loop, out, max, 0); }
|
|
968
|
+
|
|
904
969
|
static int64_t nr_now_ms(void) {
|
|
905
970
|
struct timespec t;
|
|
906
971
|
clock_gettime(CLOCK_MONOTONIC, &t);
|
|
@@ -910,6 +975,72 @@ static int64_t nr_now_ms(void) {
|
|
|
910
975
|
/* Whose turn a full wait with room for one gives: 1 the epoll set's. */
|
|
911
976
|
static int nr_turn;
|
|
912
977
|
|
|
978
|
+
/* The ordinary descriptors each loop watches, as `pollAdd` and `pollModify`
|
|
979
|
+
left them, so a wait can tell a loop that holds nothing but ring slots and
|
|
980
|
+
`signalFd()` from one that must sleep in epoll_wait. A close without
|
|
981
|
+
`pollRemove` may leave an entry behind, which only keeps that loop on
|
|
982
|
+
epoll; past NR_FDS entries no loop waits on its ring directly. */
|
|
983
|
+
#define NR_FDS 64
|
|
984
|
+
static struct {
|
|
985
|
+
int32_t loop, fd, events;
|
|
986
|
+
} nr_fds[NR_FDS];
|
|
987
|
+
static int32_t nr_nfds, nr_fds_spilled;
|
|
988
|
+
|
|
989
|
+
static void nr_fd_note(int32_t loop, int op, int32_t fd, int32_t events) {
|
|
990
|
+
int32_t at = -1;
|
|
991
|
+
for (int32_t i = 0; i < nr_nfds; i++)
|
|
992
|
+
if (nr_fds[i].loop == loop && nr_fds[i].fd == fd) at = i;
|
|
993
|
+
if (op == EPOLL_CTL_DEL) {
|
|
994
|
+
if (at >= 0) nr_fds[at] = nr_fds[--nr_nfds];
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
if (at < 0) {
|
|
998
|
+
if (nr_nfds == NR_FDS) {
|
|
999
|
+
nr_fds_spilled = 1;
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
at = nr_nfds++;
|
|
1003
|
+
nr_fds[at].loop = loop;
|
|
1004
|
+
nr_fds[at].fd = fd;
|
|
1005
|
+
}
|
|
1006
|
+
nr_fds[at].events = events;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
/* `fd` is closed or new: it watches nothing and is in no loop. */
|
|
1010
|
+
static void nr_fd_forget(int32_t fd) {
|
|
1011
|
+
for (int32_t i = 0; i < nr_nfds;)
|
|
1012
|
+
if (nr_fds[i].loop == fd || nr_fds[i].fd == fd) nr_fds[i] = nr_fds[--nr_nfds];
|
|
1013
|
+
else i++;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
/* The set a wait on `loop` may sleep on directly, in io_uring_enter rather
|
|
1017
|
+
than epoll_wait, and in `*watch` the one descriptor its ring must watch
|
|
1018
|
+
for it, or -1: there is one live set, no other loop holds a virtual
|
|
1019
|
+
source, and the loop's only ordinary descriptor, if any, is `signalFd()`
|
|
1020
|
+
watched for reading. NULL otherwise, and the wait uses epoll. Once
|
|
1021
|
+
`netClose` or `fileClose` has closed the signal descriptor its number may
|
|
1022
|
+
name a new socket, which the ring's poll, still armed on the old pipe,
|
|
1023
|
+
would never report; `nish_signal_fd_made` answers -1 from then on, so no
|
|
1024
|
+
loop with an ordinary descriptor waits directly again. */
|
|
1025
|
+
|
|
1026
|
+
static struct nr_set *nr_direct(int32_t loop, int32_t *watch) {
|
|
1027
|
+
if (nr_fds_spilled || nr_loops_spilled || nr_nloops != 1 || nr_loops[0] != loop) return NULL;
|
|
1028
|
+
struct nr_set *D = NULL;
|
|
1029
|
+
for (int32_t i = 0; i < nr_nsets; i++) {
|
|
1030
|
+
if (nr_sets[i]->dead) continue;
|
|
1031
|
+
if (D) return NULL;
|
|
1032
|
+
D = nr_sets[i];
|
|
1033
|
+
}
|
|
1034
|
+
*watch = -1;
|
|
1035
|
+
for (int32_t i = 0; i < nr_nfds; i++) {
|
|
1036
|
+
if (nr_fds[i].loop != loop) continue;
|
|
1037
|
+
if (*watch >= 0 || nr_fds[i].fd != nish_signal_fd_made() || nr_fds[i].events != 1)
|
|
1038
|
+
return NULL;
|
|
1039
|
+
*watch = nr_fds[i].fd;
|
|
1040
|
+
}
|
|
1041
|
+
return D;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
913
1044
|
/* `pollWait` for a loop that may hold virtual sources: pump every set, take
|
|
914
1045
|
what is ready, and sleep on the loop's epoll set, which holds each set's
|
|
915
1046
|
descriptor, no longer than the sets allow (NURING-8). A wake by the bound,
|
|
@@ -933,8 +1064,24 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
|
|
|
933
1064
|
int room = max - n;
|
|
934
1065
|
if (room == 0) room = max > 1 ? max / 2 : (nr_turn ^= 1);
|
|
935
1066
|
struct epoll_event e[NR_EVENTS];
|
|
936
|
-
int
|
|
937
|
-
|
|
1067
|
+
int woke = 0, k = -2, watch;
|
|
1068
|
+
/* A loop of ring slots and at most `signalFd()` sleeps on its ring, and
|
|
1069
|
+
reads its epoll set only once the ring's poll says the signal
|
|
1070
|
+
descriptor is ready; that read takes the place of NURING-18's share. */
|
|
1071
|
+
struct nr_set *D = room > 0 ? nr_direct(loop, &watch) : NULL;
|
|
1072
|
+
if (D) {
|
|
1073
|
+
int rc = nish_uring_wait_direct(D->u, watch, 1, t);
|
|
1074
|
+
if (rc != -EOPNOTSUPP) {
|
|
1075
|
+
if (rc < 0) {
|
|
1076
|
+
errno = -rc;
|
|
1077
|
+
return nish_net_fail();
|
|
1078
|
+
}
|
|
1079
|
+
nr_pump(D);
|
|
1080
|
+
woke = 1;
|
|
1081
|
+
k = watch >= 0 && nish_uring_poll_events(D->u, 1) ? epoll_wait(loop, e, room, 0) : 0;
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
if (k == -2) k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
|
|
938
1085
|
/* io_uring's task work interrupts the wait with no signal behind it, so
|
|
939
1086
|
EINTR is a wake to pump for, not an answer of 0 before the timeout. */
|
|
940
1087
|
if (k < 0 && errno != EINTR) return nish_net_fail();
|
|
@@ -967,6 +1114,51 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
|
|
|
967
1114
|
}
|
|
968
1115
|
#endif
|
|
969
1116
|
|
|
1117
|
+
#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. */
|
|
1124
|
+
static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int32_t max_wait_ms) {
|
|
1125
|
+
struct nr_set *S = NULL;
|
|
1126
|
+
for (int32_t i = 0; i < nr_nsets; i++) {
|
|
1127
|
+
if (nr_sets[i]->dead) continue;
|
|
1128
|
+
if (S) return n; /* a batch waits on one ring set, never on several */
|
|
1129
|
+
S = nr_sets[i];
|
|
1130
|
+
}
|
|
1131
|
+
if (!S) return n;
|
|
1132
|
+
/* A loop no virtual source has joined has nothing the ring can add, so it
|
|
1133
|
+
answers as `pollWait` did rather than sleep out `max_wait_ms`. */
|
|
1134
|
+
int held = nr_loops_spilled;
|
|
1135
|
+
for (int32_t i = 0; i < nr_nloops; i++)
|
|
1136
|
+
if (nr_loops[i] == loop) held = 1;
|
|
1137
|
+
if (!held) return n;
|
|
1138
|
+
int32_t watch = -1;
|
|
1139
|
+
int direct = nr_direct(loop, &watch) == S;
|
|
1140
|
+
int64_t end = nr_now_ms() + max_wait_ms;
|
|
1141
|
+
while (n < min && n < max) {
|
|
1142
|
+
int64_t left = end - nr_now_ms();
|
|
1143
|
+
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
|
+
}
|
|
1153
|
+
nr_pump(S);
|
|
1154
|
+
n += nr_collect_from(loop, out + 2 * n, max - n, 1);
|
|
1155
|
+
if (direct && watch >= 0 && nish_uring_poll_events(S->u, 0)) break;
|
|
1156
|
+
}
|
|
1157
|
+
return n;
|
|
1158
|
+
}
|
|
1159
|
+
#endif
|
|
1160
|
+
|
|
1161
|
+
|
|
970
1162
|
/* `netRingState()`: whether `tcpListenFlags` flag 8 gets rings. 0 before a
|
|
971
1163
|
listener has asked, 1 when it got them, or the negative errno that refused
|
|
972
1164
|
them, after which every listener in the process is a descriptor one: -1
|
|
@@ -1339,6 +1531,8 @@ int32_t nish_net_close(int32_t fd) {
|
|
|
1339
1531
|
struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
|
|
1340
1532
|
if (R) return nr_close_listener(R);
|
|
1341
1533
|
if (nr_nsets > 0) nr_forget_loop(fd);
|
|
1534
|
+
nr_fd_forget(fd);
|
|
1535
|
+
nish_signal_fd_closing(fd);
|
|
1342
1536
|
#endif
|
|
1343
1537
|
return close(fd) != 0 ? nish_net_fail() : 0;
|
|
1344
1538
|
}
|
|
@@ -1574,6 +1768,7 @@ int32_t nish_poll_create(void) {
|
|
|
1574
1768
|
nish_net_raise_nofile();
|
|
1575
1769
|
#if defined(__linux__)
|
|
1576
1770
|
int fd = epoll_create1(EPOLL_CLOEXEC);
|
|
1771
|
+
if (fd >= 0) nr_fd_forget(fd);
|
|
1577
1772
|
#else
|
|
1578
1773
|
int fd = kqueue();
|
|
1579
1774
|
if (fd >= 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
@@ -1619,7 +1814,9 @@ static int32_t nish_poll_ctl(int32_t loop, int op, int32_t fd, int32_t events, i
|
|
|
1619
1814
|
e.events = ((events & 1) ? EPOLLIN : 0) | ((events & 2) ? EPOLLOUT : 0);
|
|
1620
1815
|
/* The high word is 0 for a descriptor, and 1 for a ring set's own. */
|
|
1621
1816
|
e.data.u64 = (uint32_t)token;
|
|
1622
|
-
|
|
1817
|
+
if (epoll_ctl(loop, op, fd, &e) != 0) return nish_net_fail();
|
|
1818
|
+
nr_fd_note(loop, op, fd, events);
|
|
1819
|
+
return 0;
|
|
1623
1820
|
#else
|
|
1624
1821
|
int r, w;
|
|
1625
1822
|
if (op == NISH_POLL_ADD) {
|
|
@@ -1709,6 +1906,28 @@ int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
|
|
|
1709
1906
|
#endif
|
|
1710
1907
|
}
|
|
1711
1908
|
|
|
1909
|
+
/* `pollWaitBatch(loop, ready, timeoutMs, minEvents, maxWaitMs)`: wait as
|
|
1910
|
+
`pollWait` does for the first event, then, on a loop with one ring set,
|
|
1911
|
+
up to `maxWaitMs` more until `minEvents` are ready (nr_batch). */
|
|
1912
|
+
int32_t nish_poll_wait_batch(int32_t loop, nish_array *ready, int32_t timeout_ms, int32_t min_events,
|
|
1913
|
+
int32_t max_wait_ms) {
|
|
1914
|
+
#if defined(__linux__)
|
|
1915
|
+
uint64_t room = ready->len / 2;
|
|
1916
|
+
if (nr_nsets > 0 && min_events > 1 && max_wait_ms > 0 && room > 0) {
|
|
1917
|
+
int max = room < NISH_POLL_CHUNK ? (int)room : NISH_POLL_CHUNK;
|
|
1918
|
+
int32_t *out = (int32_t *)ready->data;
|
|
1919
|
+
nr_epoch++;
|
|
1920
|
+
int n = nr_poll_wait(loop, out, max, timeout_ms);
|
|
1921
|
+
if (n <= 0 || n >= min_events || n >= max) return n;
|
|
1922
|
+
return nr_batch(loop, out, n, max, min_events, max_wait_ms);
|
|
1923
|
+
}
|
|
1924
|
+
#else
|
|
1925
|
+
(void)min_events;
|
|
1926
|
+
(void)max_wait_ms;
|
|
1927
|
+
#endif
|
|
1928
|
+
return nish_poll_wait(loop, ready, timeout_ms);
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1712
1931
|
/* The Unix-domain address `path` names into `s`, its length into `n`, and 1
|
|
1713
1932
|
or 0 for whether it is abstract; or the failure, before any socket exists.
|
|
1714
1933
|
An empty path or one with a NUL inside is -22, and so is a bare `@`. A path
|
package/runtime/runtime-os.c
CHANGED
|
@@ -324,9 +324,23 @@ int64_t nish_file_size(int32_t fd) {
|
|
|
324
324
|
return fstat(fd, &st) == 0 ? (int64_t)st.st_size : nish_fs_err(errno);
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
+
#if !defined(__wasi__) && !defined(__wasm__)
|
|
328
|
+
/* Weak, so that runtime.c, runtime-text.c and runtime-os.c still link as a set
|
|
329
|
+
of their own: runtime-host.c defines it, and is linked wherever `signalFd()`
|
|
330
|
+
can have made the pipe this hook watches for. */
|
|
331
|
+
extern void nish_signal_fd_closing(int32_t fd) __attribute__((weak));
|
|
332
|
+
#endif
|
|
333
|
+
|
|
327
334
|
/* `fileClose(fd)`: never retried on `EINTR`, because Linux has released the
|
|
328
335
|
number by then and a retry could close a descriptor opened since. */
|
|
329
|
-
int32_t nish_file_close(int32_t fd) {
|
|
336
|
+
int32_t nish_file_close(int32_t fd) {
|
|
337
|
+
if (fd < 0) return -9;
|
|
338
|
+
#if !defined(__wasi__) && !defined(__wasm__)
|
|
339
|
+
/* The signal pipe's number is about to be free for reuse (NURING-49). */
|
|
340
|
+
if (nish_signal_fd_closing) nish_signal_fd_closing(fd);
|
|
341
|
+
#endif
|
|
342
|
+
return close(fd) == 0 ? 0 : nish_fs_err(errno);
|
|
343
|
+
}
|
|
330
344
|
|
|
331
345
|
/* ---- Files and directories with an explicit mode (docs/security/runtime.md,
|
|
332
346
|
RT-24 to RT-26): `writeFileSyncMode`, `createFileExclusiveSync` and
|