From d0d6647fdbfbe1a5764c2624e46eee35052d0da6 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 3 Sep 2020 15:22:43 +0200 Subject: [PATCH] std: add seteuid/setegid to std.os Currently these are only implemented for linux. --- lib/std/os.zig | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index 3574468a3..8b923989e 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -2512,11 +2512,12 @@ pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) Read } } -pub const SetIdError = error{ - ResourceLimitReached, +pub const SetEidError = error{ InvalidUserId, PermissionDenied, -} || UnexpectedError; +}; + +pub const SetIdError = error{ResourceLimitReached} || SetEidError || UnexpectedError; pub fn setuid(uid: uid_t) SetIdError!void { switch (errno(system.setuid(uid))) { @@ -2528,6 +2529,15 @@ pub fn setuid(uid: uid_t) SetIdError!void { } } +pub fn seteuid(uid: uid_t) SetEidError!void { + switch (errno(system.seteuid(uid))) { + 0 => return, + EINVAL => return error.InvalidUserId, + EPERM => return error.PermissionDenied, + else => |err| return unexpectedErrno(err), + } +} + pub fn setreuid(ruid: uid_t, euid: uid_t) SetIdError!void { switch (errno(system.setreuid(ruid, euid))) { 0 => return, @@ -2548,6 +2558,15 @@ pub fn setgid(gid: gid_t) SetIdError!void { } } +pub fn setegid(uid: uid_t) SetEidError!void { + switch (errno(system.setegid(uid))) { + 0 => return, + EINVAL => return error.InvalidUserId, + EPERM => return error.PermissionDenied, + else => |err| return unexpectedErrno(err), + } +} + pub fn setregid(rgid: gid_t, egid: gid_t) SetIdError!void { switch (errno(system.setregid(rgid, egid))) { 0 => return,