mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Add sysctlbyname() for MacOS
This commit is contained in:
parent
5c6877b02b
commit
181cd4cbe8
21 changed files with 193 additions and 35 deletions
|
@ -237,7 +237,9 @@ int sys_munlock(const void *, size_t) libcesque;
|
|||
int sys_munlockall(void) libcesque;
|
||||
int sys_personality(uint64_t) libcesque;
|
||||
int sys_ptrace(int, ...) libcesque;
|
||||
int sys_sysctl(const int *, unsigned, void *, size_t *, void *, size_t);
|
||||
int sysctl(int *, unsigned, void *, size_t *, void *, size_t) libcesque;
|
||||
int sysctlbyname(const char *, void *, size_t *, void *, size_t) libcesque;
|
||||
int sysctlnametomib(const char *, int *, size_t *) libcesque;
|
||||
int tmpfd(void) libcesque;
|
||||
int touch(const char *, unsigned) libcesque;
|
||||
int unveil(const char *, const char *) libcesque;
|
||||
|
|
|
@ -53,7 +53,7 @@ static dontinline int __clk_tck_init(void) {
|
|||
cmd[0] = 1; // CTL_KERN
|
||||
cmd[1] = 12; // KERN_CLOCKRATE
|
||||
len = sizeof(clock);
|
||||
if (sys_sysctl(cmd, 2, &clock, &len, NULL, 0) != -1) {
|
||||
if (sysctl(cmd, 2, &clock, &len, NULL, 0) != -1) {
|
||||
x = clock.hz;
|
||||
} else {
|
||||
x = -1;
|
||||
|
|
|
@ -67,7 +67,7 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
|
|||
struct timeval x;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_KERN, KERN_BOOTTIME};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
if (sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
return -1;
|
||||
if (ts)
|
||||
*ts = timeval_totimespec(timeval_sub(timeval_real(), x));
|
||||
|
|
|
@ -51,7 +51,7 @@ static int __get_cpu_count_bsd(void) {
|
|||
} else {
|
||||
cmd[1] = HW_NCPU;
|
||||
}
|
||||
if (!sys_sysctl(cmd, 2, &c, &n, 0, 0)) {
|
||||
if (!sysctl(cmd, 2, &c, &n, 0, 0)) {
|
||||
return c;
|
||||
} else {
|
||||
return -1;
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
|
||||
int gethostname_bsd(char *name, size_t len, int kind) {
|
||||
int cmd[2] = {CTL_KERN, kind};
|
||||
if (sys_sysctl(cmd, 2, name, &len, 0, 0) != -1) {
|
||||
if (sysctl(cmd, 2, name, &len, 0, 0) != -1) {
|
||||
return 0;
|
||||
} else {
|
||||
if (errno == ENOMEM) {
|
||||
|
|
|
@ -64,7 +64,7 @@ int getloadavg(double *a, int n) {
|
|||
struct loadavg loadinfo;
|
||||
int mib[2] = {CTL_VM, VM_LOADAVG};
|
||||
size = sizeof(loadinfo);
|
||||
if ((rc = sys_sysctl(mib, 2, &loadinfo, &size, 0, 0)) != -1) {
|
||||
if ((rc = sysctl(mib, 2, &loadinfo, &size, 0, 0)) != -1) {
|
||||
for (i = 0; i < n; i++) {
|
||||
a[i] = (double)loadinfo.ldavg[i] / loadinfo.fscale;
|
||||
}
|
||||
|
|
|
@ -195,7 +195,7 @@ static inline void InitProgramExecutableNameImpl(void) {
|
|||
cmd[2] = KERN_PROC_PATHNAME_NETBSD;
|
||||
}
|
||||
cmd[3] = -1; // current process
|
||||
if (sys_sysctl(cmd, ARRAYLEN(cmd), b, &n, 0, 0) != -1) {
|
||||
if (sysctl(cmd, ARRAYLEN(cmd), b, &n, 0, 0) != -1) {
|
||||
if (!OldApeLoader(b)) {
|
||||
goto UseBuf;
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ static void GetRandomArnd(char *p, size_t n) {
|
|||
cmd[0] = 1; // CTL_KERN
|
||||
cmd[1] = IsFreebsd() ? 37 : 81; // KERN_ARND
|
||||
unassert((m = n) <= 256);
|
||||
if (sys_sysctl(cmd, 2, p, &n, 0, 0) == -1)
|
||||
if (sysctl(cmd, 2, p, &n, 0, 0) == -1)
|
||||
notpossible;
|
||||
if (m != n)
|
||||
notpossible;
|
||||
|
|
33
libc/calls/sysctl.c
Normal file
33
libc/calls/sysctl.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 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/calls.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
|
||||
int sys_sysctl(int *, unsigned, void *, size_t *, void *, size_t) libcesque;
|
||||
|
||||
int sysctl(int *name, unsigned namelen, void *oldp, size_t *oldlenp, void *newp,
|
||||
size_t newlen) {
|
||||
if (__syslib && __syslib->__version >= 10) {
|
||||
return _sysret(
|
||||
__syslib->__sysctl(name, namelen, oldp, oldlenp, newp, newlen));
|
||||
} else {
|
||||
return sys_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
|
||||
}
|
||||
}
|
31
libc/calls/sysctlbyname.c
Normal file
31
libc/calls/sysctlbyname.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 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/calls.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
|
||||
size_t newlen) {
|
||||
if (__syslib && __syslib->__version >= 10) {
|
||||
return _sysret(__syslib->__sysctlbyname(name, oldp, oldlenp, newp, newlen));
|
||||
} else {
|
||||
return enosys();
|
||||
}
|
||||
}
|
30
libc/calls/sysctlnametomib.c
Normal file
30
libc/calls/sysctlnametomib.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 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/calls.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
int sysctlnametomib(const char *name, int *mibp, size_t *sizep) {
|
||||
if (__syslib && __syslib->__version >= 10) {
|
||||
return _sysret(__syslib->__sysctlnametomib(name, mibp, sizep));
|
||||
} else {
|
||||
return enosys();
|
||||
}
|
||||
}
|
|
@ -46,7 +46,7 @@ static int64_t GetUptime(void) {
|
|||
struct timeval x;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_KERN, KERN_BOOTTIME};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
if (sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
return 0;
|
||||
return timespec_real().tv_sec - x.tv_sec;
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ static int64_t GetPhysmem(void) {
|
|||
uint64_t x = 0;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_HW, HW_PHYSMEM};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
if (sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
return 0;
|
||||
return x;
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ static void GetLoads(uint64_t loads[3]) {
|
|||
struct loadavg loadinfo;
|
||||
int mib[2] = {CTL_VM, VM_LOADAVG};
|
||||
size = sizeof(loadinfo);
|
||||
if (sys_sysctl(mib, 2, &loadinfo, &size, 0, 0) != -1) {
|
||||
if (sysctl(mib, 2, &loadinfo, &size, 0, 0) != -1) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
loads[i] = (double)loadinfo.ldavg[i] / loadinfo.fscale * 65536;
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ static void GetBsdStr(int c0, int c1, char *s) {
|
|||
size_t n = SYS_NMLN;
|
||||
int cmd[2] = {c0, c1};
|
||||
bzero(s, n), --n;
|
||||
sys_sysctl(cmd, 2, s, &n, NULL, 0);
|
||||
sysctl(cmd, 2, s, &n, NULL, 0);
|
||||
errno = e;
|
||||
// sysctl kern.version is too verbose for uname
|
||||
if ((p = strchr(s, '\n'))) {
|
||||
|
|
|
@ -254,10 +254,28 @@ static relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
|||
kprintf("\n");
|
||||
}
|
||||
|
||||
static inline void SpinLock(atomic_uint *lock) {
|
||||
int x;
|
||||
for (;;) {
|
||||
x = atomic_exchange_explicit(lock, 1, memory_order_acquire);
|
||||
if (!x)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void SpinUnlock(atomic_uint *lock) {
|
||||
atomic_store_explicit(lock, 0, memory_order_release);
|
||||
}
|
||||
|
||||
relegated void __oncrash(int sig, struct siginfo *si, void *arg) {
|
||||
static atomic_uint lock;
|
||||
BLOCK_CANCELATION;
|
||||
SpinLock(&lock);
|
||||
int err = errno;
|
||||
__restore_tty();
|
||||
ShowCrashReport(err, sig, si, arg);
|
||||
SpinUnlock(&lock);
|
||||
ALLOW_CANCELATION;
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/assert.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/blockcancel.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/aarch64.internal.h"
|
||||
|
@ -32,6 +33,7 @@
|
|||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/cxxabi.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/describebacktrace.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
|
@ -377,10 +379,25 @@ static relegated void __oncrash_impl(int sig, struct siginfo *si,
|
|||
klog(b->p, MIN(b->i, b->n));
|
||||
}
|
||||
|
||||
static inline void SpinLock(atomic_uint *lock) {
|
||||
int x;
|
||||
for (;;) {
|
||||
x = atomic_exchange_explicit(lock, 1, memory_order_acquire);
|
||||
if (!x)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void SpinUnlock(atomic_uint *lock) {
|
||||
atomic_store_explicit(lock, 0, memory_order_release);
|
||||
}
|
||||
|
||||
relegated void __oncrash(int sig, struct siginfo *si, void *arg) {
|
||||
ucontext_t *ctx = arg;
|
||||
static atomic_uint lock;
|
||||
BLOCK_CANCELATION;
|
||||
__oncrash_impl(sig, si, ctx);
|
||||
SpinLock(&lock);
|
||||
__oncrash_impl(sig, si, arg);
|
||||
SpinUnlock(&lock);
|
||||
ALLOW_CANCELATION;
|
||||
}
|
||||
|
||||
|
|
|
@ -74,9 +74,9 @@ char *GetInterpreterExecutableName(char *p, size_t n) {
|
|||
cmd[2] = 12; // KERN_PROC_PATHNAME
|
||||
} else { //
|
||||
cmd[2] = 5; // KERN_PROC_PATHNAME
|
||||
} //
|
||||
cmd[3] = -1; // current process
|
||||
if (sys_sysctl(cmd, ARRAYLEN(cmd), p, &n, 0, 0) != -1) {
|
||||
} //
|
||||
cmd[3] = -1; // current process
|
||||
if (sysctl(cmd, ARRAYLEN(cmd), p, &n, 0, 0) != -1) {
|
||||
errno = e;
|
||||
return p;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ long __get_sysctl(int x, int y) {
|
|||
int value;
|
||||
int mib[2] = {x, y};
|
||||
size_t len = sizeof(value);
|
||||
if (sys_sysctl(mib, 2, &value, &len, 0, 0) != -1) {
|
||||
if (sysctl(mib, 2, &value, &len, 0, 0) != -1) {
|
||||
return value;
|
||||
} else {
|
||||
return -1;
|
||||
|
|
|
@ -12,7 +12,7 @@ COSMOPOLITAN_C_START_
|
|||
|
||||
#define SYSLIB_MAGIC ('s' | 'l' << 8 | 'i' << 16 | 'b' << 24)
|
||||
|
||||
#define SYSLIB_VERSION 9 /* sync with ape/ape-m1.c */
|
||||
#define SYSLIB_VERSION 10 /* sync with ape/ape-m1.c */
|
||||
|
||||
/* if this number increases, then everyone on macos arm will need to
|
||||
reinstall ape loader in order to run newer ape binaries so please
|
||||
|
@ -82,6 +82,9 @@ struct Syslib {
|
|||
char *(*__dlerror)(void);
|
||||
/* v9 (2024-01-31) */
|
||||
int (*__pthread_cpu_number_np)(size_t *);
|
||||
long (*__sysctl)(int *, unsigned, void *, size_t *, void *, size_t);
|
||||
long (*__sysctlbyname)(const char *, void *, size_t *, void *, size_t);
|
||||
long (*__sysctlnametomib)(const char *, int *, size_t *);
|
||||
};
|
||||
|
||||
extern struct Syslib *__syslib;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue