diff --git a/Shell/AST.cpp b/Shell/AST.cpp index fd2c0460b8..121530c66a 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -799,7 +799,7 @@ RefPtr Execute::run(RefPtr shell) } else if (command.should_notify_if_in_background) { if (job) job->set_running_in_background(true); - shell->take_back_stdin(); + shell->restore_stdin(); } } } diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index 622dbf5478..e409ed4ede 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -435,8 +435,6 @@ RefPtr Shell::run_command(AST::Command& command) return nullptr; } if (child == 0) { - setpgid(0, 0); - tcsetpgrp(0, getpid()); tcsetattr(0, TCSANOW, &default_termios); for (auto& rewiring : rewirings) { #ifdef SH_DEBUG @@ -511,14 +509,16 @@ bool Shell::run_file(const String& filename, bool explicitly_invoked) run_command(data); return true; } -void Shell::take_back_stdin() +void Shell::restore_stdin() { - tcsetpgrp(0, m_pid); tcsetattr(0, TCSANOW, &termios); } void Shell::block_on_job(RefPtr job) { + ScopedValueRollback accepting_signal_rollback(m_is_accepting_signals); + m_is_accepting_signals = false; + if (!job) return; @@ -529,13 +529,13 @@ void Shell::block_on_job(RefPtr job) loop.quit(0); }; if (job->exited()) { - take_back_stdin(); + restore_stdin(); return; } loop.exec(); - take_back_stdin(); + restore_stdin(); } String Shell::get_history_path() @@ -872,7 +872,7 @@ Vector Shell::complete_option(const String& program_ bool Shell::read_single_line() { - take_back_stdin(); + restore_stdin(); auto line_result = editor->get_line(prompt()); if (line_result.is_error()) { diff --git a/Shell/Shell.h b/Shell/Shell.h index dba1525e0e..1b95118b2e 100644 --- a/Shell/Shell.h +++ b/Shell/Shell.h @@ -71,6 +71,8 @@ class Shell : public Core::Object { public: constexpr static auto init_file_path = "~/shell-init.sh"; + bool is_accepting_signals() const { return m_is_accepting_signals; } + int run_command(const StringView&); RefPtr run_command(AST::Command&); bool run_file(const String&, bool explicitly_invoked = true); @@ -104,7 +106,7 @@ public: Vector complete_user(const String&, size_t offset); Vector complete_option(const String&, const String&, size_t offset); - void take_back_stdin(); + void restore_stdin(); u64 find_last_job_id() const; const Job* find_job(u64 id); @@ -180,6 +182,7 @@ private: StringBuilder m_complete_line_builder; bool m_should_ignore_jobs_on_next_exit { false }; + bool m_is_accepting_signals { true }; pid_t m_pid { 0 }; HashMap> m_local_variables; diff --git a/Shell/main.cpp b/Shell/main.cpp index 0939de5eb3..d08994c3a6 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -61,6 +61,8 @@ int main(int argc, char** argv) Core::EventLoop loop; signal(SIGINT, [](int) { + if (!s_shell->is_accepting_signals()) + return; editor->interrupted(); }); @@ -68,7 +70,12 @@ int main(int argc, char** argv) editor->resized(); }); + signal(SIGTTIN, [](int) {}); + signal(SIGTTOU, [](int) {}); + signal(SIGHUP, [](int) { + if (!s_shell->is_accepting_signals()) + return; s_shell->save_history(); });