ferrix_app/
kernel.rs

1/* kernel.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//! Information about kernel modules
22
23use anyhow::Result;
24use async_std::task;
25use ferrix_lib::sys::KModules;
26use serde::{Serialize, Deserialize};
27use std::process::Command;
28
29use crate::DataLoadingState;
30
31pub async fn get_kmodules() -> DataLoadingState<KResult> {
32    let output = task::spawn_blocking(|| {
33        Command::new("pkexec")
34            .arg("/usr/bin/ferrix-polkit")
35            .arg("kmods")
36            .output()
37    })
38    .await;
39
40    if let Err(why) = output {
41        return DataLoadingState::Error(why.to_string());
42    }
43    let output = output.unwrap();
44    if !output.status.success() {
45        return DataLoadingState::Error(format!(
46            "[ferrix-polkit] Non-zero return code:\n{}",
47            String::from_utf8_lossy(&output.stderr)
48        ));
49    }
50
51    let json_str = String::from_utf8_lossy(&output.stdout);
52    let json_data = KResult::from_json(&json_str);
53
54    match json_data {
55        Ok(data) => DataLoadingState::Loaded(data),
56        Err(why) => DataLoadingState::Error(why.to_string()),
57    }
58}
59
60#[derive(Debug, Serialize, Deserialize, Clone)]
61#[serde(untagged)]
62pub enum KResult {
63    Ok { data: KModules },
64    Err { error: String },
65}
66
67impl KResult {
68    pub fn new() -> Self {
69        match KModules::new() {
70            Ok(data) => Self::Ok { data, },
71            Err(why) => Self::Err { error: why.to_string(), },
72        }
73    }
74
75    pub fn to_json(&self) -> Result<String> {
76        Ok(serde_json::to_string(&self)?)
77    }
78
79    pub fn from_json(json: &str) -> Result<Self> {
80        Ok(serde_json::from_str(json)?)
81    }
82}