strat9_kernel/shell/commands/util/audit.rs
1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use super::*;
24use alloc::string::String;
25
26/// Display recent audit log entries.
27///
28/// Usage: `audit [count]` (default: last 30 entries)
29pub fn cmd_audit(args: &[String]) -> Result<(), ShellError> {
30 let count: usize = if !args.is_empty() {
31 args[0].parse().unwrap_or(30)
32 } else {
33 30
34 };
35
36 let entries = crate::audit::recent(count);
37 let hz = crate::arch::timer::TIMER_HZ;
38
39 if entries.is_empty() {
40 shell_println!("(no audit events)");
41 return Ok(());
42 }
43
44 shell_println!(
45 "{:>6} {:>8} {:>5} {:>5} {:>10} {}",
46 "SEQ",
47 "TIME",
48 "PID",
49 "SID",
50 "CATEGORY",
51 "MESSAGE"
52 );
53 for e in &entries {
54 let secs = e.tick / hz;
55 let cs = (e.tick % hz) * 100 / hz;
56 let cat = match e.category {
57 crate::audit::AuditCategory::Silo => "silo",
58 crate::audit::AuditCategory::Capability => "cap",
59 crate::audit::AuditCategory::Syscall => "syscall",
60 crate::audit::AuditCategory::Process => "process",
61 crate::audit::AuditCategory::Security => "security",
62 };
63 shell_println!(
64 "{:>6} {:>5}.{:02} {:>5} {:>5} {:>10} {}",
65 e.seq,
66 secs,
67 cs,
68 e.pid,
69 e.silo_id,
70 cat,
71 e.message
72 );
73 }
74 shell_println!("({} total events since boot)", crate::audit::total_count());
75 Ok(())
76}