mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-30 16:28:30 +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'))) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue