os: API to configure cgroup controllers

This commit is contained in:
Mitchell Hashimoto
2024-06-04 19:36:48 -07:00
parent b5c4d2f60d
commit bbe525c964
2 changed files with 27 additions and 2 deletions

View File

@ -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.

View File

@ -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);
}