diff --git a/src/spawner/mod.rs b/src/spawner/mod.rs index c310ee8..899ffcb 100644 --- a/src/spawner/mod.rs +++ b/src/spawner/mod.rs @@ -169,6 +169,7 @@ impl<'a> Spawner<'a> { let mut builder = VoidBuilder::new(); builder.mount("/entrypoint", "/entrypoint"); + // TODO: move the weird logic out of this part for env in &spec.environment { match env { Environment::Filesystem { @@ -177,6 +178,13 @@ impl<'a> Spawner<'a> { } => { builder.mount(environment_path, environment_path); } + + Environment::Hostname(name) => { + builder.set_hostname(name); + } + Environment::DomainName(name) => { + builder.set_domain_name(name); + } } } @@ -236,6 +244,7 @@ impl<'a> Spawner<'a> { builder.keep_fd(fd); } + // TODO: move the weird logic out of this part for env in &spec.environment { match env { Environment::Filesystem { @@ -244,6 +253,13 @@ impl<'a> Spawner<'a> { } => { builder.mount(environment_path, environment_path); } + + Environment::Hostname(name) => { + builder.set_hostname(name); + } + Environment::DomainName(name) => { + builder.set_domain_name(name); + } } } @@ -307,6 +323,13 @@ impl<'a> Spawner<'a> { } => { builder.mount(host_path, environment_path); } + + Environment::Hostname(name) => { + builder.set_hostname(name); + } + Environment::DomainName(name) => { + builder.set_domain_name(name); + } } } } diff --git a/src/specification.rs b/src/specification.rs index f5cfa3c..2ce9c97 100644 --- a/src/specification.rs +++ b/src/specification.rs @@ -114,6 +114,9 @@ pub enum Environment { host_path: PathBuf, environment_path: PathBuf, }, + + Hostname(String), + DomainName(String), } #[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Debug)] diff --git a/src/void.rs b/src/void.rs index d0bedbb..06a4eda 100644 --- a/src/void.rs +++ b/src/void.rs @@ -13,7 +13,7 @@ use std::path::{Path, PathBuf}; use nix::fcntl::{FcntlArg, FdFlag}; use nix::mount::{mount, umount2, MntFlags, MsFlags}; use nix::sys::signal::{signal, SigHandler, Signal}; -use nix::unistd::{close, getgid, getuid, pivot_root, Gid, Pid, Uid}; +use nix::unistd::{close, getgid, getuid, pivot_root, sethostname, Gid, Pid, Uid}; use close_fds::CloseFdsBuilder; @@ -28,6 +28,9 @@ impl fmt::Display for VoidHandle { } pub struct VoidBuilder { + hostname: Option, + domain_name: Option, + mounts: HashMap, fds: HashSet, } @@ -35,11 +38,23 @@ pub struct VoidBuilder { impl VoidBuilder { pub fn new() -> VoidBuilder { VoidBuilder { + hostname: None, + domain_name: None, mounts: HashMap::new(), fds: HashSet::new(), } } + pub fn set_hostname>(&mut self, hostname: T) -> &mut Self { + self.hostname = Some(hostname.into()); + self + } + + pub fn set_domain_name>(&mut self, domain_name: T) -> &mut Self { + self.domain_name = Some(domain_name.into()); + self + } + pub fn mount, T2: AsRef>(&mut self, src: T1, dst: T2) -> &mut Self { self.mounts.insert(src.as_ref().into(), dst.as_ref().into()); self @@ -128,7 +143,16 @@ impl VoidBuilder { * parent values for each of these. */ fn void_uts_namespace(&self) -> Result<()> { - // TODO: void uts namespace + sethostname(self.hostname.as_deref().unwrap_or("void")).map_err(|e| Error::Nix { + msg: "sethostname", + src: e, + })?; + + setdomainname(self.domain_name.as_deref().unwrap_or("(none)")).map_err(|e| Error::Nix { + msg: "setdomainname", + src: e, + })?; + Ok(()) } @@ -342,3 +366,13 @@ impl VoidBuilder { Ok(()) } } + +pub fn setdomainname>(name: S) -> nix::Result<()> { + use std::os::unix::ffi::OsStrExt; + + let ptr = name.as_ref().as_bytes().as_ptr() as *const libc::c_char; + let len = name.as_ref().len() as libc::size_t; + + let res = unsafe { libc::setdomainname(ptr, len) }; + nix::Error::result(res).map(drop) +}