From 2b99617ae07b7f5d6e58494f5fbec1cb3d3446f0 Mon Sep 17 00:00:00 2001
From: Michael Aaron Murphy <mmstickman@gmail.com>
Date: Sat, 29 Jul 2017 17:42:28 -0400
Subject: [PATCH] Background & Foreground Fixes

---
 src/shell/pipe_exec/fork.rs | 3 +--
 src/shell/pipe_exec/mod.rs  | 4 +++-
 src/sys/unix.rs             | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/shell/pipe_exec/fork.rs b/src/shell/pipe_exec/fork.rs
index c38ffd6d..c0b76fc7 100644
--- a/src/shell/pipe_exec/fork.rs
+++ b/src/shell/pipe_exec/fork.rs
@@ -16,10 +16,9 @@ use std::process::exit;
 pub fn fork_pipe(shell: &mut Shell, commands: Vec<(RefinedJob, JobKind)>, command_name: String) -> i32 {
     match unsafe { sys::fork() } {
         Ok(0) => {
-            // The child fork should not have any signals blocked, so the shell can control it.
-            signals::unblock();
             let _ = sys::reset_signal(sys::SIGINT);
             let _ = sys::reset_signal(sys::SIGHUP);
+            let _ = sys::reset_signal(sys::SIGTERM);
             // This ensures that the child fork has a unique PGID.
             create_process_group(0);
             // After execution of it's commands, exit with the last command's status.
diff --git a/src/shell/pipe_exec/mod.rs b/src/shell/pipe_exec/mod.rs
index d449bf0e..250a53e3 100644
--- a/src/shell/pipe_exec/mod.rs
+++ b/src/shell/pipe_exec/mod.rs
@@ -23,7 +23,6 @@ use std::os::unix::process::CommandExt;
 use std::process::{exit, Command};
 use sys;
 
-
 /// Use dup2 to replace `old` with `new` using `old`s file descriptor ID
 fn redir(old: RawFd, new: RawFd) {
     if let Err(e) = sys::dup2(old, new) {
@@ -257,6 +256,9 @@ pub fn pipe(shell: &mut Shell, commands: Vec<(RefinedJob, JobKind)>, foreground:
                                     match unsafe { sys::fork() } {
                                         Ok(0) => {
                                             signals::unblock();
+                                            let _ = sys::reset_signal(sys::SIGINT);
+                                            let _ = sys::reset_signal(sys::SIGHUP);
+                                            let _ = sys::reset_signal(sys::SIGTERM);
                                             create_process_group(pgid);
                                             let args: Vec<&str> = args
                                                 .iter()
diff --git a/src/sys/unix.rs b/src/sys/unix.rs
index b516fbf6..a020ea72 100644
--- a/src/sys/unix.rs
+++ b/src/sys/unix.rs
@@ -252,7 +252,7 @@ pub mod job_control {
     {
         let mut exit_status = 0;
         loop {
-            match wait() {
+            match waitpid(-1, Some(WUNTRACED)) {
                 Ok(WaitStatus::Exited(pid, status)) => if pid == (last_pid as i32) {
                     break status as i32;
                 } else {
-- 
GitLab