Skip to main content

linuxutils_system/
ctrlaltdel.rs

1use linuxutils_common::man::ManContent;
2
3pub const MAN: ManContent = ManContent::empty();
4
5use clap::Parser;
6use std::process::ExitCode;
7
8const PROC_PATH: &str = "/proc/sys/kernel/ctrl-alt-del";
9
10#[derive(Parser)]
11#[command(
12    name = "ctrlaltdel",
13    about = "Set the function of the Ctrl-Alt-Del combination"
14)]
15pub struct Args {
16    /// Mode: hard or soft (omit to query current setting)
17    mode: Option<String>,
18}
19
20pub fn run(args: Args) -> ExitCode {
21    match args.mode.as_deref() {
22        None => match std::fs::read_to_string(PROC_PATH) {
23            Ok(val) => {
24                match val.trim() {
25                    "0" => println!("soft"),
26                    "1" => println!("hard"),
27                    other => println!("{other}"),
28                }
29                ExitCode::SUCCESS
30            }
31            Err(e) => {
32                eprintln!("ctrlaltdel: failed to read {PROC_PATH}: {e}");
33                ExitCode::FAILURE
34            }
35        },
36        Some("hard") => {
37            if let Err(e) = std::fs::write(PROC_PATH, "1\n") {
38                eprintln!("ctrlaltdel: failed to set hard: {e}");
39                return ExitCode::FAILURE;
40            }
41            ExitCode::SUCCESS
42        }
43        Some("soft") => {
44            if let Err(e) = std::fs::write(PROC_PATH, "0\n") {
45                eprintln!("ctrlaltdel: failed to set soft: {e}");
46                return ExitCode::FAILURE;
47            }
48            ExitCode::SUCCESS
49        }
50        Some(other) => {
51            eprintln!(
52                "ctrlaltdel: unknown mode '{other}' (expected 'hard' or 'soft')"
53            );
54            ExitCode::FAILURE
55        }
56    }
57}