Skip to content

Commit

Permalink
Merge pull request #252 from eminence/mmperms
Browse files Browse the repository at this point in the history
Add docs and a as_str method to MMPermission
  • Loading branch information
eminence authored Feb 18, 2023
2 parents 6c6ddbd + af96123 commit b7f66d5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ bitflags! {

bitflags! {
/// The permissions a process has on memory map entries.
///
/// Note that the `SHARED` and `PRIVATE` are mutually exclusive, so while you can
/// use `MMPermissions::all()` to construct an instance that has all bits set,
/// this particular value would never been seen in procfs.
#[derive(Default)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct MMPermissions: u8 {
Expand Down Expand Up @@ -239,6 +243,26 @@ impl MMPermissions {
_ => Self::NONE,
}
}
/// Returns this permission map as a 4-character string, similar to what you
/// might see in `/proc/\<pid\>/maps`.
///
/// Note that the SHARED and PRIVATE bits are mutually exclusive, so this
/// string is 4 characters long, not 5.
pub fn as_str(&self) -> String {
let mut s = String::with_capacity(4);
s.push(if self.contains(Self::READ) { 'r' } else { '-' });
s.push(if self.contains(Self::WRITE) { 'w' } else { '-' });
s.push(if self.contains(Self::EXECUTE) { 'x' } else { '-' });
s.push(if self.contains(Self::SHARED) {
's'
} else if self.contains(Self::PRIVATE) {
'p'
} else {
'-'
});

s
}
}

impl FromStr for MMPermissions {
Expand Down Expand Up @@ -1767,5 +1791,9 @@ mod tests {
assert_eq!("rw-p".parse(), Ok(P::READ | P::WRITE | P::PRIVATE));
assert_eq!("r-xs".parse(), Ok(P::READ | P::EXECUTE | P::SHARED));
assert_eq!("----".parse(), Ok(P::NONE));

assert_eq!((P::READ | P::WRITE | P::PRIVATE).as_str(), "rw-p");
assert_eq!((P::READ | P::EXECUTE | P::SHARED).as_str(), "r-xs");
assert_eq!(P::NONE.as_str(), "----");
}
}

0 comments on commit b7f66d5

Please sign in to comment.