1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:27:43 +00:00

HackStudio: Propagate error from TerminalWrapper

Use the ErrorOr pattern with the Core::System wrappers to propagate more
errors from the TerminalWrapper.

The run_command method, when called with WaitForExit::Yes now returns an
error on command failure.
This commit is contained in:
Lucas CHOLLET 2022-03-02 15:16:53 +01:00 committed by Andreas Kling
parent 5d29f64c99
commit 9a83d34543
4 changed files with 67 additions and 90 deletions

View file

@ -7,7 +7,7 @@
#include "TerminalWrapper.h"
#include <AK/String.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/MessageBox.h>
@ -24,52 +24,41 @@
namespace HackStudio {
void TerminalWrapper::run_command(const String& command, Optional<String> working_directory, WaitForExit wait_for_exit)
ErrorOr<void> TerminalWrapper::run_command(const String& command, Optional<String> working_directory, WaitForExit wait_for_exit, Optional<StringView> failure_message)
{
if (m_pid != -1) {
GUI::MessageBox::show(window(),
"A command is already running in this TerminalWrapper",
"Can't run command",
GUI::MessageBox::Type::Error);
return;
return {};
}
auto ptm_res = setup_master_pseudoterminal();
if (ptm_res.is_error()) {
perror("setup_master_pseudoterminal");
return;
}
auto ptm_fd = TRY(setup_master_pseudoterminal());
int ptm_fd = ptm_res.value();
m_child_exited = false;
m_child_exit_status.clear();
m_pid = fork();
if (m_pid < 0) {
perror("fork");
return;
}
m_pid = TRY(Core::System::fork());
if (m_pid > 0) {
if (wait_for_exit == WaitForExit::Yes) {
GUI::Application::the()->event_loop().spin_until([this]() {
return m_child_exited;
});
VERIFY(m_child_exit_status.has_value());
if (m_child_exit_status.value() != 0)
return Error::from_string_literal(failure_message.value_or("Command execution failed"sv));
}
return;
return {};
}
if (working_directory.has_value()) {
if (chdir(working_directory->characters())) {
perror("chdir");
exit(1);
}
}
if (working_directory.has_value())
TRY(Core::System::chdir(working_directory->view()));
if (setup_slave_pseudoterminal(ptm_fd).is_error()) {
perror("setup_pseudoterminal");
exit(1);
}
TRY(setup_slave_pseudoterminal(ptm_fd));
auto parts = command.split(' ');
VERIFY(!parts.is_empty());
@ -77,6 +66,7 @@ void TerminalWrapper::run_command(const String& command, Optional<String> workin
for (size_t i = 0; i < parts.size(); i++) {
args[i] = parts[i].characters();
}
auto rc = execvp(args[0], const_cast<char**>(args));
if (rc < 0) {
perror("execve");
@ -87,29 +77,28 @@ void TerminalWrapper::run_command(const String& command, Optional<String> workin
ErrorOr<int> TerminalWrapper::setup_master_pseudoterminal(WaitForChildOnExit wait_for_child)
{
int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
if (ptm_fd < 0) {
perror("posix_openpt");
VERIFY_NOT_REACHED();
}
if (grantpt(ptm_fd) < 0) {
perror("grantpt");
VERIFY_NOT_REACHED();
}
if (unlockpt(ptm_fd) < 0) {
perror("unlockpt");
VERIFY_NOT_REACHED();
}
int ptm_fd = TRY(Core::System::posix_openpt(O_RDWR | O_CLOEXEC));
bool error_happened = true;
ScopeGuard close_ptm { [&]() {
if (error_happened) {
if (auto result = Core::System::close(ptm_fd); result.is_error())
warnln("{}", result.release_error());
}
} };
TRY(Core::System::grantpt(ptm_fd));
TRY(Core::System::unlockpt(ptm_fd));
m_terminal_widget->set_pty_master_fd(ptm_fd);
m_terminal_widget->on_command_exit = [this, wait_for_child] {
if (wait_for_child == WaitForChildOnExit::Yes) {
int wstatus;
int rc = waitpid(m_pid, &wstatus, 0);
if (rc < 0) {
perror("waitpid");
auto result = Core::System::waitpid(m_pid, 0);
if (result.is_error()) {
warnln("{}", result.error());
VERIFY_NOT_REACHED();
}
int wstatus = result.release_value().status;
if (WIFEXITED(wstatus)) {
m_terminal_widget->inject_string(String::formatted("\033[{};1m(Command exited with code {})\033[0m\r\n", wstatus == 0 ? 32 : 31, WEXITSTATUS(wstatus)));
@ -130,6 +119,8 @@ ErrorOr<int> TerminalWrapper::setup_master_pseudoterminal(WaitForChildOnExit wai
terminal().scroll_to_bottom();
error_happened = false;
return ptm_fd;
}
@ -137,55 +128,41 @@ ErrorOr<void> TerminalWrapper::setup_slave_pseudoterminal(int master_fd)
{
setsid();
const char* tty_name = ptsname(master_fd);
if (!tty_name)
return Error::from_errno(errno);
auto tty_name = TRY(Core::System::ptsname(master_fd));
close(master_fd);
int pts_fd = open(tty_name, O_RDWR);
if (pts_fd < 0)
return Error::from_errno(errno);
int pts_fd = TRY(Core::System::open(tty_name, O_RDWR));
tcsetpgrp(pts_fd, getpid());
// NOTE: It's okay if this fails.
int rc = ioctl(0, TIOCNOTTY);
ioctl(0, TIOCNOTTY);
close(0);
close(1);
close(2);
rc = dup2(pts_fd, 0);
if (rc < 0)
return Error::from_errno(errno);
TRY(Core::System::dup2(pts_fd, 0));
TRY(Core::System::dup2(pts_fd, 1));
TRY(Core::System::dup2(pts_fd, 2));
rc = dup2(pts_fd, 1);
if (rc < 0)
return Error::from_errno(errno);
TRY(Core::System::close(pts_fd));
rc = dup2(pts_fd, 2);
if (rc < 0)
return Error::from_errno(errno);
rc = close(pts_fd);
if (rc < 0)
return Error::from_errno(errno);
rc = ioctl(0, TIOCSCTTY);
if (rc < 0)
return Error::from_errno(errno);
TRY(Core::System::ioctl(0, TIOCSCTTY));
setenv("TERM", "xterm", true);
return {};
}
void TerminalWrapper::kill_running_command()
ErrorOr<void> TerminalWrapper::kill_running_command()
{
VERIFY(m_pid != -1);
// Kill our child process and its whole process group.
[[maybe_unused]] auto rc = killpg(m_pid, SIGTERM);
TRY(Core::System::killpg(m_pid, SIGTERM));
return {};
}
void TerminalWrapper::clear_including_history()
@ -199,9 +176,11 @@ TerminalWrapper::TerminalWrapper(bool user_spawned)
set_layout<GUI::VerticalBoxLayout>();
m_terminal_widget = add<VT::TerminalWidget>(-1, false);
if (user_spawned)
run_command("Shell");
if (user_spawned) {
auto maybe_error = run_command("Shell");
if (maybe_error.is_error())
warnln("{}", maybe_error.release_error());
}
}
int TerminalWrapper::child_exit_status() const