caps/
bounding.rs

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