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.
This commit is contained in:
rmg-x 2024-11-27 15:43:12 -06:00 committed by Andrew Kaster
parent cddbe7d10f
commit aa4dcca0b2
Notes: github-actions[bot] 2024-12-06 00:09:34 +00:00
2 changed files with 9 additions and 3 deletions

View file

@ -121,6 +121,10 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)
[&](FileAction::CloseFile const& action) -> ErrorOr<void> {
CHECK(posix_spawn_file_actions_addclose(&spawn_actions, action.fd));
return {};
},
[&](FileAction::DupFd const& action) -> ErrorOr<void> {
CHECK(posix_spawn_file_actions_adddup2(&spawn_actions, action.write_fd, action.fd));
return {};
}));
}

View file

@ -11,7 +11,6 @@
#include <AK/ByteString.h>
#include <AK/Forward.h>
#include <AK/Span.h>
#include <LibCore/File.h>
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<ByteString> const& arguments {};
Optional<ByteString> working_directory {};
using FileActionType = Variant<FileAction::OpenFile, FileAction::CloseFile>;
using FileActionType = Variant<FileAction::OpenFile, FileAction::CloseFile, FileAction::DupFd>;
Vector<FileActionType> file_actions {};
};