Shell: Avoid recreating the event loop before exec()

This stops the local socket creation spam.
This commit is contained in:
AnotherTest 2020-12-07 19:37:04 +03:30 committed by Andreas Kling
parent 3c41487db7
commit 0141f7e7fd
Notes: sideshowbarker 2024-07-19 00:58:26 +09:00
3 changed files with 28 additions and 15 deletions

View file

@ -27,6 +27,7 @@
#include "Shell.h"
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <inttypes.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) \
if (name == #builtin) { \
retval = builtin_##builtin(argv.size() - 1, argv.data()); \

View file

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

View file

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