authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-17 13:01:52-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-06-17 13:01:52-07:00
log96acc204b2c6080f0169ca3721c3b95a433c185b
treea7dfe641c7ffcf9932d8b749b6d55c0b3a40ae37
parentd41111d7ef531f6f55a19c56205d6d2f1134c224
parent4054d001445098cdb72a02243e13180f1fba6d79
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16052 from mikdusan/bsd-getdents

fix bad return types for BSDs getdents and debitrot openbsd

7 files changed, 57 insertions(+), 30 deletions(-)

lib/std/Thread.zig+1-1
...@@ -624,7 +624,7 @@ const PosixThreadImpl = struct {...@@ -624,7 +624,7 @@ const PosixThreadImpl = struct {
624 .openbsd => {624 .openbsd => {
625 var count: c_int = undefined;625 var count: c_int = undefined;
626 var count_size: usize = @sizeOf(c_int);626 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 };
628 os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {628 os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {
629 error.NameTooLong, error.UnknownName => unreachable,629 error.NameTooLong, error.UnknownName => unreachable,
630 else => |e| return e,630 else => |e| return e,
lib/std/c/dragonfly.zig+1-1
...@@ -9,7 +9,7 @@ pub fn _errno() *c_int {...@@ -9,7 +9,7 @@ pub fn _errno() *c_int {
9 return &errno;9 return &errno;
10}10}
1111
12pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;12pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int;
13pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;13pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
14pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;14pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
15pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;15pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
lib/std/c/freebsd.zig+1-1
...@@ -75,7 +75,7 @@ pub const _errno = __error;...@@ -75,7 +75,7 @@ pub const _errno = __error;
7575
76pub extern "c" var malloc_options: [*:0]const u8;76pub extern "c" var malloc_options: [*:0]const u8;
7777
78pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;78pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) isize;
79pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;79pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
80pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;80pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
81pub extern "c" fn getentropy(buf_ptr: [*]u8, buf_len: usize) c_int;81pub extern "c" fn getentropy(buf_ptr: [*]u8, buf_len: usize) c_int;
lib/std/c/openbsd.zig+29-26
...@@ -16,7 +16,7 @@ pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;...@@ -16,7 +16,7 @@ pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
16pub extern "c" fn getthrid() pid_t;16pub extern "c" fn getthrid() pid_t;
17pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;17pub 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;
20pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;20pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
2121
22pub const pthread_mutex_t = extern struct {22pub const pthread_mutex_t = extern struct {
...@@ -1606,31 +1606,34 @@ pub const KERN = struct {...@@ -1606,31 +1606,34 @@ pub const KERN = struct {
1606 pub const PROC_NENV = 4;1606 pub const PROC_NENV = 4;
1607};1607};
16081608
1609pub const HW_MACHINE = 1;1609pub const HW = struct {
1610pub const HW_MODEL = 2;1610 pub const MACHINE = 1;
1611pub const HW_NCPU = 3;1611 pub const MODEL = 2;
1612pub const HW_BYTEORDER = 4;1612 pub const NCPU = 3;
1613pub const HW_PHYSMEM = 5;1613 pub const BYTEORDER = 4;
1614pub const HW_USERMEM = 6;1614 pub const PHYSMEM = 5;
1615pub const HW_PAGESIZE = 7;1615 pub const USERMEM = 6;
1616pub const HW_DISKNAMES = 8;1616 pub const PAGESIZE = 7;
1617pub const HW_DISKSTATS = 9;1617 pub const DISKNAMES = 8;
1618pub const HW_DISKCOUNT = 10;1618 pub const DISKSTATS = 9;
1619pub const HW_SENSORS = 11;1619 pub const DISKCOUNT = 10;
1620pub const HW_CPUSPEED = 12;1620 pub const SENSORS = 11;
1621pub const HW_SETPERF = 13;1621 pub const CPUSPEED = 12;
1622pub const HW_VENDOR = 14;1622 pub const SETPERF = 13;
1623pub const HW_PRODUCT = 15;1623 pub const VENDOR = 14;
1624pub const HW_VERSION = 16;1624 pub const PRODUCT = 15;
1625pub const HW_SERIALNO = 17;1625 pub const VERSION = 16;
1626pub const HW_UUID = 18;1626 pub const SERIALNO = 17;
1627pub const HW_PHYSMEM64 = 19;1627 pub const UUID = 18;
1628pub const HW_USERMEM64 = 20;1628 pub const PHYSMEM64 = 19;
1629pub const HW_NCPUFOUND = 21;1629 pub const USERMEM64 = 20;
1630pub const HW_ALLOWPOWERDOWN = 22;1630 pub const NCPUFOUND = 21;
1631pub const HW_PERFPOLICY = 23;1631 pub const ALLOWPOWERDOWN = 22;
1632pub const HW_SMT = 24;1632 pub const PERFPOLICY = 23;
1633pub const HW_NCPUONLINE = 25;1633 pub const SMT = 24;
1634 pub const NCPUONLINE = 25;
1635 pub const POWER = 26;
1636};
16341637
1635/// TODO refines if necessary1638/// TODO refines if necessary
1636pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {1639pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {
lib/std/fs.zig+3
...@@ -478,6 +478,9 @@ pub const IterableDir = struct {...@@ -478,6 +478,9 @@ pub const IterableDir = struct {
478 .FAULT => unreachable,478 .FAULT => unreachable,
479 .NOTDIR => unreachable,479 .NOTDIR => unreachable,
480 .INVAL => unreachable,480 .INVAL => unreachable,
481 // Introduced in freebsd 13.2: directory unlinked but still open.
482 // To be consistent, iteration ends if the directory being iterated is deleted during iteration.
483 .NOENT => return null,
481 else => |err| return os.unexpectedErrno(err),484 else => |err| return os.unexpectedErrno(err),
482 }485 }
483 if (rc == 0) return null;486 if (rc == 0) return null;
lib/std/os.zig+4
...@@ -88,6 +88,10 @@ pub const F = system.F;...@@ -88,6 +88,10 @@ pub const F = system.F;
88pub const FD_CLOEXEC = system.FD_CLOEXEC;88pub const FD_CLOEXEC = system.FD_CLOEXEC;
89pub const Flock = system.Flock;89pub const Flock = system.Flock;
90pub const HOST_NAME_MAX = system.HOST_NAME_MAX;90pub const HOST_NAME_MAX = system.HOST_NAME_MAX;
91pub const HW = switch (builtin.os.tag) {
92 .openbsd => system.HW,
93 else => .{},
94};
91pub const IFNAMESIZE = system.IFNAMESIZE;95pub const IFNAMESIZE = system.IFNAMESIZE;
92pub const IOV_MAX = system.IOV_MAX;96pub const IOV_MAX = system.IOV_MAX;
93pub const IPPROTO = system.IPPROTO;97pub const IPPROTO = system.IPPROTO;
lib/std/process.zig+18-1
...@@ -1163,7 +1163,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {...@@ -1163,7 +1163,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
1163 .linux => {1163 .linux => {
1164 return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;1164 return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;
1165 },1165 },
1166 .freebsd, .netbsd, .openbsd, .dragonfly, .macos => {1166 .freebsd, .netbsd, .dragonfly, .macos => {
1167 var physmem: c_ulong = undefined;1167 var physmem: c_ulong = undefined;
1168 var len: usize = @sizeOf(c_ulong);1168 var len: usize = @sizeOf(c_ulong);
1169 const name = switch (builtin.os.tag) {1169 const name = switch (builtin.os.tag) {
...@@ -1177,6 +1177,23 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {...@@ -1177,6 +1177,23 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
1177 };1177 };
1178 return @intCast(usize, physmem);1178 return @intCast(usize, physmem);
1179 },1179 },
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 },
1180 .windows => {1197 .windows => {
1181 var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;1198 var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;
1182 const rc = std.os.windows.ntdll.NtQuerySystemInformation(1199 const rc = std.os.windows.ntdll.NtQuerySystemInformation(