|
| 1 | +// This file is part of the uutils procps package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use clap::{arg, crate_version, value_parser, Arg, Command}; |
| 7 | +#[cfg(unix)] |
| 8 | +use nix::{sys::signal, sys::signal::Signal, unistd::Pid}; |
| 9 | +use uu_snice::{ |
| 10 | + collect_pids, construct_verbose_result, print_signals, process_matcher, ActionResult, |
| 11 | +}; |
| 12 | +use uucore::error::USimpleError; |
| 13 | +#[cfg(unix)] |
| 14 | +use uucore::signals::signal_by_name_or_value; |
| 15 | +use uucore::{error::UResult, format_usage, help_about, help_usage}; |
| 16 | + |
| 17 | +const ABOUT: &str = help_about!("skill.md"); |
| 18 | +const USAGE: &str = help_usage!("skill.md"); |
| 19 | + |
| 20 | +#[uucore::main] |
| 21 | +pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
| 22 | + let matches = uu_app().try_get_matches_from(args)?; |
| 23 | + let settings = process_matcher::Settings::try_new(&matches)?; |
| 24 | + |
| 25 | + // Case0: Print SIGNALS |
| 26 | + if let Some(display) = &settings.display { |
| 27 | + print_signals(display); |
| 28 | + return Ok(()); |
| 29 | + } |
| 30 | + |
| 31 | + // Case1: Send signal |
| 32 | + if let Some(targets) = settings.expressions { |
| 33 | + let pids = collect_pids(&targets); |
| 34 | + |
| 35 | + #[cfg(unix)] |
| 36 | + let signal_str = matches.get_one::<String>("signal").cloned(); |
| 37 | + |
| 38 | + #[cfg(unix)] |
| 39 | + let signal = if let Some(sig) = signal_str { |
| 40 | + (signal_by_name_or_value(sig.strip_prefix('-').unwrap()).unwrap() as i32).try_into()? |
| 41 | + } else { |
| 42 | + Signal::SIGTERM |
| 43 | + }; |
| 44 | + |
| 45 | + #[cfg(unix)] |
| 46 | + let results = perform_action(&pids, &signal); |
| 47 | + #[cfg(not(unix))] |
| 48 | + let results: Vec<Option<ActionResult>> = Vec::new(); |
| 49 | + |
| 50 | + if results.iter().all(|it| it.is_none()) || results.is_empty() { |
| 51 | + return Err(USimpleError::new(1, "no process selection criteria")); |
| 52 | + } |
| 53 | + |
| 54 | + if settings.verbose { |
| 55 | + let output = construct_verbose_result(&pids, &results).trim().to_owned(); |
| 56 | + println!("{output}"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(unix)] |
| 64 | +fn perform_action(pids: &[u32], signal: &Signal) -> Vec<Option<ActionResult>> { |
| 65 | + pids.iter() |
| 66 | + .map(|pid| { |
| 67 | + { |
| 68 | + Some(match signal::kill(Pid::from_raw(*pid as i32), *signal) { |
| 69 | + Ok(_) => ActionResult::Success, |
| 70 | + |
| 71 | + Err(_) => ActionResult::PermissionDenied, |
| 72 | + }) |
| 73 | + } |
| 74 | + }) |
| 75 | + .collect() |
| 76 | +} |
| 77 | + |
| 78 | +#[allow(clippy::cognitive_complexity)] |
| 79 | +pub fn uu_app() -> Command { |
| 80 | + Command::new(uucore::util_name()) |
| 81 | + .version(crate_version!()) |
| 82 | + .about(ABOUT) |
| 83 | + .override_usage(format_usage(USAGE)) |
| 84 | + .infer_long_args(true) |
| 85 | + .arg_required_else_help(true) |
| 86 | + .arg(Arg::new("signal")) |
| 87 | + .args([ |
| 88 | + // arg!(-f --fast "fast mode (not implemented)"), |
| 89 | + // arg!(-i --interactive "interactive"), |
| 90 | + arg!(-l --list "list all signal names"), |
| 91 | + arg!(-L --table "list all signal names in a nice table"), |
| 92 | + // arg!(-n --"no-action" "do not actually kill processes; just print what would happen"), |
| 93 | + arg!(-v --verbose "explain what is being done"), |
| 94 | + // arg!(-w --warnings "enable warnings (not implemented)"), |
| 95 | + // Expressions |
| 96 | + arg!(-c --command <command> ... "expression is a command name"), |
| 97 | + arg!(-p --pid <pid> ... "expression is a process id number") |
| 98 | + .value_parser(value_parser!(u32)), |
| 99 | + arg!(-t --tty <tty> ... "expression is a terminal"), |
| 100 | + arg!(-u --user <username> ... "expression is a username"), |
| 101 | + // arg!(--ns <PID> "match the processes that belong to the same namespace as <pid>"), |
| 102 | + // arg!(--nslist <ns> "list which namespaces will be considered for the --ns option.") |
| 103 | + // .value_delimiter(',') |
| 104 | + // .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]), |
| 105 | + ]) |
| 106 | +} |
0 commit comments