summaryrefslogtreecommitdiffstats
path: root/third_party/rust/procfs-core/src/sysvipc_shm.rs
blob: 9b9081360d169caa56a856e72129494e7f3faeb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::io;

use super::{expect, ProcResult};
use std::str::FromStr;

#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

/// A shared memory segment parsed from `/proc/sysvipc/shm`
/// Relation with [crate::process::MMapPath::Vsys]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[allow(non_snake_case)]
pub struct Shm {
    /// Segment key
    pub key: i32,
    /// Segment ID, unique
    pub shmid: u64,
    /// Access permissions, as octal
    pub perms: u16,
    /// Size in bytes
    pub size: u64,
    /// Creator PID
    pub cpid: i32,
    /// Last operator PID
    pub lpid: i32,
    /// Number of attached processes
    pub nattch: u32,
    /// User ID
    pub uid: u16,
    /// Group ID
    pub gid: u16,
    /// Creator UID
    pub cuid: u16,
    /// Creator GID
    pub cgid: u16,
    /// Time of last `shmat` (attach), epoch
    pub atime: u64,
    /// Time of last `shmdt` (detach), epoch
    pub dtime: u64,
    /// Time of last permission change, epoch
    pub ctime: u64,
    /// Current part of the shared memory resident in memory
    pub rss: u64,
    /// Current part of the shared memory in SWAP
    pub swap: u64,
}

/// A set of shared memory segments parsed from `/proc/sysvipc/shm`
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct SharedMemorySegments(pub Vec<Shm>);

impl super::FromBufRead for SharedMemorySegments {
    fn from_buf_read<R: io::BufRead>(r: R) -> ProcResult<Self> {
        let mut vec = Vec::new();

        // See printing code here:
        // https://elixir.bootlin.com/linux/latest/source/ipc/shm.c#L1737
        for line in r.lines().skip(1) {
            let line = expect!(line);
            let mut s = line.split_whitespace();

            let key = expect!(i32::from_str(expect!(s.next())));
            let shmid = expect!(u64::from_str(expect!(s.next())));
            let perms = expect!(u16::from_str(expect!(s.next())));
            let size = expect!(u64::from_str(expect!(s.next())));
            let cpid = expect!(i32::from_str(expect!(s.next())));
            let lpid = expect!(i32::from_str(expect!(s.next())));
            let nattch = expect!(u32::from_str(expect!(s.next())));
            let uid = expect!(u16::from_str(expect!(s.next())));
            let gid = expect!(u16::from_str(expect!(s.next())));
            let cuid = expect!(u16::from_str(expect!(s.next())));
            let cgid = expect!(u16::from_str(expect!(s.next())));
            let atime = expect!(u64::from_str(expect!(s.next())));
            let dtime = expect!(u64::from_str(expect!(s.next())));
            let ctime = expect!(u64::from_str(expect!(s.next())));
            let rss = expect!(u64::from_str(expect!(s.next())));
            let swap = expect!(u64::from_str(expect!(s.next())));

            let shm = Shm {
                key,
                shmid,
                perms,
                size,
                cpid,
                lpid,
                nattch,
                uid,
                gid,
                cuid,
                cgid,
                atime,
                dtime,
                ctime,
                rss,
                swap,
            };

            vec.push(shm);
        }

        Ok(SharedMemorySegments(vec))
    }
}