Skip to content

Commit

Permalink
Merge pull request #124 from kunai-project/opt-namespace
Browse files Browse the repository at this point in the history
optimize(user): do not setns when it is not needed
  • Loading branch information
qjerome authored Oct 14, 2024
2 parents 216305a + a8b0ffd commit 4438c13
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions kunai/src/util/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ impl Namespace {
#[derive(Debug)]
pub struct Switcher {
pub namespace: Namespace,
src: File,
dst: File,
src: Option<File>,
dst: Option<File>,
}

#[derive(Debug, Error)]
Expand All @@ -191,30 +191,44 @@ pub enum Error {
impl Switcher {
pub fn new(kind: Kind, pid: u32) -> Result<Self, Error> {
let self_pid = process::id();
let ns = Namespace::from_pid(kind, pid)?;

// namespace of the current process
let src = Namespace::open(kind, self_pid)?;
// namespace of the target process
let dst = Namespace::open(kind, pid)?;
let self_ns = Namespace::from_pid(kind, self_pid)?;
let target_ns = Namespace::from_pid(kind, pid)?;

let (src, dst) = if self_ns == target_ns {
(None, None)
} else {
// namespace of the current process
let src = Namespace::open(kind, self_pid)?;
// namespace of the target process
let dst = Namespace::open(kind, pid)?;
(Some(src), Some(dst))
};

Ok(Self {
namespace: ns,
namespace: target_ns,
src,
dst,
})
}

#[inline]
pub fn enter(&self) -> Result<(), Error> {
// according to setns doc we can set nstype = 0 if we know what kind of NS we navigate into
setns(self.dst.as_raw_fd(), CLONE_NEWNS).map_err(|e| Error::SetNs(self.namespace, e))
if let Some(dst) = self.dst.as_ref() {
// according to setns doc we can set nstype = 0 if we know what kind of NS we navigate into
setns(dst.as_raw_fd(), CLONE_NEWNS).map_err(|e| Error::SetNs(self.namespace, e))
} else {
Ok(())
}
}

#[inline]
pub fn exit(&self) -> Result<(), Error> {
// according to setns doc we can set nstype = 0 if we know what kind of NS we navigate into
setns(self.src.as_raw_fd(), CLONE_NEWNS).map_err(|e| Error::SetNs(self.namespace, e))
if let Some(src) = self.src.as_ref() {
// according to setns doc we can set nstype = 0 if we know what kind of NS we navigate into
setns(src.as_raw_fd(), CLONE_NEWNS).map_err(|e| Error::SetNs(self.namespace, e))
} else {
Ok(())
}
}
}

Expand Down

0 comments on commit 4438c13

Please sign in to comment.