Path: blob/main/core/kernel/src/wasm/posix/other.ts
1068 views
import { notImplemented } from "./util";12export default function other(context) {3const { callFunction, posix, recv, send, wasi } = context;45context.state.user_from_uid_cache = {};67function sendStatvfs(bufPtr, x) {8callFunction(9"set_statvfs",10bufPtr,11x.f_bsize,12x.f_frsize,13BigInt(x.f_blocks),14BigInt(x.f_bfree),15BigInt(x.f_bavail),16BigInt(x.f_files),17BigInt(x.f_ffree),18BigInt(x.f_favail),19x.f_fsid,20x.f_flag,21x.f_namemax22);23}2425function real_fd(virtual_fd: number): number {26const data = wasi.FD_MAP.get(virtual_fd);27if (data == null) {28return -1;29}30return data.real;31}3233const lib = {34syslog: () => {35notImplemented("syslog");36},37login_tty: (fd: number): number => {38if (posix.login_tty == null) {39notImplemented("login_tty");40}41posix.login_tty(real_fd(fd));42return 0;43},4445// TODO: worry about virtual filesystem that WASI provides,46// versus this just being the straight real one?!47// int statvfs(const char *restrict path, struct statvfs *restrict buf);48statvfs: (pathPtr: string, bufPtr: number): number => {49if (posix.statvfs == null) {50notImplemented("statvfs");51}52const path = recv.string(pathPtr);53sendStatvfs(bufPtr, posix.statvfs(path));54return 0;55},5657// int fstatvfs(int fd, struct statvfs *buf);58fstatvfs: (fd: number, bufPtr: number): number => {59if (posix.fstatvfs == null) {60notImplemented("fstatvfs");61}62sendStatvfs(bufPtr, posix.fstatvfs(real_fd(fd)));63return 0;64},6566ctermid: (ptr?: number): number => {67if (posix.ctermid == null) {68notImplemented("ctermid");69}70if (ptr) {71const s = posix.ctermid();72send.string(s, { ptr, len: s.length + 1 });73return ptr;74}75if (context.state.ctermidPtr) {76return context.state.ctermidPtr;77}78const s = posix.ctermid();79return (context.state.ctermidPtr = send.string(s));80},8182// password stuff83// int getpwnam_r(const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);84getpwnam_r: (85_namePtr: number,86_passwdPtr: number,87_bufferPtr: number,88_bufsize: number,89result_ptr_ptr: number90): number => {91// this means "not found".92send.pointer(result_ptr_ptr, 0);93return 0;94},9596// struct passwd *getpwuid(uid_t uid);97getpwuid: () => {98// not found99return 0;100},101102// int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,103// size_t bufsize, struct passwd **result);104getpwuid_r: (105_uid: number,106_passwdPtr: number,107_bufferPtr: number,108_bufsize: number,109result_ptr_ptr: number110): number => {111send.pointer(result_ptr_ptr, 0);112return 0;113},114115openpty: () => {116// TOOD: plan to do this inspired by https://github.com/microsoft/node-pty, either117// using that or just a little inspired by it to add to posix-node.118notImplemented("openpty");119},120121msync: () => {122// This is part of mmap.123notImplemented("msync");124},125126madvise: () => {127notImplemented("madvise");128},129130mremap: () => {131notImplemented("mremap");132},133134// The curses cpython module wants this:135// FILE *tmpfile(void);136/* ~/test/tmpfile$ more a.c137#include<stdio.h>138int main() {139FILE* f = tmpfile();140printf("f = %p\n", f);141}142~/test/tmpfile$ zig cc -target wasm32-wasi ./a.c143./a.c:3:14: warning: 'tmpfile' is deprecated: tmpfile is not defined on WASI [-Wdeprecated-declarations]144*/145tmpfile: () => {146notImplemented("tmpfile");147},148149openlog: () => {150notImplemented("openlog");151},152153// curses also wants this:154// int tcflush(int fildes, int action);155tcflush: () => {156notImplemented("tcflush");157},158159// struct passwd *getpwnam(const char *login);160getpwnam: () => {161console.log("STUB: getpwnam");162// return 0 indicates failure163return 0;164},165166// int getrlimit(int resource, struct rlimit *rlp);167getrlimit: () => {168notImplemented("getrlimit");169},170171// int setrlimit(int resource, const struct rlimit *rlp);172setrlimit: () => {173notImplemented("setrlimit");174},175176// numpy wants this thing that can't exist in wasm:177// int backtrace(void** array, int size);178// Commenting this out and instead patching numpy to not try to use this, since we179// have to do that anyways to get it to build with clang15.180// backtrace: () => {181// notImplemented("backgrace");182// },183184// These are for coreutils, and we come up with a WebAssembly version,185// which is the documented fallback.186// char * user_from_uid(uid_t uid, int nouser);187// char * group_from_gid(gid_t gid, int nogroup);188// TODO: for speed this would be better at the C level.189user_from_uid: (uid: number, nouser: number = 0): number => {190if (nouser) {191return 0;192}193// cache the pointers for speed and to reduce memory leaks194if (context.state.user_from_uid_cache[uid]) {195return context.state.user_from_uid_cache[uid];196}197return (context.state.user_from_uid_cache[uid] = send.string(`${uid}`));198},199group_from_gid: (gid: number, nogroup: number = 0): number => {200return lib.user_from_uid(gid, nogroup);201},202203// TODO -- see how this is used in code, or maybe make it204// do something like "#define getrusage(A,B) memset(B,0,sizeof(*B))"205// to make everything 0, as a stub.206// int getrusage(int who, struct rusage *r_usage);207getrusage: (_who: number, _r_usage_ptr: number): number => {208notImplemented("getrusage");209return 0;210},211212// C++ stuff we don't support:213_Znwm: () => {214// operator new215notImplemented("_Znwm");216},217_ZdlPv: () => {218// operator delete219notImplemented("_ZdlPv");220},221__cxa_throw: () => {222notImplemented("__cxa_throw");223},224// exception225__cxa_allocate_exception: () => {226notImplemented("__cxa_allocate_exception");227},228_ZNSt20bad_array_new_lengthC1Ev: () => {229notImplemented("_ZNSt20bad_array_new_lengthC1Ev");230},231_ZNSt20bad_array_new_lengthD1Ev: () => {232notImplemented("_ZNSt20bad_array_new_lengthD1Ev");233},234_ZTISt20bad_array_new_length: () => {235notImplemented("_ZTISt20bad_array_new_length");236},237238ngettext: () => {239notImplemented("ngettext");240},241dngettext: () => {242notImplemented("dngettext");243},244dcngettext: () => {245notImplemented("dcngettext");246},247};248249return lib;250}251252253