Skip to content

Commit 181cd4c

Browse files
committed
Add sysctlbyname() for MacOS
1 parent 5c6877b commit 181cd4c

21 files changed

+193
-35
lines changed

ape/ape-m1.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <string.h>
3232
#include <sys/mman.h>
3333
#include <sys/random.h>
34+
#include <sys/sysctl.h>
35+
#include <sys/types.h>
3436
#include <sys/uio.h>
3537
#include <time.h>
3638
#include <unistd.h>
@@ -39,7 +41,7 @@
3941
/* maximum path size that cosmo can take */
4042
#define PATHSIZE (PATH_MAX < 1024 ? PATH_MAX : 1024)
4143
#define SYSLIB_MAGIC ('s' | 'l' << 8 | 'i' << 16 | 'b' << 24)
42-
#define SYSLIB_VERSION 9 /* sync with libc/runtime/syslib.internal.h */
44+
#define SYSLIB_VERSION 10 /* sync with libc/runtime/syslib.internal.h */
4345

4446
struct Syslib {
4547
int magic;
@@ -106,6 +108,10 @@ struct Syslib {
106108
OPTIONAL (cosmo lib should check __syslib->version) */
107109
/* v9 (2024-01-31) */
108110
int (*pthread_cpu_number_np)(size_t *);
111+
/* v10 (2024-05-02) */
112+
long (*sysctl)(int *, u_int, void *, size_t *, void *, size_t);
113+
long (*sysctlbyname)(const char *, void *, size_t *, void *, size_t);
114+
long (*sysctlnametomib)(const char *, int *, size_t *);
109115
};
110116

111117
#define ELFCLASS32 1
@@ -148,8 +154,8 @@ struct Syslib {
148154
#define _COMM_PAGE_APRR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110)
149155
#define _COMM_PAGE_APRR_WRITE_DISABLE (_COMM_PAGE_START_ADDRESS + 0x118)
150156

151-
#define MIN(X, Y) ((Y) > (X) ? (X) : (Y))
152-
#define MAX(X, Y) ((Y) < (X) ? (X) : (Y))
157+
#define Min(X, Y) ((Y) > (X) ? (X) : (Y))
158+
#define Max(X, Y) ((Y) < (X) ? (X) : (Y))
153159

154160
#define READ32(S) \
155161
((unsigned)(255 & (S)[3]) << 030 | (unsigned)(255 & (S)[2]) << 020 | \
@@ -552,6 +558,20 @@ static long sys_pselect(int nfds, fd_set *readfds, fd_set *writefds,
552558
return sysret(pselect(nfds, readfds, writefds, errorfds, timeout, sigmask));
553559
}
554560

561+
static long sys_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
562+
void *newp, size_t newlen) {
563+
return sysret(sysctl(name, namelen, oldp, oldlenp, newp, newlen));
564+
}
565+
566+
static long sys_sysctlbyname(const char *name, void *oldp, size_t *oldlenp,
567+
void *newp, size_t newlen) {
568+
return sysret(sysctlbyname(name, oldp, oldlenp, newp, newlen));
569+
}
570+
571+
static long sys_sysctlnametomib(const char *name, int *mibp, size_t *sizep) {
572+
return sysret(sysctlnametomib(name, mibp, sizep));
573+
}
574+
555575
__attribute__((__noreturn__)) static void Spawn(const char *exe, int fd,
556576
long *sp, struct ElfEhdr *e,
557577
struct ElfPhdr *p,
@@ -596,7 +616,7 @@ __attribute__((__noreturn__)) static void Spawn(const char *exe, int fd,
596616
continue;
597617
c = p[j].p_vaddr & -pagesz;
598618
d = (p[j].p_vaddr + p[j].p_memsz + (pagesz - 1)) & -pagesz;
599-
if (MAX(a, c) < MIN(b, d)) {
619+
if (Max(a, c) < Min(b, d)) {
600620
Pexit(exe, 0, "ELF segments overlap each others virtual memory");
601621
}
602622
}
@@ -670,7 +690,7 @@ __attribute__((__noreturn__)) static void Spawn(const char *exe, int fd,
670690
a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */
671691
b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */
672692
c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */
673-
wipe = MIN(b - a, c - a);
693+
wipe = Min(b - a, c - a);
674694
if (wipe && (~prot1 & PROT_WRITE)) {
675695
prot1 = PROT_READ | PROT_WRITE;
676696
}
@@ -970,6 +990,9 @@ int main(int argc, char **argv, char **envp) {
970990
M->lib.dlclose = dlclose;
971991
M->lib.dlerror = dlerror;
972992
M->lib.pthread_cpu_number_np = pthread_cpu_number_np;
993+
M->lib.sysctl = sys_sysctl;
994+
M->lib.sysctlbyname = sys_sysctlbyname;
995+
M->lib.sysctlnametomib = sys_sysctlnametomib;
973996

974997
/* getenv("_") is close enough to at_execfn */
975998
execfn = 0;

libc/calls/calls.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ int sys_munlock(const void *, size_t) libcesque;
237237
int sys_munlockall(void) libcesque;
238238
int sys_personality(uint64_t) libcesque;
239239
int sys_ptrace(int, ...) libcesque;
240-
int sys_sysctl(const int *, unsigned, void *, size_t *, void *, size_t);
240+
int sysctl(int *, unsigned, void *, size_t *, void *, size_t) libcesque;
241+
int sysctlbyname(const char *, void *, size_t *, void *, size_t) libcesque;
242+
int sysctlnametomib(const char *, int *, size_t *) libcesque;
241243
int tmpfd(void) libcesque;
242244
int touch(const char *, unsigned) libcesque;
243245
int unveil(const char *, const char *) libcesque;

libc/calls/clktck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static dontinline int __clk_tck_init(void) {
5353
cmd[0] = 1; // CTL_KERN
5454
cmd[1] = 12; // KERN_CLOCKRATE
5555
len = sizeof(clock);
56-
if (sys_sysctl(cmd, 2, &clock, &len, NULL, 0) != -1) {
56+
if (sysctl(cmd, 2, &clock, &len, NULL, 0) != -1) {
5757
x = clock.hz;
5858
} else {
5959
x = -1;

libc/calls/clock_gettime-xnu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
6767
struct timeval x;
6868
size_t n = sizeof(x);
6969
int mib[] = {CTL_KERN, KERN_BOOTTIME};
70-
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
70+
if (sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
7171
return -1;
7272
if (ts)
7373
*ts = timeval_totimespec(timeval_sub(timeval_real(), x));

libc/calls/getcpucount.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int __get_cpu_count_bsd(void) {
5151
} else {
5252
cmd[1] = HW_NCPU;
5353
}
54-
if (!sys_sysctl(cmd, 2, &c, &n, 0, 0)) {
54+
if (!sysctl(cmd, 2, &c, &n, 0, 0)) {
5555
return c;
5656
} else {
5757
return -1;

libc/calls/gethostname-bsd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
int gethostname_bsd(char *name, size_t len, int kind) {
2626
int cmd[2] = {CTL_KERN, kind};
27-
if (sys_sysctl(cmd, 2, name, &len, 0, 0) != -1) {
27+
if (sysctl(cmd, 2, name, &len, 0, 0) != -1) {
2828
return 0;
2929
} else {
3030
if (errno == ENOMEM) {

libc/calls/getloadavg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int getloadavg(double *a, int n) {
6464
struct loadavg loadinfo;
6565
int mib[2] = {CTL_VM, VM_LOADAVG};
6666
size = sizeof(loadinfo);
67-
if ((rc = sys_sysctl(mib, 2, &loadinfo, &size, 0, 0)) != -1) {
67+
if ((rc = sysctl(mib, 2, &loadinfo, &size, 0, 0)) != -1) {
6868
for (i = 0; i < n; i++) {
6969
a[i] = (double)loadinfo.ldavg[i] / loadinfo.fscale;
7070
}

libc/calls/getprogramexecutablename.greg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static inline void InitProgramExecutableNameImpl(void) {
195195
cmd[2] = KERN_PROC_PATHNAME_NETBSD;
196196
}
197197
cmd[3] = -1; // current process
198-
if (sys_sysctl(cmd, ARRAYLEN(cmd), b, &n, 0, 0) != -1) {
198+
if (sysctl(cmd, ARRAYLEN(cmd), b, &n, 0, 0) != -1) {
199199
if (!OldApeLoader(b)) {
200200
goto UseBuf;
201201
}

libc/calls/getrandom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void GetRandomArnd(char *p, size_t n) {
7070
cmd[0] = 1; // CTL_KERN
7171
cmd[1] = IsFreebsd() ? 37 : 81; // KERN_ARND
7272
unassert((m = n) <= 256);
73-
if (sys_sysctl(cmd, 2, p, &n, 0, 0) == -1)
73+
if (sysctl(cmd, 2, p, &n, 0, 0) == -1)
7474
notpossible;
7575
if (m != n)
7676
notpossible;

libc/calls/sysctl.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2024 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/calls.h"
20+
#include "libc/calls/syscall-sysv.internal.h"
21+
#include "libc/runtime/syslib.internal.h"
22+
23+
int sys_sysctl(int *, unsigned, void *, size_t *, void *, size_t) libcesque;
24+
25+
int sysctl(int *name, unsigned namelen, void *oldp, size_t *oldlenp, void *newp,
26+
size_t newlen) {
27+
if (__syslib && __syslib->__version >= 10) {
28+
return _sysret(
29+
__syslib->__sysctl(name, namelen, oldp, oldlenp, newp, newlen));
30+
} else {
31+
return sys_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
32+
}
33+
}

0 commit comments

Comments
 (0)