From 7d9da342597811e463c5407b94177a0d0d4061dd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 5 Jun 2024 09:30:21 -0700 Subject: [PATCH] termio/exec: move subprocess into cgroup --- src/os/cgroup.zig | 12 ++++++++++++ src/termio/Exec.zig | 3 +-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/os/cgroup.zig b/src/os/cgroup.zig index 8e666e89d..aee772954 100644 --- a/src/os/cgroup.zig +++ b/src/os/cgroup.zig @@ -52,6 +52,18 @@ pub fn create( } } +/// Move the given PID into the given cgroup. +pub fn moveInto( + cgroup: []const u8, + pid: std.os.linux.pid_t, +) !void { + var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const path = try std.fmt.bufPrint(&buf, "/sys/fs/cgroup{s}/cgroup.procs", .{cgroup}); + const file = try std.fs.cwd().openFile(path, .{ .mode = .write_only }); + defer file.close(); + try file.writer().print("{}", .{pid}); +} + /// Returns all available cgroup controllers for the given cgroup. /// The cgroup should have a '/'-prefix. /// diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 0809a4b9b..1978659dd 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -1346,8 +1346,7 @@ const Subprocess = struct { // If we have a cgroup set, then we want to move into that cgroup. if (comptime builtin.os.tag == .linux) { if (self.linux_cgroup) |cgroup| { - // TODO: do it - _ = cgroup; + try internal_os.cgroup.moveInto(cgroup, 0); } } }