From 6f9e1068a94c771dc25232324cbf38652b1aefac Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Fri, 7 Jun 2019 05:18:17 +0200 Subject: [PATCH] WIP: slog --- Cargo.toml | 5 ++- src/lib.rs | 41 +++++++++++++------ src/param.rs | 37 ++++++----------- src/process.rs | 6 +-- src/running.rs | 106 ++++++++++++++++++++++++++++++++++--------------- src/stopped.rs | 46 +++++++++++++++++---- src/sys.rs | 23 ++++++----- 7 files changed, 171 insertions(+), 93 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6bdfb1338..539273669 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,8 @@ bitflags = "^1" byteorder = "^1.2.3" failure = "~0.1.1" libc = "~0.2.41" -log="0.4" +slog = "2.4" +slog-stdlog = "3.0.2" sysctl = "~0.3.0" nix= "^0.14.0" rctl = "0.1.0" @@ -38,5 +39,5 @@ serde = { version="1.0", features = ["derive"], optional=true} serde_json = { version="1.0", optional=true } [dev-dependencies] -pretty_env_logger = "0.3" prettytable-rs = "0.8.0" +slog-term = "2.4.0" diff --git a/src/lib.rs b/src/lib.rs index 3bbc89474..40e89e27d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! it aims to provide the features exposed by the FreeBSD Jail Library //! [jail(3)](https://www.freebsd.org/cgi/man.cgi?query=jail&sektion=3&manpath=FreeBSD+11.1-stable) -#![type_length_limit="17825821"] +#![type_length_limit = "17825821"] extern crate byteorder; @@ -13,7 +13,8 @@ extern crate failure; extern crate libc; #[macro_use] -extern crate log; +extern crate slog; +extern crate slog_stdlog; extern crate sysctl; @@ -43,6 +44,8 @@ use std::convert; use std::net; use std::path; +use slog::Drain; + mod error; pub use error::JailError; @@ -59,6 +62,13 @@ pub mod process; #[cfg(test)] mod tests; +#[doc(hidden)] +fn default_logger() -> slog::Logger { + let drain = slog_stdlog::StdLog.fuse(); + //let drain = slog_envlogger::new(drain).fuse(); + slog::Logger::root(drain, o!()) +} + /// Represents a running or stopped jail. #[cfg(target_os = "freebsd")] #[derive(Debug, PartialEq, Clone)] @@ -75,15 +85,22 @@ impl convert::From for Jail { impl convert::From for Jail { fn from(stopped: StoppedJail) -> Self { - trace!("Jail::from({:?})", stopped); + trace!(stopped.logger, "Jail::from({:?})", stopped); Jail::Stopped(stopped) } } impl Jail { + fn get_logger(&self) -> &slog::Logger { + match self { + Jail::Running(ref r) => &r.logger, + Jail::Stopped(ref s) => &s.logger, + } + } + /// Check if a jail is running pub fn is_started(&self) -> bool { - trace!("Jail::is_started({:?})", self); + trace!(self.get_logger(), "Jail::is_started({:?})", self); match self { Jail::Running(_) => true, Jail::Stopped(_) => false, @@ -95,7 +112,7 @@ impl Jail { /// This calls start() on a stopped Jail, and is a no-op for an already /// running Jail. pub fn start(self) -> Result { - trace!("Jail::start({:?})", self); + trace!(self.get_logger(), "Jail::start({:?})", self); match self { Jail::Running(r) => Ok(Jail::Running(r)), Jail::Stopped(s) => Ok(Jail::Running(s.start()?)), @@ -107,7 +124,7 @@ impl Jail { /// This calls stop() on a started Jail, and is a no-op for an already /// stopped Jail. pub fn stop(self) -> Result { - trace!("Jail::stop({:?})", self); + trace!(self.get_logger(), "Jail::stop({:?})", self); match self { Jail::Running(r) => Ok(Jail::Stopped(r.stop()?)), Jail::Stopped(s) => Ok(Jail::Stopped(s)), @@ -116,7 +133,7 @@ impl Jail { /// Get the name of the Jail pub fn name(&self) -> Result { - trace!("Jail::name({:?})", self); + trace!(self.get_logger(), "Jail::name({:?})", self); match self { Jail::Running(r) => r.name(), Jail::Stopped(s) => s @@ -128,7 +145,7 @@ impl Jail { /// Get the name of the Jail pub fn path(&self) -> Result { - trace!("Jail::path({:?})", self); + trace!(self.get_logger(), "Jail::path({:?})", self); match self { Jail::Running(r) => r.path(), Jail::Stopped(s) => s @@ -140,7 +157,7 @@ impl Jail { /// Get the hostname of the Jail pub fn hostname(&self) -> Result { - trace!("Jail::hostname({:?})", self); + trace!(self.get_logger(), "Jail::hostname({:?})", self); match self { Jail::Running(r) => r.hostname(), Jail::Stopped(s) => s @@ -152,7 +169,7 @@ impl Jail { /// Get the IP Addresses of a jail pub fn ips(&self) -> Result, JailError> { - trace!("Jail::ips({:?})", self); + trace!(self.get_logger(), "Jail::ips({:?})", self); match self { Jail::Running(r) => r.ips(), Jail::Stopped(s) => Ok(s.ips.clone()), @@ -161,7 +178,7 @@ impl Jail { /// Get a jail parameter pub fn param(&self, name: &str) -> Result { - trace!("Jail::param({:?})", self); + trace!(self.get_logger(), "Jail::param({:?})", self); match self { Jail::Running(r) => r.param(name), Jail::Stopped(s) => s @@ -173,7 +190,7 @@ impl Jail { } pub fn params(&self) -> Result, JailError> { - trace!("Jail::params({:?})", self); + trace!(self.get_logger(), "Jail::params({:?})", self); match self { Jail::Running(r) => r.params(), Jail::Stopped(s) => Ok(s.params.clone()), diff --git a/src/param.rs b/src/param.rs index 338661ab1..6907c46cf 100644 --- a/src/param.rs +++ b/src/param.rs @@ -34,7 +34,6 @@ impl Type { /// assert_eq!(Type::of_param("ip6.addr").unwrap(), Type::Ipv6Addrs); /// ``` pub fn of_param(name: &str) -> Result { - trace!("Type::of_param(name={:?})", name); let (ctl_type, _, _) = info(name)?; ctltype_to_type(name, ctl_type) @@ -50,7 +49,6 @@ impl Type { /// assert_eq!(Type::Int.is_string(), false); /// ``` pub fn is_string(&self) -> bool { - trace!("Type::is_string({:?})", self); match self { Type::String => true, _ => false, @@ -66,7 +64,6 @@ impl Type { /// assert_eq!(Type::String.is_numeric(), false); /// ``` pub fn is_numeric(&self) -> bool { - trace!("Type::is_numeric({:?})", self); match self { Type::U8 => true, Type::U16 => true, @@ -96,7 +93,6 @@ impl Type { /// assert_eq!(Type::String.is_signed(), false); /// ``` pub fn is_signed(&self) -> bool { - trace!("Type::is_signed({:?})", self); match self { Type::S8 => true, Type::S16 => true, @@ -119,7 +115,6 @@ impl Type { /// assert_eq!(Type::String.is_ip(), false); /// ``` pub fn is_ip(&self) -> bool { - trace!("Type::is_ip({:?})", self); match self { Type::Ipv4Addrs => true, Type::Ipv6Addrs => true, @@ -137,7 +132,6 @@ impl Type { /// assert_eq!(Type::Ipv6Addrs.is_ipv4(), false); /// ``` pub fn is_ipv4(&self) -> bool { - trace!("Type::is_ipv4({:?})", self); match self { Type::Ipv4Addrs => true, _ => false, @@ -154,7 +148,6 @@ impl Type { /// assert_eq!(Type::Ipv4Addrs.is_ipv6(), false); /// ``` pub fn is_ipv6(&self) -> bool { - trace!("Type::is_ipv6({:?})", self); match self { Type::Ipv6Addrs => true, _ => false, @@ -184,7 +177,6 @@ impl Type { impl convert::Into for Type { fn into(self: Type) -> CtlType { - trace!("Type::into::({:?})", self); match self { Type::String => CtlType::String, Type::U8 => CtlType::U8, @@ -269,14 +261,12 @@ impl Value { /// assert!(Value::Int(42).get_type().is_signed()); /// ``` pub fn get_type(&self) -> Type { - trace!("Value::get_type({:?})", self); self.into() } /// Format the value into a vector of bytes as expected by the jail /// parameter API. pub fn as_bytes(self) -> Result, JailError> { - trace!("Value::as_bytes({:?})", self); let mut bytes: Vec = vec![]; // Some conversions are identity on 64 bit, but not on 32 bit and vice versa @@ -356,7 +346,6 @@ impl Value { /// not_ipv4_addrs.unpack_ipv4().unwrap(); /// ``` pub fn unpack_ipv4(self) -> Result, JailError> { - trace!("Value::unpack_ipv4({:?})", self); match self { Value::Ipv4Addrs(v) => Ok(v), _ => Err(JailError::ParameterUnpackError), @@ -391,7 +380,6 @@ impl Value { /// rfc1918.unpack_ipv6().unwrap(); /// ``` pub fn unpack_ipv6(self) -> Result, JailError> { - trace!("Value::unpack_ipv6({:?})", self); match self { Value::Ipv6Addrs(v) => Ok(v), _ => Err(JailError::ParameterUnpackError), @@ -416,7 +404,6 @@ impl Value { /// not_a_string.unpack_string().unwrap(); /// ``` pub fn unpack_string(self) -> Result { - trace!("Value::unpack_string({:?})", self); match self { Value::String(v) => Ok(v), _ => Err(JailError::ParameterUnpackError), @@ -443,7 +430,6 @@ impl Value { /// assert!(Value::S64(64i64).unpack_u64().is_err()); /// ``` pub fn unpack_u64(self) -> Result { - trace!("Value::unpack_u64({:?})", self); #[cfg_attr(feature = "cargo-clippy", allow(identity_conversion))] match self { Value::U64(v) => Ok(v), @@ -480,7 +466,6 @@ impl Value { /// assert!(Value::U64(64u64).unpack_i64().is_err()); /// ``` pub fn unpack_i64(self) -> Result { - trace!("Value::unpack_i64({:?})", self); #[cfg_attr(feature = "cargo-clippy", allow(identity_conversion))] match self { Value::S64(v) => Ok(v), @@ -500,7 +485,6 @@ impl Value { #[cfg(target_os = "freebsd")] fn info(name: &str) -> Result<(CtlType, CtlFlags, usize), JailError> { - trace!("info({:?})", name); // Get parameter type let ctlname = format!("security.jail.param.{}", name); @@ -551,7 +535,6 @@ fn info(name: &str) -> Result<(CtlType, CtlFlags, usize), JailError> { #[cfg(target_os = "freebsd")] fn ctltype_to_type(name: &str, ctl_type: CtlType) -> Result { - trace!("ctltype_to_type({:?}, ctl_type={:?})", name, ctl_type); let param_type = match ctl_type { CtlType::Int => Type::Int, CtlType::S64 => Type::S64, @@ -597,8 +580,8 @@ fn ctltype_to_type(name: &str, ctl_type: CtlType) -> Result { /// # jail.kill().expect("could not stop jail"); /// ``` #[cfg(target_os = "freebsd")] -pub fn get(jid: i32, name: &str) -> Result { - trace!("get(jid={}, name={:?})", jid, name); +pub fn get(jid: i32, name: &str, logger: &slog::Logger) -> Result { + trace!(logger, "get(jid={}, name={:?})", jid, name); let (paramtype, _, typesize) = info(name)?; // ip4.addr and ip6.addr are arrays, which can be up to @@ -772,8 +755,14 @@ pub fn get(jid: i32, name: &str) -> Result { /// # } /// # jail.kill().expect("could not stop jail"); /// ``` -pub fn set(jid: i32, name: &str, value: Value) -> Result<(), JailError> { - trace!("set(jid={}, name={:?}, value={:?})", jid, name, value); +pub fn set(jid: i32, name: &str, value: Value, logger: &slog::Logger) -> Result<(), JailError> { + trace!( + logger, + "set(jid={}, name={:?}, value={:?})", + jid, + name, + value + ); let (ctltype, ctl_flags, _) = info(name)?; // Check if this is a tunable. @@ -839,8 +828,8 @@ pub fn set(jid: i32, name: &str, value: Value) -> Result<(), JailError> { /// assert_eq!(params.get("allow.raw_sockets"), Some(¶m::Value::Int(1))); /// # jail.kill().expect("could not stop jail"); /// ``` -pub fn get_all(jid: i32) -> Result, JailError> { - trace!("get_all(jid={})", jid); +pub fn get_all(jid: i32, logger: &slog::Logger) -> Result, JailError> { + trace!(logger, "get_all(jid={})", jid); let params: Result, JailError> = Ctl::new("security.jail.param") .map_err(JailError::SysctlError)? .into_iter() @@ -866,7 +855,7 @@ pub fn get_all(jid: i32) -> Result, JailError> { .filter(|name| name != "ip6.addr") .filter(|name| name != "ip4.addr") // get parameters - .map(|name| get(jid, &name).map(|v| (name, v))) + .map(|name| get(jid, &name, logger).map(|v| (name, v))) .collect(); Ok(HashMap::from_iter(params?)) diff --git a/src/process.rs b/src/process.rs index 0997c8dbd..2f86f869b 100644 --- a/src/process.rs +++ b/src/process.rs @@ -43,10 +43,10 @@ pub trait Jailed { #[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); - let jail = *jail; + //trace!("process::Command::jail({:?}, jail={:?})", self, jail); + let jail = jail.clone(); self.before_exec(move || { - trace!("before_exec handler: attaching"); + //trace!("before_exec handler: attaching"); jail.attach().map_err(|err| match err { JailError::JailAttachError(e) => e, _ => panic!("jail.attach() failed with unexpected error"), diff --git a/src/running.rs b/src/running.rs index 131961753..93885bf2b 100644 --- a/src/running.rs +++ b/src/running.rs @@ -5,6 +5,7 @@ use sys; use JailError; use StoppedJail; +use default_logger; use std::collections::HashMap; use std::convert::TryFrom; use std::io::{Error, ErrorKind}; @@ -12,13 +13,35 @@ use std::net; use std::path; /// Represents a running jail. -#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] +#[derive(Clone, Debug)] #[cfg(target_os = "freebsd")] pub struct RunningJail { + pub logger: slog::Logger, + /// The `jid` of the jail pub jid: i32, } +impl PartialEq for RunningJail { + fn eq(&self, other: &Self) -> bool { + self.jid == other.jid + } +} + +impl Eq for RunningJail {} + +impl Ord for RunningJail { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.jid.cmp(&other.jid) + } +} + +impl PartialOrd for RunningJail { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + /// Represent a running jail. #[cfg(target_os = "freebsd")] impl RunningJail { @@ -51,8 +74,9 @@ impl RunningJail { /// .expect("No Jail with this JID"); /// ``` pub fn from_jid(jid: i32) -> Option { - trace!("RunningJail::from_jid({})", jid); - match sys::jail_exists(jid) { + let logger = default_logger(); + trace!(logger, "RunningJail::from_jid({})", jid); + match sys::jail_exists(jid, &logger) { true => Some(Self::from_jid_unchecked(jid)), false => None, } @@ -78,8 +102,9 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn from_jid_unchecked(jid: i32) -> RunningJail { - trace!("RunningJail::from_jid_unchecked({})", jid); - RunningJail { jid } + let logger = default_logger(); + trace!(logger, "RunningJail::from_jid_unchecked({})", jid); + RunningJail { logger, jid } } /// Create a [RunningJail](struct.RunningJail.html) given the jail `name`. @@ -103,8 +128,9 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn from_name(name: &str) -> Result { - trace!("RunningJail::from_name({})", name); - sys::jail_getid(name).map(RunningJail::from_jid_unchecked) + let logger = default_logger(); + trace!(logger, "RunningJail::from_name({})", name); + sys::jail_getid(name, &logger).map(RunningJail::from_jid_unchecked) } /// Return the jail's `name`. @@ -123,7 +149,7 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn name(self: &RunningJail) -> Result { - trace!("RunningJail::name({:?})", self); + trace!(self.logger, "RunningJail::name({:?})", self); self.param("name")?.unpack_string() } @@ -147,7 +173,7 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn path(self: &RunningJail) -> Result { - trace!("RunningJail::path({:?})", self); + trace!(self.logger, "RunningJail::path({:?})", self); Ok(self.param("path")?.unpack_string()?.into()) } @@ -171,7 +197,7 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn hostname(self: &RunningJail) -> Result { - trace!("RunningJail::hostname({:?})", self); + trace!(self.logger, "RunningJail::hostname({:?})", self); self.param("host.hostname")?.unpack_string() } @@ -194,7 +220,7 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn ips(self: &RunningJail) -> Result, JailError> { - trace!("RunningJail::ips({:?})", self); + trace!(self.logger, "RunningJail::ips({:?})", self); let mut ips: Vec = vec![]; ips.extend( self.param("ip4.addr")? @@ -228,8 +254,8 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn param(self: &Self, name: &str) -> Result { - trace!("RunningJail::param({:?}, name={})", self, name); - param::get(self.jid, name) + trace!(self.logger, "RunningJail::param({:?}, name={})", self, name); + param::get(self.jid, name, &self.logger) } /// Return a HashMap of all jail parameters. @@ -254,8 +280,8 @@ impl RunningJail { /// # running.kill().expect("could not stop jail"); /// ``` pub fn params(self: &Self) -> Result, JailError> { - trace!("RunningJail::params({:?})", self); - param::get_all(self.jid) + trace!(self.logger, "RunningJail::params({:?})", self); + param::get_all(self.jid, &self.logger) } /// Set a jail parameter. @@ -276,12 +302,13 @@ impl RunningJail { /// ``` pub fn param_set(self: &Self, name: &str, value: param::Value) -> Result<(), JailError> { trace!( + self.logger, "RunningJail::param_set({:?}, name={:?}, value={:?})", self, name, value ); - param::set(self.jid, name, value) + param::set(self.jid, name, value, &self.logger) } /// Kill a running jail, consuming it. @@ -298,9 +325,9 @@ impl RunningJail { /// running.kill(); /// ``` pub fn kill(self: RunningJail) -> Result<(), JailError> { - trace!("RunningJail::kill({:?})", self); + trace!(self.logger, "RunningJail::kill({:?})", self); let name = self.name()?; - sys::jail_remove(self.jid)?; + sys::jail_remove(self.jid, &self.logger)?; // Tear down RCTL rules { @@ -345,7 +372,7 @@ impl RunningJail { /// # running.kill().unwrap(); /// ``` pub fn save(self: &RunningJail) -> Result { - trace!("RunningJail::save({:?})", self); + trace!(self.logger, "RunningJail::save({:?})", self); let mut stopped = StoppedJail::new(self.path()?); stopped.name = self.name().ok(); @@ -401,7 +428,7 @@ impl RunningJail { /// //assert_eq!(stopped.hostname, Some("testjail_save.example.com".into())); /// ``` pub fn stop(self: RunningJail) -> Result { - trace!("RunningJail::stop({:?})", self); + trace!(self.logger, "RunningJail::stop({:?})", self); let stopped = self.save()?; self.kill()?; @@ -429,7 +456,7 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn restart(self: RunningJail) -> Result { - trace!("RunningJail::restart({:?})", self); + trace!(self.logger, "RunningJail::restart({:?})", self); let stopped = self.stop()?; stopped.start() } @@ -459,7 +486,6 @@ impl RunningJail { /// # } /// ``` pub fn all() -> RunningJails { - trace!("RunningJail::all()"); RunningJails::default() } @@ -482,7 +508,7 @@ impl RunningJail { /// # running.kill(); /// ``` pub fn racct_statistics(&self) -> Result, JailError> { - trace!("RunningJail::racct_statistics({:?})", self); + trace!(self.logger, "RunningJail::racct_statistics({:?})", self); // First let's try to get the RACCT statistics in the happy path rctl::Subject::jail_name(self.name()?) .usage() @@ -491,7 +517,7 @@ impl RunningJail { /// Jail the current process into the given jail. pub fn attach(&self) -> Result<(), JailError> { - trace!("RunningJail::attach({:?})", self); + trace!(self.logger, "RunningJail::attach({:?})", self); let ret = unsafe { libc::jail_attach(self.jid) }; match ret { 0 => Ok(()), @@ -541,8 +567,8 @@ impl RunningJail { /// jail.kill().expect_err("Jail should be dead by now."); /// ``` pub fn defer_cleanup(&self) -> Result<(), JailError> { - trace!("RunningJail::defer_cleanup({:?})", self); - sys::jail_clearpersist(self.jid) + trace!(self.logger, "RunningJail::defer_cleanup({:?})", self); + sys::jail_clearpersist(self.jid, &self.logger) } } @@ -559,23 +585,34 @@ impl TryFrom for RunningJail { /// See [RunningJail::all()](struct.RunningJail.html#method.all) for a usage /// example. #[cfg(target_os = "freebsd")] -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone)] pub struct RunningJails { + logger: slog::Logger, lastjid: i32, } +impl PartialEq for RunningJails { + fn eq(&self, other: &Self) -> bool { + self.lastjid == other.lastjid + } +} + +impl Eq for RunningJails {} + #[cfg(target_os = "freebsd")] impl Default for RunningJails { fn default() -> Self { - trace!("RunningJails::default()"); - RunningJails { lastjid: 0 } + let logger = default_logger(); + trace!(logger, "RunningJails::default()"); + RunningJails { logger, lastjid: 0 } } } #[cfg(target_os = "freebsd")] impl RunningJails { pub fn new() -> Self { - trace!("RunningJails::new()"); + let logger = default_logger(); + trace!(logger, "RunningJails::new()"); RunningJails::default() } } @@ -585,14 +622,17 @@ impl Iterator for RunningJails { type Item = RunningJail; fn next(&mut self) -> Option { - trace!("RunningJails::next({:?})", self); - let jid = match sys::jail_nextjid(self.lastjid) { + trace!(self.logger, "RunningJails::next({:?})", self); + let jid = match sys::jail_nextjid(self.lastjid, &self.logger) { Ok(j) => j, Err(_) => return None, }; self.lastjid = jid; - Some(RunningJail { jid }) + Some(RunningJail { + logger: self.logger.clone(), + jid, + }) } } diff --git a/src/stopped.rs b/src/stopped.rs index 73cbe9ebc..d165dcb6d 100644 --- a/src/stopped.rs +++ b/src/stopped.rs @@ -11,14 +11,17 @@ use RunningJail; #[cfg(feature = "serialize")] use serde::Serialize; +use default_logger; use std::convert::TryFrom; use std::fmt; /// Represent a stopped jail including all information required to start it -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, Debug)] #[cfg(target_os = "freebsd")] #[cfg_attr(feature = "serialize", derive(Serialize))] pub struct StoppedJail { + pub logger: slog::Logger, + /// The path of root file system of the jail pub path: Option, @@ -38,11 +41,24 @@ pub struct StoppedJail { pub limits: Vec<(rctl::Resource, rctl::Limit, rctl::Action)>, } +impl PartialEq for StoppedJail { + fn eq(&self, other: &Self) -> bool { + self.path == other.path + && self.name == other.name + && self.hostname == other.hostname + && self.params == other.params + && self.ips == other.ips + && self.limits == other.limits + } +} + #[cfg(target_os = "freebsd")] impl Default for StoppedJail { fn default() -> StoppedJail { - trace!("StoppedJail::default()"); + let logger = default_logger(); + trace!(logger, "StoppedJail::default()"); StoppedJail { + logger, path: None, name: None, hostname: None, @@ -73,7 +89,8 @@ impl StoppedJail { /// let j = StoppedJail::new("/rescue"); /// ``` pub fn new + fmt::Debug>(path: P) -> StoppedJail { - trace!("StoppedJail::new(path={:?})", path); + let logger = default_logger(); + trace!(logger, "StoppedJail::new(path={:?})", path); let mut ret: StoppedJail = Default::default(); ret.path = Some(path.into()); ret @@ -95,7 +112,7 @@ impl StoppedJail { /// # running.kill(); /// ``` pub fn start(self: StoppedJail) -> Result { - trace!("StoppedJail::start({:?})", self); + trace!(self.logger, "StoppedJail::start({:?})", self); let path = match self.path { None => return Err(JailError::PathNotGiven), Some(ref p) => p.clone(), @@ -148,7 +165,8 @@ impl StoppedJail { ); } - let ret = sys::jail_create(&path, params).map(RunningJail::from_jid_unchecked)?; + let ret = + sys::jail_create(&path, params, &self.logger).map(RunningJail::from_jid_unchecked)?; // Set resource limits if !self.limits.is_empty() { @@ -184,7 +202,12 @@ impl StoppedJail { /// assert_eq!(stopped.name, Some("test_stopped_name".to_string())); /// ``` pub fn name + fmt::Debug>(mut self: Self, name: S) -> Self { - trace!("StoppedJail::start({:?}, name={:?})", self, name); + trace!( + self.logger, + "StoppedJail::start({:?}, name={:?})", + self, + name + ); self.name = Some(name.into()); self } @@ -203,7 +226,12 @@ impl StoppedJail { /// assert_eq!(stopped.hostname, Some("example.com".to_string())); /// ``` pub fn hostname + fmt::Debug>(mut self: Self, hostname: S) -> Self { - trace!("StoppedJail::hostname({:?}, hostname={:?})", self, hostname); + trace!( + self.logger, + "StoppedJail::hostname({:?}, hostname={:?})", + self, + hostname + ); self.hostname = Some(hostname.into()); self } @@ -226,6 +254,7 @@ impl StoppedJail { value: param::Value, ) -> Self { trace!( + self.logger, "StoppedJail::param({:?}, param={:?}, value={:?})", self, param, @@ -256,6 +285,7 @@ impl StoppedJail { action: rctl::Action, ) -> Self { trace!( + self.logger, "StoppedJail::limit({:?}, resource={:?}, limit={:?}, action={:?})", self, resource, @@ -279,7 +309,7 @@ impl StoppedJail { /// .ip("fe80::2".parse().expect("could not parse ::1")); /// ``` pub fn ip(mut self: Self, ip: net::IpAddr) -> Self { - trace!("StoppedJail::ip({:?}, ip={:?})", self, ip); + trace!(self.logger, "StoppedJail::ip({:?}, ip={:?})", self, ip); self.ips.push(ip); self } diff --git a/src/sys.rs b/src/sys.rs index 6beeadf14..fd3da91c4 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -62,8 +62,9 @@ bitflags! { pub fn jail_create( path: &path::Path, params: HashMap, + logger: &slog::Logger, ) -> Result { - trace!("jail_create(path={:?}, params={:?})", path, params); + trace!(logger, "jail_create(path={:?}, params={:?})", path, params); // Note: we keep an owned copy of the raw parameter representations // around that we only drop after the unsafe jail_set call. @@ -127,8 +128,8 @@ pub fn jail_create( } /// Test if a jail exists. Returns -pub fn jail_exists(jid: i32) -> bool { - trace!("jail_exists({})", jid); +pub fn jail_exists(jid: i32, logger: &slog::Logger) -> bool { + trace!(logger, "jail_exists({})", jid); let mut errmsg: [u8; 256] = unsafe { mem::zeroed() }; let mut jiov: Vec = vec![ iovec!(b"jid\0" => (&jid as *const _, mem::size_of::())), @@ -151,8 +152,8 @@ pub fn jail_exists(jid: i32) -> bool { /// Clear the persist flag #[cfg(target_os = "freebsd")] -pub fn jail_clearpersist(jid: i32) -> Result<(), JailError> { - trace!("jail_clearpersist({})", jid); +pub fn jail_clearpersist(jid: i32, logger: &slog::Logger) -> Result<(), JailError> { + trace!(logger, "jail_clearpersist({})", jid); let mut errmsg: [u8; 256] = unsafe { mem::zeroed() }; let mut jiov: Vec = vec![ iovec!(b"jid\0" => (&jid as *const _, mem::size_of::())), @@ -189,8 +190,8 @@ pub fn jail_clearpersist(jid: i32) -> Result<(), JailError> { /// This function attempts to parse the name into an `i32` first, which is /// returned if successful. #[cfg(target_os = "freebsd")] -pub fn jail_getid(name: &str) -> Result { - trace!("jail_getid(name={:?})", name); +pub fn jail_getid(name: &str, logger: &slog::Logger) -> Result { + trace!(logger, "jail_getid(name={:?})", name); let mut errmsg: [u8; 256] = unsafe { mem::zeroed() }; if let Ok(jid) = name.parse::() { @@ -228,8 +229,8 @@ pub fn jail_getid(name: &str) -> Result { /// Get the next `jid` given the last `jid`. #[cfg(target_os = "freebsd")] -pub fn jail_nextjid(lastjid: i32) -> Result { - trace!("jail_nextjid(lastjid={})", lastjid); +pub fn jail_nextjid(lastjid: i32, logger: &slog::Logger) -> Result { + trace!(logger, "jail_nextjid(lastjid={})", lastjid); let mut errmsg: [u8; 256] = unsafe { mem::zeroed() }; let mut jiov: Vec = vec![ @@ -266,8 +267,8 @@ pub fn jail_nextjid(lastjid: i32) -> Result { /// This will kill all processes belonging to the jail, and remove any children /// of that jail. #[cfg(target_os = "freebsd")] -pub fn jail_remove(jid: i32) -> Result<(), JailError> { - trace!("jail_remove(jid={})", jid); +pub fn jail_remove(jid: i32, logger: &slog::Logger) -> Result<(), JailError> { + trace!(logger, "jail_remove(jid={})", jid); let ret = unsafe { libc::jail_remove(jid) }; match ret { 0 => Ok(()),