diff options
| author | Joakim Hulthe <joakim.hulthe@mullvad.net> | 2026-01-02 12:20:11 +0100 |
|---|---|---|
| committer | Joakim Hulthe <joakim.hulthe@mullvad.net> | 2026-01-22 09:49:13 +0100 |
| commit | 1235160e6685f78d065c83cec73c931a7b2cf5cc (patch) | |
| tree | ea4b8e1575c9ab5b9de7a40d6ff527236dae227c | |
| parent | 6f48daea272d4bf813b782438fe4bcaac0c7febc (diff) | |
| download | mullvadvpn-1235160e6685f78d065c83cec73c931a7b2cf5cc.tar.xz mullvadvpn-1235160e6685f78d065c83cec73c931a7b2cf5cc.zip | |
Clean up parsing of /proc/<pid>/cgroup
| -rw-r--r-- | mullvad-exclude/src/main.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/mullvad-exclude/src/main.rs b/mullvad-exclude/src/main.rs index fa8e0472a3..e88834c05e 100644 --- a/mullvad-exclude/src/main.rs +++ b/mullvad-exclude/src/main.rs @@ -37,22 +37,28 @@ mod inner { } } + /// Get the [`CGroup2`] of the current process. fn get_current_cgroup() -> anyhow::Result<CGroup2> { let cgroup_file = std::fs::read_to_string("/proc/self/cgroup") .context("Failed to read /proc/self/cgroup")?; - // TODO: is there a nicer way to get current cgroup? - // /proc/self/cgroup contains a line that looks like this: - // 0::/user.slice/user-1000.slice/user@1000.service/app.slice/app-launcher-appname-1234.scope - let cgroup_path = cgroup_file + /// Parse a line from /proc/<pid>/cgroup. See `man cgroup(7)` + fn parse_line(line: &str) -> Option<(&str, &str, &str)> { + let line = line.trim(); + let (hierarchy_id, line) = line.split_once(':')?; + let (controller_list, cgroup_path) = line.split_once(':')?; + Some((hierarchy_id, controller_list, cgroup_path)) + } + + let (_, _, cgroup_path) = cgroup_file .lines() - .filter_map(|line| line.strip_prefix("0::/")) + .filter_map(parse_line) + .filter(|&(hierarchy_id, _, _)| hierarchy_id == "0") // cgroup2 hierarchy_id is 0 .next() - .context("Expected a line starting with '0::/' containing the cgroup path") - .context("Failed to parse /proc/self/cgroup")? - .trim(); - let cgroup_fs_path = Path::new("/sys/fs/cgroup").join(cgroup_path); - let cgroup = CGroup2::open(cgroup_fs_path).context("Failed to open cgroup")?; + .context("Expected a line starting with '0::/' containing the cgroup2 path") + .context("Failed to parse /proc/self/cgroup")?; + let cgroup_fs_path = Path::new("/sys/fs/cgroup").join(cgroup_path.trim_start_matches('/')); + let cgroup = CGroup2::open(cgroup_fs_path).context("Failed to open cgroup2")?; Ok(cgroup) } |
