Merge pull request #16052 from mikdusan/bsd-getdents

fix bad return types for BSDs getdents and debitrot openbsd
This commit is contained in:
Andrew Kelley 2023-06-17 13:01:52 -07:00 committed by GitHub
commit 96acc204b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 30 deletions

View File

@ -624,7 +624,7 @@ const PosixThreadImpl = struct {
.openbsd => { .openbsd => {
var count: c_int = undefined; var count: c_int = undefined;
var count_size: usize = @sizeOf(c_int); var count_size: usize = @sizeOf(c_int);
const mib = [_]c_int{ os.CTL.HW, os.system.HW_NCPUONLINE }; const mib = [_]c_int{ os.CTL.HW, os.system.HW.NCPUONLINE };
os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) { os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {
error.NameTooLong, error.UnknownName => unreachable, error.NameTooLong, error.UnknownName => unreachable,
else => |e| return e, else => |e| return e,

View File

@ -9,7 +9,7 @@ pub fn _errno() *c_int {
return &errno; return &errno;
} }
pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int;
pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize; pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int; pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;

View File

@ -75,7 +75,7 @@ pub const _errno = __error;
pub extern "c" var malloc_options: [*:0]const u8; pub extern "c" var malloc_options: [*:0]const u8;
pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) isize;
pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize; pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
pub extern "c" fn getentropy(buf_ptr: [*]u8, buf_len: usize) c_int; pub extern "c" fn getentropy(buf_ptr: [*]u8, buf_len: usize) c_int;

View File

@ -16,7 +16,7 @@ pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
pub extern "c" fn getthrid() pid_t; pub extern "c" fn getthrid() pid_t;
pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int; pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int;
pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
pub const pthread_mutex_t = extern struct { pub const pthread_mutex_t = extern struct {
@ -1606,31 +1606,34 @@ pub const KERN = struct {
pub const PROC_NENV = 4; pub const PROC_NENV = 4;
}; };
pub const HW_MACHINE = 1; pub const HW = struct {
pub const HW_MODEL = 2; pub const MACHINE = 1;
pub const HW_NCPU = 3; pub const MODEL = 2;
pub const HW_BYTEORDER = 4; pub const NCPU = 3;
pub const HW_PHYSMEM = 5; pub const BYTEORDER = 4;
pub const HW_USERMEM = 6; pub const PHYSMEM = 5;
pub const HW_PAGESIZE = 7; pub const USERMEM = 6;
pub const HW_DISKNAMES = 8; pub const PAGESIZE = 7;
pub const HW_DISKSTATS = 9; pub const DISKNAMES = 8;
pub const HW_DISKCOUNT = 10; pub const DISKSTATS = 9;
pub const HW_SENSORS = 11; pub const DISKCOUNT = 10;
pub const HW_CPUSPEED = 12; pub const SENSORS = 11;
pub const HW_SETPERF = 13; pub const CPUSPEED = 12;
pub const HW_VENDOR = 14; pub const SETPERF = 13;
pub const HW_PRODUCT = 15; pub const VENDOR = 14;
pub const HW_VERSION = 16; pub const PRODUCT = 15;
pub const HW_SERIALNO = 17; pub const VERSION = 16;
pub const HW_UUID = 18; pub const SERIALNO = 17;
pub const HW_PHYSMEM64 = 19; pub const UUID = 18;
pub const HW_USERMEM64 = 20; pub const PHYSMEM64 = 19;
pub const HW_NCPUFOUND = 21; pub const USERMEM64 = 20;
pub const HW_ALLOWPOWERDOWN = 22; pub const NCPUFOUND = 21;
pub const HW_PERFPOLICY = 23; pub const ALLOWPOWERDOWN = 22;
pub const HW_SMT = 24; pub const PERFPOLICY = 23;
pub const HW_NCPUONLINE = 25; pub const SMT = 24;
pub const NCPUONLINE = 25;
pub const POWER = 26;
};
/// TODO refines if necessary /// TODO refines if necessary
pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) { pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {

View File

@ -478,6 +478,9 @@ pub const IterableDir = struct {
.FAULT => unreachable, .FAULT => unreachable,
.NOTDIR => unreachable, .NOTDIR => unreachable,
.INVAL => unreachable, .INVAL => unreachable,
// Introduced in freebsd 13.2: directory unlinked but still open.
// To be consistent, iteration ends if the directory being iterated is deleted during iteration.
.NOENT => return null,
else => |err| return os.unexpectedErrno(err), else => |err| return os.unexpectedErrno(err),
} }
if (rc == 0) return null; if (rc == 0) return null;

View File

@ -88,6 +88,10 @@ pub const F = system.F;
pub const FD_CLOEXEC = system.FD_CLOEXEC; pub const FD_CLOEXEC = system.FD_CLOEXEC;
pub const Flock = system.Flock; pub const Flock = system.Flock;
pub const HOST_NAME_MAX = system.HOST_NAME_MAX; pub const HOST_NAME_MAX = system.HOST_NAME_MAX;
pub const HW = switch (builtin.os.tag) {
.openbsd => system.HW,
else => .{},
};
pub const IFNAMESIZE = system.IFNAMESIZE; pub const IFNAMESIZE = system.IFNAMESIZE;
pub const IOV_MAX = system.IOV_MAX; pub const IOV_MAX = system.IOV_MAX;
pub const IPPROTO = system.IPPROTO; pub const IPPROTO = system.IPPROTO;

View File

@ -1163,7 +1163,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
.linux => { .linux => {
return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;
}, },
.freebsd, .netbsd, .openbsd, .dragonfly, .macos => { .freebsd, .netbsd, .dragonfly, .macos => {
var physmem: c_ulong = undefined; var physmem: c_ulong = undefined;
var len: usize = @sizeOf(c_ulong); var len: usize = @sizeOf(c_ulong);
const name = switch (builtin.os.tag) { const name = switch (builtin.os.tag) {
@ -1177,6 +1177,23 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
}; };
return @intCast(usize, physmem); return @intCast(usize, physmem);
}, },
.openbsd => {
const mib: [2]c_int = [_]c_int{
std.os.CTL.HW,
std.os.HW.PHYSMEM64,
};
var physmem: i64 = undefined;
var len: usize = @sizeOf(@TypeOf(physmem));
std.os.sysctl(&mib, &physmem, &len, null, 0) catch |err| switch (err) {
error.NameTooLong => unreachable, // constant, known good value
error.PermissionDenied => unreachable, // only when setting values,
error.SystemResources => unreachable, // memory already on the stack
error.UnknownName => unreachable, // constant, known good value
else => return error.UnknownTotalSystemMemory,
};
assert(physmem >= 0);
return @bitCast(usize, physmem);
},
.windows => { .windows => {
var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined; var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;
const rc = std.os.windows.ntdll.NtQuerySystemInformation( const rc = std.os.windows.ntdll.NtQuerySystemInformation(