From bbe525c964adb9fa9f18755c67cd3c8035874320 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 4 Jun 2024 19:36:48 -0700 Subject: [PATCH] os: API to configure cgroup controllers --- src/apprt/gtk/cgroup.zig | 7 +++++-- src/os/cgroup.zig | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/apprt/gtk/cgroup.zig b/src/apprt/gtk/cgroup.zig index d808d1484..ecee3f149 100644 --- a/src/apprt/gtk/cgroup.zig +++ b/src/apprt/gtk/cgroup.zig @@ -67,8 +67,11 @@ fn enableControllers(alloc: Allocator, cgroup: []const u8) !void { if (it.rest().len > 0) try builder.append(' '); } - // TODO - log.warn("enabling controllers={s}", .{builder.items}); + // Enable them all + try internal_os.cgroup.configureControllers( + cgroup, + builder.items, + ); } /// Create a transient systemd scope unit for the current process. diff --git a/src/os/cgroup.zig b/src/os/cgroup.zig index 0a7cc541f..2f4f5d884 100644 --- a/src/os/cgroup.zig +++ b/src/os/cgroup.zig @@ -61,3 +61,25 @@ pub fn controllers(alloc: Allocator, cgroup: []const u8) ![]const u8 { const result = std.mem.trimRight(u8, contents, " \r\n"); return try alloc.dupe(u8, result); } + +/// Configure the set of controllers in the cgroup. The "v" should +/// be in a valid format for "cgroup.subtree_control" +pub fn configureControllers( + cgroup: []const u8, + v: []const u8, +) !void { + assert(cgroup[0] == '/'); + var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + + // Read the available controllers. These will be space separated. + const path = try std.fmt.bufPrint( + &buf, + "/sys/fs/cgroup{s}/cgroup.subtree_control", + .{cgroup}, + ); + const file = try std.fs.cwd().openFile(path, .{ .mode = .write_only }); + defer file.close(); + + // Write + try file.writer().writeAll(v); +}