std.posix: goodbye to some functions

- fstat
- inotify_init1
- inotify_add_watch, inotify_add_watchZ
- inotify_rm_watch
- sysctlbynameZ
This commit is contained in:
Andrew Kelley 2026-01-29 20:24:33 -08:00
parent e7e168727e
commit 36eb8dec98
5 changed files with 40 additions and 143 deletions

View file

@ -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),
}
},
}
}

View file

@ -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,

View file

@ -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{

View file

@ -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 => {

View file

@ -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) {