commit e8a4e47d384ab059c856fc99755f5e176bab67d2 (tree)
parent c6a18e9534025bb84f2707b3d2bd7437f2168a7e
Author: Brandon Black <bblack@wikimedia.org>
Date: Fri, 11 Jul 2025 08:33:15 -0500
Add setsid to std.(c|posix)
The interface and errors for this seem to be very universal and
generic. Note Linux already has this defined as a syscall as well.
Diffstat:
2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/lib/std/c.zig b/lib/std/c.zig
@@ -10808,6 +10808,7 @@ pub extern "c" fn if_nametoindex([*:0]const u8) c_int;
pub extern "c" fn getpid() pid_t;
pub extern "c" fn getppid() pid_t;
+pub extern "c" fn setsid() pid_t;
/// These are implementation defined but share identical values in at least musl and glibc:
/// - https://git.musl-libc.org/cgit/musl/tree/include/locale.h?id=ab31e9d6a0fa7c5c408856c89df2dfb12c344039#n18
diff --git a/lib/std/posix.zig b/lib/std/posix.zig
@@ -7089,6 +7089,20 @@ 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 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)) {