use crate::sys::utils::get_all_utf8_data;
use std::path::{Path, PathBuf};
use super::super::cgroup::types::*;
pub(super) fn get_cgroup_path(path: &Path) -> Option<CGroupPath> {
let content = get_all_utf8_data(path, 4096).ok()?;
let cgroup_path = parse_cgroup_path(&content);
if cgroup_path.is_empty() {
return None;
}
Some(cgroup_path)
}
pub(super) fn get_cgroup_mounts(path: &Path) -> Option<CGroupMounts> {
let content = get_all_utf8_data(path, 16_385).ok()?;
Some(parse_cgroup_mounts(&content))
}
pub(super) fn cgroup_base_for_path(
cgroup_path: &Path,
mounts: &[CGroupMount],
) -> Option<CGroupBase> {
mounts
.iter()
.find_map(|mount| resolve_cgroup_base(cgroup_path, mount))
}
pub(super) fn resolve_cgroup_base(cgroup_path: &Path, mount: &CGroupMount) -> Option<CGroupBase> {
let relative_path = if mount.root.as_os_str().is_empty() {
cgroup_path
} else {
cgroup_path.strip_prefix(&mount.root).ok()?
};
Some(CGroupBase::new(
join_cgroup_path(&mount.mount_point, relative_path),
mount.mount_point.clone(),
))
}
pub(super) fn join_cgroup_path(root: &Path, path: &Path) -> PathBuf {
if path.as_os_str().is_empty() {
return root.to_path_buf();
}
root.join(path)
}
pub(super) fn parse_cgroup_mounts(content: &str) -> CGroupMounts {
let mut mounts = CGroupMounts::default();
for line in content.lines() {
let mut fields = line.split(' ');
if let Some(root) = fields.nth(3)
&& let Some(mount_point) = fields.next()
&& fields.by_ref().skip(1).any(|field| field == "-")
&& let Some(filesystem_type) = fields.next()
&& let Some(super_options) = fields.nth(1)
{
let mount = CGroupMount {
root: normalize_mountinfo_path(root),
mount_point: PathBuf::from(decode_mountinfo_path(mount_point)),
};
match filesystem_type {
"cgroup2" => mounts.v2.push(mount),
"cgroup" if super_options.split(',').any(|option| option == "memory") => {
mounts.v1_memory.push(mount)
}
_ => (),
}
}
}
mounts
}
pub(super) fn parse_cgroup_path(content: &str) -> CGroupPath {
let mut cgroup_path = CGroupPath::default();
for line in content.lines() {
let mut fields = line.splitn(3, ':');
if let Some(hierarchy_id) = fields.next()
&& let Some(controllers) = fields.next()
&& let Some(path) = fields.next()
{
if hierarchy_id == "0" && controllers.is_empty() {
cgroup_path.v2 = Some(normalize_cgroup_path(path));
} else if controllers
.split(',')
.any(|controller| controller == "memory")
{
cgroup_path.v1_memory = Some(normalize_cgroup_path(path));
}
}
}
cgroup_path
}
pub(super) fn normalize_cgroup_path(path: &str) -> PathBuf {
if let Ok(path) = Path::new(path).strip_prefix("/") {
return path.to_path_buf();
}
PathBuf::from(path)
}
pub(super) fn normalize_mountinfo_path(path: &str) -> PathBuf {
let path = decode_mountinfo_path(path);
if let Ok(path) = Path::new(&path).strip_prefix("/") {
return path.to_path_buf();
}
PathBuf::from(path)
}
pub(super) fn decode_mountinfo_path(path: &str) -> String {
let bytes = path.as_bytes();
let mut decoded = Vec::with_capacity(bytes.len());
let mut pos = 0;
while pos < bytes.len() {
if bytes[pos] == b'\\'
&& pos + 3 < bytes.len()
&& let Some(value) = decode_octal_escape(&bytes[pos + 1..pos + 4])
{
decoded.push(value);
pos += 4;
continue;
}
decoded.push(bytes[pos]);
pos += 1;
}
String::from_utf8(decoded).unwrap_or_else(|_| path.to_owned())
}
pub(super) fn decode_octal_escape(digits: &[u8]) -> Option<u8> {
let mut value = 0;
for digit in digits {
if !(b'0'..=b'7').contains(digit) {
return None;
}
value = value * 8 + (digit - b'0');
}
Some(value)
}