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 {...@@ -809,12 +809,15 @@ const PosixThreadImpl = struct {
809 else => {809 else => {
810 var count: c_int = undefined;810 var count: c_int = undefined;
811 var count_len: usize = @sizeOf(c_int);811 var count_len: usize = @sizeOf(c_int);
812 const name = if (comptime target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu";812 const name = comptime if (target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
813 posix.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) {813 switch (posix.errno(posix.system.sysctlbyname(name, &count, &count_len, null, 0))) {
814 error.UnknownName => unreachable,814 .SUCCESS => return @intCast(count),
815 else => |e| return e,815 .FAULT => unreachable,
816 };816 .PERM => return error.PermissionDenied,
817 return @as(usize, @intCast(count));817 .NOMEM => return error.SystemResources,
818 .NOENT => unreachable,
819 else => |err| return posix.unexpectedErrno(err),
820 }
818 },821 },
819 }822 }
820 }823 }
lib/std/posix.zig-113
...@@ -677,89 +677,6 @@ pub fn getpeername(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock...@@ -677,89 +677,6 @@ pub fn getpeername(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock
677 }677 }
678}678}
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
763pub const FanotifyInitError = error{680pub const FanotifyInitError = error{
764 ProcessFdQuotaExceeded,681 ProcessFdQuotaExceeded,
765 SystemFdQuotaExceeded,682 SystemFdQuotaExceeded,
...@@ -996,36 +913,6 @@ pub fn sysctl(...@@ -996,36 +913,6 @@ pub fn sysctl(
996 }913 }
997}914}
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
1029pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void {916pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void {
1030 switch (errno(system.gettimeofday(tv, tz))) {917 switch (errno(system.gettimeofday(tv, tz))) {
1031 .SUCCESS => return,918 .SUCCESS => return,
lib/std/process.zig+14-12
...@@ -556,26 +556,28 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 {...@@ -556,26 +556,28 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
556 const name = if (native_os == .netbsd) "hw.physmem64" else "hw.physmem";556 const name = if (native_os == .netbsd) "hw.physmem64" else "hw.physmem";
557 var physmem: c_ulong = undefined;557 var physmem: c_ulong = undefined;
558 var len: usize = @sizeOf(c_ulong);558 var len: usize = @sizeOf(c_ulong);
559 posix.sysctlbynameZ(name, &physmem, &len, null, 0) catch |err| switch (err) {559 switch (posix.errno(posix.system.sysctlbyname(name, &physmem, &len, null, 0))) {
560 error.PermissionDenied => unreachable, // only when setting values,560 .SUCCESS => return @intCast(physmem),
561 error.SystemResources => unreachable, // memory already on the stack561 .FAULT => unreachable,
562 error.UnknownName => unreachable,562 .PERM => unreachable, // only when setting values
563 .NOMEM => unreachable, // memory already on the stack
564 .NOENT => unreachable,
563 else => return error.UnknownTotalSystemMemory,565 else => return error.UnknownTotalSystemMemory,
564 };566 }
565 return @intCast(physmem);
566 },567 },
567 // whole Darwin family568 // whole Darwin family
568 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {569 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
569 // "hw.memsize" returns uint64_t570 // "hw.memsize" returns uint64_t
570 var physmem: u64 = undefined;571 var physmem: u64 = undefined;
571 var len: usize = @sizeOf(u64);572 var len: usize = @sizeOf(u64);
572 posix.sysctlbynameZ("hw.memsize", &physmem, &len, null, 0) catch |err| switch (err) {573 switch (posix.errno(posix.system.sysctlbyname("hw.memsize", &physmem, &len, null, 0))) {
573 error.PermissionDenied => unreachable, // only when setting values,574 .SUCCESS => return physmem,
574 error.SystemResources => unreachable, // memory already on the stack575 .FAULT => unreachable,
575 error.UnknownName => unreachable, // constant, known good value576 .PERM => unreachable, // only when setting values
577 .NOMEM => unreachable, // memory already on the stack
578 .NOENT => unreachable, // constant, known good value
576 else => return error.UnknownTotalSystemMemory,579 else => return error.UnknownTotalSystemMemory,
577 };580 }
578 return physmem;
579 },581 },
580 .openbsd => {582 .openbsd => {
581 const mib: [2]c_int = [_]c_int{583 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 {...@@ -260,12 +260,14 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {
260 var value: u32 = undefined;260 var value: u32 = undefined;
261 var len: usize = @sizeOf(@TypeOf(value));261 var len: usize = @sizeOf(@TypeOf(value));
262262
263 posix.sysctlbynameZ(key, &value, &len, null, 0) catch |err| switch (err) {263 switch (posix.errno(posix.system.sysctlbyname(key, &value, &len, null, 0))) {
264 error.PermissionDenied => unreachable, // only when setting values,264 .SUCCESS => {},
265 error.SystemResources => unreachable, // memory already on the stack265 .FAULT => unreachable,
266 error.UnknownName => unreachable, // constant, known good value266 .PERM => unreachable, // only when setting values,
267 error.Unexpected => return error.OSVersionDetectionFail,267 .NOMEM => unreachable, // memory already on the stack
268 };268 .NOENT => unreachable, // constant, known good value
269 else => return error.OSVersionDetectionFail,
270 }
269271
270 switch (builtin.target.os.tag) {272 switch (builtin.target.os.tag) {
271 .freebsd => {273 .freebsd => {
lib/std/zig/system/darwin/macos.zig+9-6
...@@ -2,6 +2,7 @@ const builtin = @import("builtin");...@@ -2,6 +2,7 @@ const builtin = @import("builtin");
22
3const std = @import("std");3const std = @import("std");
4const Io = std.Io;4const Io = std.Io;
5const posix = std.posix;
5const assert = std.debug.assert;6const assert = std.debug.assert;
6const mem = std.mem;7const mem = std.mem;
7const testing = std.testing;8const testing = std.testing;
...@@ -399,12 +400,14 @@ test "detect" {...@@ -399,12 +400,14 @@ test "detect" {
399pub fn detectNativeCpuAndFeatures() ?Target.Cpu {400pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
400 var cpu_family: std.c.CPUFAMILY = undefined;401 var cpu_family: std.c.CPUFAMILY = undefined;
401 var len: usize = @sizeOf(std.c.CPUFAMILY);402 var len: usize = @sizeOf(std.c.CPUFAMILY);
402 std.posix.sysctlbynameZ("hw.cpufamily", &cpu_family, &len, null, 0) catch |err| switch (err) {403 switch (posix.errno(posix.system.sysctlbyname("hw.cpufamily", &cpu_family, &len, null, 0))) {
403 error.PermissionDenied => unreachable, // only when setting values,404 .SUCCESS => {},
404 error.SystemResources => unreachable, // memory already on the stack405 .FAULT => unreachable, // segmentation fault
405 error.UnknownName => unreachable, // constant, known good value406 .PERM => unreachable, // only when setting values,
406 error.Unexpected => unreachable, // EFAULT: stack should be safe, EISDIR/ENOTDIR: constant, known good value407 .NOMEM => unreachable, // memory already on the stack
407 };408 .NOENT => unreachable, // constant, known good value
409 else => unreachable,
410 }
408411
409 const current_arch = builtin.cpu.arch;412 const current_arch = builtin.cpu.arch;
410 switch (current_arch) {413 switch (current_arch) {