mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-04 06:10:03 +08:00
Sockets: sock_accept, sock_recv, sock_send and sock_shutdown on sockets the program inherited, with socket file types, poll and non-blocking; KWWASI_TCPLISTEN=ADDR:PORT[,...] opens listening TCP sockets at descriptors 4, 5, ... (exit 126 when one is already open); a write to a socket whose peer went away is EPIPE, not SIGPIPE. Threads: modules that import wasi.thread-spawn and a shared env.memory run on the shared path, each wasm thread with a worker, a channel and a kwwasi connection of its own (a kwwasi pthread sharing the process's descriptors), so a thread blocked on stdin holds up no other; proc_exit or a trap in any thread ends the program; the message path refuses such a module with exit 126. Clocks: realtime and monotonic follow the guest's (kwwasi sends both readings at START, no request per call); the CPU clocks are the worker's CPU time on Node, the process clock covering every thread through worker.cpuUsage(); unknown clock ids are EINVAL and the resolution is the timer's measured step. Files: fd_allocate; fd_fdstat_set_flags refuses a sync flag change with ENOTSUP; more Linux errnos mapped; KWWASI_CWD=cwd resolves every path without a leading "/" from the cwd, with no guessing. A wasm64 module is refused with exit 126, and a sleep on the message path no longer ends a fraction of a millisecond early. The kwwasi protocol is version 3. test/wasi-binfmt.ts runs its guest commands as root and covers the above.
76 lines
2.0 KiB
C
76 lines
2.0 KiB
C
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// The guest-side client of test/wasi/sock.c (aarch64, test/wasi-binfmt.ts).
|
|
// It retries its connect or datagram for 3 s, while the server starts.
|
|
// sockclient PORT send TEXT connect, send TEXT in two pieces, shut down
|
|
// writing, print what comes back
|
|
// sockclient PORT go TEXT the same, but wait for "go\n" before sending
|
|
// sockclient PORT udp TEXT send TEXT as a datagram, print the answer
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static void nap(void)
|
|
{
|
|
nanosleep(&(struct timespec){ .tv_nsec = 100000000 }, NULL);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };
|
|
char buf[512];
|
|
ssize_t n;
|
|
int s = -1;
|
|
|
|
if (argc != 4)
|
|
return 2;
|
|
a.sin_port = htons(atoi(argv[1]));
|
|
if (!strcmp(argv[2], "udp")) {
|
|
struct pollfd p = { .events = POLLIN };
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
p.fd = s;
|
|
for (int i = 0; i < 30; i++) {
|
|
sendto(s, argv[3], strlen(argv[3]), 0, (struct sockaddr *)&a, sizeof(a));
|
|
if (poll(&p, 1, 100) == 1 && (n = recv(s, buf, sizeof(buf), 0)) > 0) {
|
|
printf("%.*s\n", (int)n, buf);
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
for (int i = 0; i < 30; i++) {
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (!connect(s, (struct sockaddr *)&a, sizeof(a)))
|
|
break;
|
|
close(s);
|
|
s = -1;
|
|
nap();
|
|
}
|
|
if (s < 0)
|
|
return 1;
|
|
if (!strcmp(argv[2], "go")) {
|
|
size_t got = 0;
|
|
|
|
while (got < 3 && (n = recv(s, buf + got, 3 - got, 0)) > 0)
|
|
got += n;
|
|
if (got != 3 || memcmp(buf, "go\n", 3))
|
|
return 1;
|
|
}
|
|
/* In two pieces, apart, so that WAITALL has something to wait for. */
|
|
n = strlen(argv[3]) / 2;
|
|
send(s, argv[3], n, 0);
|
|
nap();
|
|
send(s, argv[3] + n, strlen(argv[3]) - n, 0);
|
|
shutdown(s, SHUT_WR);
|
|
while ((n = read(s, buf, sizeof(buf))) > 0)
|
|
fwrite(buf, 1, n, stdout);
|
|
return 0;
|
|
}
|