ferrix_lib/
vulnerabilities.rs

1/* vulnerabilities.rs
2 *
3 * Copyright 2025 Michail Krasnov <mskrasnov07@ya.ru>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21//! Get information about CPU vulnerabilities
22
23use anyhow::Result;
24use serde::{Deserialize, Serialize};
25use std::fs::{read_dir, read_to_string};
26
27use crate::traits::ToJson;
28
29pub type Name = String;
30pub type Description = String;
31
32#[derive(Debug, Deserialize, Serialize, Clone)]
33pub struct Vulnerabilities {
34    pub list: Vec<(Name, Description)>,
35}
36
37const VULN_FILES_DIR: &str = "/sys/devices/system/cpu/vulnerabilities/";
38
39impl Vulnerabilities {
40    pub fn new() -> Result<Self> {
41        let mut list = Vec::new();
42
43        for file in read_dir(VULN_FILES_DIR)? {
44            let file = file?.path();
45            if file.is_file() {
46                let name = match file.file_name() {
47                    Some(name) => name.to_string_lossy(),
48                    None => continue,
49                };
50                let description = read_to_string(&file)?;
51
52                list.push((name.to_string(), description));
53            }
54        }
55        list.sort_by_key(|l| l.0.clone());
56
57        Ok(Self { list })
58    }
59}
60
61impl ToJson for Vulnerabilities {}