mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 22:17:43 +00:00
LibCore+Tests: Unify process handling into Command class
The Test262RunnerHandler class in test-test262 was made in order to spawn a subprocess, connect to its input/output error pipes, and obtain its return value. Later on, a copy of this implementation was added to TestSed with modifications, such as adding support for reading from the output pipes as well. Unify these two implementations into a new Core::Command class. This new implementation is more closely modeled from the TestSed implementation due to the extra functionality it implemented.
This commit is contained in:
parent
cb920b23cc
commit
5dd93474ee
4 changed files with 160 additions and 249 deletions
|
@ -13,15 +13,11 @@
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/Command.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/Process.h>
|
|
||||||
#include <LibCore/System.h>
|
|
||||||
#include <LibFileSystem/FileSystem.h>
|
#include <LibFileSystem/FileSystem.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <LibTest/TestRunnerUtil.h>
|
#include <LibTest/TestRunnerUtil.h>
|
||||||
#include <spawn.h>
|
|
||||||
#include <sys/wait.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
enum class TestResult {
|
enum class TestResult {
|
||||||
Passed,
|
Passed,
|
||||||
|
@ -111,117 +107,6 @@ static StringView emoji_for_result(TestResult result)
|
||||||
|
|
||||||
static constexpr StringView total_test_emoji = "🧪"sv;
|
static constexpr StringView total_test_emoji = "🧪"sv;
|
||||||
|
|
||||||
class Test262RunnerHandler {
|
|
||||||
public:
|
|
||||||
static ErrorOr<OwnPtr<Test262RunnerHandler>> create(StringView command, char const* const arguments[])
|
|
||||||
{
|
|
||||||
auto write_pipe_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
|
||||||
auto read_pipe_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
|
||||||
|
|
||||||
posix_spawn_file_actions_t file_actions;
|
|
||||||
posix_spawn_file_actions_init(&file_actions);
|
|
||||||
posix_spawn_file_actions_adddup2(&file_actions, write_pipe_fds[0], STDIN_FILENO);
|
|
||||||
posix_spawn_file_actions_adddup2(&file_actions, read_pipe_fds[1], STDOUT_FILENO);
|
|
||||||
|
|
||||||
auto pid = TRY(Core::System::posix_spawnp(command, &file_actions, nullptr, const_cast<char**>(arguments), environ));
|
|
||||||
|
|
||||||
posix_spawn_file_actions_destroy(&file_actions);
|
|
||||||
ArmedScopeGuard runner_kill { [&pid] { kill(pid, SIGKILL); } };
|
|
||||||
|
|
||||||
TRY(Core::System::close(write_pipe_fds[0]));
|
|
||||||
TRY(Core::System::close(read_pipe_fds[1]));
|
|
||||||
|
|
||||||
auto infile = TRY(Core::File::adopt_fd(read_pipe_fds[0], Core::File::OpenMode::Read));
|
|
||||||
|
|
||||||
auto outfile = TRY(Core::File::adopt_fd(write_pipe_fds[1], Core::File::OpenMode::Write));
|
|
||||||
|
|
||||||
runner_kill.disarm();
|
|
||||||
|
|
||||||
return make<Test262RunnerHandler>(pid, move(infile), move(outfile));
|
|
||||||
}
|
|
||||||
|
|
||||||
Test262RunnerHandler(pid_t pid, NonnullOwnPtr<Core::File> in_file, NonnullOwnPtr<Core::File> out_file)
|
|
||||||
: m_pid(pid)
|
|
||||||
, m_input(move(in_file))
|
|
||||||
, m_output(move(out_file))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool write_lines(Span<DeprecatedString> lines)
|
|
||||||
{
|
|
||||||
// It's possible the process dies before we can write all the tests
|
|
||||||
// to the stdin. So make sure that we don't crash but just stop writing.
|
|
||||||
struct sigaction action_handler {
|
|
||||||
.sa_handler = SIG_IGN, .sa_mask = {}, .sa_flags = 0,
|
|
||||||
};
|
|
||||||
struct sigaction old_action_handler;
|
|
||||||
if (sigaction(SIGPIPE, &action_handler, &old_action_handler) < 0) {
|
|
||||||
perror("sigaction");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (DeprecatedString const& line : lines) {
|
|
||||||
if (m_output->write_until_depleted(DeprecatedString::formatted("{}\n", line).bytes()).is_error())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that the input stream ends here, whether we were able to write all lines or not
|
|
||||||
m_output->close();
|
|
||||||
|
|
||||||
// It's not really a problem if this signal failed
|
|
||||||
if (sigaction(SIGPIPE, &old_action_handler, nullptr) < 0)
|
|
||||||
perror("sigaction");
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
DeprecatedString read_all()
|
|
||||||
{
|
|
||||||
auto all_output_or_error = m_input->read_until_eof();
|
|
||||||
if (all_output_or_error.is_error()) {
|
|
||||||
warnln("Got error: {} while reading runner output", all_output_or_error.error());
|
|
||||||
return ""sv;
|
|
||||||
}
|
|
||||||
return DeprecatedString(all_output_or_error.value().bytes(), Chomp);
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class ProcessResult {
|
|
||||||
Running,
|
|
||||||
DoneWithZeroExitCode,
|
|
||||||
Failed,
|
|
||||||
FailedFromTimeout,
|
|
||||||
Unknown,
|
|
||||||
};
|
|
||||||
|
|
||||||
ErrorOr<ProcessResult> status()
|
|
||||||
{
|
|
||||||
if (m_pid == -1)
|
|
||||||
return ProcessResult::Unknown;
|
|
||||||
|
|
||||||
m_output->close();
|
|
||||||
|
|
||||||
auto wait_result = TRY(Core::System::waitpid(m_pid, WNOHANG));
|
|
||||||
if (wait_result.pid == 0) {
|
|
||||||
// Attempt to kill it, since it has not finished yet somehow
|
|
||||||
return ProcessResult::Running;
|
|
||||||
}
|
|
||||||
m_pid = -1;
|
|
||||||
|
|
||||||
if (WIFSIGNALED(wait_result.status) && WTERMSIG(wait_result.status) == SIGALRM)
|
|
||||||
return ProcessResult::FailedFromTimeout;
|
|
||||||
|
|
||||||
if (WIFEXITED(wait_result.status) && WEXITSTATUS(wait_result.status) == 0)
|
|
||||||
return ProcessResult::DoneWithZeroExitCode;
|
|
||||||
|
|
||||||
return ProcessResult::Failed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
pid_t m_pid;
|
|
||||||
NonnullOwnPtr<Core::File> m_input;
|
|
||||||
NonnullOwnPtr<Core::File> m_output;
|
|
||||||
};
|
|
||||||
|
|
||||||
static ErrorOr<HashMap<size_t, TestResult>> run_test_files(Span<DeprecatedString> files, size_t offset, StringView command, char const* const arguments[])
|
static ErrorOr<HashMap<size_t, TestResult>> run_test_files(Span<DeprecatedString> files, size_t offset, StringView command, char const* const arguments[])
|
||||||
{
|
{
|
||||||
HashMap<size_t, TestResult> results {};
|
HashMap<size_t, TestResult> results {};
|
||||||
|
@ -234,7 +119,7 @@ static ErrorOr<HashMap<size_t, TestResult>> run_test_files(Span<DeprecatedString
|
||||||
};
|
};
|
||||||
|
|
||||||
while (test_index < files.size()) {
|
while (test_index < files.size()) {
|
||||||
auto runner_process_or_error = Test262RunnerHandler::create(command, arguments);
|
auto runner_process_or_error = Core::Command::create(command, arguments);
|
||||||
if (runner_process_or_error.is_error()) {
|
if (runner_process_or_error.is_error()) {
|
||||||
fail_all_after();
|
fail_all_after();
|
||||||
return results;
|
return results;
|
||||||
|
@ -246,12 +131,19 @@ static ErrorOr<HashMap<size_t, TestResult>> run_test_files(Span<DeprecatedString
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString output = runner_process->read_all();
|
auto output_or_error = runner_process->read_all();
|
||||||
|
DeprecatedString output;
|
||||||
|
|
||||||
|
if (output_or_error.is_error())
|
||||||
|
warnln("Got error: {} while reading runner output", output_or_error.error());
|
||||||
|
else
|
||||||
|
output = DeprecatedString(output_or_error.release_value().standard_error.bytes(), Chomp);
|
||||||
|
|
||||||
auto status_or_error = runner_process->status();
|
auto status_or_error = runner_process->status();
|
||||||
bool failed = false;
|
bool failed = false;
|
||||||
if (!status_or_error.is_error()) {
|
if (!status_or_error.is_error()) {
|
||||||
VERIFY(status_or_error.value() != Test262RunnerHandler::ProcessResult::Running);
|
VERIFY(status_or_error.value() != Core::Command::ProcessResult::Running);
|
||||||
failed = status_or_error.value() != Test262RunnerHandler::ProcessResult::DoneWithZeroExitCode;
|
failed = status_or_error.value() != Core::Command::ProcessResult::DoneWithZeroExitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (StringView line : output.split_view('\n')) {
|
for (StringView line : output.split_view('\n')) {
|
||||||
|
@ -285,7 +177,7 @@ static ErrorOr<HashMap<size_t, TestResult>> run_test_files(Span<DeprecatedString
|
||||||
|
|
||||||
if (failed) {
|
if (failed) {
|
||||||
TestResult result = TestResult::ProcessError;
|
TestResult result = TestResult::ProcessError;
|
||||||
if (!status_or_error.is_error() && status_or_error.value() == Test262RunnerHandler::ProcessResult::FailedFromTimeout) {
|
if (!status_or_error.is_error() && status_or_error.value() == Core::Command::ProcessResult::FailedFromTimeout) {
|
||||||
result = TestResult::TimeoutError;
|
result = TestResult::TimeoutError;
|
||||||
}
|
}
|
||||||
// assume the last test failed, if by SIGALRM signal it's a timeout
|
// assume the last test failed, if by SIGALRM signal it's a timeout
|
||||||
|
|
|
@ -6,145 +6,20 @@
|
||||||
|
|
||||||
#include <AK/ScopeGuard.h>
|
#include <AK/ScopeGuard.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
|
#include <LibCore/Command.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/System.h>
|
|
||||||
#include <LibTest/Macros.h>
|
#include <LibTest/Macros.h>
|
||||||
#include <LibTest/TestCase.h>
|
#include <LibTest/TestCase.h>
|
||||||
#include <spawn.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
class Process {
|
|
||||||
public:
|
|
||||||
struct ProcessOutputs {
|
|
||||||
AK::ByteBuffer standard_output;
|
|
||||||
AK::ByteBuffer standard_error;
|
|
||||||
};
|
|
||||||
|
|
||||||
static ErrorOr<OwnPtr<Process>> create(StringView command, char const* const arguments[])
|
|
||||||
{
|
|
||||||
auto stdin_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
|
||||||
auto stdout_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
|
||||||
auto stderr_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
|
||||||
|
|
||||||
posix_spawn_file_actions_t file_actions;
|
|
||||||
posix_spawn_file_actions_init(&file_actions);
|
|
||||||
posix_spawn_file_actions_adddup2(&file_actions, stdin_fds[0], STDIN_FILENO);
|
|
||||||
posix_spawn_file_actions_adddup2(&file_actions, stdout_fds[1], STDOUT_FILENO);
|
|
||||||
posix_spawn_file_actions_adddup2(&file_actions, stderr_fds[1], STDERR_FILENO);
|
|
||||||
|
|
||||||
auto pid = TRY(Core::System::posix_spawnp(command, &file_actions, nullptr, const_cast<char**>(arguments), environ));
|
|
||||||
|
|
||||||
posix_spawn_file_actions_destroy(&file_actions);
|
|
||||||
ArmedScopeGuard runner_kill { [&pid] { kill(pid, SIGKILL); } };
|
|
||||||
|
|
||||||
TRY(Core::System::close(stdin_fds[0]));
|
|
||||||
TRY(Core::System::close(stdout_fds[1]));
|
|
||||||
TRY(Core::System::close(stderr_fds[1]));
|
|
||||||
|
|
||||||
auto stdin_file = TRY(Core::File::adopt_fd(stdin_fds[1], Core::File::OpenMode::Write));
|
|
||||||
auto stdout_file = TRY(Core::File::adopt_fd(stdout_fds[0], Core::File::OpenMode::Read));
|
|
||||||
auto stderr_file = TRY(Core::File::adopt_fd(stderr_fds[0], Core::File::OpenMode::Read));
|
|
||||||
|
|
||||||
runner_kill.disarm();
|
|
||||||
|
|
||||||
return make<Process>(pid, move(stdin_file), move(stdout_file), move(stderr_file));
|
|
||||||
}
|
|
||||||
|
|
||||||
Process(pid_t pid, NonnullOwnPtr<Core::File> stdin_file, NonnullOwnPtr<Core::File> stdout_file, NonnullOwnPtr<Core::File> stderr_file)
|
|
||||||
: m_pid(pid)
|
|
||||||
, m_stdin(move(stdin_file))
|
|
||||||
, m_stdout(move(stdout_file))
|
|
||||||
, m_stderr(move(stderr_file))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<void> write(StringView input)
|
|
||||||
{
|
|
||||||
TRY(m_stdin->write_until_depleted(input.bytes()));
|
|
||||||
m_stdin->close();
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool write_lines(Span<DeprecatedString> lines)
|
|
||||||
{
|
|
||||||
// It's possible the process dies before we can write all the tests
|
|
||||||
// to the stdin. So make sure that we don't crash but just stop writing.
|
|
||||||
struct sigaction action_handler {
|
|
||||||
.sa_handler = SIG_IGN, .sa_mask = {}, .sa_flags = 0,
|
|
||||||
};
|
|
||||||
struct sigaction old_action_handler;
|
|
||||||
if (sigaction(SIGPIPE, &action_handler, &old_action_handler) < 0) {
|
|
||||||
perror("sigaction");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (DeprecatedString const& line : lines) {
|
|
||||||
if (m_stdin->write_until_depleted(DeprecatedString::formatted("{}\n", line).bytes()).is_error())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that the input stream ends here, whether we were able to write all lines or not
|
|
||||||
m_stdin->close();
|
|
||||||
|
|
||||||
// It's not really a problem if this signal failed
|
|
||||||
if (sigaction(SIGPIPE, &old_action_handler, nullptr) < 0)
|
|
||||||
perror("sigaction");
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<ProcessOutputs> read_all()
|
|
||||||
{
|
|
||||||
return ProcessOutputs { TRY(m_stdout->read_until_eof()), TRY(m_stderr->read_until_eof()) };
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class ProcessResult {
|
|
||||||
Running,
|
|
||||||
DoneWithZeroExitCode,
|
|
||||||
Failed,
|
|
||||||
FailedFromTimeout,
|
|
||||||
Unknown,
|
|
||||||
};
|
|
||||||
|
|
||||||
ErrorOr<ProcessResult> status(int options = 0)
|
|
||||||
{
|
|
||||||
if (m_pid == -1)
|
|
||||||
return ProcessResult::Unknown;
|
|
||||||
|
|
||||||
m_stdin->close();
|
|
||||||
|
|
||||||
auto wait_result = TRY(Core::System::waitpid(m_pid, options));
|
|
||||||
if (wait_result.pid == 0) {
|
|
||||||
// Attempt to kill it, since it has not finished yet somehow
|
|
||||||
return ProcessResult::Running;
|
|
||||||
}
|
|
||||||
m_pid = -1;
|
|
||||||
|
|
||||||
if (WIFSIGNALED(wait_result.status) && WTERMSIG(wait_result.status) == SIGALRM)
|
|
||||||
return ProcessResult::FailedFromTimeout;
|
|
||||||
|
|
||||||
if (WIFEXITED(wait_result.status) && WEXITSTATUS(wait_result.status) == 0)
|
|
||||||
return ProcessResult::DoneWithZeroExitCode;
|
|
||||||
|
|
||||||
return ProcessResult::Failed;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
pid_t m_pid;
|
|
||||||
NonnullOwnPtr<Core::File> m_stdin;
|
|
||||||
NonnullOwnPtr<Core::File> m_stdout;
|
|
||||||
NonnullOwnPtr<Core::File> m_stderr;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void run_sed(Vector<char const*>&& arguments, StringView standard_input, StringView expected_stdout)
|
static void run_sed(Vector<char const*>&& arguments, StringView standard_input, StringView expected_stdout)
|
||||||
{
|
{
|
||||||
MUST(arguments.try_insert(0, "sed"));
|
MUST(arguments.try_insert(0, "sed"));
|
||||||
MUST(arguments.try_append(nullptr));
|
MUST(arguments.try_append(nullptr));
|
||||||
auto sed = MUST(Process::create("sed"sv, arguments.data()));
|
auto sed = MUST(Core::Command::create("sed"sv, arguments.data()));
|
||||||
MUST(sed->write(standard_input));
|
MUST(sed->write(standard_input));
|
||||||
auto [stdout, stderr] = MUST(sed->read_all());
|
auto [stdout, stderr] = MUST(sed->read_all());
|
||||||
auto status = MUST(sed->status());
|
auto status = MUST(sed->status());
|
||||||
if (status != Process::ProcessResult::DoneWithZeroExitCode) {
|
if (status != Core::Command::ProcessResult::DoneWithZeroExitCode) {
|
||||||
FAIL(DeprecatedString::formatted("sed didn't exit cleanly: status: {}, stdout:{}, stderr: {}", static_cast<int>(status), StringView { stdout.bytes() }, StringView { stderr.bytes() }));
|
FAIL(DeprecatedString::formatted("sed didn't exit cleanly: status: {}, stdout:{}, stderr: {}", static_cast<int>(status), StringView { stdout.bytes() }, StringView { stderr.bytes() }));
|
||||||
}
|
}
|
||||||
EXPECT_EQ(StringView { expected_stdout.bytes() }, StringView { stdout.bytes() });
|
EXPECT_EQ(StringView { expected_stdout.bytes() }, StringView { stdout.bytes() });
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
||||||
|
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -8,6 +10,7 @@
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
#include <AK/ScopeGuard.h>
|
#include <AK/ScopeGuard.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
@ -15,6 +18,108 @@
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<Command>> Command::create(StringView command, char const* const arguments[])
|
||||||
|
{
|
||||||
|
auto stdin_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
||||||
|
auto stdout_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
||||||
|
auto stderr_fds = TRY(Core::System::pipe2(O_CLOEXEC));
|
||||||
|
|
||||||
|
posix_spawn_file_actions_t file_actions;
|
||||||
|
posix_spawn_file_actions_init(&file_actions);
|
||||||
|
posix_spawn_file_actions_adddup2(&file_actions, stdin_fds[0], STDIN_FILENO);
|
||||||
|
posix_spawn_file_actions_adddup2(&file_actions, stdout_fds[1], STDOUT_FILENO);
|
||||||
|
posix_spawn_file_actions_adddup2(&file_actions, stderr_fds[1], STDERR_FILENO);
|
||||||
|
|
||||||
|
auto pid = TRY(Core::System::posix_spawnp(command, &file_actions, nullptr, const_cast<char**>(arguments), System::environment()));
|
||||||
|
|
||||||
|
posix_spawn_file_actions_destroy(&file_actions);
|
||||||
|
ArmedScopeGuard runner_kill { [&pid] { kill(pid, SIGKILL); } };
|
||||||
|
|
||||||
|
TRY(Core::System::close(stdin_fds[0]));
|
||||||
|
TRY(Core::System::close(stdout_fds[1]));
|
||||||
|
TRY(Core::System::close(stderr_fds[1]));
|
||||||
|
|
||||||
|
auto stdin_file = TRY(Core::File::adopt_fd(stdin_fds[1], Core::File::OpenMode::Write));
|
||||||
|
auto stdout_file = TRY(Core::File::adopt_fd(stdout_fds[0], Core::File::OpenMode::Read));
|
||||||
|
auto stderr_file = TRY(Core::File::adopt_fd(stderr_fds[0], Core::File::OpenMode::Read));
|
||||||
|
|
||||||
|
runner_kill.disarm();
|
||||||
|
|
||||||
|
return make<Command>(pid, move(stdin_file), move(stdout_file), move(stderr_file));
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::Command(pid_t pid, NonnullOwnPtr<Core::File> stdin_file, NonnullOwnPtr<Core::File> stdout_file, NonnullOwnPtr<Core::File> stderr_file)
|
||||||
|
: m_pid(pid)
|
||||||
|
, m_stdin(move(stdin_file))
|
||||||
|
, m_stdout(move(stdout_file))
|
||||||
|
, m_stderr(move(stderr_file))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> Command::write(StringView input)
|
||||||
|
{
|
||||||
|
TRY(m_stdin->write_until_depleted(input.bytes()));
|
||||||
|
m_stdin->close();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Command::write_lines(Span<DeprecatedString> lines)
|
||||||
|
{
|
||||||
|
// It's possible the process dies before we can write everything to the
|
||||||
|
// stdin. So make sure that we don't crash but just stop writing.
|
||||||
|
|
||||||
|
struct sigaction action_handler { };
|
||||||
|
action_handler.sa_handler = SIG_IGN;
|
||||||
|
|
||||||
|
struct sigaction old_action_handler;
|
||||||
|
if (sigaction(SIGPIPE, &action_handler, &old_action_handler) < 0) {
|
||||||
|
perror("sigaction");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DeprecatedString const& line : lines) {
|
||||||
|
if (m_stdin->write_until_depleted(DeprecatedString::formatted("{}\n", line).bytes()).is_error())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the input stream ends here, whether we were able to write all lines or not
|
||||||
|
m_stdin->close();
|
||||||
|
|
||||||
|
// It's not really a problem if this signal failed
|
||||||
|
if (sigaction(SIGPIPE, &old_action_handler, nullptr) < 0)
|
||||||
|
perror("sigaction");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<Command::ProcessOutputs> Command::read_all()
|
||||||
|
{
|
||||||
|
return ProcessOutputs { TRY(m_stdout->read_until_eof()), TRY(m_stderr->read_until_eof()) };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<Command::ProcessResult> Command::status(int options)
|
||||||
|
{
|
||||||
|
if (m_pid == -1)
|
||||||
|
return ProcessResult::Unknown;
|
||||||
|
|
||||||
|
m_stdin->close();
|
||||||
|
|
||||||
|
auto wait_result = TRY(Core::System::waitpid(m_pid, options));
|
||||||
|
if (wait_result.pid == 0) {
|
||||||
|
// Attempt to kill it, since it has not finished yet somehow
|
||||||
|
return ProcessResult::Running;
|
||||||
|
}
|
||||||
|
m_pid = -1;
|
||||||
|
|
||||||
|
if (WIFSIGNALED(wait_result.status) && WTERMSIG(wait_result.status) == SIGALRM)
|
||||||
|
return ProcessResult::FailedFromTimeout;
|
||||||
|
|
||||||
|
if (WIFEXITED(wait_result.status) && WEXITSTATUS(wait_result.status) == 0)
|
||||||
|
return ProcessResult::DoneWithZeroExitCode;
|
||||||
|
|
||||||
|
return ProcessResult::Failed;
|
||||||
|
}
|
||||||
|
|
||||||
// Only supported in serenity mode because we use `posix_spawn_file_actions_addchdir`
|
// Only supported in serenity mode because we use `posix_spawn_file_actions_addchdir`
|
||||||
#ifdef AK_OS_SERENITY
|
#ifdef AK_OS_SERENITY
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
||||||
|
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -10,10 +12,13 @@
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/Forward.h>
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
// FIXME: Unify this and the below 'command' functions with Command class below
|
||||||
struct CommandResult {
|
struct CommandResult {
|
||||||
int exit_code { 0 };
|
int exit_code { 0 };
|
||||||
ByteBuffer output;
|
ByteBuffer output;
|
||||||
|
@ -23,4 +28,38 @@ struct CommandResult {
|
||||||
ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir);
|
ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir);
|
||||||
ErrorOr<CommandResult> command(DeprecatedString const& command_string, Optional<LexicalPath> chdir);
|
ErrorOr<CommandResult> command(DeprecatedString const& command_string, Optional<LexicalPath> chdir);
|
||||||
|
|
||||||
|
class Command {
|
||||||
|
public:
|
||||||
|
struct ProcessOutputs {
|
||||||
|
ByteBuffer standard_output;
|
||||||
|
ByteBuffer standard_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
static ErrorOr<OwnPtr<Command>> create(StringView command, char const* const arguments[]);
|
||||||
|
|
||||||
|
Command(pid_t pid, NonnullOwnPtr<Core::File> stdin_file, NonnullOwnPtr<Core::File> stdout_file, NonnullOwnPtr<Core::File> stderr_file);
|
||||||
|
|
||||||
|
ErrorOr<void> write(StringView input);
|
||||||
|
|
||||||
|
bool write_lines(Span<DeprecatedString> lines);
|
||||||
|
|
||||||
|
ErrorOr<ProcessOutputs> read_all();
|
||||||
|
|
||||||
|
enum class ProcessResult {
|
||||||
|
Running,
|
||||||
|
DoneWithZeroExitCode,
|
||||||
|
Failed,
|
||||||
|
FailedFromTimeout,
|
||||||
|
Unknown,
|
||||||
|
};
|
||||||
|
|
||||||
|
ErrorOr<ProcessResult> status(int options = 0);
|
||||||
|
|
||||||
|
private:
|
||||||
|
pid_t m_pid { -1 };
|
||||||
|
NonnullOwnPtr<Core::File> m_stdin;
|
||||||
|
NonnullOwnPtr<Core::File> m_stdout;
|
||||||
|
NonnullOwnPtr<Core::File> m_stderr;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue