1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 12:05:08 +00:00

Shell: Wait for the parent to deal with the PGID stuff before execvp()

This commit is contained in:
AnotherTest 2020-08-12 01:14:34 +04:30 committed by Andreas Kling
parent be395aab9a
commit 2b51250863
2 changed files with 35 additions and 0 deletions

View file

@ -53,6 +53,8 @@ public:
dbg() << "Command \"" << m_cmd << "\" finished in " << elapsed << " ms"; dbg() << "Command \"" << m_cmd << "\" finished in " << elapsed << " ms";
} }
#endif #endif
if (m_pipeline)
m_pipeline = nullptr;
} }
pid_t pgid() const { return m_pgid; } pid_t pgid() const { return m_pgid; }

View file

@ -513,6 +513,12 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
if (run_builtin(argv.size() - 1, argv.data(), retval)) if (run_builtin(argv.size() - 1, argv.data(), retval))
return nullptr; return nullptr;
int sync_pipe[2];
if (pipe(sync_pipe) < 0) {
perror("pipe");
return nullptr;
}
pid_t child = fork(); pid_t child = fork();
if (child < 0) { if (child < 0) {
perror("fork"); perror("fork");
@ -520,6 +526,8 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
} }
if (child == 0) { if (child == 0) {
close(sync_pipe[1]);
tcsetattr(0, TCSANOW, &default_termios); tcsetattr(0, TCSANOW, &default_termios);
for (auto& rewiring : rewirings) { for (auto& rewiring : rewirings) {
@ -539,6 +547,18 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
fds.collect(); fds.collect();
u8 c;
while (read(sync_pipe[0], &c, 1) < 0) {
if (errno != EINTR) {
perror("read");
// There's nothing interesting we can do here.
break;
}
dbg() << "Oof";
}
close(sync_pipe[0]);
int rc = execvp(argv[0], const_cast<char* const*>(argv.data())); int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
if (rc < 0) { if (rc < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
@ -567,6 +587,8 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
close(sync_pipe[0]);
bool is_first = !command.pipeline || (command.pipeline && command.pipeline->pgid == -1); bool is_first = !command.pipeline || (command.pipeline && command.pipeline->pgid == -1);
if (command.pipeline) { if (command.pipeline) {
@ -584,6 +606,17 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
tcsetpgrp(STDIN_FILENO, pgid); tcsetpgrp(STDIN_FILENO, pgid);
} }
while (write(sync_pipe[1], "x", 1) < 0) {
if (errno != EINTR) {
perror("write");
// There's nothing interesting we can do here.
break;
}
dbg() << "Oof";
}
close(sync_pipe[1]);
StringBuilder cmd; StringBuilder cmd;
cmd.join(" ", command.argv); cmd.join(" ", command.argv);