| 1 |
use std::io; |
| 2 |
|
| 3 |
use super::{expect, ProcResult}; |
| 4 |
use std::str::FromStr; |
| 5 |
|
| 6 |
#[cfg(feature = "serde1")] |
| 7 |
use serde::{Deserialize, Serialize}; |
| 8 |
|
| 9 |
/// A shared memory segment parsed from `/proc/sysvipc/shm` |
| 10 |
/// Relation with [crate::process::MMapPath::Vsys] |
| 11 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 12 |
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] |
| 13 |
#[allow(non_snake_case)] |
| 14 |
pub struct Shm { |
| 15 |
/// Segment key |
| 16 |
pub key: i32, |
| 17 |
/// Segment ID, unique |
| 18 |
pub shmid: u64, |
| 19 |
/// Access permissions, as octal |
| 20 |
pub perms: u16, |
| 21 |
/// Size in bytes |
| 22 |
pub size: u64, |
| 23 |
/// Creator PID |
| 24 |
pub cpid: i32, |
| 25 |
/// Last operator PID |
| 26 |
pub lpid: i32, |
| 27 |
/// Number of attached processes |
| 28 |
pub nattch: u32, |
| 29 |
/// User ID |
| 30 |
pub uid: u16, |
| 31 |
/// Group ID |
| 32 |
pub gid: u16, |
| 33 |
/// Creator UID |
| 34 |
pub cuid: u16, |
| 35 |
/// Creator GID |
| 36 |
pub cgid: u16, |
| 37 |
/// Time of last `shmat` (attach), epoch |
| 38 |
pub atime: u64, |
| 39 |
/// Time of last `shmdt` (detach), epoch |
| 40 |
pub dtime: u64, |
| 41 |
/// Time of last permission change, epoch |
| 42 |
pub ctime: u64, |
| 43 |
/// Current part of the shared memory resident in memory |
| 44 |
pub rss: u64, |
| 45 |
/// Current part of the shared memory in SWAP |
| 46 |
pub swap: u64, |
| 47 |
} |
| 48 |
|
| 49 |
/// A set of shared memory segments parsed from `/proc/sysvipc/shm` |
| 50 |
#[derive(Debug, Clone)] |
| 51 |
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] |
| 52 |
pub struct SharedMemorySegments(pub Vec<Shm>); |
| 53 |
|
| 54 |
impl super::FromBufRead for SharedMemorySegments { |
| 55 |
fn from_buf_read<R: io::BufRead>(r: R) -> ProcResult<Self> { |
| 56 |
let mut vec = Vec::new(); |
| 57 |
|
| 58 |
// See printing code here: |
| 59 |
// https://elixir.bootlin.com/linux/latest/source/ipc/shm.c#L1737 |
| 60 |
for line in r.lines().skip(1) { |
| 61 |
let line = expect!(line); |
| 62 |
let mut s = line.split_whitespace(); |
| 63 |
|
| 64 |
let key = expect!(i32::from_str(expect!(s.next()))); |
| 65 |
let shmid = expect!(u64::from_str(expect!(s.next()))); |
| 66 |
let perms = expect!(u16::from_str(expect!(s.next()))); |
| 67 |
let size = expect!(u64::from_str(expect!(s.next()))); |
| 68 |
let cpid = expect!(i32::from_str(expect!(s.next()))); |
| 69 |
let lpid = expect!(i32::from_str(expect!(s.next()))); |
| 70 |
let nattch = expect!(u32::from_str(expect!(s.next()))); |
| 71 |
let uid = expect!(u16::from_str(expect!(s.next()))); |
| 72 |
let gid = expect!(u16::from_str(expect!(s.next()))); |
| 73 |
let cuid = expect!(u16::from_str(expect!(s.next()))); |
| 74 |
let cgid = expect!(u16::from_str(expect!(s.next()))); |
| 75 |
let atime = expect!(u64::from_str(expect!(s.next()))); |
| 76 |
let dtime = expect!(u64::from_str(expect!(s.next()))); |
| 77 |
let ctime = expect!(u64::from_str(expect!(s.next()))); |
| 78 |
let rss = expect!(u64::from_str(expect!(s.next()))); |
| 79 |
let swap = expect!(u64::from_str(expect!(s.next()))); |
| 80 |
|
| 81 |
let shm = Shm { |
| 82 |
key, |
| 83 |
shmid, |
| 84 |
perms, |
| 85 |
size, |
| 86 |
cpid, |
| 87 |
lpid, |
| 88 |
nattch, |
| 89 |
uid, |
| 90 |
gid, |
| 91 |
cuid, |
| 92 |
cgid, |
| 93 |
atime, |
| 94 |
dtime, |
| 95 |
ctime, |
| 96 |
rss, |
| 97 |
swap, |
| 98 |
}; |
| 99 |
|
| 100 |
vec.push(shm); |
| 101 |
} |
| 102 |
|
| 103 |
Ok(SharedMemorySegments(vec)) |
| 104 |
} |
| 105 |
} |