From aa4dcca0b21af01008bca943d1498fa75fdc0f1f Mon Sep 17 00:00:00 2001 From: rmg-x Date: Wed, 27 Nov 2024 15:43:12 -0600 Subject: [PATCH] LibCore/Process: Add `DupFd` file action This commit provides a new file action allowing callers to provide a `write_fd` that Process will "redirect" the child's stdin/stdout to. --- Libraries/LibCore/Process.cpp | 4 ++++ Libraries/LibCore/Process.h | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Libraries/LibCore/Process.cpp b/Libraries/LibCore/Process.cpp index 4305a97cfac..588515f53dd 100644 --- a/Libraries/LibCore/Process.cpp +++ b/Libraries/LibCore/Process.cpp @@ -121,6 +121,10 @@ ErrorOr Process::spawn(ProcessSpawnOptions const& options) [&](FileAction::CloseFile const& action) -> ErrorOr { CHECK(posix_spawn_file_actions_addclose(&spawn_actions, action.fd)); return {}; + }, + [&](FileAction::DupFd const& action) -> ErrorOr { + CHECK(posix_spawn_file_actions_adddup2(&spawn_actions, action.write_fd, action.fd)); + return {}; })); } diff --git a/Libraries/LibCore/Process.h b/Libraries/LibCore/Process.h index 24145022b23..7ae9b571801 100644 --- a/Libraries/LibCore/Process.h +++ b/Libraries/LibCore/Process.h @@ -11,7 +11,6 @@ #include #include -#include #include namespace Core { @@ -29,7 +28,10 @@ struct CloseFile { int fd { -1 }; }; -// FIXME: Implement other file actions +struct DupFd { + int write_fd { -1 }; + int fd { -1 }; +}; } @@ -40,7 +42,7 @@ struct ProcessSpawnOptions { Vector const& arguments {}; Optional working_directory {}; - using FileActionType = Variant; + using FileActionType = Variant; Vector file_actions {}; };