2020-06-15 14:18:57 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_CALLS_INTERNAL_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_CALLS_INTERNAL_H_
|
|
|
|
#include "libc/calls/calls.h"
|
2020-11-28 20:01:51 +00:00
|
|
|
#include "libc/calls/struct/iovec.h"
|
2020-09-07 04:39:00 +00:00
|
|
|
#include "libc/calls/struct/itimerval.h"
|
2021-09-04 05:19:41 +00:00
|
|
|
#include "libc/calls/struct/metastat.internal.h"
|
2022-04-18 07:01:26 +00:00
|
|
|
#include "libc/calls/struct/rlimit.h"
|
2021-04-18 18:34:59 +00:00
|
|
|
#include "libc/calls/struct/rusage.h"
|
2021-02-06 07:45:34 +00:00
|
|
|
#include "libc/calls/struct/sigaction-xnu.internal.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/calls/struct/siginfo-xnu.internal.h"
|
2021-04-18 18:34:59 +00:00
|
|
|
#include "libc/calls/struct/siginfo.h"
|
|
|
|
#include "libc/calls/struct/sigval.h"
|
2021-09-04 05:19:41 +00:00
|
|
|
#include "libc/calls/struct/stat.h"
|
2020-09-07 04:39:00 +00:00
|
|
|
#include "libc/calls/struct/timespec.h"
|
|
|
|
#include "libc/calls/struct/timeval.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/calls/struct/winsize.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/calls/ucontext.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/limits.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-02-06 07:45:34 +00:00
|
|
|
#include "libc/nt/struct/context.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/nt/struct/ntexceptionpointers.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/struct/overlapped.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/struct/securityattributes.h"
|
|
|
|
#include "libc/nt/struct/startupinfo.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/nt/struct/systeminfo.h"
|
2021-02-06 07:45:34 +00:00
|
|
|
#include "libc/nt/struct/win32fileattributedata.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2020-09-07 04:39:00 +00:00
|
|
|
#include "libc/time/struct/timezone.h"
|
|
|
|
#include "libc/time/struct/utimbuf.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
#define kSigactionMinRva 8 /* >SIG_{ERR,DFL,IGN,...} */
|
|
|
|
|
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
|
|
|
|
#define kIoMotion ((const int8_t[3]){1, 0, 0})
|
|
|
|
|
|
|
|
struct IoctlPtmGet {
|
|
|
|
int theduxfd;
|
|
|
|
int workerfd;
|
|
|
|
char theduxname[16];
|
|
|
|
char workername[16];
|
|
|
|
};
|
|
|
|
|
2021-02-02 16:14:45 +00:00
|
|
|
enum FdKind {
|
|
|
|
kFdEmpty,
|
|
|
|
kFdFile,
|
|
|
|
kFdSocket,
|
|
|
|
kFdProcess,
|
|
|
|
kFdConsole,
|
|
|
|
kFdSerial,
|
|
|
|
kFdZip,
|
|
|
|
kFdEpoll,
|
2021-02-03 18:50:08 +00:00
|
|
|
kFdReserved
|
2021-02-02 16:14:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Fd {
|
2021-02-08 12:04:42 +00:00
|
|
|
enum FdKind kind;
|
|
|
|
unsigned flags;
|
2022-04-15 06:39:48 +00:00
|
|
|
unsigned mode;
|
2021-02-02 16:14:45 +00:00
|
|
|
int64_t handle;
|
|
|
|
int64_t extra;
|
2022-03-25 14:11:44 +00:00
|
|
|
bool zombie;
|
2021-02-02 16:14:45 +00:00
|
|
|
};
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
struct Fds {
|
2022-05-21 01:51:41 +00:00
|
|
|
int f; /* lowest free slot */
|
2021-02-06 07:45:34 +00:00
|
|
|
size_t n; /* monotonic capacity */
|
2021-02-05 17:44:54 +00:00
|
|
|
struct Fd *p;
|
2020-06-15 14:18:57 +00:00
|
|
|
struct Fd __init_p[OPEN_MAX];
|
|
|
|
};
|
|
|
|
|
2020-11-13 09:27:49 +00:00
|
|
|
hidden extern struct Fds g_fds;
|
2022-05-23 22:06:11 +00:00
|
|
|
hidden extern const struct Fd kEmptyFd;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
int __reservefd(int) hidden;
|
2022-05-17 11:14:28 +00:00
|
|
|
int __reservefd_unlocked(int) hidden;
|
2021-02-03 14:22:51 +00:00
|
|
|
void __releasefd(int) hidden;
|
2022-05-17 11:14:28 +00:00
|
|
|
void __releasefd_unlocked(int) hidden;
|
2020-11-28 20:01:51 +00:00
|
|
|
int __ensurefds(int) hidden;
|
2022-05-17 11:14:28 +00:00
|
|
|
int __ensurefds_unlocked(int) hidden;
|
2022-04-24 16:59:22 +00:00
|
|
|
void __printfds(void) hidden;
|
2021-01-28 03:34:02 +00:00
|
|
|
|
2022-05-18 23:41:29 +00:00
|
|
|
forceinline int64_t __getfdhandleactual(int fd) {
|
|
|
|
return g_fds.p[fd].handle;
|
|
|
|
}
|
|
|
|
|
2021-01-28 03:34:02 +00:00
|
|
|
forceinline bool __isfdopen(int fd) {
|
|
|
|
return 0 <= fd && fd < g_fds.n && g_fds.p[fd].kind != kFdEmpty;
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:14:45 +00:00
|
|
|
forceinline bool __isfdkind(int fd, int kind) {
|
2021-01-28 03:34:02 +00:00
|
|
|
return 0 <= fd && fd < g_fds.n && g_fds.p[fd].kind == kind;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-08 03:30:04 +00:00
|
|
|
forceinline size_t _clampio(size_t size) {
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!IsTrustworthy()) {
|
|
|
|
return MIN(size, 0x7ffff000);
|
|
|
|
} else {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define i32 int32_t
|
|
|
|
#define i64 int64_t
|
|
|
|
#define u32 uint32_t
|
|
|
|
#define u64 uint64_t
|
|
|
|
#define sigset struct sigset
|
|
|
|
|
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
│ cosmopolitan § syscalls » system five » synthetic jump slots ─╬─│┼
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
2021-02-07 14:11:44 +00:00
|
|
|
axdx_t sys_gettimeofday(struct timeval *, struct timezone *, void *) hidden;
|
2021-04-18 18:34:59 +00:00
|
|
|
i32 __sys_getrusage(i32, struct rusage *) hidden;
|
2022-04-20 16:56:53 +00:00
|
|
|
i32 __sys_sigprocmask(i32, const sigset *, sigset *, u64) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
i32 __sys_utimensat(i32, const char *, const struct timespec *, i32) hidden;
|
2021-04-18 18:34:59 +00:00
|
|
|
i32 __sys_wait4(i32, i32 *, i32, struct rusage *) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
i32 sys_clock_gettime(i32, struct timespec *) hidden;
|
|
|
|
i32 sys_fstat(i32, struct stat *) hidden;
|
|
|
|
i32 sys_fstatat(i32, const char *, struct stat *, i32) hidden;
|
|
|
|
i32 sys_futimes(i32, const struct timeval *) hidden;
|
|
|
|
i32 sys_futimesat(i32, const char *, const struct timeval *) hidden;
|
|
|
|
i32 sys_getitimer(i32, struct itimerval *) hidden;
|
|
|
|
i32 sys_getrlimit(i32, struct rlimit *) hidden;
|
|
|
|
i32 sys_getrusage(i32, struct rusage *) hidden;
|
|
|
|
i32 sys_lutimes(const char *, const struct timeval *) hidden;
|
|
|
|
i32 sys_nanosleep(const struct timespec *, struct timespec *) hidden;
|
|
|
|
i32 sys_setitimer(i32, const struct itimerval *, struct itimerval *) hidden;
|
|
|
|
i32 sys_setrlimit(i32, const struct rlimit *) hidden;
|
2022-04-20 16:56:53 +00:00
|
|
|
i32 sys_sigprocmask(i32, const sigset *, sigset *) hidden;
|
2021-04-18 18:34:59 +00:00
|
|
|
i32 sys_sigqueue(i32, i32, const union sigval) hidden;
|
|
|
|
i32 sys_sigqueueinfo(i32, const siginfo_t *) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
i32 sys_sigsuspend(const sigset *, u64) hidden;
|
|
|
|
i32 sys_sysinfo(struct sysinfo *) hidden;
|
|
|
|
i32 sys_utime(const char *, const struct utimbuf *) hidden;
|
|
|
|
i32 sys_utimensat(i32, const char *, const struct timespec *, i32) hidden;
|
|
|
|
i32 sys_utimes(const char *, const struct timeval *) hidden;
|
|
|
|
i32 sys_wait4(i32, i32 *, i32, struct rusage *) hidden;
|
2021-02-07 14:11:44 +00:00
|
|
|
i64 sys_preadv(i32, struct iovec *, i32, i64, i64) hidden;
|
|
|
|
i64 sys_pwritev(i32, const struct iovec *, i32, i64, i64) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
i64 sys_vmsplice(i32, const struct iovec *, i64, u32) hidden;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
│ cosmopolitan § syscalls » system five » support ─╬─│┼
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
int sys_nanosleep_xnu(const struct timespec *, struct timespec *) hidden;
|
2022-05-23 22:06:11 +00:00
|
|
|
int sys_utimensat_xnu(int, const char *, const struct timespec *, int) hidden;
|
2021-03-08 18:07:02 +00:00
|
|
|
size_t __iovec_size(const struct iovec *, size_t) hidden;
|
2021-04-24 20:58:34 +00:00
|
|
|
ssize_t WritevUninterruptible(int, struct iovec *, int);
|
2022-05-23 22:06:11 +00:00
|
|
|
void __rusage2linux(struct rusage *) hidden;
|
|
|
|
void __sigenter_xnu(void *, i32, i32, struct siginfo_xnu *,
|
|
|
|
struct __darwin_ucontext *) hidden;
|
|
|
|
void __stat2cosmo(struct stat *restrict, const union metastat *) hidden;
|
2021-08-12 07:42:14 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
│ cosmopolitan § syscalls » windows nt » veneers ─╬─│┼
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
2022-05-23 22:06:11 +00:00
|
|
|
int ioctl_tiocgwinsz_nt(struct Fd *, struct winsize *) hidden;
|
2021-02-06 07:45:34 +00:00
|
|
|
int sys_close_nt(struct Fd *) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
int sys_fstat_nt(i64, struct stat *) hidden;
|
Improve ZIP filesystem and change its prefix
The ZIP filesystem has a breaking change. You now need to use /zip/ to
open() / opendir() / etc. assets within the ZIP structure of your APE
binary, instead of the previous convention of using zip: or zip! URIs.
This is needed because Python likes to use absolute paths, and having
ZIP paths encoded like URIs simply broke too many things.
Many more system calls have been updated to be able to operate on ZIP
files and file descriptors. In particular fcntl() and ioctl() since
Python would do things like ask if a ZIP file is a terminal and get
confused when the old implementation mistakenly said yes, because the
fastest way to guarantee native file descriptors is to dup(2). This
change also improves the async signal safety of zipos and ensures it
doesn't maintain any open file descriptors beyond that which the user
has opened.
This change makes a lot of progress towards adding magic numbers that
are specific to platforms other than Linux. The philosophy here is that,
if you use an operating system like FreeBSD, then you should be able to
take advantage of FreeBSD exclusive features, even if we don't polyfill
them on other platforms. For example, you can now open() a file with the
O_VERIFY flag. If your program runs on other platforms, then Cosmo will
automatically set O_VERIFY to zero. This lets you safely use it without
the need for #ifdef or ifstatements which detract from readability.
One of the blindspots of the ASAN memory hardening we use to offer Rust
like assurances has always been that memory passed to the kernel via
system calls (e.g. writev) can't be checked automatically since the
kernel wasn't built with MODE=asan. This change makes more progress
ensuring that each system call will verify the soundness of memory
before it's passed to the kernel. The code for doing these checks is
fast, particularly for buffers, where it can verify 64 bytes a cycle.
- Correct O_LOOP definition on NT
- Introduce program_executable_name
- Add ASAN guards to more system calls
- Improve termios compatibility with BSDs
- Fix bug in Windows auxiliary value encoding
- Add BSD and XNU specific errnos and open flags
- Add check to ensure build doesn't talk to internet
2021-08-22 08:04:18 +00:00
|
|
|
int sys_fstatat_nt(int, const char *, struct stat *, int) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
int sys_getrusage_nt(int, struct rusage *) hidden;
|
|
|
|
int sys_gettimeofday_nt(struct timeval *, struct timezone *) hidden;
|
|
|
|
int sys_lstat_nt(const char *, struct stat *) hidden;
|
|
|
|
int sys_nanosleep_nt(const struct timespec *, struct timespec *) hidden;
|
|
|
|
int sys_setitimer_nt(int, const struct itimerval *, struct itimerval *) hidden;
|
2022-04-18 07:01:26 +00:00
|
|
|
int sys_setrlimit_nt(int, const struct rlimit *) hidden;
|
2022-05-23 22:06:11 +00:00
|
|
|
int sys_sysinfo_nt(struct sysinfo *) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
int sys_utimensat_nt(int, const char *, const struct timespec *, int) hidden;
|
Improve ZIP filesystem and change its prefix
The ZIP filesystem has a breaking change. You now need to use /zip/ to
open() / opendir() / etc. assets within the ZIP structure of your APE
binary, instead of the previous convention of using zip: or zip! URIs.
This is needed because Python likes to use absolute paths, and having
ZIP paths encoded like URIs simply broke too many things.
Many more system calls have been updated to be able to operate on ZIP
files and file descriptors. In particular fcntl() and ioctl() since
Python would do things like ask if a ZIP file is a terminal and get
confused when the old implementation mistakenly said yes, because the
fastest way to guarantee native file descriptors is to dup(2). This
change also improves the async signal safety of zipos and ensures it
doesn't maintain any open file descriptors beyond that which the user
has opened.
This change makes a lot of progress towards adding magic numbers that
are specific to platforms other than Linux. The philosophy here is that,
if you use an operating system like FreeBSD, then you should be able to
take advantage of FreeBSD exclusive features, even if we don't polyfill
them on other platforms. For example, you can now open() a file with the
O_VERIFY flag. If your program runs on other platforms, then Cosmo will
automatically set O_VERIFY to zero. This lets you safely use it without
the need for #ifdef or ifstatements which detract from readability.
One of the blindspots of the ASAN memory hardening we use to offer Rust
like assurances has always been that memory passed to the kernel via
system calls (e.g. writev) can't be checked automatically since the
kernel wasn't built with MODE=asan. This change makes more progress
ensuring that each system call will verify the soundness of memory
before it's passed to the kernel. The code for doing these checks is
fast, particularly for buffers, where it can verify 64 bytes a cycle.
- Correct O_LOOP definition on NT
- Introduce program_executable_name
- Add ASAN guards to more system calls
- Improve termios compatibility with BSDs
- Fix bug in Windows auxiliary value encoding
- Add BSD and XNU specific errnos and open flags
- Add check to ensure build doesn't talk to internet
2021-08-22 08:04:18 +00:00
|
|
|
int sys_utimes_nt(const char *, const struct timeval[2]) hidden;
|
2021-02-04 03:35:29 +00:00
|
|
|
ssize_t sys_read_nt(struct Fd *, const struct iovec *, size_t, ssize_t) hidden;
|
2022-03-19 10:37:00 +00:00
|
|
|
ssize_t sys_write_nt(int, const struct iovec *, size_t, ssize_t) hidden;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
│ cosmopolitan § syscalls » windows nt » support ─╬─│┼
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
2022-03-25 14:11:44 +00:00
|
|
|
bool _check_interrupts(bool, struct Fd *) hidden;
|
2021-10-02 15:17:04 +00:00
|
|
|
int sys_clock_gettime_nt(int, struct timespec *) hidden;
|
2021-02-26 02:30:17 +00:00
|
|
|
ssize_t sys_readv_nt(struct Fd *, const struct iovec *, int) hidden;
|
2022-03-19 10:37:00 +00:00
|
|
|
ssize_t sys_writev_nt(int, const struct iovec *, int) hidden;
|
2021-10-02 15:17:04 +00:00
|
|
|
unsigned __wincrash_nt(struct NtExceptionPointers *);
|
2022-04-12 12:20:17 +00:00
|
|
|
void _ntcontext2linux(struct ucontext *, const struct NtContext *) hidden;
|
|
|
|
void _ntlinux2context(struct NtContext *, const ucontext_t *) hidden;
|
2022-04-15 06:39:48 +00:00
|
|
|
struct NtOverlapped *_offset2overlap(int64_t, int64_t,
|
|
|
|
struct NtOverlapped *) hidden;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-01-17 01:52:15 +00:00
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
│ cosmopolitan § syscalls » metal ─╬─│┼
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
2021-02-26 02:30:17 +00:00
|
|
|
int sys_fstat_metal(int, struct stat *);
|
|
|
|
int sys_openat_metal(int, const char *, int, unsigned);
|
|
|
|
ssize_t sys_readv_metal(struct Fd *, const struct iovec *, int) hidden;
|
|
|
|
ssize_t sys_writev_metal(struct Fd *, const struct iovec *, int) hidden;
|
2021-01-17 01:52:15 +00:00
|
|
|
|
2020-11-28 20:01:51 +00:00
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
│ cosmopolitan § syscalls » drivers ─╬─│┼
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
2021-02-26 02:30:17 +00:00
|
|
|
ssize_t sys_readv_serial(struct Fd *, const struct iovec *, int) hidden;
|
|
|
|
ssize_t sys_writev_serial(struct Fd *, const struct iovec *, int) hidden;
|
2020-11-28 20:01:51 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
#undef sigset
|
|
|
|
#undef i32
|
|
|
|
#undef i64
|
|
|
|
#undef u32
|
|
|
|
#undef u64
|
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_CALLS_INTERNAL_H_ */
|