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

Shell: Moves pipelined processes to one process group

This commit is contained in:
AnotherTest 2020-08-11 15:54:46 +04:30 committed by Andreas Kling
parent 8a17527bc5
commit ab3e787334
6 changed files with 44 additions and 11 deletions

View file

@ -1142,6 +1142,14 @@ RefPtr<Value> Pipe::run(RefPtr<Shell> shell)
last_in_left.should_wait = false; last_in_left.should_wait = false;
last_in_left.is_pipe_source = true; last_in_left.is_pipe_source = true;
if (first_in_right.pipeline) {
last_in_left.pipeline = first_in_right.pipeline;
} else {
auto pipeline = adopt(*new Pipeline);
last_in_left.pipeline = pipeline;
first_in_right.pipeline = pipeline;
}
Vector<Command> commands; Vector<Command> commands;
commands.append(left); commands.append(left);
commands.append(last_in_left); commands.append(last_in_left);

View file

@ -150,9 +150,15 @@ private:
virtual bool is_fd_redirection() const override { return true; } virtual bool is_fd_redirection() const override { return true; }
}; };
class Pipeline : public RefCounted<Pipeline> {
public:
pid_t pgid { -1 };
};
struct Command { struct Command {
Vector<String> argv; Vector<String> argv;
NonnullRefPtrVector<Redirection> redirections; NonnullRefPtrVector<Redirection> redirections;
mutable RefPtr<Pipeline> pipeline;
bool should_wait { true }; bool should_wait { true };
bool is_pipe_source { false }; bool is_pipe_source { false };
bool should_notify_if_in_background { true }; bool should_notify_if_in_background { true };
@ -190,7 +196,7 @@ public:
} }
CommandValue(Vector<String> argv) CommandValue(Vector<String> argv)
: m_command({ move(argv), {}, true, false, true }) : m_command({ move(argv), {}, {}, true, false, true })
{ {
} }

View file

@ -32,5 +32,6 @@ namespace AST {
class Node; class Node;
class Value; class Value;
class SyntaxError; class SyntaxError;
class Pipeline;
} }

View file

@ -25,6 +25,7 @@
*/ */
#include "Job.h" #include "Job.h"
#include "AST.h"
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h> #include <sys/wait.h>

View file

@ -27,6 +27,7 @@
#pragma once #pragma once
#include "Execution.h" #include "Execution.h"
#include "Forward.h"
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/JsonValue.h> #include <AK/JsonValue.h>
@ -42,7 +43,7 @@
class Job : public RefCounted<Job> { class Job : public RefCounted<Job> {
public: public:
static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String command, u64 job_id) { return adopt(*new Job(pid, pgid, move(command), job_id)); } static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String command, u64 job_id, AST::Pipeline* pipeline = nullptr) { return adopt(*new Job(pid, pgid, move(command), job_id, pipeline)); }
~Job() ~Job()
{ {
@ -121,12 +122,15 @@ public:
bool print_status(PrintStatusMode); bool print_status(PrintStatusMode);
private: private:
Job(pid_t pid, unsigned pgid, String cmd, u64 job_id) Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Pipeline* pipeline)
: m_pgid(pgid) : m_pgid(pgid)
, m_pid(pid) , m_pid(pid)
, m_job_id(job_id) , m_job_id(job_id)
, m_cmd(move(cmd)) , m_cmd(move(cmd))
{ {
if (pipeline)
m_pipeline = *pipeline;
set_running_in_background(false); set_running_in_background(false);
m_command_timer.start(); m_command_timer.start();
} }
@ -143,4 +147,5 @@ private:
mutable bool m_active { true }; mutable bool m_active { true };
mutable bool m_is_suspended { false }; mutable bool m_is_suspended { false };
bool m_should_be_disowned { false }; bool m_should_be_disowned { false };
RefPtr<AST::Pipeline> m_pipeline;
}; };

View file

@ -518,15 +518,10 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
perror("fork"); perror("fork");
return nullptr; return nullptr;
} }
if (child == 0) { if (child == 0) {
setpgid(0, 0);
tcsetattr(0, TCSANOW, &default_termios); tcsetattr(0, TCSANOW, &default_termios);
if (command.should_wait) {
auto pid = getpid();
auto pgid = getpgid(pid);
tcsetpgrp(STDOUT_FILENO, pgid);
tcsetpgrp(STDIN_FILENO, pgid);
}
for (auto& rewiring : rewirings) { for (auto& rewiring : rewirings) {
#ifdef SH_DEBUG #ifdef SH_DEBUG
dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), rewiring.dest_fd, rewiring.source_fd); dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), rewiring.dest_fd, rewiring.source_fd);
@ -572,10 +567,27 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
bool is_first = !command.pipeline || (command.pipeline && command.pipeline->pgid == -1);
if (command.pipeline) {
if (is_first) {
command.pipeline->pgid = child;
}
}
pid_t pgid = is_first ? child : (command.pipeline ? command.pipeline->pgid : child);
if (setpgid(child, pgid) < 0)
perror("setpgid");
if (command.should_wait) {
tcsetpgrp(STDOUT_FILENO, pgid);
tcsetpgrp(STDIN_FILENO, pgid);
}
StringBuilder cmd; StringBuilder cmd;
cmd.join(" ", command.argv); cmd.join(" ", command.argv);
auto job = Job::create(child, (unsigned)child, cmd.build(), find_last_job_id() + 1); auto job = Job::create(child, pgid, cmd.build(), find_last_job_id() + 1, command.pipeline);
jobs.set((u64)child, job); jobs.set((u64)child, job);
job->on_exit = [](auto job) { job->on_exit = [](auto job) {