19 releases

0.5.6 Oct 17, 2025
0.5.5 Nov 2, 2022
0.5.4 Aug 5, 2022
0.5.3 Sep 27, 2021
0.0.1 Feb 7, 2017

#17 in Unix APIs

Download history 100882/week @ 2025-10-21 98010/week @ 2025-10-28 103143/week @ 2025-11-04 95371/week @ 2025-11-11 103138/week @ 2025-11-18 83033/week @ 2025-11-25 103852/week @ 2025-12-02 93883/week @ 2025-12-09 105667/week @ 2025-12-16 49940/week @ 2025-12-23 53882/week @ 2025-12-30 105383/week @ 2026-01-06 97918/week @ 2026-01-13 108843/week @ 2026-01-20 104056/week @ 2026-01-27 123564/week @ 2026-02-03

453,193 downloads per month
Used in 928 crates (49 directly)

MIT/Apache

41KB
797 lines

caps

crates.io Documentation

A pure-Rust library to work with Linux capabilities.

caps provides support for manipulating capabilities available in modern Linux kernels. It supports traditional POSIX sets (Effective, Inheritable, Permitted) as well as Linux-specific Ambient and Bounding capabilities sets.

caps provides a simple and idiomatic interface to handle capabilities on Linux. See capabilities(7) for more details.

Motivations

This library tries to achieve the following goals:

  • fully support modern kernels, including recent capabilities and sets
  • provide an idiomatic interface
  • be usable in static targets, without requiring an external C library

Example

type ExResult<T> = Result<T, Box<dyn std::error::Error + 'static>>;

fn manipulate_caps() -> ExResult<()> {
    use caps::{Capability, CapSet};

    // Retrieve permitted set.
    let cur = caps::read(None, CapSet::Permitted)?;
    println!("Current permitted caps: {:?}.", cur);
    
    // Retrieve effective set.
    let cur = caps::read(None, CapSet::Effective)?;
    println!("Current effective caps: {:?}.", cur);
    
    // Check if CAP_CHOWN is in permitted set.
    let perm_chown = caps::has_cap(None, CapSet::Permitted, Capability::CAP_CHOWN)?;
    if !perm_chown {
        return Err("Try running this as root!".into());
    }

    // Clear all effective caps.
    caps::clear(None, CapSet::Effective)?;
    println!("Cleared effective caps.");
    let cur = caps::read(None, CapSet::Effective)?;
    println!("Current effective caps: {:?}.", cur);

    // Since `CAP_CHOWN` is still in permitted, it can be raised again.
    caps::raise(None, CapSet::Effective, Capability::CAP_CHOWN)?;
    println!("Raised CAP_CHOWN in effective set.");
    let cur = caps::read(None, CapSet::Effective)?;
    println!("Current effective caps: {:?}.", cur);

    Ok(())
}

Some more examples are available under examples.

License

Licensed under either of

at your option.

Dependencies

~190KB