sandbox: fix the issue of missing setting hostname

When setup the persisten uts namespace, it's should
set the hostname for this ns.

Fixes: #175

Signed-off-by: fupan.lfp <fupan.lfp@antfin.com>
This commit is contained in:
fupan.lfp 2020-03-30 20:21:14 +08:00
parent 7d9bdf7b01
commit 986e666b0b
2 changed files with 20 additions and 4 deletions

View File

@ -37,6 +37,8 @@ pub struct Namespace {
pub path: String, pub path: String,
persistent_ns_dir: String, persistent_ns_dir: String,
ns_type: NamespaceType, ns_type: NamespaceType,
//only used for uts namespace
pub hostname: Option<String>,
} }
impl Namespace { impl Namespace {
@ -46,6 +48,7 @@ impl Namespace {
path: String::from(""), path: String::from(""),
persistent_ns_dir: String::from(PERSISTENT_NS_DIR), persistent_ns_dir: String::from(PERSISTENT_NS_DIR),
ns_type: NamespaceType::IPC, ns_type: NamespaceType::IPC,
hostname: None,
} }
} }
@ -54,8 +57,11 @@ impl Namespace {
self self
} }
pub fn as_uts(mut self) -> Self { pub fn as_uts(mut self, hostname: &str) -> Self {
self.ns_type = NamespaceType::UTS; self.ns_type = NamespaceType::UTS;
if hostname != "" {
self.hostname = Some(String::from(hostname));
}
self self
} }
@ -82,6 +88,7 @@ impl Namespace {
} }
self.path = new_ns_path.clone().into_os_string().into_string().unwrap(); self.path = new_ns_path.clone().into_os_string().into_string().unwrap();
let hostname = self.hostname.clone();
let new_thread = thread::spawn(move || { let new_thread = thread::spawn(move || {
let origin_ns_path = get_current_thread_ns_path(&ns_type.get()); let origin_ns_path = get_current_thread_ns_path(&ns_type.get());
@ -98,6 +105,12 @@ impl Namespace {
return Err(err.to_string()); return Err(err.to_string());
} }
if ns_type == NamespaceType::UTS && hostname.is_some() {
match nix::unistd::sethostname(hostname.unwrap()) {
Err(err) => return Err(err.to_string()),
Ok(_) => (),
}
}
// Bind mount the new namespace from the current thread onto the mount point to persist it. // Bind mount the new namespace from the current thread onto the mount point to persist it.
let source: &str = origin_ns_path.as_str(); let source: &str = origin_ns_path.as_str();
let destination: &str = new_ns_path.as_path().to_str().unwrap_or("none"); let destination: &str = new_ns_path.as_path().to_str().unwrap_or("none");
@ -136,7 +149,7 @@ impl Namespace {
} }
/// Represents the Namespace type. /// Represents the Namespace type.
#[derive(Clone, Copy)] #[derive(Clone, Copy, PartialEq)]
enum NamespaceType { enum NamespaceType {
IPC, IPC,
UTS, UTS,
@ -201,7 +214,7 @@ mod tests {
let tmpdir = Builder::new().prefix("ipc").tempdir().unwrap(); let tmpdir = Builder::new().prefix("ipc").tempdir().unwrap();
let ns_uts = Namespace::new(&logger) let ns_uts = Namespace::new(&logger)
.as_uts() .as_uts("test_hostname")
.set_root_dir(tmpdir.path().to_str().unwrap()) .set_root_dir(tmpdir.path().to_str().unwrap())
.setup(); .setup();

View File

@ -171,7 +171,10 @@ impl Sandbox {
}; };
// // Set up shared UTS namespace // // Set up shared UTS namespace
self.shared_utsns = match Namespace::new(&self.logger).as_uts().setup() { self.shared_utsns = match Namespace::new(&self.logger)
.as_uts(self.hostname.as_str())
.setup()
{
Ok(ns) => ns, Ok(ns) => ns,
Err(err) => { Err(err) => {
return Err(ErrorKind::ErrorCode(format!( return Err(ErrorKind::ErrorCode(format!(