瀏覽代碼

Shell: Avoid recreating the event loop before exec()

This stops the local socket creation spam.
AnotherTest 4 年之前
父節點
當前提交
0141f7e7fd
共有 3 個文件被更改,包括 28 次插入15 次删除
  1. 4 0
      Shell/Builtin.cpp
  2. 23 15
      Shell/Shell.cpp
  3. 1 0
      Shell/Shell.h

+ 4 - 0
Shell/Builtin.cpp

@@ -27,6 +27,7 @@
 #include "Shell.h"
 #include "Shell.h"
 #include <AK/LexicalPath.h>
 #include <AK/LexicalPath.h>
 #include <LibCore/ArgsParser.h>
 #include <LibCore/ArgsParser.h>
+#include <LibCore/EventLoop.h>
 #include <LibCore/File.h>
 #include <LibCore/File.h>
 #include <inttypes.h>
 #include <inttypes.h>
 #include <signal.h>
 #include <signal.h>
@@ -830,6 +831,9 @@ bool Shell::run_builtin(const AST::Command& command, const NonnullRefPtrVector<A
         }
         }
     }
     }
 
 
+    Core::EventLoop loop;
+    setup_signals();
+
 #define __ENUMERATE_SHELL_BUILTIN(builtin)                        \
 #define __ENUMERATE_SHELL_BUILTIN(builtin)                        \
     if (name == #builtin) {                                       \
     if (name == #builtin) {                                       \
         retval = builtin_##builtin(argv.size() - 1, argv.data()); \
         retval = builtin_##builtin(argv.size() - 1, argv.data()); \

+ 23 - 15
Shell/Shell.cpp

@@ -68,21 +68,23 @@ namespace Shell {
 
 
 void Shell::setup_signals()
 void Shell::setup_signals()
 {
 {
-    Core::EventLoop::register_signal(SIGCHLD, [this](int) {
+    if (m_should_reinstall_signal_handlers) {
+        Core::EventLoop::register_signal(SIGCHLD, [this](int) {
 #ifdef SH_DEBUG
 #ifdef SH_DEBUG
-        dbgln("SIGCHLD!");
+            dbgln("SIGCHLD!");
 #endif
 #endif
-        notify_child_event();
-    });
+            notify_child_event();
+        });
 
 
-    Core::EventLoop::register_signal(SIGTSTP, [this](auto) {
-        auto job = current_job();
-        kill_job(job, SIGTSTP);
-        if (job) {
-            job->set_is_suspended(true);
-            job->unblock();
-        }
-    });
+        Core::EventLoop::register_signal(SIGTSTP, [this](auto) {
+            auto job = current_job();
+            kill_job(job, SIGTSTP);
+            if (job) {
+                job->set_is_suspended(true);
+                job->unblock();
+            }
+        });
+    }
 }
 }
 
 
 void Shell::print_path(const String& path)
 void Shell::print_path(const String& path)
@@ -484,6 +486,9 @@ bool Shell::invoke_function(const AST::Command& command, int& retval)
     argv.take_first();
     argv.take_first();
     set_local_variable("ARGV", adopt(*new AST::ListValue(move(argv))), true);
     set_local_variable("ARGV", adopt(*new AST::ListValue(move(argv))), true);
 
 
+    Core::EventLoop loop;
+    setup_signals();
+
     function.body->run(*this);
     function.body->run(*this);
 
 
     retval = last_return_code;
     retval = last_return_code;
@@ -670,6 +675,8 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
         return IterationDecision::Continue;
         return IterationDecision::Continue;
     };
     };
 
 
+    TemporaryChange signal_handler_install { m_should_reinstall_signal_handlers, false };
+
     for (auto& redirection : m_global_redirections) {
     for (auto& redirection : m_global_redirections) {
         if (resolve_redirection(redirection) == IterationDecision::Break)
         if (resolve_redirection(redirection) == IterationDecision::Break)
             return nullptr;
             return nullptr;
@@ -732,6 +739,7 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
         m_is_subshell = true;
         m_is_subshell = true;
         m_pid = getpid();
         m_pid = getpid();
         Core::EventLoop::notify_forked(Core::EventLoop::ForkEvent::Child);
         Core::EventLoop::notify_forked(Core::EventLoop::ForkEvent::Child);
+        TemporaryChange signal_handler_install { m_should_reinstall_signal_handlers, true };
 
 
         if (apply_rewirings() == IterationDecision::Break)
         if (apply_rewirings() == IterationDecision::Break)
             _exit(126);
             _exit(126);
@@ -756,12 +764,12 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
         if (!m_is_subshell && command.should_wait)
         if (!m_is_subshell && command.should_wait)
             tcsetattr(0, TCSANOW, &default_termios);
             tcsetattr(0, TCSANOW, &default_termios);
 
 
-        Core::EventLoop mainloop;
-        setup_signals();
-
         if (command.should_immediately_execute_next) {
         if (command.should_immediately_execute_next) {
             ASSERT(command.argv.is_empty());
             ASSERT(command.argv.is_empty());
 
 
+            Core::EventLoop mainloop;
+            setup_signals();
+
             for (auto& next_in_chain : command.next_chain)
             for (auto& next_in_chain : command.next_chain)
                 run_tail(command, next_in_chain, 0);
                 run_tail(command, next_in_chain, 0);
 
 

+ 1 - 0
Shell/Shell.h

@@ -260,6 +260,7 @@ private:
     HashMap<String, String> m_aliases;
     HashMap<String, String> m_aliases;
     bool m_is_interactive { true };
     bool m_is_interactive { true };
     bool m_is_subshell { false };
     bool m_is_subshell { false };
+    bool m_should_reinstall_signal_handlers { true };
 
 
     bool m_should_format_live { false };
     bool m_should_format_live { false };