Path: blob/main/system/lib/libc/emscripten_syscall_stubs.c
6162 views
/*1* Copyright 2021 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*6* Unimplemented/dummy syscall implementations. These fall into 3 categories.7*8* 1. Fake it, use dummy/placeholder values and return success.9* 2. Fake it, as above but warn at runtime if called.10* 3. Return ENOSYS and warn at runtime if called.11*/1213#include <errno.h>14#include <stdio.h>15#include <sys/types.h>16#include <unistd.h>17#include <syscall_arch.h>18#include <string.h>19#include <sys/resource.h>20#include <sys/stat.h>21#include <time.h>22#include <sys/utsname.h>23#include <emscripten/console.h>24#include <emscripten/version.h>25#include <emscripten/stack.h>2627static int g_pid = 42;28static int g_pgid = 42;29static int g_ppid = 1;30static int g_sid = 42;31static mode_t g_umask = S_IWGRP | S_IWOTH;3233#ifdef NDEBUG34#define REPORT(name)35#else36#define REPORT(name) \37emscripten_err("warning: unsupported syscall: __syscall_" #name);38#endif3940#define UNIMPLEMENTED(name, args) \41weak int __syscall_##name args { \42REPORT(name); \43return -ENOSYS; \44}4546#define STRINGIFY(s) #s47#define STR(s) STRINGIFY(s)4849weak int __syscall_uname(intptr_t buf) {50if (!buf) {51return -EFAULT;52}53const char* full_version = STR(__EMSCRIPTEN_MAJOR__) "." \54STR(__EMSCRIPTEN_MINOR__) "." \55STR(__EMSCRIPTEN_TINY__);5657struct utsname *utsname = (struct utsname *)buf;5859strcpy(utsname->sysname, "Emscripten");60strcpy(utsname->nodename, "emscripten");61strcpy(utsname->release, full_version);62strcpy(utsname->version, "#1");63#ifdef __wasm64__64strcpy(utsname->machine, "wasm64");65#else66strcpy(utsname->machine, "wasm32");67#endif68return 0;69}7071weak int __syscall_setpgid(int pid, int pgid) {72if (pid && pid != g_pid) {73return -ESRCH;74}75if (pgid && pgid != g_pgid) {76return -EPERM;77}78return 0;79}8081weak int __syscall_sync() {82return 0;83}8485weak int __syscall_getsid(int pid) {86if (pid && pid != g_pid) {87return -ESRCH;88}89return g_sid;90}9192weak int __syscall_getpgid(int pid) {93if (pid && pid != g_pid) {94return -ESRCH;95}96return g_pgid;97}9899weak int __syscall_getpid() {100return g_pid;101}102103weak int __syscall_getppid() {104return g_ppid;105}106107weak int __syscall_linkat(int olddirfd, intptr_t oldpath, int newdirfd, intptr_t newpath, int flags) {108return -EMLINK; // no hardlinks for us109}110111weak int __syscall_getgroups32(int size, intptr_t list) {112if (size < 1) {113return -EINVAL;114}115((gid_t*)list)[0] = 0;116return 1;117}118119weak int __syscall_setsid() {120return 0; // no-op121}122123weak int __syscall_umask(int mask) {124int old = g_umask;125g_umask = mask;126return old;127}128129struct kusage {130long utime_tv_sec;131long utime_tv_usec;132long stime_tv_sec;133long stime_tv_usec;134};135136weak int __syscall_getrusage(int who, intptr_t usage) {137REPORT(getrusage);138struct kusage *u = (struct kusage*)usage;139u->utime_tv_sec = 1;140u->utime_tv_usec = 2;141u->stime_tv_sec = 3;142u->stime_tv_usec = 4;143return 0;144}145146weak int __syscall_getpriority(int which, int who) {147return 0;148}149150weak int __syscall_setpriority(int which, int who, int prio) {151return -EPERM;152}153154weak int __syscall_setdomainname(intptr_t name, size_t size) {155return -EPERM;156}157158weak int __syscall_getuid32(void) {159return 0;160}161162weak int __syscall_getgid32(void) {163return 0;164}165166weak int __syscall_geteuid32(void) {167return 0;168}169170weak int __syscall_getegid32(void) {171return 0;172}173174weak int __syscall_getresuid32(intptr_t ruid, intptr_t euid, intptr_t suid) {175*((uid_t *)ruid) = 0;176*((uid_t *)euid) = 0;177*((uid_t *)suid) = 0;178return 0;179}180181weak int __syscall_getresgid32(intptr_t ruid, intptr_t euid, intptr_t suid) {182REPORT(getresgid32);183*((uid_t *)ruid) = 0;184*((uid_t *)euid) = 0;185*((uid_t *)suid) = 0;186return 0;187}188189weak int __syscall_pause() {190REPORT(pause);191return -EINTR; // we can't pause192}193194weak int __syscall_madvise(intptr_t addr, size_t length, int advice) {195REPORT(madvise);196// advice is welcome, but ignored197return 0;198}199200weak int __syscall_mlock(intptr_t addr, size_t len) {201REPORT(mlock);202return 0;203}204205weak int __syscall_munlock(intptr_t addr, size_t len) {206REPORT(munlock);207return 0;208}209210weak int __syscall_mprotect(size_t addr, size_t len, int prot) {211REPORT(mprotect);212return 0; // let's not and say we did213}214215weak int __syscall_mremap(intptr_t old_addr, size_t old_size, size_t new_size, int flags, intptr_t new_addr) {216REPORT(mremap);217return -ENOMEM; // never succeed218}219220weak int __syscall_mlockall(int flags) {221REPORT(mlockall);222return 0;223}224225weak int __syscall_munlockall() {226REPORT(munlockall);227return 0;228}229230weak int __syscall_prlimit64(int pid, int resource, intptr_t new_limit, intptr_t old_limit) {231REPORT(prlimit64);232struct rlimit *old = (struct rlimit *)old_limit;233if (new_limit) {234return -EPERM;235}236if (old) {237if (resource == RLIMIT_NOFILE) {238// See FS.MAX_OPEN_FDS in src/lib/libfs.js239old->rlim_cur = 4096;240old->rlim_max = 4096;241} else if (resource == RLIMIT_STACK) {242uintptr_t end = emscripten_stack_get_end();243uintptr_t base = emscripten_stack_get_base();244245old->rlim_cur = base - end;246// we can not change the stack size, so the maximum is the same as the current247old->rlim_max = base - end;248} else {249// Just report no limits250old->rlim_cur = RLIM_INFINITY;251old->rlim_max = RLIM_INFINITY;252}253}254return 0;255}256257weak int __syscall_setsockopt(int sockfd, int level, int optname, intptr_t optval, size_t optlen, int dummy) {258REPORT(setsockopt);259return -ENOPROTOOPT; // The option is unknown at the level indicated.260}261262UNIMPLEMENTED(acct, (intptr_t filename))263UNIMPLEMENTED(mincore, (intptr_t addr, size_t length, intptr_t vec))264UNIMPLEMENTED(pipe2, (intptr_t fds, int flags))265UNIMPLEMENTED(pselect6, (int nfds, intptr_t readfds, intptr_t writefds, intptr_t exceptfds, intptr_t timeout, intptr_t sigmaks))266UNIMPLEMENTED(ppoll, (intptr_t fds, int nfds, intptr_t timeout, intptr_t sigmask, int size))267UNIMPLEMENTED(recvmmsg, (int sockfd, intptr_t msgvec, size_t vlen, int flags, ...))268UNIMPLEMENTED(sendmmsg, (int sockfd, intptr_t msgvec, size_t vlen, int flags, ...))269UNIMPLEMENTED(shutdown, (int sockfd, int how, int dummy, int dummy2, int dummy3, int dummy4))270UNIMPLEMENTED(socketpair, (int domain, int type, int protocol, intptr_t fds, int dummy, int dummy2))271UNIMPLEMENTED(wait4,(int pid, intptr_t wstatus, int options, int rusage))272273274