2021-11-29 18:21:55 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
|
|
use nu_protocol::{ast::Call, span};
|
|
|
|
use nu_protocol::{
|
2022-02-13 02:18:27 +00:00
|
|
|
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
|
2022-12-21 19:20:46 +00:00
|
|
|
Signature, Spanned, SyntaxShape, Type, Value,
|
2021-11-29 18:21:55 +00:00
|
|
|
};
|
|
|
|
use std::process::{Command as CommandSys, Stdio};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Kill;
|
|
|
|
|
|
|
|
impl Command for Kill {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"kill"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Kill a process using the process id."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
let signature = Signature::build("kill")
|
2022-12-21 19:20:46 +00:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
|
|
|
.allow_variants_without_examples(true)
|
2021-11-29 18:21:55 +00:00
|
|
|
.required(
|
|
|
|
"pid",
|
|
|
|
SyntaxShape::Int,
|
2023-12-15 06:32:37 +00:00
|
|
|
"Process id of process that is to be killed.",
|
2021-11-29 18:21:55 +00:00
|
|
|
)
|
2023-12-15 06:32:37 +00:00
|
|
|
.rest("rest", SyntaxShape::Int, "Rest of processes to kill.")
|
2021-11-29 18:21:55 +00:00
|
|
|
.switch("force", "forcefully kill the process", Some('f'))
|
|
|
|
.switch("quiet", "won't print anything to the console", Some('q'))
|
|
|
|
.category(Category::Platform);
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
signature.named(
|
|
|
|
"signal",
|
|
|
|
SyntaxShape::Int,
|
|
|
|
"signal decimal number to be sent instead of the default 15 (unsupported on Windows)",
|
|
|
|
Some('s'),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-06 17:11:04 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["stop", "end", "close"]
|
|
|
|
}
|
|
|
|
|
2021-11-29 18:21:55 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let pid: i64 = call.req(engine_state, stack, 0)?;
|
|
|
|
let rest: Vec<i64> = call.rest(engine_state, stack, 1)?;
|
2024-01-11 15:19:48 +00:00
|
|
|
let force: bool = call.has_flag(engine_state, stack, "force")?;
|
2021-11-29 18:21:55 +00:00
|
|
|
let signal: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "signal")?;
|
2024-01-11 15:19:48 +00:00
|
|
|
let quiet: bool = call.has_flag(engine_state, stack, "quiet")?;
|
2021-11-29 18:21:55 +00:00
|
|
|
|
|
|
|
let mut cmd = if cfg!(windows) {
|
|
|
|
let mut cmd = CommandSys::new("taskkill");
|
|
|
|
|
|
|
|
if force {
|
|
|
|
cmd.arg("/F");
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.arg("/PID");
|
|
|
|
cmd.arg(pid.to_string());
|
|
|
|
|
|
|
|
// each pid must written as `/PID 0` otherwise
|
|
|
|
// taskkill will act as `killall` unix command
|
|
|
|
for id in &rest {
|
|
|
|
cmd.arg("/PID");
|
|
|
|
cmd.arg(id.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd
|
|
|
|
} else {
|
|
|
|
let mut cmd = CommandSys::new("kill");
|
|
|
|
if force {
|
|
|
|
if let Some(Spanned {
|
|
|
|
item: _,
|
|
|
|
span: signal_span,
|
|
|
|
}) = signal
|
|
|
|
{
|
|
|
|
return Err(ShellError::IncompatibleParameters {
|
|
|
|
left_message: "force".to_string(),
|
2021-12-04 12:38:21 +00:00
|
|
|
left_span: call
|
|
|
|
.get_named_arg("force")
|
2023-12-06 23:40:03 +00:00
|
|
|
.ok_or_else(|| ShellError::GenericError {
|
|
|
|
error: "Flag error".into(),
|
|
|
|
msg: "flag force not found".into(),
|
|
|
|
span: Some(call.head),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
2021-12-04 12:38:21 +00:00
|
|
|
})?
|
|
|
|
.span,
|
2021-11-29 18:21:55 +00:00
|
|
|
right_message: "signal".to_string(),
|
|
|
|
right_span: span(&[
|
2021-12-04 12:38:21 +00:00
|
|
|
call.get_named_arg("signal")
|
2023-12-06 23:40:03 +00:00
|
|
|
.ok_or_else(|| ShellError::GenericError {
|
|
|
|
error: "Flag error".into(),
|
|
|
|
msg: "flag signal not found".into(),
|
|
|
|
span: Some(call.head),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
2021-12-04 12:38:21 +00:00
|
|
|
})?
|
|
|
|
.span,
|
2021-11-29 18:21:55 +00:00
|
|
|
signal_span,
|
|
|
|
]),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cmd.arg("-9");
|
|
|
|
} else if let Some(signal_value) = signal {
|
2022-01-13 19:40:25 +00:00
|
|
|
cmd.arg(format!("-{}", signal_value.item));
|
2021-11-29 18:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.arg(pid.to_string());
|
|
|
|
|
|
|
|
cmd.args(rest.iter().map(move |id| id.to_string()));
|
|
|
|
|
|
|
|
cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
// pipe everything to null
|
|
|
|
if quiet {
|
|
|
|
cmd.stdin(Stdio::null())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null());
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:40:03 +00:00
|
|
|
let output = cmd.output().map_err(|e| ShellError::GenericError {
|
|
|
|
error: "failed to execute shell command".into(),
|
|
|
|
msg: e.to_string(),
|
|
|
|
span: Some(call.head),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
2022-08-18 16:58:51 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
if !quiet && !output.status.success() {
|
2023-12-06 23:40:03 +00:00
|
|
|
return Err(ShellError::GenericError {
|
|
|
|
error: "process didn't terminate successfully".into(),
|
|
|
|
msg: String::from_utf8(output.stderr).unwrap_or_default(),
|
|
|
|
span: Some(call.head),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
|
|
|
});
|
2022-08-18 16:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 02:18:27 +00:00
|
|
|
let val = String::from(
|
|
|
|
String::from_utf8(output.stdout)
|
2023-12-06 23:40:03 +00:00
|
|
|
.map_err(|e| ShellError::GenericError {
|
|
|
|
error: "failed to convert output to string".into(),
|
|
|
|
msg: e.to_string(),
|
|
|
|
span: Some(call.head),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
2022-08-18 16:58:51 +00:00
|
|
|
})?
|
2022-02-13 02:18:27 +00:00
|
|
|
.trim_end(),
|
|
|
|
);
|
|
|
|
if val.is_empty() {
|
2023-09-03 14:27:29 +00:00
|
|
|
Ok(Value::nothing(call.head).into_pipeline_data())
|
2022-02-13 02:18:27 +00:00
|
|
|
} else {
|
2023-09-03 14:27:29 +00:00
|
|
|
Ok(vec![Value::string(val, call.head)]
|
|
|
|
.into_iter()
|
|
|
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
2022-02-13 02:18:27 +00:00
|
|
|
}
|
2021-11-29 18:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Kill the pid using the most memory",
|
2022-02-09 23:20:20 +00:00
|
|
|
example: "ps | sort-by mem | last | kill $in.pid",
|
2021-11-29 18:21:55 +00:00
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Force kill a given pid",
|
|
|
|
example: "kill --force 12345",
|
|
|
|
result: None,
|
|
|
|
},
|
2022-12-05 00:39:54 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2021-11-29 18:21:55 +00:00
|
|
|
Example {
|
|
|
|
description: "Send INT signal",
|
|
|
|
example: "kill -s 2 12345",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Kill;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::test_examples;
|
|
|
|
test_examples(Kill {})
|
|
|
|
}
|
|
|
|
}
|