summaryrefslogtreecommitdiff
path: root/Shell
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-08-12 01:14:34 +0430
committerAndreas Kling <kling@serenityos.org>2020-08-12 11:41:18 +0200
commit2b512508634da0c27468af0ba6f1256bc01b1344 (patch)
tree0f7597e282b186fa9dc32a7c8f8a8cf58537b88f /Shell
parentbe395aab9a123a88058e73871fc30899763b5e29 (diff)
downloadserenity-2b512508634da0c27468af0ba6f1256bc01b1344.zip
Shell: Wait for the parent to deal with the PGID stuff before execvp()
Diffstat (limited to 'Shell')
-rw-r--r--Shell/Job.h2
-rw-r--r--Shell/Shell.cpp33
2 files changed, 35 insertions, 0 deletions
diff --git a/Shell/Job.h b/Shell/Job.h
index 7b13042091..5dd53cc4ac 100644
--- a/Shell/Job.h
+++ b/Shell/Job.h
@@ -53,6 +53,8 @@ public:
dbg() << "Command \"" << m_cmd << "\" finished in " << elapsed << " ms";
}
#endif
+ if (m_pipeline)
+ m_pipeline = nullptr;
}
pid_t pgid() const { return m_pgid; }
diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp
index 58eeda947d..b9d621f16c 100644
--- a/Shell/Shell.cpp
+++ b/Shell/Shell.cpp
@@ -513,6 +513,12 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
if (run_builtin(argv.size() - 1, argv.data(), retval))
return nullptr;
+ int sync_pipe[2];
+ if (pipe(sync_pipe) < 0) {
+ perror("pipe");
+ return nullptr;
+ }
+
pid_t child = fork();
if (child < 0) {
perror("fork");
@@ -520,6 +526,8 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
}
if (child == 0) {
+ close(sync_pipe[1]);
+
tcsetattr(0, TCSANOW, &default_termios);
for (auto& rewiring : rewirings) {
@@ -539,6 +547,18 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
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()));
if (rc < 0) {
if (errno == ENOENT) {
@@ -567,6 +587,8 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
ASSERT_NOT_REACHED();
}
+ close(sync_pipe[0]);
+
bool is_first = !command.pipeline || (command.pipeline && command.pipeline->pgid == -1);
if (command.pipeline) {
@@ -584,6 +606,17 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
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;
cmd.join(" ", command.argv);