Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exit): suspended jobs warning and sending kill signals #287

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions brush-core/src/builtins/exit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Parser;

use std::io::Write;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, a Ctrl+D based exit won't actually invoke the exit built-in. Do we need to make an additional change to make sure that path also works in the same way?


use crate::{builtins, commands};

/// Exit the shell.
Expand All @@ -14,6 +16,18 @@ impl builtins::Command for ExitCommand {
&self,
context: commands::ExecutionContext<'_>,
) -> Result<crate::builtins::ExitCode, crate::error::Error> {
if !context.shell.jobs.jobs.is_empty() && context.shell.user_tried_exiting == 0 {
writeln!(context.stdout(), "brush: You have suspended jobs.")?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor code note: would be nice to avoid hard-coding the display name of the shell. If I recall correctly Shell::shell_name can be used.


context.shell.user_tried_exiting = 2; // get's decreased this input too so next input will be 1

return Ok(builtins::ExitCode::Custom(1))
}

for job in &mut context.shell.jobs.jobs {
job.kill(Some(nix::sys::signal::SIGHUP))?;
}

let code_8bit: u8;

#[allow(clippy::cast_sign_loss)]
Expand Down
2 changes: 1 addition & 1 deletion brush-core/src/builtins/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl builtins::Command for KillCommand {
if pid_or_job_spec.starts_with('%') {
// It's a job spec.
if let Some(job) = context.shell.jobs.resolve_job_spec(pid_or_job_spec) {
job.kill()?;
job.kill(None)?;
} else {
writeln!(
context.stderr(),
Expand Down
4 changes: 2 additions & 2 deletions brush-core/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ impl Job {
}

/// Kills the job.
pub fn kill(&mut self) -> Result<(), error::Error> {
pub fn kill(&mut self, signal: Option<nix::sys::signal::Signal>) -> Result<(), error::Error> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may run into challenges exposing a nix type in this function--I recall it being used in non-Unix platforms (e.g., Windows). An alternative approach might be to define a separate abstract enum type for signals we can send to jobs. It might require adding some new functions under the sys module too.

if let Some(pid) = self.get_process_group_id() {
sys::signal::kill_process(pid)
sys::signal::kill_process(pid, signal)
} else {
Err(error::Error::FailedToSendSignal)
}
Expand Down
5 changes: 5 additions & 0 deletions brush-core/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub struct Shell {
// Additional state
/// The status of the last completed command.
pub last_exit_status: u8,

/// User tried exiting in the previous/current input if != 0.
pub user_tried_exiting: u8,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this to be an integer instead of a simple bool flag? Are there cases in which you think we'd need to track how many times the user tried exiting?


/// Clone depth from the original ancestor shell.
pub depth: usize,
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Clone for Shell {
builtins: self.builtins.clone(),
program_location_cache: self.program_location_cache.clone(),
depth: self.depth + 1,
user_tried_exiting: self.user_tried_exiting
}
}
}
Expand Down Expand Up @@ -191,6 +195,7 @@ impl Shell {
builtins: builtins::get_default_builtins(options),
program_location_cache: pathcache::PathCache::default(),
depth: 0,
user_tried_exiting: 0
};

// TODO: Without this a script that sets extglob will fail because we
Expand Down
4 changes: 2 additions & 2 deletions brush-core/src/sys/unix/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub(crate) fn continue_process(pid: sys::process::ProcessId) -> Result<(), error
Ok(())
}

pub(crate) fn kill_process(pid: sys::process::ProcessId) -> Result<(), error::Error> {
nix::sys::signal::kill(nix::unistd::Pid::from_raw(pid), nix::sys::signal::SIGKILL)
pub(crate) fn kill_process(pid: sys::process::ProcessId, signal: Option<nix::sys::signal::Signal>) -> Result<(), error::Error> {
nix::sys::signal::kill(nix::unistd::Pid::from_raw(pid), signal.unwrap_or_else(|| nix::sys::signal::SIGKILL))
.map_err(|_errno| error::Error::FailedToSendSignal)?;

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions brush-interactive/src/interactive_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ pub trait InteractiveShell {
ReadResult::Input(read_result) => {
let mut shell_mut = self.shell_mut();

if shell_mut.as_mut().user_tried_exiting > 0 && !read_result.is_empty() {
shell_mut.as_mut().user_tried_exiting -= 1;
}

let precmd_prompt = shell_mut.as_mut().compose_precmd_prompt().await?;
if !precmd_prompt.is_empty() {
print!("{precmd_prompt}");
Expand Down
Loading