Skip to content

Commit c408641

Browse files
committed
jailer: hotfix inheritance of cpuset config
This hotfix add a racy but decent solution to creating and inheriting cpuset settings from parent cgroups. Will add the final fix soon. Signed-off-by: Alexandru Agache <[email protected]>
1 parent 030570c commit c408641

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

jailer/src/cgroup.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22
use std::ffi::OsStr;
3-
use std::fs::{self, File};
3+
use std::fs::{self, File, OpenOptions};
44
use std::io::{BufRead, BufReader, Write};
55
use std::path::{Path, PathBuf};
66
use std::process;
@@ -34,6 +34,28 @@ where
3434
.map_err(|e| Error::Write(PathBuf::from(file_path.as_ref()), e))
3535
}
3636

37+
fn inherit_from_parent(path: &mut PathBuf, what: &'static str) -> Result<()> {
38+
// This unwrap() is safe because path_buf is guaranteed to have a parent.
39+
let mut parent_what = PathBuf::from(path.parent().unwrap());
40+
parent_what.push(what);
41+
42+
let f = OpenOptions::new()
43+
.read(true)
44+
.open(&parent_what)
45+
.map_err(|e| Error::FileOpen(parent_what.clone(), e))?;
46+
47+
let mut line = String::new();
48+
BufReader::new(f)
49+
.read_line(&mut line)
50+
.map_err(|e| Error::ReadLine(parent_what.clone(), e))?;
51+
52+
path.push(what);
53+
writeln_special(&path, &line)?;
54+
path.pop();
55+
56+
Ok(())
57+
}
58+
3759
impl Cgroup {
3860
pub fn new(id: &str, numa_node: u32, exec_file_name: &OsStr) -> Result<Self> {
3961
let f =
@@ -84,17 +106,27 @@ impl Cgroup {
84106

85107
for (controller, mut path_buf) in found_controllers.drain() {
86108
path_buf.push(exec_file_name);
109+
110+
// TODO: fix this racy thing.
111+
if controller == "cpuset" && !path_buf.exists() {
112+
fs::create_dir_all(&path_buf).map_err(|e| Error::CreateDir(path_buf.clone(), e))?;
113+
114+
inherit_from_parent(&mut path_buf, "cpuset.mems")?;
115+
inherit_from_parent(&mut path_buf, "cpuset.cpus")?;
116+
}
117+
87118
path_buf.push(id);
88119

89-
// Create the folders first. The cpuset.cpus and cpuset.mems files really do appear to
90-
// be inherited AND populated automatically :-s
91120
fs::create_dir_all(&path_buf).map_err(|e| Error::CreateDir(path_buf.clone(), e))?;
92121

93122
if controller == "cpuset" {
94123
// Enforce NUMA node restriction.
95124
path_buf.push("cpuset.mems");
96125
writeln_special(&path_buf, numa_node)?;
97126
path_buf.pop();
127+
128+
// Inherit cpuset
129+
inherit_from_parent(&mut path_buf, "cpuset.cpus")?;
98130
}
99131

100132
// And now add "tasks" to get the path of the corresponding tasks file.

0 commit comments

Comments
 (0)