authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-29 20:24:33-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-30 01:58:49-08:00
log36eb8dec98c743f369a2badf6a56599c736a45ae
treeb6af4445b3d486aaf63378f40d2ca1de032c04f6
parente7e168727e3c024869de9d114a7dd49d2a80de4d

std.posix: goodbye to some functions

- fstat - inotify_init1 - inotify_add_watch, inotify_add_watchZ - inotify_rm_watch - sysctlbynameZ

5 files changed, 40 insertions(+), 143 deletions(-)

lib/std/Thread.zig+9-6
......@@ -809,12 +809,15 @@ const PosixThreadImpl = struct {
809809 else => {
810810 var count: c_int = undefined;
811811 var count_len: usize = @sizeOf(c_int);
812 const name = if (comptime target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
813 posix.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) {
814 error.UnknownName => unreachable,
815 else => |e| return e,
816 };
817 return @as(usize, @intCast(count));
812 const name = comptime if (target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
813 switch (posix.errno(posix.system.sysctlbyname(name, &count, &count_len, null, 0))) {
814 .SUCCESS => return @intCast(count),
815 .FAULT => unreachable,
816 .PERM => return error.PermissionDenied,
817 .NOMEM => return error.SystemResources,
818 .NOENT => unreachable,
819 else => |err| return posix.unexpectedErrno(err),
820 }
818821 },
819822 }
820823 }
lib/std/posix.zig-113
......@@ -677,89 +677,6 @@ pub fn getpeername(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock
677677 }
678678}
679679
680pub const FStatError = std.Io.File.StatError;
681
682/// Return information about a file descriptor.
683pub fn fstat(fd: fd_t) FStatError!Stat {
684 if (native_os == .wasi and !builtin.link_libc) {
685 @compileError("unsupported OS");
686 }
687
688 var stat = mem.zeroes(Stat);
689 switch (errno(system.fstat(fd, &stat))) {
690 .SUCCESS => return stat,
691 .INVAL => unreachable,
692 .BADF => unreachable, // Always a race condition.
693 .NOMEM => return error.SystemResources,
694 .ACCES => return error.AccessDenied,
695 else => |err| return unexpectedErrno(err),
696 }
697}
698
699pub const INotifyInitError = error{
700 ProcessFdQuotaExceeded,
701 SystemFdQuotaExceeded,
702 SystemResources,
703} || UnexpectedError;
704
705/// initialize an inotify instance
706pub fn inotify_init1(flags: u32) INotifyInitError!i32 {
707 const rc = system.inotify_init1(flags);
708 switch (errno(rc)) {
709 .SUCCESS => return @intCast(rc),
710 .INVAL => unreachable,
711 .MFILE => return error.ProcessFdQuotaExceeded,
712 .NFILE => return error.SystemFdQuotaExceeded,
713 .NOMEM => return error.SystemResources,
714 else => |err| return unexpectedErrno(err),
715 }
716}
717
718pub const INotifyAddWatchError = error{
719 AccessDenied,
720 NameTooLong,
721 FileNotFound,
722 SystemResources,
723 UserResourceLimitReached,
724 NotDir,
725 WatchAlreadyExists,
726} || UnexpectedError;
727
728/// add a watch to an initialized inotify instance
729pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INotifyAddWatchError!i32 {
730 const pathname_c = try toPosixPath(pathname);
731 return inotify_add_watchZ(inotify_fd, &pathname_c, mask);
732}
733
734/// Same as `inotify_add_watch` except pathname is null-terminated.
735pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 {
736 const rc = system.inotify_add_watch(inotify_fd, pathname, mask);
737 switch (errno(rc)) {
738 .SUCCESS => return @intCast(rc),
739 .ACCES => return error.AccessDenied,
740 .BADF => unreachable,
741 .FAULT => unreachable,
742 .INVAL => unreachable,
743 .NAMETOOLONG => return error.NameTooLong,
744 .NOENT => return error.FileNotFound,
745 .NOMEM => return error.SystemResources,
746 .NOSPC => return error.UserResourceLimitReached,
747 .NOTDIR => return error.NotDir,
748 .EXIST => return error.WatchAlreadyExists,
749 else => |err| return unexpectedErrno(err),
750 }
751}
752
753/// remove an existing watch from an inotify instance
754pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void {
755 switch (errno(system.inotify_rm_watch(inotify_fd, wd))) {
756 .SUCCESS => return,
757 .BADF => unreachable,
758 .INVAL => unreachable,
759 else => unreachable,
760 }
761}
762
763680pub const FanotifyInitError = error{
764681 ProcessFdQuotaExceeded,
765682 SystemFdQuotaExceeded,
......@@ -996,36 +913,6 @@ pub fn sysctl(
996913 }
997914}
998915
999pub const SysCtlByNameError = error{
1000 PermissionDenied,
1001 SystemResources,
1002 UnknownName,
1003} || UnexpectedError;
1004
1005pub fn sysctlbynameZ(
1006 name: [*:0]const u8,
1007 oldp: ?*anyopaque,
1008 oldlenp: ?*usize,
1009 newp: ?*anyopaque,
1010 newlen: usize,
1011) SysCtlByNameError!void {
1012 if (native_os == .wasi) {
1013 @compileError("sysctl not supported on WASI");
1014 }
1015 if (native_os == .haiku) {
1016 @compileError("sysctl not supported on Haiku");
1017 }
1018
1019 switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) {
1020 .SUCCESS => return,
1021 .FAULT => unreachable,
1022 .PERM => return error.PermissionDenied,
1023 .NOMEM => return error.SystemResources,
1024 .NOENT => return error.UnknownName,
1025 else => |err| return unexpectedErrno(err),
1026 }
1027}
1028
1029916pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void {
1030917 switch (errno(system.gettimeofday(tv, tz))) {
1031918 .SUCCESS => return,
lib/std/process.zig+14-12
......@@ -556,26 +556,28 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
556556 const name = if (native_os == .netbsd) "hw.physmem64" else "hw.physmem";
557557 var physmem: c_ulong = undefined;
558558 var len: usize = @sizeOf(c_ulong);
559 posix.sysctlbynameZ(name, &physmem, &len, null, 0) catch |err| switch (err) {
560 error.PermissionDenied => unreachable, // only when setting values,
561 error.SystemResources => unreachable, // memory already on the stack
562 error.UnknownName => unreachable,
559 switch (posix.errno(posix.system.sysctlbyname(name, &physmem, &len, null, 0))) {
560 .SUCCESS => return @intCast(physmem),
561 .FAULT => unreachable,
562 .PERM => unreachable, // only when setting values
563 .NOMEM => unreachable, // memory already on the stack
564 .NOENT => unreachable,
563565 else => return error.UnknownTotalSystemMemory,
564 };
565 return @intCast(physmem);
566 }
566567 },
567568 // whole Darwin family
568569 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
569570 // "hw.memsize" returns uint64_t
570571 var physmem: u64 = undefined;
571572 var len: usize = @sizeOf(u64);
572 posix.sysctlbynameZ("hw.memsize", &physmem, &len, null, 0) catch |err| switch (err) {
573 error.PermissionDenied => unreachable, // only when setting values,
574 error.SystemResources => unreachable, // memory already on the stack
575 error.UnknownName => unreachable, // constant, known good value
573 switch (posix.errno(posix.system.sysctlbyname("hw.memsize", &physmem, &len, null, 0))) {
574 .SUCCESS => return physmem,
575 .FAULT => unreachable,
576 .PERM => unreachable, // only when setting values
577 .NOMEM => unreachable, // memory already on the stack
578 .NOENT => unreachable, // constant, known good value
576579 else => return error.UnknownTotalSystemMemory,
577 };
578 return physmem;
580 }
579581 },
580582 .openbsd => {
581583 const mib: [2]c_int = [_]c_int{
lib/std/zig/system.zig+8-6
......@@ -260,12 +260,14 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {
260260 var value: u32 = undefined;
261261 var len: usize = @sizeOf(@TypeOf(value));
262262
263 posix.sysctlbynameZ(key, &value, &len, null, 0) catch |err| switch (err) {
264 error.PermissionDenied => unreachable, // only when setting values,
265 error.SystemResources => unreachable, // memory already on the stack
266 error.UnknownName => unreachable, // constant, known good value
267 error.Unexpected => return error.OSVersionDetectionFail,
268 };
263 switch (posix.errno(posix.system.sysctlbyname(key, &value, &len, null, 0))) {
264 .SUCCESS => {},
265 .FAULT => unreachable,
266 .PERM => unreachable, // only when setting values,
267 .NOMEM => unreachable, // memory already on the stack
268 .NOENT => unreachable, // constant, known good value
269 else => return error.OSVersionDetectionFail,
270 }
269271
270272 switch (builtin.target.os.tag) {
271273 .freebsd => {
lib/std/zig/system/darwin/macos.zig+9-6
......@@ -2,6 +2,7 @@ const builtin = @import("builtin");
22
33const std = @import("std");
44const Io = std.Io;
5const posix = std.posix;
56const assert = std.debug.assert;
67const mem = std.mem;
78const testing = std.testing;
......@@ -399,12 +400,14 @@ test "detect" {
399400pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
400401 var cpu_family: std.c.CPUFAMILY = undefined;
401402 var len: usize = @sizeOf(std.c.CPUFAMILY);
402 std.posix.sysctlbynameZ("hw.cpufamily", &cpu_family, &len, null, 0) catch |err| switch (err) {
403 error.PermissionDenied => unreachable, // only when setting values,
404 error.SystemResources => unreachable, // memory already on the stack
405 error.UnknownName => unreachable, // constant, known good value
406 error.Unexpected => unreachable, // EFAULT: stack should be safe, EISDIR/ENOTDIR: constant, known good value
407 };
403 switch (posix.errno(posix.system.sysctlbyname("hw.cpufamily", &cpu_family, &len, null, 0))) {
404 .SUCCESS => {},
405 .FAULT => unreachable, // segmentation fault
406 .PERM => unreachable, // only when setting values,
407 .NOMEM => unreachable, // memory already on the stack
408 .NOENT => unreachable, // constant, known good value
409 else => unreachable,
410 }
408411
409412 const current_arch = builtin.cpu.arch;
410413 switch (current_arch) {