Skip to main content

strat9_kernel/shell/commands/proc/
mod.rs

1//! Process management commands (kill)
2use crate::{shell::ShellError, shell_println};
3use alloc::string::String;
4
5pub fn cmd_kill(args: &[String]) -> Result<(), ShellError> {
6    if args.is_empty() {
7        shell_println!("Usage: kill <pid>");
8        return Err(ShellError::InvalidArguments);
9    }
10    let pid_val: u32 = args[0].parse().map_err(|_| {
11        shell_println!("kill: invalid pid '{}'", args[0]);
12        ShellError::InvalidArguments
13    })?;
14
15    let task_id = match crate::process::get_task_id_by_pid(pid_val) {
16        Some(tid) => tid,
17        None => {
18            shell_println!("kill: no task with pid {}", pid_val);
19            return Err(ShellError::ExecutionFailed);
20        }
21    };
22
23    if crate::process::kill_task(task_id) {
24        shell_println!(
25            "kill: terminated pid {} (tid={})",
26            pid_val,
27            task_id.as_u64()
28        );
29        Ok(())
30    } else {
31        shell_println!("kill: failed to terminate pid {}", pid_val);
32        Err(ShellError::ExecutionFailed)
33    }
34}