mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Fix race condition in create_v2_cgroup
There used to be a race condition while creating the cgroup hierarchy: if a parent directory was created after .exists() but before .create_dir, the function failed unnecessarily. This commit changes the function to use create_dir_all, which is the atomic variant of the above pattern, and fixes some surrounding docstrings on the way. Signed-off-by: Markus Rudy <mr@edgeless.systems>
This commit is contained in:
@@ -550,18 +550,18 @@ fn create_v2_cgroup(
|
|||||||
// path: "a/b/c"
|
// path: "a/b/c"
|
||||||
let elements = path.split('/').collect::<Vec<&str>>();
|
let elements = path.split('/').collect::<Vec<&str>>();
|
||||||
let last_index = elements.len() - 1;
|
let last_index = elements.len() - 1;
|
||||||
|
// Build up the directory hierarchy element by element, enabling the controllers for all
|
||||||
|
// parents along the way.
|
||||||
for (i, ele) in elements.iter().enumerate() {
|
for (i, ele) in elements.iter().enumerate() {
|
||||||
// ROOT/a
|
// ROOT/a
|
||||||
fp.push(ele);
|
fp.push(ele);
|
||||||
// create dir, need not check if is a file or directory
|
// create dir if necessary
|
||||||
if !fp.exists() {
|
if let Err(e) = std::fs::create_dir_all(fp.clone()) {
|
||||||
if let Err(e) = std::fs::create_dir(fp.clone()) {
|
return Err(Error::with_cause(ErrorKind::FsError, e));
|
||||||
return Err(Error::with_cause(ErrorKind::FsError, e));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if i < last_index {
|
if i < last_index {
|
||||||
// enable controllers for substree
|
// enable controllers for subtree
|
||||||
enable_controllers(&controllers, &fp);
|
enable_controllers(&controllers, &fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user