const std = @import("std"); const c = std.c; pub const PosixSpawnError = error{ AccessDenied, ArgumentListTooLong, BadFileDescriptor, FileBusy, FileNotFound, FileSystem, InvalidAddress, InvalidExecutable, InvalidSpawnArguments, IsDirectory, NameTooLong, NotDirectory, OutOfMemory, PermissionDenied, ProcessFdQuotaExceeded, SymLinkLoop, SystemFdQuotaExceeded, SystemResources, UnsupportedArchitecture, } || std.posix.UnexpectedError; fn posixSpawnError(result: c_int) PosixSpawnError { const code: c.E = @enumFromInt(@as(u16, @intCast(result))); return switch (code) { .@"2BIG" => error.ArgumentListTooLong, .ACCES => error.AccessDenied, .BADF => error.BadFileDescriptor, .IO => error.FileSystem, .TXTBSY => error.FileBusy, .NOENT => error.FileNotFound, .FAULT => error.InvalidAddress, .NOEXEC, .BADEXEC, .SHLIBVERS, .FTYPE => error.InvalidExecutable, .INVAL => error.InvalidSpawnArguments, .ISDIR => error.IsDirectory, .NAMETOOLONG => error.NameTooLong, .NOTDIR => error.NotDirectory, .NOMEM => error.OutOfMemory, .PERM => error.PermissionDenied, .MFILE => error.ProcessFdQuotaExceeded, .NFILE => error.SystemFdQuotaExceeded, .AGAIN, .PROCLIM => error.SystemResources, .LOOP => error.SymLinkLoop, .BADARCH => error.UnsupportedArchitecture, else => std.posix.unexpectedErrno(code), }; } pub fn launchSuspended(path: [:0]const u8) !c.pid_t { std.debug.assert(path.len != 0); var attrs: c.posix_spawnattr_t = undefined; const attr_init_res = c.posix_spawnattr_init(&attrs); if (attr_init_res != 0) return posixSpawnError(attr_init_res); defer { const attr_dstr_res = c.posix_spawnattr_destroy(&attrs); std.debug.assert(attr_dstr_res == 0); } const flags: c.POSIX_SPAWN = .{ .START_SUSPENDED = true, }; const flags_res = c.posix_spawnattr_setflags(&attrs, flags); if (flags_res != 0) return posixSpawnError(flags_res); var pid: c.pid_t = 0; const argv = [_:null]?[*:0]const u8{ path.ptr }; const result = c.posix_spawn(&pid, path.ptr, null, &attrs, &argv, c.environ); if (result != 0) return posixSpawnError(result); std.debug.assert(pid > 0); std.debug.assert(pid != c.getpid()); return pid; }