Files
unkernel/test/wasi/ls.c
Pooya Parsa b5b75b53de Run WASI programs from the guest on the host's wasm engine (prototype)
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.
2026-11-19 07:08:32 +00:00

45 lines
1.1 KiB
C

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// A wasm32-wasi test program for kwwasi (test/wasi-binfmt.ts): lists a
// directory (default ".") sorted, with each entry's type and size from stat.
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
static int cmp(const void *a, const void *b)
{
return strcmp(*(char *const *)a, *(char *const *)b);
}
int main(int argc, char **argv)
{
const char *dir = argc > 1 ? argv[1] : ".";
char *names[1024];
int n = 0;
DIR *d = opendir(dir);
struct dirent *e;
if (!d) {
perror(dir);
return 1;
}
while ((e = readdir(d)) && n < 1024)
names[n++] = strdup(e->d_name);
closedir(d);
qsort(names, n, sizeof(*names), cmp);
for (int i = 0; i < n; i++) {
char path[1024];
struct stat st;
snprintf(path, sizeof(path), "%s/%s", dir, names[i]);
if (stat(path, &st))
printf("%s ?\n", names[i]);
else
printf("%s %s %lld\n", names[i], S_ISDIR(st.st_mode) ? "dir" : S_ISREG(st.st_mode) ? "file" : "other",
(long long)st.st_size);
}
return 0;
}