mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-08 20:28:30 +00:00
Make improvements
- This change fixes a bug that allowed unbuffered printf() output (to streams like stderr) to be truncated. This regression was introduced some time between now and the last release. - POSIX specifies all functions as thread safe by default. This change works towards cleaning up our use of the @threadsafe / @threadunsafe documentation annotations to reflect that. The goal is (1) to use @threadunsafe to document functions which POSIX say needn't be thread safe, and (2) use @threadsafe to document functions that we chose to implement as thread safe even though POSIX didn't mandate it. - Tidy up the clock_gettime() implementation. We're now trying out a cleaner approach to system call support that aims to maintain the Linux errno convention as long as possible. This also fixes bugs that existed previously, where the vDSO errno wasn't being translated properly. The gettimeofday() system call is now a wrapper for clock_gettime(), which reduces bloat in apps that use both. - The recently-introduced improvements to the execute bit on Windows has had bugs fixed. access(X_OK) on a directory on Windows now succeeds. fstat() will now perform the MZ/#! ReadFile() operation correctly. - Windows.h is no longer included in libc/isystem/, because it confused PCRE's build system into thinking Cosmopolitan is a WIN32 platform. Cosmo's Windows.h polyfill was never even really that good, since it only defines a subset of the subset of WIN32 APIs that Cosmo defines. - The setlongerjmp() / longerjmp() APIs are removed. While they're nice APIs that are superior to the standardized setjmp / longjmp functions, they weren't superior enough to not be dead code in the monorepo. If you use these APIs, please file an issue and they'll be restored. - The .com appending magic has now been removed from APE Loader.
This commit is contained in:
parent
b99512ac58
commit
ff77f2a6af
226 changed files with 708 additions and 2657 deletions
|
@ -107,6 +107,8 @@ o//libc/calls/statfs2cosmo.o: private \
|
|||
# we always want -O2 because:
|
||||
# division is expensive if not optimized
|
||||
o/$(MODE)/libc/calls/clock.o \
|
||||
o/$(MODE)/libc/calls/gettimeofday.o \
|
||||
o/$(MODE)/libc/calls/clock_gettime-mono.o \
|
||||
o/$(MODE)/libc/calls/timespec_tomillis.o \
|
||||
o/$(MODE)/libc/calls/timespec_tomicros.o \
|
||||
o/$(MODE)/libc/calls/timespec_totimeval.o \
|
||||
|
|
|
@ -52,7 +52,7 @@ int64_t clock(void) {
|
|||
struct rusage ru;
|
||||
struct timespec ts;
|
||||
e = errno;
|
||||
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == -1) {
|
||||
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) {
|
||||
errno = e;
|
||||
if (getrusage(RUSAGE_SELF, &ru) != -1) {
|
||||
ts = timeval_totimespec(timeval_add(ru.ru_utime, ru.ru_stime));
|
||||
|
|
|
@ -54,7 +54,6 @@ static int sys_clock_getres_xnu(int clock, struct timespec *ts) {
|
|||
* @error EPERM if pledge() is in play without stdio promise
|
||||
* @error EINVAL if `clock` isn't supported on this system
|
||||
* @error EFAULT if `ts` points to bad memory
|
||||
* @threadsafe
|
||||
*/
|
||||
int clock_getres(int clock, struct timespec *ts) {
|
||||
int rc;
|
||||
|
|
|
@ -16,15 +16,19 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Fast Monotonic Clock Polyfill for XNU/NT.
|
||||
*/
|
||||
|
||||
static struct {
|
||||
_Atomic(uint32_t) once;
|
||||
atomic_uint once;
|
||||
struct timespec base_wall;
|
||||
uint64_t base_tick;
|
||||
} g_mono;
|
||||
|
@ -37,13 +41,18 @@ static void sys_clock_gettime_mono_init(void) {
|
|||
int sys_clock_gettime_mono(struct timespec *time) {
|
||||
uint64_t nanos;
|
||||
uint64_t cycles;
|
||||
if (X86_HAVE(INVTSC)) {
|
||||
cosmo_once(&g_mono.once, sys_clock_gettime_mono_init);
|
||||
cycles = rdtsc() - g_mono.base_tick;
|
||||
nanos = cycles / 3;
|
||||
*time = timespec_add(g_mono.base_wall, timespec_fromnanos(nanos));
|
||||
return 0;
|
||||
} else {
|
||||
return einval();
|
||||
}
|
||||
#ifdef __x86_64__
|
||||
// intel architecture guarantees that a mapping exists between rdtsc &
|
||||
// nanoseconds only if the cpu advertises invariant timestamps support
|
||||
if (!X86_HAVE(INVTSC)) return -EINVAL;
|
||||
#endif
|
||||
cosmo_once(&g_mono.once, sys_clock_gettime_mono_init);
|
||||
cycles = rdtsc() - g_mono.base_tick;
|
||||
// this is a crude approximation, that's worked reasonably well so far
|
||||
// only the kernel knows the actual mapping between rdtsc and nanosecs
|
||||
// which we could attempt to measure ourselves using clock_gettime but
|
||||
// we'd need to impose 100 ms of startup latency for a guess this good
|
||||
nanos = cycles / 3;
|
||||
*time = timespec_add(g_mono.base_wall, timespec_fromnanos(nanos));
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,12 +16,12 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/clock_gettime.internal.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
|
||||
struct NtFileTime ft;
|
||||
|
@ -32,6 +32,6 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
|
|||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
return sys_clock_gettime_mono(ts);
|
||||
} else {
|
||||
return einval();
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
|
@ -16,11 +16,10 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
|
||||
int sys_clock_gettime_m1(int clock, struct timespec *ts) {
|
||||
return _sysret(__syslib->__clock_gettime(clock, ts));
|
||||
int sys_clock_gettime(int clock, struct timespec *ts) {
|
||||
return __syscall2i(clock, (long)ts, __NR_clock_gettime);
|
||||
}
|
|
@ -16,28 +16,46 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/clock_gettime.internal.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
|
||||
axdx_t ad;
|
||||
long ax, dx;
|
||||
if (clock == CLOCK_REALTIME) {
|
||||
ad = sys_gettimeofday((struct timeval *)ts, 0, 0);
|
||||
if (ad.ax != -1) {
|
||||
if (ad.ax) {
|
||||
ts->tv_sec = ad.ax;
|
||||
ts->tv_nsec = ad.dx;
|
||||
}
|
||||
ts->tv_nsec *= 1000;
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
// invoke the system call
|
||||
//
|
||||
// int gettimeofday(struct timeval *tp,
|
||||
// struct timezone *tzp,
|
||||
// uint64_t *mach_absolute_time);
|
||||
//
|
||||
// as follows
|
||||
//
|
||||
// ax, dx = gettimeofday(&ts, 0, 0);
|
||||
//
|
||||
// to support multiple calling conventions
|
||||
//
|
||||
// 1. new xnu returns *ts in memory via rdi
|
||||
// 2. old xnu returns *ts in rax:rdx regs
|
||||
//
|
||||
// we assume this system call always succeeds
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax), "=d"(dx)
|
||||
: "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (ax) {
|
||||
ts->tv_sec = ax;
|
||||
ts->tv_nsec = dx;
|
||||
}
|
||||
ts->tv_nsec *= 1000;
|
||||
return 0;
|
||||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
return sys_clock_gettime_mono(ts);
|
||||
} else {
|
||||
return einval();
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
|
@ -16,82 +16,14 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/asan.internal.h"
|
||||
#include "libc/calls/clock_gettime.internal.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/asmflag.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/mem/alloca.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
/**
|
||||
* Returns nanosecond time.
|
||||
*
|
||||
* This is a high-precision timer that supports multiple definitions of
|
||||
* time. Among the more popular is CLOCK_MONOTONIC. This function has a
|
||||
* zero syscall implementation of that on modern x86.
|
||||
*
|
||||
* rdtsc l: 13𝑐 4𝑛𝑠
|
||||
* gettimeofday l: 44𝑐 14𝑛𝑠
|
||||
* clock_gettime l: 40𝑐 13𝑛𝑠
|
||||
* __clock_gettime l: 35𝑐 11𝑛𝑠
|
||||
* sys_clock_gettime l: 220𝑐 71𝑛𝑠
|
||||
*
|
||||
* @param clock can be one of:
|
||||
* - `CLOCK_REALTIME`: universally supported
|
||||
* - `CLOCK_REALTIME_FAST`: ditto but faster on freebsd
|
||||
* - `CLOCK_REALTIME_PRECISE`: ditto but better on freebsd
|
||||
* - `CLOCK_REALTIME_COARSE`: : like `CLOCK_REALTIME_FAST` w/ Linux 2.6.32+
|
||||
* - `CLOCK_MONOTONIC`: universally supported
|
||||
* - `CLOCK_MONOTONIC_FAST`: ditto but faster on freebsd
|
||||
* - `CLOCK_MONOTONIC_PRECISE`: ditto but better on freebsd
|
||||
* - `CLOCK_MONOTONIC_COARSE`: : like `CLOCK_MONOTONIC_FAST` w/ Linux 2.6.32+
|
||||
* - `CLOCK_MONOTONIC_RAW`: is actually monotonic but needs Linux 2.6.28+
|
||||
* - `CLOCK_PROCESS_CPUTIME_ID`: linux and bsd
|
||||
* - `CLOCK_THREAD_CPUTIME_ID`: linux and bsd
|
||||
* - `CLOCK_MONOTONIC_COARSE`: linux, freebsd
|
||||
* - `CLOCK_PROF`: linux and netbsd
|
||||
* - `CLOCK_BOOTTIME`: linux and openbsd
|
||||
* - `CLOCK_REALTIME_ALARM`: linux-only
|
||||
* - `CLOCK_BOOTTIME_ALARM`: linux-only
|
||||
* - `CLOCK_TAI`: linux-only
|
||||
* @param ts is where the result is stored
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @error EPERM if pledge() is in play without stdio promise
|
||||
* @error EINVAL if `clock` isn't supported on this system
|
||||
* @error EFAULT if `ts` points to bad memory
|
||||
* @see strftime(), gettimeofday()
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int clock_gettime(int clock, struct timespec *ts) {
|
||||
// threads on win32 stacks call this so we can't asan check *ts
|
||||
int rc;
|
||||
if (clock == 127) {
|
||||
rc = einval(); // 127 is used by consts.sh to mean unsupported
|
||||
} else {
|
||||
rc = __clock_gettime(clock, ts);
|
||||
}
|
||||
#if SYSDEBUG
|
||||
if (__tls_enabled && !(__get_tls()->tib_flags & TIB_FLAG_TIME_CRITICAL)) {
|
||||
POLLTRACE("clock_gettime(%s, [%s]) → %d% m", DescribeClockName(clock),
|
||||
DescribeTimespec(rc, ts), rc);
|
||||
}
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
|
||||
#ifdef __aarch64__
|
||||
#define CGT_VDSO __vdsosym("LINUX_2.6.39", "__kernel_clock_gettime")
|
||||
|
@ -99,39 +31,72 @@ int clock_gettime(int clock, struct timespec *ts) {
|
|||
#define CGT_VDSO __vdsosym("LINUX_2.6", "__vdso_clock_gettime")
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Returns pointer to fastest clock_gettime().
|
||||
*/
|
||||
clock_gettime_f *__clock_gettime_get(bool *opt_out_isfast) {
|
||||
bool isfast;
|
||||
clock_gettime_f *res;
|
||||
if (IsLinux() && (res = CGT_VDSO)) {
|
||||
isfast = true;
|
||||
} else if (IsXnu()) {
|
||||
#ifdef __x86_64__
|
||||
res = sys_clock_gettime_xnu;
|
||||
isfast = false;
|
||||
#elif defined(__aarch64__)
|
||||
res = sys_clock_gettime_m1;
|
||||
isfast = true;
|
||||
#else
|
||||
#error "unsupported architecture"
|
||||
#endif
|
||||
typedef int clock_gettime_f(int, struct timespec *);
|
||||
|
||||
static clock_gettime_f *__clock_gettime_get(void) {
|
||||
clock_gettime_f *cgt;
|
||||
if (IsLinux() && (cgt = CGT_VDSO)) {
|
||||
return cgt;
|
||||
} else if (__syslib) {
|
||||
return (void *)__syslib->__clock_gettime;
|
||||
} else if (IsWindows()) {
|
||||
isfast = true;
|
||||
res = sys_clock_gettime_nt;
|
||||
return sys_clock_gettime_nt;
|
||||
#ifdef __x86_64__
|
||||
} else if (IsXnu()) {
|
||||
return sys_clock_gettime_xnu;
|
||||
#endif
|
||||
} else {
|
||||
isfast = false;
|
||||
res = sys_clock_gettime;
|
||||
return sys_clock_gettime;
|
||||
}
|
||||
if (opt_out_isfast) {
|
||||
*opt_out_isfast = isfast;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int __clock_gettime_init(int clockid, struct timespec *ts) {
|
||||
clock_gettime_f *gettime;
|
||||
__clock_gettime = gettime = __clock_gettime_get(0);
|
||||
return gettime(clockid, ts);
|
||||
static int __clock_gettime_init(int, struct timespec *);
|
||||
static clock_gettime_f *__clock_gettime = __clock_gettime_init;
|
||||
static int __clock_gettime_init(int clockid, struct timespec *ts) {
|
||||
clock_gettime_f *cgt;
|
||||
__clock_gettime = cgt = __clock_gettime_get();
|
||||
return cgt(clockid, ts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns nanosecond time.
|
||||
*
|
||||
* @param clock can be one of:
|
||||
* - `CLOCK_REALTIME`: universally supported
|
||||
* - `CLOCK_REALTIME_FAST`: ditto but faster on freebsd
|
||||
* - `CLOCK_REALTIME_PRECISE`: ditto but better on freebsd
|
||||
* - `CLOCK_REALTIME_COARSE`: : like `CLOCK_REALTIME_FAST` w/ Linux 2.6.32+
|
||||
* - `CLOCK_MONOTONIC`: universally supported (except on XNU/NT w/o INVTSC)
|
||||
* - `CLOCK_MONOTONIC_FAST`: ditto but faster on freebsd
|
||||
* - `CLOCK_MONOTONIC_PRECISE`: ditto but better on freebsd
|
||||
* - `CLOCK_MONOTONIC_COARSE`: : like `CLOCK_MONOTONIC_FAST` w/ Linux 2.6.32+
|
||||
* - `CLOCK_MONOTONIC_RAW`: is actually monotonic but needs Linux 2.6.28+
|
||||
* - `CLOCK_PROCESS_CPUTIME_ID`: linux and bsd (NetBSD permits OR'd PID)
|
||||
* - `CLOCK_THREAD_CPUTIME_ID`: linux and bsd (NetBSD permits OR'd TID)
|
||||
* - `CLOCK_MONOTONIC_COARSE`: linux, freebsd
|
||||
* - `CLOCK_PROF`: linux and netbsd
|
||||
* - `CLOCK_BOOTTIME`: linux and openbsd
|
||||
* - `CLOCK_REALTIME_ALARM`: linux-only
|
||||
* - `CLOCK_BOOTTIME_ALARM`: linux-only
|
||||
* - `CLOCK_TAI`: linux-only
|
||||
* @param ts is where the result is stored (or null to do clock check)
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EFAULT if `ts` points to invalid memory
|
||||
* @error EINVAL if `clock` isn't supported on this system
|
||||
* @error EPERM if pledge() is in play without stdio promise
|
||||
* @error ESRCH on NetBSD if PID/TID OR'd into `clock` wasn't found
|
||||
* @see strftime(), gettimeofday()
|
||||
* @asyncsignalsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int clock_gettime(int clock, struct timespec *ts) {
|
||||
// threads on win32 stacks call this so we can't asan check *ts
|
||||
int rc = __clock_gettime(clock, ts);
|
||||
if (rc) {
|
||||
errno = -rc;
|
||||
rc = -1;
|
||||
}
|
||||
TIMETRACE("clock_gettime(%s, [%s]) → %d% m", DescribeClockName(clock),
|
||||
DescribeTimespec(rc, ts), rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_CLOCK_GETTIME_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_CLOCK_GETTIME_H_
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
typedef int clock_gettime_f(int, struct timespec *);
|
||||
|
||||
extern clock_gettime_f *__clock_gettime;
|
||||
clock_gettime_f *__clock_gettime_get(bool *);
|
||||
int __clock_gettime_init(int, struct timespec *);
|
||||
int sys_clock_gettime_mono(struct timespec *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_CLOCK_GETTIME_H_ */
|
|
@ -19,40 +19,40 @@
|
|||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/sysv/consts/timer.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
int sys_clock_nanosleep_xnu(int clock, int flags, const struct timespec *req,
|
||||
struct timespec *rem) {
|
||||
#ifdef __x86_64__
|
||||
struct timeval abs, now, rel;
|
||||
if (clock == CLOCK_REALTIME) {
|
||||
if (flags & TIMER_ABSTIME) {
|
||||
abs = timespec_totimeval(*req);
|
||||
sys_gettimeofday_xnu(&now, 0, 0);
|
||||
if (timeval_cmp(abs, now) > 0) {
|
||||
rel = timeval_sub(abs, now);
|
||||
return sys_select(0, 0, 0, 0, &rel);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
if (flags & TIMER_ABSTIME) {
|
||||
struct timespec now;
|
||||
sys_clock_gettime_xnu(clock, &now);
|
||||
if (timespec_cmp(*req, now) > 0) {
|
||||
struct timeval rel = timespec_totimeval(timespec_sub(*req, now));
|
||||
return sys_select(0, 0, 0, 0, &rel);
|
||||
} else {
|
||||
return sys_nanosleep_xnu(req, rem);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return enotsup();
|
||||
int rc;
|
||||
struct timespec beg;
|
||||
if (rem) sys_clock_gettime_xnu(CLOCK_REALTIME, &beg);
|
||||
struct timeval rel = timespec_totimeval(*req); // rounds up
|
||||
rc = sys_select(0, 0, 0, 0, &rel);
|
||||
if (rc == -1 && rem && errno == EINTR) {
|
||||
struct timespec end;
|
||||
sys_clock_gettime_xnu(CLOCK_REALTIME, &end);
|
||||
*rem = timespec_subz(*req, timespec_sub(end, beg));
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
#else
|
||||
long res;
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include "libc/calls/blockcancel.internal.h"
|
||||
#include "libc/calls/blocksigs.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/clock_gettime.internal.h"
|
||||
#include "libc/calls/cp.internal.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
|
@ -76,16 +75,15 @@ static errno_t sys_clock_nanosleep(int clock, int flags,
|
|||
static struct timespec GetNanosleepLatency(void) {
|
||||
errno_t rc;
|
||||
int64_t nanos;
|
||||
clock_gettime_f *cgt;
|
||||
struct timespec x, y, w = {0, 1};
|
||||
if (!(nanos = g_nanosleep_latency)) {
|
||||
BLOCK_SIGNALS;
|
||||
for (cgt = __clock_gettime_get(0);;) {
|
||||
npassert(!cgt(CLOCK_REALTIME_PRECISE, &x));
|
||||
for (;;) {
|
||||
unassert(!clock_gettime(CLOCK_REALTIME_PRECISE, &x));
|
||||
rc = sys_clock_nanosleep(CLOCK_REALTIME, 0, &w, 0);
|
||||
npassert(!rc || rc == EINTR);
|
||||
unassert(!rc || rc == EINTR);
|
||||
if (!rc) {
|
||||
npassert(!cgt(CLOCK_REALTIME_PRECISE, &y));
|
||||
unassert(!clock_gettime(CLOCK_REALTIME_PRECISE, &y));
|
||||
nanos = timespec_tonanos(timespec_sub(y, x));
|
||||
g_nanosleep_latency = nanos;
|
||||
break;
|
||||
|
@ -107,7 +105,6 @@ static errno_t CheckCancel(void) {
|
|||
static errno_t SpinNanosleep(int clock, int flags, const struct timespec *req,
|
||||
struct timespec *rem) {
|
||||
errno_t rc;
|
||||
clock_gettime_f *cgt;
|
||||
struct timespec now, start, elapsed;
|
||||
if ((rc = CheckCancel())) {
|
||||
if (rc == EINTR && !flags && rem) {
|
||||
|
@ -115,11 +112,10 @@ static errno_t SpinNanosleep(int clock, int flags, const struct timespec *req,
|
|||
}
|
||||
return rc;
|
||||
}
|
||||
cgt = __clock_gettime_get(0);
|
||||
npassert(!cgt(CLOCK_REALTIME, &start));
|
||||
unassert(!clock_gettime(CLOCK_REALTIME, &start));
|
||||
for (;;) {
|
||||
pthread_yield();
|
||||
npassert(!cgt(CLOCK_REALTIME, &now));
|
||||
unassert(!clock_gettime(CLOCK_REALTIME, &now));
|
||||
if (flags & TIMER_ABSTIME) {
|
||||
if (timespec_cmp(now, *req) >= 0) {
|
||||
return 0;
|
||||
|
@ -177,7 +173,7 @@ static bool ShouldUseSpinNanosleep(int clock, int flags,
|
|||
return false;
|
||||
}
|
||||
e = errno;
|
||||
if (__clock_gettime_get(0)(clock, &now)) {
|
||||
if (clock_gettime(clock, &now)) {
|
||||
// punt to the nanosleep system call
|
||||
errno = e;
|
||||
return false;
|
||||
|
@ -258,12 +254,8 @@ errno_t clock_nanosleep(int clock, int flags, const struct timespec *req,
|
|||
} else {
|
||||
rc = sys_clock_nanosleep(clock, flags, req, rem);
|
||||
}
|
||||
#if SYSDEBUG
|
||||
if (__tls_enabled && !(__get_tls()->tib_flags & TIB_FLAG_TIME_CRITICAL)) {
|
||||
STRACE("clock_nanosleep(%s, %s, %s, [%s]) → %s", DescribeClockName(clock),
|
||||
DescribeSleepFlags(flags), DescribeTimespec(0, req),
|
||||
DescribeTimespec(rc, rem), DescribeErrno(rc));
|
||||
}
|
||||
#endif
|
||||
TIMETRACE("clock_nanosleep(%s, %s, %s, [%s]) → %s", DescribeClockName(clock),
|
||||
DescribeSleepFlags(flags), DescribeTimespec(0, req),
|
||||
DescribeTimespec(rc, rem), DescribeErrno(rc));
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @restartable
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int creat(const char *file, uint32_t mode) {
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/bsr.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
|
|
|
@ -35,3 +35,5 @@ int fstatvfs(int fd, struct statvfs *sv) {
|
|||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__weak_reference(fstatvfs, fstatvfs64);
|
||||
|
|
|
@ -60,7 +60,6 @@
|
|||
* @raise ENOSYS on bare metal
|
||||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int ftruncate(int fd, int64_t length) {
|
||||
int rc;
|
||||
|
|
|
@ -40,7 +40,6 @@
|
|||
* @raise EFAULT if `ts` memory was invalid
|
||||
* @raise ENOSYS on RHEL5 or bare metal
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int futimens(int fd, const struct timespec ts[2]) {
|
||||
int rc;
|
||||
|
|
|
@ -37,7 +37,6 @@
|
|||
* @raise ENOSYS on RHEL5 or bare metal
|
||||
* @see futimens() for modern version
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int futimes(int fd, const struct timeval tv[2]) {
|
||||
int rc;
|
||||
|
|
|
@ -30,7 +30,6 @@
|
|||
// @see makecontext()
|
||||
// @see swapcontext()
|
||||
// @see setcontext()
|
||||
// @threadsafe
|
||||
.ftrace1
|
||||
getcontext:
|
||||
.ftrace2
|
||||
|
|
|
@ -28,7 +28,6 @@
|
|||
* @return parent process id (always successful)
|
||||
* @note slow on Windows; needs to iterate process tree
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int getppid(void) {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/struct/rusage.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
|
||||
axdx_t sys_gettimeofday_m1(struct timeval *tv, struct timezone *tz, void *wut) {
|
||||
axdx_t ad;
|
||||
struct timespec ts;
|
||||
ad.ax = _sysret(__syslib->__clock_gettime(CLOCK_REALTIME, &ts));
|
||||
ad.dx = 0;
|
||||
if (!ad.ax && tv) {
|
||||
*tv = timespec_totimeval(ts);
|
||||
}
|
||||
return ad;
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/time/struct/timezone.h"
|
||||
|
||||
axdx_t sys_gettimeofday_metal(struct timeval *tv, struct timezone *tz,
|
||||
void *wut) {
|
||||
return (axdx_t){-1, 0};
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
textwindows axdx_t sys_gettimeofday_nt(struct timeval *tv, struct timezone *tz,
|
||||
void *wut) {
|
||||
struct NtFileTime ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
if (tv) *tv = FileTimeToTimeVal(ft);
|
||||
if (tz) bzero(tz, sizeof(*tz));
|
||||
return (axdx_t){0, 0};
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
|
||||
axdx_t sys_gettimeofday_xnu(struct timeval *tv, struct timezone *tz,
|
||||
void *wut) {
|
||||
axdx_t ad;
|
||||
ad = sys_gettimeofday(tv, tz, wut);
|
||||
if (ad.ax > 0 && tv) {
|
||||
tv->tv_sec = ad.ax;
|
||||
tv->tv_usec = ad.dx;
|
||||
ad.ax = 0;
|
||||
}
|
||||
return ad;
|
||||
}
|
|
@ -16,24 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/itimerval.internal.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/time/struct/timezone.h"
|
||||
|
||||
typedef axdx_t gettimeofday_f(struct timeval *, struct timezone *, void *);
|
||||
static gettimeofday_f __gettimeofday_init;
|
||||
static gettimeofday_f *__gettimeofday = __gettimeofday_init;
|
||||
|
||||
/**
|
||||
* Returns system wall time in microseconds, e.g.
|
||||
*
|
||||
|
@ -47,60 +34,26 @@ static gettimeofday_f *__gettimeofday = __gettimeofday_init;
|
|||
* iso8601(p, &tm);
|
||||
* printf("%s\n", p);
|
||||
*
|
||||
* @param tv points to timeval that receives result if non-NULL
|
||||
* @param tz receives UTC timezone if non-NULL
|
||||
* @param tv points to timeval that receives result if non-null
|
||||
* @param tz is completely ignored
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EFAULT if `tv` points to invalid memory
|
||||
* @see clock_gettime() for nanosecond precision
|
||||
* @see strftime() for string formatting
|
||||
* @asyncsignalsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int gettimeofday(struct timeval *tv, struct timezone *tz) {
|
||||
int rc = __gettimeofday(tv, tz, 0).ax;
|
||||
#if SYSDEBUG
|
||||
if (__tls_enabled && !(__get_tls()->tib_flags & TIB_FLAG_TIME_CRITICAL)) {
|
||||
POLLTRACE("gettimeofday([%s], %p) → %d% m", DescribeTimeval(rc, tv), tz,
|
||||
rc);
|
||||
}
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns pointer to fastest gettimeofday().
|
||||
*/
|
||||
gettimeofday_f *__gettimeofday_get(bool *opt_out_isfast) {
|
||||
bool isfast;
|
||||
gettimeofday_f *res;
|
||||
if (IsLinux() && (res = __vdsosym("LINUX_2.6", "__vdso_gettimeofday"))) {
|
||||
isfast = true;
|
||||
} else if (IsWindows()) {
|
||||
isfast = true;
|
||||
res = sys_gettimeofday_nt;
|
||||
} else if (IsXnu()) {
|
||||
#ifdef __x86_64__
|
||||
res = sys_gettimeofday_xnu;
|
||||
isfast = false;
|
||||
#elif defined(__aarch64__)
|
||||
res = sys_gettimeofday_m1;
|
||||
isfast = true;
|
||||
#else
|
||||
#error "unsupported architecture"
|
||||
#endif
|
||||
} else if (IsMetal()) {
|
||||
isfast = false;
|
||||
res = sys_gettimeofday_metal;
|
||||
if (tv) {
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts) != -1) {
|
||||
tv->tv_sec = ts.tv_sec;
|
||||
tv->tv_usec = (ts.tv_nsec + 999) / 1000;
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
isfast = false;
|
||||
res = sys_gettimeofday;
|
||||
return 0;
|
||||
}
|
||||
if (opt_out_isfast) {
|
||||
*opt_out_isfast = isfast;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static axdx_t __gettimeofday_init(struct timeval *tv, //
|
||||
struct timezone *tz, //
|
||||
void *arg) {
|
||||
gettimeofday_f *gettime;
|
||||
__gettimeofday = gettime = __gettimeofday_get(0);
|
||||
return gettime(tv, tz, 0);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,6 @@
|
|||
*
|
||||
* @return user id (always successful)
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
uint32_t getuid(void) {
|
||||
|
@ -62,7 +61,6 @@ uint32_t getuid(void) {
|
|||
*
|
||||
* @return group id (always successful)
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
uint32_t getgid(void) {
|
||||
|
|
|
@ -72,7 +72,6 @@
|
|||
* @raise EINVAL if resulting offset would be negative
|
||||
* @raise EINVAL if `whence` isn't valid
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int64_t lseek(int fd, int64_t offset, int whence) {
|
||||
|
|
|
@ -44,7 +44,6 @@
|
|||
* @raise ENOENT if `path` is an empty string
|
||||
* @raise ELOOP if loop was detected resolving components of `path`
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int makedirs(const char *path, unsigned mode) {
|
||||
int c, e, i, n;
|
||||
|
|
|
@ -45,7 +45,6 @@
|
|||
* @see makedirs() which is higher-level
|
||||
* @see mkdirat() for modern call
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int mkdir(const char *path, unsigned mode) {
|
||||
return mkdirat(AT_FDCWD, path, mode);
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
* but it works on everything else including bare metal.
|
||||
*
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int sys_munmap(void *p, size_t n) {
|
||||
int rc;
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/sock/internal.h"
|
||||
|
||||
// nanosleep() on xnu: a bloodbath of a polyfill
|
||||
// consider using clock_nanosleep(TIMER_ABSTIME)
|
||||
int sys_nanosleep_xnu(const struct timespec *req, struct timespec *rem) {
|
||||
#ifdef __x86_64__
|
||||
int rc;
|
||||
struct timeval wt, t1, t2, td;
|
||||
if (rem) sys_gettimeofday_xnu(&t1, 0, 0);
|
||||
wt = timespec_totimeval(*req); // rounds up
|
||||
rc = sys_select(0, 0, 0, 0, &wt);
|
||||
if (rem && rc == -1 && errno == EINTR) {
|
||||
sys_gettimeofday_xnu(&t2, 0, 0);
|
||||
td = timeval_sub(t2, t1);
|
||||
if (timeval_cmp(td, wt) >= 0) {
|
||||
rem->tv_sec = 0;
|
||||
rem->tv_nsec = 0;
|
||||
} else {
|
||||
*rem = timeval_totimespec(timeval_sub(wt, td));
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
#else
|
||||
return _sysret(__syslib->__nanosleep(req, rem));
|
||||
#endif
|
||||
}
|
|
@ -37,11 +37,11 @@
|
|||
* @norestart
|
||||
*/
|
||||
int nanosleep(const struct timespec *req, struct timespec *rem) {
|
||||
int rc;
|
||||
if (!(rc = clock_nanosleep(CLOCK_REALTIME, 0, req, rem))) {
|
||||
errno_t err;
|
||||
if (!(err = clock_nanosleep(CLOCK_REALTIME, 0, req, rem))) {
|
||||
return 0;
|
||||
} else {
|
||||
errno = rc;
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
|
@ -35,6 +36,7 @@
|
|||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/byhandlefileinformation.h"
|
||||
#include "libc/nt/struct/genericmapping.h"
|
||||
#include "libc/nt/struct/privilegeset.h"
|
||||
#include "libc/nt/struct/securitydescriptor.h"
|
||||
|
@ -62,6 +64,7 @@ textwindows int ntaccesscheck(const char16_t *pathname, uint32_t flags) {
|
|||
struct NtGenericMapping mapping;
|
||||
struct NtPrivilegeSet privileges;
|
||||
uint32_t secsize, granted, privsize;
|
||||
struct NtByHandleFileInformation wst;
|
||||
int64_t hToken, hImpersonatedToken, hFile;
|
||||
intptr_t buffer[1024 / sizeof(intptr_t)];
|
||||
if (flags & X_OK) flags |= R_OK;
|
||||
|
@ -100,7 +103,9 @@ textwindows int ntaccesscheck(const char16_t *pathname, uint32_t flags) {
|
|||
0, kNtOpenExisting,
|
||||
kNtFileAttributeNormal | kNtFileFlagBackupSemantics,
|
||||
0)) != -1) {
|
||||
if (IsWindowsExecutable(hFile)) {
|
||||
unassert(GetFileInformationByHandle(hFile, &wst));
|
||||
if ((wst.dwFileAttributes & kNtFileAttributeDirectory) ||
|
||||
IsWindowsExecutable(hFile)) {
|
||||
rc = 0;
|
||||
} else {
|
||||
rc = eacces();
|
||||
|
|
|
@ -32,7 +32,6 @@
|
|||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @restartable
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int open(const char *file, int flags, ...) {
|
||||
|
|
|
@ -168,7 +168,6 @@
|
|||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @restartable
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int openat(int dirfd, const char *path, int flags, ...) {
|
||||
|
|
|
@ -2297,7 +2297,6 @@ static privileged void AppendPledge(struct Filter *f, //
|
|||
* @param ipromises is inverted integer bitmask of pledge() promises
|
||||
* @return 0 on success, or negative error number on error
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
privileged int sys_pledge_linux(unsigned long ipromises, int mode) {
|
||||
|
|
|
@ -234,7 +234,6 @@
|
|||
* @raise ENOSYS if `pledge(0, 0)` was used and security is not possible
|
||||
* @raise EINVAL if `execpromises` on Linux isn't a subset of `promises`
|
||||
* @raise EINVAL if `promises` allows exec and `execpromises` is null
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int pledge(const char *promises, const char *execpromises) {
|
||||
|
|
|
@ -62,7 +62,6 @@
|
|||
* @raise EINTR if signal was delivered
|
||||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @norestart
|
||||
*/
|
||||
int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
|
||||
|
|
|
@ -46,7 +46,6 @@ int sys_fadvise_netbsd(int, int, int64_t, int64_t, int) asm("sys_fadvise");
|
|||
* @raise ENOTSUP if `fd` is a /zip file
|
||||
* @raise ENOSYS on XNU and OpenBSD
|
||||
* @returnserrno
|
||||
* @threadsafe
|
||||
*/
|
||||
errno_t posix_fadvise(int fd, int64_t offset, int64_t len, int advice) {
|
||||
int rc, e = errno;
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
*
|
||||
* @return 0 on success, or errno on error
|
||||
* @returnserrno
|
||||
* @threadsafe
|
||||
*/
|
||||
errno_t posix_madvise(void *addr, uint64_t len, int advice) {
|
||||
int rc, e = errno;
|
||||
|
|
|
@ -57,7 +57,6 @@
|
|||
* @raise EINTR if signal was delivered
|
||||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @norestart
|
||||
*/
|
||||
int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
|
||||
|
|
|
@ -52,7 +52,6 @@
|
|||
* @see pwrite(), write()
|
||||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
ssize_t pread(int fd, void *buf, size_t size, int64_t offset) {
|
||||
|
|
|
@ -46,7 +46,6 @@
|
|||
* @see pread(), write()
|
||||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
ssize_t pwrite(int fd, const void *buf, size_t size, int64_t offset) {
|
||||
|
|
|
@ -53,8 +53,6 @@
|
|||
#include "libc/thread/tls.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
|
||||
|
||||
static const struct {
|
||||
int vk;
|
||||
int normal_str;
|
||||
|
@ -578,7 +576,7 @@ textwindows ssize_t sys_read_nt_impl(int fd, void *data, size_t size,
|
|||
goto BlockingOperation;
|
||||
}
|
||||
}
|
||||
__imp_CloseHandle(overlap.hEvent); // __imp_ to avoid log noise
|
||||
CloseHandle(overlap.hEvent);
|
||||
|
||||
if (!pwriting && seekable) {
|
||||
if (ok) f->pointer = offset + got;
|
||||
|
|
|
@ -51,7 +51,6 @@ int __ensurefds_unlocked(int fd) {
|
|||
/**
|
||||
* Grows file descriptor array memory if needed.
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int __ensurefds(int fd) {
|
||||
__fds_lock();
|
||||
|
@ -89,7 +88,6 @@ int __reservefd_unlocked(int start) {
|
|||
/**
|
||||
* Finds open file descriptor slot.
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int __reservefd(int start) {
|
||||
int fd;
|
||||
|
|
|
@ -85,6 +85,14 @@ privileged void __siginfo2cosmo(struct siginfo *si,
|
|||
notpossible;
|
||||
}
|
||||
|
||||
// Turn BUS_OBJERR into BUS_ADRERR for consistency with Linux.
|
||||
// See test/libc/calls/sigbus_test.c
|
||||
if (IsFreebsd() || IsOpenbsd()) {
|
||||
if (si_signo == 10 && si_code == 3) {
|
||||
si_code = 2;
|
||||
}
|
||||
}
|
||||
|
||||
*si = (struct siginfo){0};
|
||||
si->si_signo = si_signo;
|
||||
si->si_errno = si_errno;
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
COSMOPOLITAN_C_START_
|
||||
|
||||
extern int __vforked;
|
||||
extern bool __time_critical;
|
||||
extern pthread_mutex_t __fds_lock_obj;
|
||||
extern unsigned __sighandrvas[NSIG + 1];
|
||||
extern unsigned __sighandflags[NSIG + 1];
|
||||
|
|
|
@ -35,3 +35,5 @@ int statvfs(const char *path, struct statvfs *sv) {
|
|||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__strong_reference(statvfs, statvfs64);
|
||||
|
|
|
@ -16,6 +16,12 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/clock_gettime.internal.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
clock_gettime_f *__clock_gettime = __clock_gettime_init;
|
||||
/**
|
||||
* Changes time, the old fashioned way.
|
||||
*/
|
||||
int stime(const int64_t *t) {
|
||||
return settimeofday(&(struct timeval){*t}, 0);
|
||||
}
|
|
@ -10,18 +10,18 @@ int __sys_clock_nanosleep(int, int, const struct timespec *, struct timespec *);
|
|||
int __sys_utimensat(int, const char *, const struct timespec[2], int);
|
||||
int __utimens(int, const char *, const struct timespec[2], int);
|
||||
int sys_clock_getres(int, struct timespec *);
|
||||
int sys_clock_settime(int, const struct timespec *);
|
||||
int sys_clock_gettime(int, struct timespec *);
|
||||
int sys_clock_gettime_nt(int, struct timespec *);
|
||||
int sys_clock_gettime_m1(int, struct timespec *);
|
||||
int sys_clock_gettime_mono(struct timespec *);
|
||||
int sys_clock_gettime_nt(int, struct timespec *);
|
||||
int sys_clock_gettime_xnu(int, struct timespec *);
|
||||
int sys_clock_nanosleep_nt(int, int, const struct timespec *, struct timespec *);
|
||||
int sys_clock_nanosleep_openbsd(int, int, const struct timespec *, struct timespec *);
|
||||
int sys_clock_nanosleep_xnu(int, int, const struct timespec *, struct timespec *);
|
||||
int sys_clock_settime(int, const struct timespec *);
|
||||
int sys_futimens(int, const struct timespec[2]);
|
||||
int sys_nanosleep(const struct timespec *, struct timespec *);
|
||||
int sys_nanosleep_nt(const struct timespec *, struct timespec *);
|
||||
int sys_nanosleep_xnu(const struct timespec *, struct timespec *);
|
||||
int sys_sem_timedwait(int64_t, const struct timespec *);
|
||||
int sys_utimensat(int, const char *, const struct timespec[2], int);
|
||||
int sys_utimensat_nt(int, const char *, const struct timespec[2], int);
|
||||
|
|
|
@ -6,16 +6,11 @@
|
|||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
axdx_t sys_gettimeofday(struct timeval *, struct timezone *, void *);
|
||||
int sys_settimeofday(const struct timeval *, const struct timezone *);
|
||||
int sys_futimes(int, const struct timeval *);
|
||||
int sys_lutimes(const char *, const struct timeval *);
|
||||
int sys_utimes(const char *, const struct timeval *);
|
||||
axdx_t sys_gettimeofday_m1(struct timeval *, struct timezone *, void *);
|
||||
axdx_t sys_gettimeofday_xnu(struct timeval *, struct timezone *, void *);
|
||||
axdx_t sys_gettimeofday_nt(struct timeval *, struct timezone *, void *);
|
||||
int sys_utimes_nt(const char *, const struct timeval[2]);
|
||||
axdx_t sys_gettimeofday_metal(struct timeval *, struct timezone *, void *);
|
||||
|
||||
const char *DescribeTimeval(char[45], int, const struct timeval *);
|
||||
#define DescribeTimeval(rc, ts) DescribeTimeval(alloca(45), rc, ts)
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
//
|
||||
// @return 0 on success, or -1 w/ errno
|
||||
// @returnstwice
|
||||
// @threadsafe
|
||||
.ftrace1
|
||||
swapcontext:
|
||||
.ftrace2
|
||||
|
|
|
@ -6,6 +6,11 @@ COSMOPOLITAN_C_START_
|
|||
│ cosmopolitan § syscalls » system five » structless support ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
long __syscall2(long, long, int);
|
||||
int __syscall2i(long, long, int) asm("__syscall2");
|
||||
long __syscall3(long, long, long, int);
|
||||
int __syscall3i(long, long, long, int) asm("__syscall3");
|
||||
|
||||
bool __is_linux_2_6_23(void);
|
||||
bool32 sys_isatty_metal(int);
|
||||
int __fixupnewfd(int, int);
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_SYSPARAM_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_SYSPARAM_H_
|
||||
|
||||
#define MAXSYMLINKS 20
|
||||
#define MAXHOSTNAMELEN 64
|
||||
#define MAXNAMLEN 255
|
||||
#define MAXPATHLEN PATH_MAX
|
||||
#define NBBY 8
|
||||
#define NGROUPS 32
|
||||
#define CANBSIZ 255
|
||||
#define NOFILE 256
|
||||
#define NCARGS 131072
|
||||
#define DEV_BSIZE 512
|
||||
#define NOGROUP (-1)
|
||||
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define __bitop(x, i, o) ((x)[(i) / 8] o(1 << (i) % 8))
|
||||
#define setbit(x, i) __bitop(x, i, |=)
|
||||
#define clrbit(x, i) __bitop(x, i, &= ~)
|
||||
#define isset(x, i) __bitop(x, i, &)
|
||||
#define isclr(x, i) !isset(x, i)
|
||||
|
||||
#undef roundup
|
||||
#define roundup(n, d) (howmany(n, d) * (d))
|
||||
#define powerof2(n) !(((n)-1) & (n))
|
||||
#define howmany(n, d) (((n) + ((d)-1)) / (d))
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_SYSPARAM_H_ */
|
|
@ -31,6 +31,6 @@
|
|||
*/
|
||||
struct timespec timespec_real(void) {
|
||||
struct timespec ts;
|
||||
npassert(!clock_gettime(CLOCK_REALTIME, &ts));
|
||||
unassert(!clock_gettime(CLOCK_REALTIME, &ts));
|
||||
return ts;
|
||||
}
|
||||
|
|
|
@ -71,7 +71,6 @@ int _mkstemp(char *, int);
|
|||
* @see tmpfile() for stdio version
|
||||
* @cancellationpoint
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
int tmpfd(void) {
|
||||
|
|
|
@ -61,7 +61,6 @@
|
|||
* @raise ENOSYS on bare metal
|
||||
* @cancellationpoint
|
||||
* @see ftruncate()
|
||||
* @threadsafe
|
||||
*/
|
||||
int truncate(const char *path, int64_t length) {
|
||||
int rc;
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sysparam.h"
|
||||
#include "libc/stdio/sysparam.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/paths.h"
|
||||
|
|
|
@ -85,7 +85,6 @@ static errno_t ttyname_linux(int fd, char *buf, size_t size) {
|
|||
* @return 0 on success, or error number on error
|
||||
* @raise ERANGE if `size` was too small
|
||||
* @returnserrno
|
||||
* @threadsafe
|
||||
*/
|
||||
errno_t ttyname_r(int fd, char *buf, size_t size) {
|
||||
errno_t e, res;
|
||||
|
|
|
@ -43,7 +43,6 @@ static int __contextmask(const sigset_t *opt_set, sigset_t *opt_out_oldset) {
|
|||
* @see swapcontext()
|
||||
* @see makecontext()
|
||||
* @see getcontext()
|
||||
* @threadsafe
|
||||
*/
|
||||
int setcontext(const ucontext_t *uc) {
|
||||
if (__contextmask(&uc->uc_sigmask, 0)) return -1;
|
||||
|
|
|
@ -460,7 +460,6 @@ int sys_unveil_linux(const char *path, const char *permissions) {
|
|||
* @note on Linux this function requires Linux Kernel 5.13+ and version 6.2+
|
||||
* to properly support truncation operations
|
||||
* @see [1] https://docs.kernel.org/userspace-api/landlock.html
|
||||
* @threadsafe
|
||||
*/
|
||||
int unveil(const char *path, const char *permissions) {
|
||||
int e, rc;
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
* @return 0 on success, or -1 w/ errno
|
||||
* @see utimensat() for modern version
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int utime(const char *path, const struct utimbuf *times) {
|
||||
struct timeval tv[2];
|
||||
|
|
|
@ -17,8 +17,9 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/nt/createfile.h"
|
||||
#include "libc/nt/enum/accessmask.h"
|
||||
#include "libc/nt/enum/creationdisposition.h"
|
||||
|
|
|
@ -51,7 +51,6 @@
|
|||
* @raise ENOTSUP on XNU or RHEL5 when `dirfd` isn't `AT_FDCWD`
|
||||
* @raise ENOSYS on bare metal
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int utimensat(int dirfd, const char *path, const struct timespec ts[2],
|
||||
int flags) {
|
||||
|
|
|
@ -32,7 +32,6 @@
|
|||
* @note truncates to second precision on rhel5
|
||||
* @see utimensat() for modern version
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
int utimes(const char *path, const struct timeval tv[2]) {
|
||||
int rc;
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "libc/elf/struct/verdaux.h"
|
||||
#include "libc/elf/struct/verdef.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/intrin/getauxval.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -55,7 +56,7 @@ static inline int CheckDsoSymbolVersion(Elf64_Verdef *vd, int sym,
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns address of vDSO function.
|
||||
* Returns address of "Virtual Dynamic Shared Object" function on Linux.
|
||||
*/
|
||||
void *__vdsosym(const char *version, const char *name) {
|
||||
void *p;
|
||||
|
@ -64,23 +65,19 @@ void *__vdsosym(const char *version, const char *name) {
|
|||
Elf64_Phdr *phdr;
|
||||
char *strtab = 0;
|
||||
size_t *dyn, base;
|
||||
unsigned long *ap;
|
||||
Elf64_Sym *symtab = 0;
|
||||
uint16_t *versym = 0;
|
||||
Elf_Symndx *hashtab = 0;
|
||||
Elf64_Verdef *verdef = 0;
|
||||
struct AuxiliaryValue av;
|
||||
|
||||
for (ehdr = 0, ap = __auxv; ap[0]; ap += 2) {
|
||||
if (ap[0] == AT_SYSINFO_EHDR) {
|
||||
ehdr = (void *)ap[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ehdr || READ32LE(ehdr->e_ident) != READ32LE("\177ELF")) {
|
||||
KERNTRACE("__vdsosym() → AT_SYSINFO_EHDR ELF not found");
|
||||
av = __getauxval(AT_SYSINFO_EHDR);
|
||||
if (!av.isfound) {
|
||||
KERNTRACE("__vdsosym() → missing AT_SYSINFO_EHDR");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ehdr = (void *)av.value;
|
||||
phdr = (void *)((char *)ehdr + ehdr->e_phoff);
|
||||
for (base = -1, dyn = 0, i = 0; i < ehdr->e_phnum;
|
||||
i++, phdr = (void *)((char *)phdr + ehdr->e_phentsize)) {
|
||||
|
|
|
@ -64,6 +64,7 @@ typedef uint32_t nlink_t; /* uint16_t on xnu */
|
|||
#define fstat64 fstat
|
||||
#define fstatat64 fstatat
|
||||
#define fstatfs64 fstatfs
|
||||
#define fstatvfs64 fstatvfs
|
||||
#define getrlimit64 getrlimit
|
||||
#define ino64_t ino_t
|
||||
#define lockf64 lockf
|
||||
|
@ -83,6 +84,7 @@ typedef uint32_t nlink_t; /* uint16_t on xnu */
|
|||
#define setrlimit64 setrlimit
|
||||
#define stat64 stat
|
||||
#define statfs64 statfs
|
||||
#define statvfs64 statvfs
|
||||
#define versionsort64 versionsort
|
||||
#endif
|
||||
|
||||
|
|
|
@ -17,12 +17,27 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/events.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/overlapped.h"
|
||||
|
||||
// checks if file should be considered an executable on windows
|
||||
textwindows int IsWindowsExecutable(int64_t handle) {
|
||||
|
||||
// read first two bytes of file
|
||||
// access() and stat() aren't cancelation points
|
||||
char buf[2];
|
||||
uint32_t got;
|
||||
return ReadFile(handle, buf, 2, &got, 0) && got == 2 &&
|
||||
struct NtOverlapped overlap = {.hEvent = CreateEvent(0, 0, 0, 0)};
|
||||
bool ok = (ReadFile(handle, buf, 2, 0, &overlap) ||
|
||||
GetLastError() == kNtErrorIoPending) &&
|
||||
GetOverlappedResult(handle, &overlap, &got, true);
|
||||
CloseHandle(overlap.hEvent);
|
||||
|
||||
// it's an executable if it starts with `MZ` or `#!`
|
||||
return ok && got == 2 && //
|
||||
((buf[0] == 'M' && buf[1] == 'Z') || //
|
||||
(buf[0] == '#' && buf[1] == '!'));
|
||||
}
|
||||
|
|
|
@ -47,8 +47,6 @@
|
|||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
|
||||
|
||||
static bool IsMouseModeCommand(int x) {
|
||||
return x == 1000 || // SET_VT200_MOUSE
|
||||
x == 1002 || // SET_BTN_EVENT_MOUSE
|
||||
|
@ -202,7 +200,7 @@ static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size,
|
|||
goto BlockingOperation;
|
||||
}
|
||||
}
|
||||
__imp_CloseHandle(overlap.hEvent); // __imp_ to avoid log noise
|
||||
CloseHandle(overlap.hEvent);
|
||||
|
||||
if (seekable && !pwriting) {
|
||||
if (ok) f->pointer = offset + sent;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue