authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-06-15 14:48:20-04:00
committergravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-06-15 14:48:20-04:00
log40fc71bf710ae3704c85c718636a190a5106c0cf
tree96d348047db31367e1bcb86a754546ab0398f252
parent64ddba955a1f98ab77cfb39844a09f472ebc4609
signaturelock-open Commit is signed but in an unrecognized format.

openbsd: fix std.c.getdents and debitrot

- fix getdents return type usize → c_int - special-case process.zig to use sysctl instead of sysctlbyname - use struct/field pattern for sysctl HW_* constants

4 files changed, 52 insertions(+), 28 deletions(-)

lib/std/Thread.zig+1-1
......@@ -624,7 +624,7 @@ const PosixThreadImpl = struct {
624624 .openbsd => {
625625 var count: c_int = undefined;
626626 var count_size: usize = @sizeOf(c_int);
627 const mib = [_]c_int{ os.CTL.HW, os.system.HW_NCPUONLINE };
627 const mib = [_]c_int{ os.CTL.HW, os.system.HW.NCPUONLINE };
628628 os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {
629629 error.NameTooLong, error.UnknownName => unreachable,
630630 else => |e| return e,
lib/std/c/openbsd.zig+29-26
......@@ -16,7 +16,7 @@ pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
1616pub extern "c" fn getthrid() pid_t;
1717pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
1818
19pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
19pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int;
2020pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
2121
2222pub const pthread_mutex_t = extern struct {
......@@ -1606,31 +1606,34 @@ pub const KERN = struct {
16061606 pub const PROC_NENV = 4;
16071607};
16081608
1609pub const HW_MACHINE = 1;
1610pub const HW_MODEL = 2;
1611pub const HW_NCPU = 3;
1612pub const HW_BYTEORDER = 4;
1613pub const HW_PHYSMEM = 5;
1614pub const HW_USERMEM = 6;
1615pub const HW_PAGESIZE = 7;
1616pub const HW_DISKNAMES = 8;
1617pub const HW_DISKSTATS = 9;
1618pub const HW_DISKCOUNT = 10;
1619pub const HW_SENSORS = 11;
1620pub const HW_CPUSPEED = 12;
1621pub const HW_SETPERF = 13;
1622pub const HW_VENDOR = 14;
1623pub const HW_PRODUCT = 15;
1624pub const HW_VERSION = 16;
1625pub const HW_SERIALNO = 17;
1626pub const HW_UUID = 18;
1627pub const HW_PHYSMEM64 = 19;
1628pub const HW_USERMEM64 = 20;
1629pub const HW_NCPUFOUND = 21;
1630pub const HW_ALLOWPOWERDOWN = 22;
1631pub const HW_PERFPOLICY = 23;
1632pub const HW_SMT = 24;
1633pub const HW_NCPUONLINE = 25;
1609pub const HW = struct {
1610 pub const MACHINE = 1;
1611 pub const MODEL = 2;
1612 pub const NCPU = 3;
1613 pub const BYTEORDER = 4;
1614 pub const PHYSMEM = 5;
1615 pub const USERMEM = 6;
1616 pub const PAGESIZE = 7;
1617 pub const DISKNAMES = 8;
1618 pub const DISKSTATS = 9;
1619 pub const DISKCOUNT = 10;
1620 pub const SENSORS = 11;
1621 pub const CPUSPEED = 12;
1622 pub const SETPERF = 13;
1623 pub const VENDOR = 14;
1624 pub const PRODUCT = 15;
1625 pub const VERSION = 16;
1626 pub const SERIALNO = 17;
1627 pub const UUID = 18;
1628 pub const PHYSMEM64 = 19;
1629 pub const USERMEM64 = 20;
1630 pub const NCPUFOUND = 21;
1631 pub const ALLOWPOWERDOWN = 22;
1632 pub const PERFPOLICY = 23;
1633 pub const SMT = 24;
1634 pub const NCPUONLINE = 25;
1635 pub const POWER = 26;
1636};
16341637
16351638/// TODO refines if necessary
16361639pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {
lib/std/os.zig+4
......@@ -87,6 +87,10 @@ pub const F = system.F;
8787pub const FD_CLOEXEC = system.FD_CLOEXEC;
8888pub const Flock = system.Flock;
8989pub const HOST_NAME_MAX = system.HOST_NAME_MAX;
90pub const HW = switch (builtin.os.tag) {
91 .openbsd => system.HW,
92 else => .{},
93};
9094pub const IFNAMESIZE = system.IFNAMESIZE;
9195pub const IOV_MAX = system.IOV_MAX;
9296pub const IPPROTO = system.IPPROTO;
lib/std/process.zig+18-1
......@@ -1163,7 +1163,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
11631163 .linux => {
11641164 return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;
11651165 },
1166 .freebsd, .netbsd, .openbsd, .dragonfly, .macos => {
1166 .freebsd, .netbsd, .dragonfly, .macos => {
11671167 var physmem: c_ulong = undefined;
11681168 var len: usize = @sizeOf(c_ulong);
11691169 const name = switch (builtin.os.tag) {
......@@ -1177,6 +1177,23 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
11771177 };
11781178 return @intCast(usize, physmem);
11791179 },
1180 .openbsd => {
1181 const mib: [2]c_int = [_]c_int{
1182 std.os.CTL.HW,
1183 std.os.HW.PHYSMEM64,
1184 };
1185 var physmem: i64 = undefined;
1186 var len: usize = @sizeOf(@TypeOf(physmem));
1187 std.os.sysctl(&mib, &physmem, &len, null, 0) catch |err| switch (err) {
1188 error.NameTooLong => unreachable, // constant, known good value
1189 error.PermissionDenied => unreachable, // only when setting values,
1190 error.SystemResources => unreachable, // memory already on the stack
1191 error.UnknownName => unreachable, // constant, known good value
1192 else => return error.UnknownTotalSystemMemory,
1193 };
1194 assert(physmem >= 0);
1195 return @bitCast(usize, physmem);
1196 },
11801197 .windows => {
11811198 var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;
11821199 const rc = std.os.windows.ntdll.NtQuerySystemInformation(