From ceae9600e3ce7003907c08af82142242ca3e294c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 7 Jan 2026 14:25:29 -0800 Subject: [PATCH] std.posix: remove setuid, seteuid, setgid, setegid, getuid, etc applications and libraries should reach for the lower level APIs instead --- lib/std/posix.zig | 75 ------------------------------------------ lib/std/posix/test.zig | 8 ++--- 2 files changed, 4 insertions(+), 79 deletions(-) diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 84619317aa..532711f1b4 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -551,67 +551,6 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { } } -pub const SetEidError = error{ - InvalidUserId, - PermissionDenied, -} || UnexpectedError; - -pub const SetIdError = error{ResourceLimitReached} || SetEidError; - -pub fn setuid(uid: uid_t) SetIdError!void { - switch (errno(system.setuid(uid))) { - .SUCCESS => return, - .AGAIN => return error.ResourceLimitReached, - .INVAL => return error.InvalidUserId, - .PERM => return error.PermissionDenied, - else => |err| return unexpectedErrno(err), - } -} - -pub fn seteuid(uid: uid_t) SetEidError!void { - switch (errno(system.seteuid(uid))) { - .SUCCESS => return, - .INVAL => return error.InvalidUserId, - .PERM => return error.PermissionDenied, - else => |err| return unexpectedErrno(err), - } -} - -pub fn setgid(gid: gid_t) SetIdError!void { - switch (errno(system.setgid(gid))) { - .SUCCESS => return, - .AGAIN => return error.ResourceLimitReached, - .INVAL => return error.InvalidUserId, - .PERM => return error.PermissionDenied, - else => |err| return unexpectedErrno(err), - } -} - -pub fn setegid(uid: uid_t) SetEidError!void { - switch (errno(system.setegid(uid))) { - .SUCCESS => return, - .INVAL => return error.InvalidUserId, - .PERM => return error.PermissionDenied, - else => |err| return unexpectedErrno(err), - } -} - -pub fn getuid() uid_t { - return system.getuid(); -} - -pub fn geteuid() uid_t { - return system.geteuid(); -} - -pub fn getgid() gid_t { - return system.getgid(); -} - -pub fn getegid() gid_t { - return system.getegid(); -} - pub const SocketError = error{ /// Permission to create a socket of the specified type and/or /// pro‐tocol is denied. @@ -2800,20 +2739,6 @@ pub fn tcsetpgrp(handle: fd_t, pgrp: pid_t) TermioSetPgrpError!void { } } -pub const SetSidError = error{ - /// The calling process is already a process group leader, or the process group ID of a process other than the calling process matches the process ID of the calling process. - PermissionDenied, -} || UnexpectedError; - -pub fn setsid() SetSidError!pid_t { - const rc = system.setsid(); - switch (errno(rc)) { - .SUCCESS => return @intCast(rc), - .PERM => return error.PermissionDenied, - else => |err| return unexpectedErrno(err), - } -} - pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t { const rc = system.signalfd(fd, mask, flags); switch (errno(rc)) { diff --git a/lib/std/posix/test.zig b/lib/std/posix/test.zig index 31ed23211e..d2e34c0556 100644 --- a/lib/std/posix/test.zig +++ b/lib/std/posix/test.zig @@ -35,14 +35,14 @@ test "check WASI CWD" { test "getuid" { if (native_os == .windows or native_os == .wasi) return error.SkipZigTest; - _ = posix.getuid(); - _ = posix.geteuid(); + _ = posix.system.getuid(); + _ = posix.system.geteuid(); } test "getgid" { if (native_os == .windows or native_os == .wasi) return error.SkipZigTest; - _ = posix.getgid(); - _ = posix.getegid(); + _ = posix.system.getgid(); + _ = posix.system.getegid(); } test "sigaltstack" {