Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support domainname #1214

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/libcontainer/src/process/container_init_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ fn apply_rest_namespaces(
if let Some(hostname) = spec.hostname() {
syscall.set_hostname(hostname)?;
}

if let Some(domainname) = spec.domainname() {
syscall.set_domainname(domainname)?;
}
}
}
Ok(())
Expand Down Expand Up @@ -576,6 +580,14 @@ mod tests {
.get_hostname_args();
assert_eq!(1, got_hostnames.len());
assert_eq!("youki".to_string(), got_hostnames[0]);

let got_domainnames = syscall
.as_ref()
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_domainname_args();
assert_eq!(0, got_domainnames.len());
Ok(())
}

Expand Down
21 changes: 20 additions & 1 deletion crates/libcontainer/src/syscall/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{any::Any, mem, path::Path, ptr};

use anyhow::{anyhow, bail, Context, Result};
use caps::{CapSet, CapsHashSet};
use libc::{c_char, uid_t};
use libc::{c_char, uid_t, setdomainname, EFAULT, EINVAL, EPERM};
use nix::fcntl;
use nix::{
errno::Errno,
Expand Down Expand Up @@ -198,6 +198,25 @@ impl Syscall for LinuxSyscall {
Ok(())
}

/// Sets domainname for process (see
/// [setdomainname(2)](https://man7.org/linux/man-pages/man2/setdomainname.2.html)).
fn set_domainname(&self, domainname: &str) -> Result<()> {
let ptr = domainname.as_bytes().as_ptr() as *const c_char;
let len = domainname.len();
let res = unsafe { setdomainname(ptr, len) };
if res == EFAULT {
bail!("Failed to set {} as domainname. domainname pointed outside of user address space.", domainname)
}
if res == EINVAL {
bail!("Failed to set {} as domainname. domainname len was negative or too large.", domainname)
}
if res == EPERM {
bail!("Failed to set {} as domainname. the caller did not have the CAP_SYS_ADMIN capability.", domainname)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using match instead of if statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified to match.
And when setdomainname syscall executed errno handling is invalid so fix it.


Ok(())
}

/// Sets resource limit for process
fn set_rlimit(&self, rlimit: &LinuxRlimit) -> Result<()> {
let rlim = &libc::rlimit {
Expand Down
1 change: 1 addition & 0 deletions crates/libcontainer/src/syscall/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait Syscall {
fn unshare(&self, flags: CloneFlags) -> Result<()>;
fn set_capability(&self, cset: CapSet, value: &CapsHashSet) -> Result<()>;
fn set_hostname(&self, hostname: &str) -> Result<()>;
fn set_domainname(&self, domainname: &str) -> Result<()>;
fn set_rlimit(&self, rlimit: &LinuxRlimit) -> Result<()>;
fn get_pwuid(&self, uid: u32) -> Option<Arc<OsStr>>;
fn mount(
Expand Down
16 changes: 16 additions & 0 deletions crates/libcontainer/src/syscall/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum ArgName {
Mknod,
Chown,
Hostname,
Domainname,
Groups,
Capability,
}
Expand All @@ -73,6 +74,7 @@ impl ArgName {
ArgName::Mknod,
ArgName::Chown,
ArgName::Hostname,
ArgName::Domainname,
ArgName::Groups,
ArgName::Capability,
]
Expand Down Expand Up @@ -163,6 +165,11 @@ impl Syscall for TestHelperSyscall {
.act(ArgName::Hostname, Box::new(hostname.to_owned()))
}

fn set_domainname(&self, domainname: &str) -> anyhow::Result<()> {
self.mocks
.act(ArgName::Domainname, Box::new(domainname.to_owned()))
}

fn set_rlimit(&self, _rlimit: &LinuxRlimit) -> anyhow::Result<()> {
todo!()
}
Expand Down Expand Up @@ -315,6 +322,15 @@ impl TestHelperSyscall {
.collect::<Vec<String>>()
}

pub fn get_domainname_args(&self) -> Vec<String> {
self.mocks
.fetch(ArgName::Domainname)
.values
.iter()
.map(|x| x.downcast_ref::<String>().unwrap().clone())
.collect::<Vec<String>>()
}

pub fn get_groups_args(&self) -> Vec<Vec<Gid>> {
self.mocks
.fetch(ArgName::Groups)
Expand Down