diff --git a/src/Command.zig b/src/Command.zig index 6b6752e05..c8cf00758 100644 --- a/src/Command.zig +++ b/src/Command.zig @@ -19,7 +19,6 @@ const Command = @This(); const std = @import("std"); const builtin = @import("builtin"); const internal_os = @import("os/main.zig"); -const termio = @import("termio.zig"); const windows = internal_os.windows; const TempDir = internal_os.TempDir; const mem = std.mem; @@ -64,7 +63,7 @@ stderr: ?File = null, /// exec process takes over, such as signal handlers, setsid, setuid, etc. pre_exec: ?*const PreExecFn = null, -linux_cgroup: termio.Options.LinuxCgroup = termio.Options.linux_cgroup_default, +linux_cgroup: LinuxCgroup = linux_cgroup_default, /// If set, then the process will be created attached to this pseudo console. /// `stdin`, `stdout`, and `stderr` will be ignored if set. @@ -78,6 +77,11 @@ data: ?*anyopaque = null, /// Process ID is set after start is called. pid: ?posix.pid_t = null, +/// LinuxCGroup type depends on our target OS +pub const LinuxCgroup = if (builtin.os.tag == .linux) ?[]const u8 else void; +pub const linux_cgroup_default = if (LinuxCgroup == void) +{} else null; + /// The various methods a process may exit. pub const Exit = if (builtin.os.tag == .windows) union(enum) { Exited: u32, @@ -138,8 +142,13 @@ fn startPosix(self: *Command, arena: Allocator) !void { else @compileError("missing env vars"); + // Fork. If we have a cgroup specified on Linxu then we use clone const pid: posix.pid_t = switch (builtin.os.tag) { - .linux => if (self.linux_cgroup) |cgroup| try internal_os.cgroup.cloneInto(cgroup) else try posix.fork(), + .linux => if (self.linux_cgroup) |cgroup| + try internal_os.cgroup.cloneInto(cgroup) + else + try posix.fork(), + else => try posix.fork(), }; diff --git a/src/os/cgroup.zig b/src/os/cgroup.zig index 5afc8cdca..417f5641e 100644 --- a/src/os/cgroup.zig +++ b/src/os/cgroup.zig @@ -79,15 +79,14 @@ pub fn cloneInto(cgroup: []const u8) !posix.pid_t { const fd: linux.fd_t = fd: { const rc = linux.open(path, linux.O{ .PATH = true, .DIRECTORY = true }, 0); switch (posix.errno(rc)) { - .SUCCESS => { - break :fd @as(linux.fd_t, @intCast(rc)); - }, + .SUCCESS => break :fd @as(linux.fd_t, @intCast(rc)), else => |errno| { log.err("unable to open cgroup dir {s}: {}", .{ path, errno }); - break :fd -1; + return error.CloneError; }, } }; + assert(fd >= 0); const args: extern struct { flags: u64, @@ -102,7 +101,7 @@ pub fn cloneInto(cgroup: []const u8) !posix.pid_t { set_tid_size: u64, cgroup: u64, } = .{ - .flags = if (fd >= 0) linux.CLONE.INTO_CGROUP else 0, + .flags = linux.CLONE.INTO_CGROUP, .pidfd = 0, .child_tid = 0, .parent_tid = 0, @@ -112,19 +111,17 @@ pub fn cloneInto(cgroup: []const u8) !posix.pid_t { .tls = 0, .set_tid = 0, .set_tid_size = 0, - .cgroup = if (fd >= 0) @intCast(fd) else 0, + .cgroup = @intCast(fd), }; const rc = linux.syscall2(linux.SYS.clone3, @intFromPtr(&args), @sizeOf(@TypeOf(args))); - switch (posix.errno(rc)) { - .SUCCESS => { - return @as(posix.pid_t, @intCast(rc)); - }, - else => |errno| { + return switch (posix.errno(rc)) { + .SUCCESS => @as(posix.pid_t, @intCast(rc)), + else => |errno| err: { log.err("unable to clone: {}", .{errno}); - return error.CloneError; + break :err error.CloneError; }, - } + }; } /// Returns all available cgroup controllers for the given cgroup. diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 619babb47..58f1a0b50 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -897,7 +897,7 @@ const Subprocess = struct { pty: ?Pty = null, command: ?Command = null, flatpak_command: ?FlatpakHostCommand = null, - linux_cgroup: termio.Options.LinuxCgroup = termio.Options.linux_cgroup_default, + linux_cgroup: Command.LinuxCgroup = Command.linux_cgroup_default, /// Initialize the subprocess. This will NOT start it, this only sets /// up the internal state necessary to start it later. @@ -1196,8 +1196,8 @@ const Subprocess = struct { // If we have a cgroup, then we copy that into our arena so the // memory remains valid when we start. - const linux_cgroup: termio.Options.LinuxCgroup = cgroup: { - const default = termio.Options.linux_cgroup_default; + const linux_cgroup: Command.LinuxCgroup = cgroup: { + const default = Command.linux_cgroup_default; if (comptime builtin.os.tag != .linux) break :cgroup default; const path = opts.linux_cgroup orelse break :cgroup default; break :cgroup try alloc.dupe(u8, path); diff --git a/src/termio/Options.zig b/src/termio/Options.zig index 079828f38..b4edb473a 100644 --- a/src/termio/Options.zig +++ b/src/termio/Options.zig @@ -4,6 +4,7 @@ const builtin = @import("builtin"); const xev = @import("xev"); const apprt = @import("../apprt.zig"); const renderer = @import("../renderer.zig"); +const Command = @import("../Command.zig"); const Config = @import("../config.zig").Config; const termio = @import("../termio.zig"); @@ -45,7 +46,4 @@ surface_mailbox: apprt.surface.Mailbox, /// The cgroup to apply to the started termio process, if able by /// the termio implementation. This only applies to Linux. -linux_cgroup: LinuxCgroup = linux_cgroup_default, - -pub const LinuxCgroup = if (builtin.os.tag == .linux) ?[]const u8 else void; -pub const linux_cgroup_default = if (LinuxCgroup == void) {} else null; +linux_cgroup: Command.LinuxCgroup = Command.linux_cgroup_default,