From 36eb8dec98c743f369a2badf6a56599c736a45ae Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 29 Jan 2026 20:24:33 -0800 Subject: [PATCH] std.posix: goodbye to some functions - fstat - inotify_init1 - inotify_add_watch, inotify_add_watchZ - inotify_rm_watch - sysctlbynameZ --- lib/std/Thread.zig | 15 ++-- lib/std/posix.zig | 113 ---------------------------- lib/std/process.zig | 26 ++++--- lib/std/zig/system.zig | 14 ++-- lib/std/zig/system/darwin/macos.zig | 15 ++-- 5 files changed, 40 insertions(+), 143 deletions(-) diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index ad25b1728e88abe10f4fe14c36b53a66c15b4b6a..33fd4c0234cddd970e478caf40f43cc7d7edba2b 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -809,12 +809,15 @@ const PosixThreadImpl = struct { else => { var count: c_int = undefined; var count_len: usize = @sizeOf(c_int); - const name = if (comptime target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu"; - posix.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) { - error.UnknownName => unreachable, - else => |e| return e, - }; - return @as(usize, @intCast(count)); + const name = comptime if (target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu"; + switch (posix.errno(posix.system.sysctlbyname(name, &count, &count_len, null, 0))) { + .SUCCESS => return @intCast(count), + .FAULT => unreachable, + .PERM => return error.PermissionDenied, + .NOMEM => return error.SystemResources, + .NOENT => unreachable, + else => |err| return posix.unexpectedErrno(err), + } }, } } diff --git a/lib/std/posix.zig b/lib/std/posix.zig index f5f3b73a594615de3b402588067afcafd839e945..6b5686b20a3531d04dfc975d5625022932b89782 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -677,89 +677,6 @@ pub fn getpeername(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock } } -pub const FStatError = std.Io.File.StatError; - -/// Return information about a file descriptor. -pub fn fstat(fd: fd_t) FStatError!Stat { - if (native_os == .wasi and !builtin.link_libc) { - @compileError("unsupported OS"); - } - - var stat = mem.zeroes(Stat); - switch (errno(system.fstat(fd, &stat))) { - .SUCCESS => return stat, - .INVAL => unreachable, - .BADF => unreachable, // Always a race condition. - .NOMEM => return error.SystemResources, - .ACCES => return error.AccessDenied, - else => |err| return unexpectedErrno(err), - } -} - -pub const INotifyInitError = error{ - ProcessFdQuotaExceeded, - SystemFdQuotaExceeded, - SystemResources, -} || UnexpectedError; - -/// initialize an inotify instance -pub fn inotify_init1(flags: u32) INotifyInitError!i32 { - const rc = system.inotify_init1(flags); - switch (errno(rc)) { - .SUCCESS => return @intCast(rc), - .INVAL => unreachable, - .MFILE => return error.ProcessFdQuotaExceeded, - .NFILE => return error.SystemFdQuotaExceeded, - .NOMEM => return error.SystemResources, - else => |err| return unexpectedErrno(err), - } -} - -pub const INotifyAddWatchError = error{ - AccessDenied, - NameTooLong, - FileNotFound, - SystemResources, - UserResourceLimitReached, - NotDir, - WatchAlreadyExists, -} || UnexpectedError; - -/// add a watch to an initialized inotify instance -pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INotifyAddWatchError!i32 { - const pathname_c = try toPosixPath(pathname); - return inotify_add_watchZ(inotify_fd, &pathname_c, mask); -} - -/// Same as `inotify_add_watch` except pathname is null-terminated. -pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 { - const rc = system.inotify_add_watch(inotify_fd, pathname, mask); - switch (errno(rc)) { - .SUCCESS => return @intCast(rc), - .ACCES => return error.AccessDenied, - .BADF => unreachable, - .FAULT => unreachable, - .INVAL => unreachable, - .NAMETOOLONG => return error.NameTooLong, - .NOENT => return error.FileNotFound, - .NOMEM => return error.SystemResources, - .NOSPC => return error.UserResourceLimitReached, - .NOTDIR => return error.NotDir, - .EXIST => return error.WatchAlreadyExists, - else => |err| return unexpectedErrno(err), - } -} - -/// remove an existing watch from an inotify instance -pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void { - switch (errno(system.inotify_rm_watch(inotify_fd, wd))) { - .SUCCESS => return, - .BADF => unreachable, - .INVAL => unreachable, - else => unreachable, - } -} - pub const FanotifyInitError = error{ ProcessFdQuotaExceeded, SystemFdQuotaExceeded, @@ -996,36 +913,6 @@ pub fn sysctl( } } -pub const SysCtlByNameError = error{ - PermissionDenied, - SystemResources, - UnknownName, -} || UnexpectedError; - -pub fn sysctlbynameZ( - name: [*:0]const u8, - oldp: ?*anyopaque, - oldlenp: ?*usize, - newp: ?*anyopaque, - newlen: usize, -) SysCtlByNameError!void { - if (native_os == .wasi) { - @compileError("sysctl not supported on WASI"); - } - if (native_os == .haiku) { - @compileError("sysctl not supported on Haiku"); - } - - switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) { - .SUCCESS => return, - .FAULT => unreachable, - .PERM => return error.PermissionDenied, - .NOMEM => return error.SystemResources, - .NOENT => return error.UnknownName, - else => |err| return unexpectedErrno(err), - } -} - pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { switch (errno(system.gettimeofday(tv, tz))) { .SUCCESS => return, diff --git a/lib/std/process.zig b/lib/std/process.zig index 5bcacddc84a993d96586b861411af63148f361ac..8395882c167cbe0149f081cd7fb4af413ec24e08 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -556,26 +556,28 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 { const name = if (native_os == .netbsd) "hw.physmem64" else "hw.physmem"; var physmem: c_ulong = undefined; var len: usize = @sizeOf(c_ulong); - posix.sysctlbynameZ(name, &physmem, &len, null, 0) catch |err| switch (err) { - error.PermissionDenied => unreachable, // only when setting values, - error.SystemResources => unreachable, // memory already on the stack - error.UnknownName => unreachable, + switch (posix.errno(posix.system.sysctlbyname(name, &physmem, &len, null, 0))) { + .SUCCESS => return @intCast(physmem), + .FAULT => unreachable, + .PERM => unreachable, // only when setting values + .NOMEM => unreachable, // memory already on the stack + .NOENT => unreachable, else => return error.UnknownTotalSystemMemory, - }; - return @intCast(physmem); + } }, // whole Darwin family .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => { // "hw.memsize" returns uint64_t var physmem: u64 = undefined; var len: usize = @sizeOf(u64); - posix.sysctlbynameZ("hw.memsize", &physmem, &len, null, 0) catch |err| switch (err) { - error.PermissionDenied => unreachable, // only when setting values, - error.SystemResources => unreachable, // memory already on the stack - error.UnknownName => unreachable, // constant, known good value + switch (posix.errno(posix.system.sysctlbyname("hw.memsize", &physmem, &len, null, 0))) { + .SUCCESS => return physmem, + .FAULT => unreachable, + .PERM => unreachable, // only when setting values + .NOMEM => unreachable, // memory already on the stack + .NOENT => unreachable, // constant, known good value else => return error.UnknownTotalSystemMemory, - }; - return physmem; + } }, .openbsd => { const mib: [2]c_int = [_]c_int{ diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index efcf569de5a5ef12170c7c7a3ea82a9774569b1a..5046e2f51b081b77c95a9e08020eb24b688498a5 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -260,12 +260,14 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target { var value: u32 = undefined; var len: usize = @sizeOf(@TypeOf(value)); - posix.sysctlbynameZ(key, &value, &len, null, 0) catch |err| switch (err) { - error.PermissionDenied => unreachable, // only when setting values, - error.SystemResources => unreachable, // memory already on the stack - error.UnknownName => unreachable, // constant, known good value - error.Unexpected => return error.OSVersionDetectionFail, - }; + switch (posix.errno(posix.system.sysctlbyname(key, &value, &len, null, 0))) { + .SUCCESS => {}, + .FAULT => unreachable, + .PERM => unreachable, // only when setting values, + .NOMEM => unreachable, // memory already on the stack + .NOENT => unreachable, // constant, known good value + else => return error.OSVersionDetectionFail, + } switch (builtin.target.os.tag) { .freebsd => { diff --git a/lib/std/zig/system/darwin/macos.zig b/lib/std/zig/system/darwin/macos.zig index 7d80c2b588e6a6587ae7437d454666a9d8d2fed4..c9dc8b57ce5928ba1e3eca5135f243d797643728 100644 --- a/lib/std/zig/system/darwin/macos.zig +++ b/lib/std/zig/system/darwin/macos.zig @@ -2,6 +2,7 @@ const builtin = @import("builtin"); const std = @import("std"); const Io = std.Io; +const posix = std.posix; const assert = std.debug.assert; const mem = std.mem; const testing = std.testing; @@ -399,12 +400,14 @@ test "detect" { pub fn detectNativeCpuAndFeatures() ?Target.Cpu { var cpu_family: std.c.CPUFAMILY = undefined; var len: usize = @sizeOf(std.c.CPUFAMILY); - std.posix.sysctlbynameZ("hw.cpufamily", &cpu_family, &len, null, 0) catch |err| switch (err) { - error.PermissionDenied => unreachable, // only when setting values, - error.SystemResources => unreachable, // memory already on the stack - error.UnknownName => unreachable, // constant, known good value - error.Unexpected => unreachable, // EFAULT: stack should be safe, EISDIR/ENOTDIR: constant, known good value - }; + switch (posix.errno(posix.system.sysctlbyname("hw.cpufamily", &cpu_family, &len, null, 0))) { + .SUCCESS => {}, + .FAULT => unreachable, // segmentation fault + .PERM => unreachable, // only when setting values, + .NOMEM => unreachable, // memory already on the stack + .NOENT => unreachable, // constant, known good value + else => unreachable, + } const current_arch = builtin.cpu.arch; switch (current_arch) { -- 2.54.0