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

Expose jail_attach #18

Merged
merged 2 commits into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub enum JailError {
#[fail(display = "jail_set syscall failed. The error message returned was: {}", _0)]
JailSetError(String),

#[fail(display = "jail_attach syscall failed. The error message returned was: {}", _0)]
JailAttachError(#[cause] io::Error),

#[fail(display = "invalid return code from jail_remove")]
JailRemoveFailed,

Expand Down
19 changes: 6 additions & 13 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Jail-Specific extensions to the `std::process` module

use libc;

use std::process;

use std::io::{Error, ErrorKind};
use std::os::unix::process::CommandExt;

use JailError;
use RunningJail;

/// Extension to the `std::process::Command` builder to run the command in a
Expand Down Expand Up @@ -45,17 +43,12 @@ pub trait Jailed {
#[cfg(target_os = "freebsd")]
impl Jailed for process::Command {
fn jail(&mut self, jail: &RunningJail) -> &mut process::Command {
let jid = jail.jid;
let jail = *jail;
self.before_exec(move || {
let ret = unsafe { libc::jail_attach(jid) };
match ret {
0 => Ok(()),
-1 => Err(Error::last_os_error()),
_ => Err(Error::new(
ErrorKind::Other,
"invalid return value from jail_attach",
)),
}
jail.attach().map_err(|err| match err {
JailError::JailAttachError(e) => e,
_ => panic!("jail.attach() failed with unexpected error"),
})
});

self
Expand Down
17 changes: 16 additions & 1 deletion src/running.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use libc;
use param;
use rctl;
use sys;
use JailError;
use StoppedJail;

use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use std::net;
use std::path;

/// Represents a running jail.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
#[cfg(target_os = "freebsd")]
pub struct RunningJail {
/// The `jid` of the jail
Expand Down Expand Up @@ -408,6 +410,19 @@ impl RunningJail {
.usage()
.map_err(JailError::RctlError)
}

/// Jail the current process into the given jail.
pub fn attach(&self) -> Result<(), JailError> {
let ret = unsafe { libc::jail_attach(self.jid) };
match ret {
0 => Ok(()),
-1 => Err(Error::last_os_error()),
_ => Err(Error::new(
ErrorKind::Other,
"invalid return value from jail_attach",
)),
}.map_err(JailError::JailAttachError)
}
}

/// An Iterator over running Jails
Expand Down