| 1 | use crate::errors::CapsError; |
| 2 | use crate::nr; |
| 3 | use crate::Capability; |
| 4 | use std::io::Error; |
| 5 | |
| 6 | pub fn clear() -> Result<(), CapsError> { |
| 7 | for c in super::all() { |
| 8 | if has_cap(c).unwrap_or(false) { |
| 9 | drop(c)?; |
| 10 | } |
| 11 | } |
| 12 | Ok(()) |
| 13 | } |
| 14 | |
| 15 | pub fn drop(cap: Capability) -> Result<(), CapsError> { |
| 16 | let ret = unsafe { libc::prctl(nr::PR_CAPBSET_DROP, libc::c_uint::from(cap.index()), 0, 0) }; |
| 17 | match ret { |
| 18 | 0 => Ok(()), |
| 19 | _ => Err(CapsError::from(format!( |
| 20 | "PR_CAPBSET_DROP failure: {}", |
| 21 | Error::last_os_error() |
| 22 | ))), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn has_cap(cap: Capability) -> Result<bool, CapsError> { |
| 27 | let ret = unsafe { libc::prctl(nr::PR_CAPBSET_READ, libc::c_uint::from(cap.index()), 0, 0) }; |
| 28 | match ret { |
| 29 | 0 => Ok(false), |
| 30 | 1 => Ok(true), |
| 31 | _ => Err(CapsError::from(format!( |
| 32 | "PR_CAPBSET_READ failure: {}", |
| 33 | Error::last_os_error() |
| 34 | ))), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn read() -> Result<super::CapsHashSet, CapsError> { |
| 39 | let mut res = super::CapsHashSet::new(); |
| 40 | for c in super::all() { |
| 41 | if has_cap(c).unwrap_or(false) { |
| 42 | res.insert(c); |
| 43 | } |
| 44 | } |
| 45 | Ok(res) |
| 46 | } |