Skip to content

Commit

Permalink
param: add support for JailSys parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Dec 23, 2018
1 parent f16f65e commit 0bda407
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum Type {
Ulong,
Ipv4Addrs,
Ipv6Addrs,
JailSys,
}

#[cfg(target_os = "freebsd")]
Expand Down Expand Up @@ -171,6 +172,21 @@ impl Type {
_ => false,
}
}

/// Check if this type is a JailSys parameter
///
/// # Example
/// ```
/// use jail::param::Type;
/// assert_eq!(Type::JailSys.is_jailsys(), true);
/// assert_eq!(Type::Int.is_jailsys(), false);
/// ```
pub fn is_jailsys(&self) -> bool {
match self {
Type::JailSys => true,
_ => false,
}
}
}

impl<'a> convert::From<&'a Value> for Type {
Expand All @@ -191,6 +207,7 @@ impl<'a> convert::From<&'a Value> for Type {
Value::U32(_) => Type::U32,
Value::Ipv4Addrs(_) => Type::Ipv4Addrs,
Value::Ipv6Addrs(_) => Type::Ipv6Addrs,
Value::JailSys(_) => Type::JailSys,
}
}
}
Expand Down Expand Up @@ -233,10 +250,57 @@ impl convert::Into<CtlType> for Type {
Type::Ulong => CtlType::Ulong,
Type::Ipv4Addrs => CtlType::Struct,
Type::Ipv6Addrs => CtlType::Struct,
Type::JailSys => CtlType::Int,
}
}
}

/// A Tri-State JailSys parameter value
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum JailSys {
Disable,
New,
Inherit,
}

impl convert::Into<u8> for &JailSys {
fn into(self) -> u8 {
match self {
JailSys::Disable => 0,
JailSys::New => 1,
JailSys::Inherit => 2,
}
}
}

impl convert::From<u8> for JailSys {
fn from(val: u8) -> Self {
match val {
0 => JailSys::Disable,
1 => JailSys::New,
2 => JailSys::Inherit,
_ => panic!("Invalid jailsys value"),
}
}
}

impl convert::Into<&'static str> for &JailSys {
fn into(self) -> &'static str {
match self {
JailSys::Disable => "disable",
JailSys::New => "new",
JailSys::Inherit => "inherit",
}
}
}

impl std::fmt::Display for JailSys {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s: &str = self.into();
write!(f, "{}", s)
}
}

/// An enum representing the value of a parameter.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Value {
Expand Down Expand Up @@ -280,6 +344,9 @@ pub enum Value {
/// ]);
/// ```
Ipv6Addrs(Vec<net::Ipv6Addr>),

/// Represent a tri-state [JailSys] parameter.
JailSys(JailSys),
}

impl Value {
Expand Down Expand Up @@ -353,6 +420,10 @@ impl Value {
}
Ok(())
}
Value::JailSys(v) => {
let v: u8 = (&v).into();
bytes.write_int::<LittleEndian>(v.into(), mem::size_of::<libc::c_int>())
}
}
.map_err(|_| JailError::SerializeFailed)?;

Expand Down Expand Up @@ -744,6 +815,7 @@ pub fn get(jid: i32, name: &str) -> Result<Value, JailError> {

Ok(Value::Ipv6Addrs(ips))
}
Type::JailSys => Ok(Value::JailSys(JailSys::from(value[0]))),
}
}

Expand Down

0 comments on commit 0bda407

Please sign in to comment.