mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 10:09:47 +08:00
binfmt_misc hands a wasm module to kwwasi (kernel/guest/kwwasi.c), which connects to the host over AF_VSOCK port 1021 and serves the program's file I/O with its own descriptors, cwd and credentials. The host (js/core/wasi-exec.ts, serveWasiPrograms(), the CLI's --wasi) compiles the module once and runs each program in a worker of its own (js/core/wasi-program.ts) with WASI preview 1, or preview 0 adapted. Requests cross through a SharedArrayBuffer where one can be used, else with JSPI and messages. kwwasi sends a module only when the host has not compiled it for that file, resolves paths against the cwd or / by what exists, and maps open flags from their asm-generic values to the guest's. alpine.sh builds kwwasi and kw-binfmt.sh registers it; no image with them has been built yet, so test/wasi-binfmt.ts brings kwwasi in through a mount.
35 lines
998 B
C
35 lines
998 B
C
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// A wasm32-wasi test program for kwwasi (test/wasi-binfmt.ts): creates
|
|
// files and directories, for the cwd checks.
|
|
// put.wasm PATH TEXT PATH (created or truncated) gets TEXT and a newline
|
|
// put.wasm -d PATH mkdir PATH
|
|
// Exit 1 with the error on stderr when it fails.
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (argc != 3) {
|
|
fprintf(stderr, "usage: put.wasm PATH TEXT | -d PATH\n");
|
|
return 2;
|
|
}
|
|
if (!strcmp(argv[1], "-d")) {
|
|
if (mkdir(argv[2], 0777)) {
|
|
fprintf(stderr, "put.wasm: mkdir %s: %s\n", argv[2], strerror(errno));
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
if (fd < 0 || dprintf(fd, "%s\n", argv[2]) < 0 || close(fd)) {
|
|
fprintf(stderr, "put.wasm: %s: %s\n", argv[1], strerror(errno));
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|