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

Add support for Tokio Command #130

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ is-it-maintained-open-issues = { repository = "fubarnetes/libjail-rs" }

[features]
serialize = ["serde", "serde_json", "rctl/serialize"]
tokio = ["dep:tokio"]

[dependencies]
bitflags = "^1"
Expand All @@ -38,6 +39,7 @@ strum_macros = "0.21.1"
serde = { version="1.0", features = ["derive"], optional=true}
serde_json = { version="1.0", optional=true }
thiserror = "1.0"
tokio = { version = "1.28", features = ["process"], optional = true }

[dev-dependencies]
cli-table = { version="0.4", default-features=false, features=["derive"] }
Expand Down
31 changes: 26 additions & 5 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use crate::{JailError, RunningJail};
use log::trace;
use std::os::unix::process::CommandExt;
use std::process;
use std::process::Command as StdCommand;
#[cfg(feature = "tokio")]
use tokio::process::Command as TokioCommand;

/// Extension to the `std::process::Command` builder to run the command in a
/// jail.
Expand Down Expand Up @@ -34,13 +36,32 @@ pub trait Jailed {
/// Sets the child process to be executed within a jail. This translates
/// to calling `jail_attach` in the child process. Failure in the
/// `jail_attach` call will cause the spawn to fail.
fn jail(&mut self, jail: &RunningJail) -> &mut process::Command;
fn jail(&mut self, jail: &RunningJail) -> &mut Self;
}

#[cfg(target_os = "freebsd")]
impl Jailed for process::Command {
fn jail(&mut self, jail: &RunningJail) -> &mut process::Command {
trace!("process::Command::jail({:?}, jail={:?})", self, jail);
impl Jailed for StdCommand {
fn jail(&mut self, jail: &RunningJail) -> &mut Self {
trace!("std::process::Command::jail({:?}, jail={:?})", self, jail);
let jail = *jail;
unsafe {
self.pre_exec(move || {
trace!("pre_exec handler: attaching");
jail.attach().map_err(|err| match err {
JailError::JailAttachError(e) => e,
_ => panic!("jail.attach() failed with unexpected error"),
})
});
}

self
}
}

#[cfg(all(target_os = "freebsd", feature = "tokio"))]
impl Jailed for TokioCommand {
fn jail(&mut self, jail: &RunningJail) -> &mut Self {
trace!("tokio::process::Command::jail({:?}, jail={:?})", self, jail);
let jail = *jail;
unsafe {
self.pre_exec(move || {
Expand Down
Loading