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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! # Signal Bitmap Interpreter
//
//! A simple library to interpret signal bitmaps for a process, read
//! from `/proc/<pid>/status`. Supported signal bitmaps include pending
//! signals (`SigPnd`), shared pending signals (`ShdPnd`), blocked signals
//! (`SigBlk`), ignored signals (`SigIgn`), and caught signals (`SigCgt`).
#![warn(unused_extern_crates)]
use clap::{Parser, ValueEnum};
use std::{
    cmp::Ordering,
    fmt,
    fs::File,
    io::{BufRead, BufReader, Error},
};
use textwrap::{fill, Options};

const SUB_WIDTH: usize = 45;
const MAX_WIDTH: usize = 80;

const NR_SIGS: u8 = 64;
const SIGRTMIN_STR: &str = "RTMIN";
const SIGRTMAX_STR: &str = "RTMAX";
const SIGRTMIN_IDX: u8 = 0x22;
const SIGRTMAX_IDX: u8 = 0x40;

static SIG_TAB: &[&str; 32] = &[
    "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE", "KILL", "USR1",
    "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", "CONT", "STOP",
    "TSTP", "TTIN", "TTOU", "URG", "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH",
    "POLL", "IO", "PWR", "SYS",
];
static POSIX_RANGE: std::ops::Range<u8> = 0x01..0x20;
static RTMIN_RANGE: std::ops::Range<u8> = 0x20..0x32;
static RTMAX_RANGE: std::ops::Range<u8> = 0x32..0x41;

/// The type of signal bitmap.
#[derive(ValueEnum, Clone, Debug, Default)]
pub enum BitmapType {
    /// Pending signals (thread).
    #[default]
    SigPnd,

    /// Pending signals (shared between threads in a process).
    ShdPnd,

    /// Blocked signals.
    SigBlk,

    /// Ignored signals.
    SigIgn,

    /// Caught signals.
    SigCgt,
}

/// Interpret signal bitmaps for a process.
#[derive(Parser, Debug)]
#[command(version, about, long_about)]
pub struct SigBitmapArgs {
    /// PID of the process.
    #[arg(short, long)]
    pub pid: u32,

    /// Type of bitmap to interpret.
    #[arg(short, long, value_enum, default_value_t=BitmapType::SigPnd)]
    pub map: BitmapType,
}

// String representation (line prefix in `/proc<pid>/status`)
// of a signal bitmap type.
impl fmt::Display for BitmapType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            BitmapType::SigPnd => write!(f, "SigPnd:"),
            BitmapType::ShdPnd => write!(f, "ShdPnd:"),
            BitmapType::SigBlk => write!(f, "SigBlk:"),
            BitmapType::SigIgn => write!(f, "SigIgn:"),
            BitmapType::SigCgt => write!(f, "SigCgt:"),
        }
    }
}

// Return the parsed value of the string representation
// of the signal bitmap.
fn proc_bitmap(pid: &u32, typ: &BitmapType) -> u64 {
    let lpfx: String = typ.to_string();
    let file: Result<File, Error> =
        File::open(format!("/proc/{}/status", pid).as_str());

    if let Ok(fread) = file {
        let fbuff: BufReader<File> = BufReader::new(fread);
        for line in fbuff.lines().flatten() {
            if line.starts_with(&lpfx) {
                return u64::from_str_radix(
                    line.trim_start_matches(&lpfx).trim(),
                    16,
                )
                .unwrap();
            }
        }
    }

    0x0
}

// Return a string describing the signal number
// index passed in the argument `idx`.
fn sigabbrev_np(idx: &u8) -> String {
    if POSIX_RANGE.contains(idx) {
        return SIG_TAB[(*idx as usize) - 1].to_string();
    }

    if RTMIN_RANGE.contains(idx) {
        return fmt_range(idx, &SIGRTMIN_IDX, SIGRTMIN_STR);
    }

    if RTMAX_RANGE.contains(idx) {
        return fmt_range(idx, &SIGRTMAX_IDX, SIGRTMAX_STR);
    }

    "INVL".to_string()
}

// Return the string representation of a signal number.
// This is specifically used for RT{MIN,MAX}+/-N.
fn fmt_range(idx: &u8, off: &u8, tmpl: &str) -> String {
    let diff: i8 = (*idx as i8) - (*off as i8);
    match diff.cmp(&0) {
        Ordering::Equal => tmpl.to_string(),
        _ => format!("{}{:+}", tmpl, diff),
    }
}

// Return the formatted string representation of all the
// signals contained in the signal bitmap `map`.
fn fmt_bitmap(map: &u64) -> (String, u8) {
    let mut sig_idx: u8 = 0x1;
    let mut sig_cnt: u8 = 0x0;
    let mut sig_vec: Vec<String> = Vec::new();

    while sig_idx < NR_SIGS {
        if (map & (0x1_u64 << (sig_idx - 1))) != 0 {
            sig_vec.push(sigabbrev_np(&sig_idx));
            sig_cnt += 1;
        }
        sig_idx += 1;
    }

    (sig_vec.join(", "), sig_cnt)
}

/// Displays the formatted string representaion of the specified
/// type of signal bitmap for a given process. This function outputs
/// an empty map if the process doesn't exist or if there is an error
/// interpreting the signal bitmap.
///
/// # Arguments
///
/// * `args` - A reference to an `enum` containing the process
///            ID (PID) and the signal bitmap type.
///
/// # Example
/// ```
/// // Print the list of signals ignored by a process with PID: 42.
/// use sig_bitmap::{interpret, BitmapType, SigBitmapArgs};
/// let args: SigBitmapArgs = SigBitmapArgs{pid: 42, map: BitmapType::SigIgn};
/// interpret(&args);
/// ````
pub fn interpret(args: &SigBitmapArgs) {
    let bmap: u64 = proc_bitmap(&args.pid, &args.map);
    let sfmt: &str = &" ".repeat(SUB_WIDTH);

    let (mut lst, cnt): (String, u8) = fmt_bitmap(&bmap);

    if lst.is_empty() {
        lst = String::from("NONE");
    }

    let out: String = fill(
        &format!(
            "PID: {:<6} {} {:<2} [0x{:016x}]: {}",
            args.pid, args.map, cnt, bmap, lst,
        ),
        Options::new(MAX_WIDTH)
            .subsequent_indent(sfmt)
            .word_splitter(textwrap::WordSplitter::NoHyphenation)
            .break_words(false),
    );

    println!("{out}");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sigabbrev_np() {
        let tests: Vec<(&str, u8)> = Vec::<(&str, u8)>::from([
            ("KILL", 0x09),
            ("RTMIN", 0x22),
            ("RTMIN+2", 0x24),
            ("RTMAX", 0x40),
            ("RTMAX-2", 0x3e),
            ("INVL", 0x00),
        ]);

        for test in tests {
            assert_eq!(test.0, sigabbrev_np(&test.1));
        }
    }

    #[test]
    fn test_bitmaptype_str() {
        let tests: Vec<(BitmapType, &str)> = Vec::<(BitmapType, &str)>::from([
            (BitmapType::SigPnd, "SigPnd"),
            (BitmapType::ShdPnd, "ShdPnd"),
            (BitmapType::SigBlk, "SigBlk"),
            (BitmapType::SigIgn, "SigIgn"),
            (BitmapType::SigCgt, "SigCgt"),
        ]);

        for test in tests {
            assert!(test.0.to_string().contains(test.1));
        }
    }
    #[test]
    fn test_fmt_bitmap() {
        let bmap: u64 = 0xbadc0ffee;
        let sigs: &str = "INT, QUIT, ILL, ABRT, BUS, FPE, KILL, USR1, \
            SEGV, USR2, PIPE, ALRM, TERM, STKFLT, URG, XCPU, XFSZ, PROF, \
            WINCH, IO, RTMIN-2, RTMIN-1, RTMIN, RTMIN+2";
        let (sfmt, count): (String, u8) = fmt_bitmap(&bmap);
        assert_eq!(sfmt, sigs);
        assert_eq!(count, 24);
    }
}