1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
use std::os::raw::c_int; #[derive(PartialEq,Eq,Debug)] pub enum Error { UnsupportedPlatform, Other(c_int), } pub enum Promise { Stdio, RPath, WPath, CPath, DPath, TMPPath, Inet, Fattr, Chown, Flock, Unix, Dns, Getpw, Sendfd, Recvfd, Ioctl, Tty, Proc, Exec, ProtExec, Settime, Ps, Vminfo, Id, Pf, Audio, } impl Promise { pub fn to_promise_string(&self) -> &'static str { return match *self { Promise::Stdio => "stdio", Promise::RPath => "rpath", Promise::WPath => "wpath", Promise::CPath => "cpath", Promise::DPath => "dpath", Promise::TMPPath => "tmppath", Promise::Inet => "inet", Promise::Fattr => "fattr", Promise::Chown => "chown", Promise::Flock => "flock", Promise::Unix => "unix", Promise::Dns => "dns", Promise::Getpw => "getpw", Promise::Sendfd => "sendfd", Promise::Recvfd => "recvfd", Promise::Ioctl => "ioctl", Promise::Tty => "tty", Promise::Proc => "proc", Promise::Exec => "exec", Promise::ProtExec => "prot_exec", Promise::Settime => "settime", Promise::Ps => "ps", Promise::Vminfo => "vminfo", Promise::Id => "id", Promise::Pf => "pf", Promise::Audio => "audio", }; } } trait ToPromiseString { fn to_promise_string(&self) -> String; } impl ToPromiseString for [Promise] { fn to_promise_string(&self) -> String { return self.iter() .map(|p| p.to_promise_string()) .collect::<Vec<&'static str>>() .join(" "); } } #[cfg(target_os = "openbsd")] mod openbsd; #[cfg(target_os = "openbsd")] pub use openbsd::pledge_wrapper as pledge; #[cfg(not(target_os = "openbsd"))] pub fn pledge(_: &[Promise]) -> Result<(), Error> { return Err(Error::UnsupportedPlatform); } #[macro_export] macro_rules! pledge { ( $( $x:ident ),* ) => { { let mut promises = Vec::new(); $( promises.push(Promise::$x); )* pledge(&promises) } }; } #[cfg(test)] mod tests { use super::{pledge, Promise, ToPromiseString}; #[test] fn test_promise_str() { assert_eq!(vec![].to_promise_string(), ""); assert_eq!(vec![Promise::Dns].to_promise_string(), "dns"); assert_eq!(vec![Promise::Stdio, Promise::ProtExec].to_promise_string(), "stdio prot_exec"); } #[test] #[cfg(not(target_os = "openbsd"))] fn test_pledge_unsupported() { use super::Error; assert_eq!(pledge![Stdio].unwrap_err(), Error::UnsupportedPlatform); } #[test] #[cfg(target_os = "openbsd")] fn test_pledge_supported() { pledge![Stdio].unwrap(); assert!(pledge![Stdio, Audio].is_err()); } }