Path: blob/main/core/posix-wasm/src/posix-wasm.h
1396 views
#ifndef WASMPOSIX1#define WASMPOSIX23#include <sys/types.h>45#define GRND_NONBLOCK 0x00016#define GRND_RANDOM 0x00027#define GRND_INSECURE 0x000489ssize_t getrandom(void* buf, size_t buflen, unsigned int flags);10int dup(int oldfd);1112typedef unsigned char sigset_t;13int sigemptyset(sigset_t* set);14int sigaddset(sigset_t* set, int signo);15#include <unistd.h>16char* ttyname(int fd);17int ttyname_r(int fd, char* buf, size_t buflen);1819#include <sys/stat.h>2021int chmod(const char* path, mode_t mode);22int fchmod(int fd, mode_t mode);23int lchmod(const char* path, mode_t mode);24int fchmodat(int fd, const char* path, mode_t mode, int flag);25int chflags(const char* path, unsigned long flags);26int lchflags(const char* path, unsigned long flags);27void sync(void);28int chown(const char* path, uid_t owner, gid_t group);29int fchown(int fd, uid_t owner, gid_t group);30int lchown(const char* path, uid_t owner, gid_t group);31int fchownat(int fd, const char* path, uid_t owner, gid_t group, int flag);32int nice(int inc);3334struct ttyent {35char* ty_name; /* terminal device name */36char* ty_getty; /* command to execute, usually getty */37char* ty_type; /* terminal type for termcap */38#define TTY_ON 0x01 /* enable logins (start ty_getty program) */39#define TTY_SECURE 0x02 /* allow uid of 0 to login */40int ty_status; /* status flags */41char* ty_window; /* command to start up window manager */42char* ty_comment; /* comment field */43};4445struct ttyent* getttyent(void);46struct ttyent* getttynam(const char* name);4748//#include <sys/resource.h>49int getpriority(int which, id_t who);50int setpriority(int which, id_t who, int prio);5152mode_t umask(mode_t mask);5354#include <sys/time.h>55int futimes(int fd, const struct timeval tv[2]);56int lutimes(const char* filename, const struct timeval tv[2]);57int utimes(const char* path, const struct timeval times[2]);5859int execv(const char* path, char* const argv[]);60int fexecve(int fd, char* const argv[], char* const envp[]);61int execve(const char* pathname, char* const argv[], char* const envp[]);62int execlp(const char* file, const char* arg, ... /* (char *) NULL */);63int execvp(const char* file, char* const argv[]);6465struct sched_param {66int sched_priority;67};6869struct sigaction {70void (*sa_handler)(int);71void (*sa_sigaction)(int, void*, void*);72sigset_t sa_mask;73int sa_flags;74void (*sa_restorer)(void);75};7677int sigaction(int signum, const struct sigaction* act,78struct sigaction* oldact);7980pid_t fork(void);8182pid_t fork1(void);83pid_t vfork(void);8485ssize_t splice(int fd_in, void* off_in, int fd_out, void* off_out, size_t len,86unsigned int flags);87int login_tty(int fd);88int eventfd(unsigned int initval, int flags);8990int sched_get_priority_max(int policy);91int sched_get_priority_min(int policy);92int sched_setscheduler(pid_t pid, int policy, const struct sched_param* param);93int sched_getscheduler(pid_t pid);94int sched_setparam(pid_t pid, const struct sched_param* param);95int sched_getparam(pid_t pid, struct sched_param* param);96int sched_rr_get_interval(pid_t pid, struct timespec* tp);9798gid_t getgid(void);99gid_t getegid(void);100uid_t getuid(void);101uid_t geteuid(void);102int getgroups(int size, gid_t list[]);103// ncurses wants this:104pid_t getpgrp(void); /* POSIX.1 version */105// but cpython may get confused and want this (it's configurable):106// pid_t getpgrp(pid_t pid); /* BSD version */107pid_t getpgid(pid_t pid);108109int setpgrp(pid_t pid, pid_t pgid);110int kill(pid_t pid, int sig);111int killpg(int pgrp, int sig);112pid_t getppid(void);113114int plock(int opr);115116int setuid(uid_t uid);117int seteuid(uid_t euid);118int setegid(gid_t egid);119int setreuid(uid_t ruid, uid_t euid);120int setregid(gid_t rgid, gid_t egid);121int setgid(gid_t gid);122123#define WNOHANG 1124#define WUNTRACED 2125#define WSTOPPED 2126#define WEXITED 4127#define WCONTINUED 8128#define WNOWAIT 0x1000000129#define __WNOTHREAD 0x20000000130#define __WALL 0x40000000131#define __WCLONE 0x80000000132133typedef struct {134pid_t si_pid;135uid_t si_uid;136int si_signo;137int si_status;138int si_code;139int si_errno;140void* si_addr;141} siginfo_t;142143// From packages/zig/dist/lib/libc/musl/include/sys/wait.h and needed for144// Python. The #ifndef is because slightly different versions of sys/wait.h145// get included from zig for cowasm-cc versus normal zig, I think, maybe. TODO!146#include <sys/wait.h>147#ifndef _SYS_WAIT_H148typedef enum { P_ALL = 0, P_PID = 1, P_PGID = 2, P_PIDFD = 3 } idtype_t;149#define WEXITSTATUS(s) (((s)&0xff00) >> 8)150#define WTERMSIG(s) ((s)&0x7f)151#define WSTOPSIG(s) WEXITSTATUS(s)152#define WIFEXITED(s) (!WTERMSIG(s))153#define WIFSTOPPED(s) ((short)((((s)&0xffff) * 0x10001) >> 8) > 0x7f00)154#define WIFSIGNALED(s) (((s)&0xffff) - 1U < 0xffu)155#endif156157int waitid(idtype_t idtype, id_t id, siginfo_t* infop, int options);158pid_t wait(int* status);159pid_t waitpid(pid_t pid, int* status, int options);160161pid_t getpid(void);162163pid_t getsid(pid_t pid);164pid_t setsid(void);165int setpgid(pid_t pid, pid_t pgid);166167pid_t tcgetpgrp(int fd);168int tcsetpgrp(int fd, pid_t pgrp);169170int fdwalk(int (*func)(void*, int), void* cd);171int dup2(int oldfd, int newfd);172int dup3(int oldfd, int newfd, int flags);173174int lockf(int fd, int cmd, off_t len);175ssize_t preadv(int fd, const struct iovec* iov, int iovcnt, off_t offset);176ssize_t preadv2(int fd, const struct iovec* iov, int iovcnt, off_t offset,177int flags);178179int pipe(int pipefd[2]);180int pipe2(int pipefd[2], int flags);181182ssize_t pwritev2(int fd, const struct iovec* iov, int iovcnt, off_t offset,183int flags);184185typedef long long off64_t;186ssize_t copy_file_range(int fd_in, off64_t* off_in, int fd_out,187off64_t* off_out, size_t len, unsigned int flags);188189int mkstemp(char* temp);190int mkstemps(char* templ, int suffixlen);191char* mkdtemp(char* templ);192193int mkfifoat(int dirfd, const char* pathname, mode_t mode);194int mkfifo(const char* pathname, mode_t mode);195int mknodat(int dirfd, const char* pathname, mode_t mode, dev_t dev);196int mknod(const char* pathname, mode_t mode, dev_t dev);197int getloadavg(double loadavg[], int nelem);198int setresuid(uid_t ruid, uid_t euid, uid_t suid);199int setresgid(gid_t rgid, gid_t egid, gid_t sgid);200int getresuid(uid_t* ruid, uid_t* euid, uid_t* suid);201int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid);202203char* strcasestr(const char*, const char*);204205struct itimerval {206struct timeval it_interval; /* Interval for periodic timer */207struct timeval it_value; /* Time until next expiration */208};209210unsigned int alarm(unsigned int seconds);211int pause(void);212213int siginterrupt(int sig, int flag);214int getitimer(int which, struct itimerval* curr_value);215int setitimer(int which, const struct itimerval* new_value,216struct itimerval* old_value);217int sigismember(const sigset_t* set, int signo);218219int sigpending(sigset_t* set);220int sigwait(const sigset_t* set, int* sig);221int sigfillset(sigset_t* set);222int sigprocmask(int, const sigset_t* __restrict, sigset_t* __restrict);223int sigsuspend(const sigset_t*);224int sigwaitinfo(const sigset_t* set, siginfo_t* info);225int sigtimedwait(const sigset_t* set, siginfo_t* info,226const struct timespec* timeout);227228int clock_settime(clockid_t clk_id, const struct timespec* tp);229230typedef struct {231void* ss_sp; /* Base address of stack */232int ss_flags; /* Flags */233size_t ss_size; /* Number of bytes in stack */234} stack_t;235int sigaltstack(const stack_t* ss, stack_t* old_ss);236#define SA_NODEFER 0237#define SA_ONSTACK 0238#define SA_RESTART 0239#define SIGSTKSZ 0240#define SIG_BLOCK 0241#define SIG_UNBLOCK 1242#define SIG_SETMASK 2243244#define SI_ASYNCNL (-60)245#define SI_TKILL (-6)246#define SI_SIGIO (-5)247#define SI_ASYNCIO (-4)248#define SI_MESGQ (-3)249#define SI_TIMER (-2)250#define SI_QUEUE (-1)251#define SI_USER 0252#define SI_KERNEL 128253254#define RLIMIT_CORE 0255#define RLIM_INFINITY -1256#define RLIM_NLIMITS 16257258typedef int rlim_t;259struct rlimit {260rlim_t rlim_cur; /* Soft limit */261rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */262};263int getrlimit(int resource, struct rlimit* rlim);264int setrlimit(int resource, const struct rlimit* rlim);265266char* realpath(const char* path, char* resolved_path);267268int close_range(unsigned int first, unsigned int last, unsigned int flags);269270// These are not needed by Python but are needed by PARI (which isn't271// even part of CoWasm!?):272// This #define is to ensure that only one setjmp defs get defined.273#ifndef _SETJMP_H274#define _SETJMP_H275typedef void* __jmp_buf;276typedef struct __jmp_buf_tag {277__jmp_buf __jb;278unsigned long __fl;279unsigned long __ss[128 / sizeof(long)];280} jmp_buf[1];281typedef jmp_buf sigjmp_buf;282int setjmp(jmp_buf env);283int sigsetjmp(sigjmp_buf env, int savesigs);284void longjmp(jmp_buf env, int val);285void siglongjmp(sigjmp_buf env, int val);286#endif // _SETJMP_H287288#ifndef FILE289struct _IO_FILE {290char __x;291};292typedef struct _IO_FILE FILE;293#endif294295FILE* popen(const char* command, const char* type);296int pclose(FILE* stream);297298struct passwd* getpwnam(const char* name);299struct passwd* getpwuid(uid_t uid);300// void (*signal(int sig, void (*func)(int)))(int);301302// These #defines *must* be consistent with what is in kernel! See that via303// "DEBUG=posix:constants python-wasm":304305#define SO_ERROR 4306#define SO_REUSEADDR 2307#define SO_KEEPALIVE 9308#define SOMAXCONN 128309#define SOCK_SEQPACKET 5310#define __WASI_RIFLAGS_RECV_DATA_TRUNCATED 0311312typedef unsigned int socklen_t;313int setsockopt(int sockfd, int level, int optname, const void* optval,314socklen_t optlen);315316int bind(int sockfd, const void* addr, socklen_t addrlen);317int connect(int sockfd, const void* addr, socklen_t addrlen);318int getsockname(int sockfd, void* addr, socklen_t* addrlen);319int getpeername(int sockfd, void* addr, socklen_t* addrlen);320321int listen(int sockfd, int backlog);322ssize_t recvfrom(int sockfd, void* buf, size_t len, int flags, void* src_addr,323socklen_t* addrlen);324ssize_t sendto(int sockfd, const void* buf, size_t len, int flags,325const void* dest_addr, socklen_t addrlen);326int socket(int domain, int type, int protocol);327int gethostname(char* name, size_t len);328int sethostname(const char* name, size_t len);329int accept(int socket, void *addr, socklen_t *address_len);330331// These are needed to build parts of posixmodule in Python. They seem harmless332// since they are self contained and copied from333// packages/zig/dist/lib/libc/musl/include/stdlib.h We may need to change them334// if we invent some notion of fork and subprocesses for our runtime!335336#define WSTOPSIG(s) WEXITSTATUS(s)337338// needed by sqlite; copied from packages/zig/dist/lib/libc/musl/include/fcntl.h339// and packages/zig/dist/lib/libc/musl/arch/aarch64/bits/fcntl.h340// Main point is if/when I implement these at the WASI level, have to use them341// and be consistent.342343#define F_RDLCK 0344#define F_WRLCK 1345#define F_UNLCK 2346347#define F_DUPFD 0348#ifndef F_GETFD349#define F_GETFD 1350#define F_SETFD 2351#define F_GETFL 3352#define F_SETFL 4353#endif354#define F_GETLK 5355#define F_SETLK 6356#define F_SETLKW 7357#define F_SETOWN 8358#define F_GETOWN 9359#define F_SETSIG 10360#define F_GETSIG 11361362void flockfile(FILE* filehandle);363int ftrylockfile(FILE* filehandle);364void funlockfile(FILE* filehandle);365366char* strsignal(int sig);367368int fiprintf(FILE* stream, const char* format, ...);369int siprintf(char* s, const char* format, ...);370#define __small_sprintf sprintf371372int strunvis(char* dst, const char* src);373int strnvis(char* dst, size_t dlen, const char* src, int flag);374int strvis(char* dst, const char* src, int flag);375376#include <termios.h>377speed_t cfgetispeed(const struct termios* termios_p);378speed_t cfgetospeed(const struct termios* termios_p);379int tcgetattr(int fd, struct termios* tio);380int tcsetattr(int fd, int act, const struct termios* tio);381382int fchdir(int fd);383384#define SA_SIGINFO 4385386// WASI can't set time, but things will try so these should be387// implemented as functions that display an error. They are part of POSIX.388// They could work on a server though, if you're running as root! I did389// implement related things in posix-node, I think.390int settimeofday(const struct timeval*, const struct timezone*);391int adjtime(const struct timeval*, struct timeval*);392393// implemented in zig in wasm/posix/other.zig394int getpagesize(void);395396const char* getprogname(void);397398void setprogname(const char* progname);399400// The following constants are all copied from grepping the musl/wasi headers.401#define TIOCGWINSZ 0x5413402#define S_ISTXT S_ISVTX403#define LINE_MAX 4096404405long long strtonum(const char*, long long, long long, const char**);406407mode_t getmode(const void* bbox, mode_t omode);408void* setmode(const char* p);409410int mergesort(void*, size_t, size_t, int (*)(const void*, const void*));411int heapsort(void*, size_t, size_t, int (*)(const void*, const void*));412413int cowasm_fstat(int fildes, struct stat* buf);414int cowasm_lstat(const char* path, struct stat* buf);415int cowasm_stat(const char* path, struct stat* buf);416int cowasm_fstatat(int fd, const char* path, struct stat* buf, int flag);417418#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)419quad_t strtoq(const char* str, char** endptr, int base);420421#define UQUAD_MAX ULLONG_MAX422#define QUAD_MAX LLONG_MAX423#define QUAD_MIN LLONG_MIN424425#define REG_BASIC 0000426#define MAXLOGNAME 255 /* max login name length */427428int rpmatch(const char* response);429#endif // BSD_SOURCE430431// Some py-numpy cython code fails without this432// include to ensure that max_align_t is defined in433// the right place:434#include "__stddef_max_align_t.h"435436// This is needed by openssl.437#include "netdb.h"438char* inet_ntoa(struct in_addr in);439440void openlog(const char*, int, int);441442#endif443444445