From dc52404aec863a65455a85a05e4a054a27a5ac0d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 23 Apr 2024 15:31:07 -0400 Subject: [PATCH] LibCore: Add a Core::Process action to close a file after spawning --- Userland/Libraries/LibCore/Process.cpp | 4 ++++ Userland/Libraries/LibCore/Process.h | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibCore/Process.cpp b/Userland/Libraries/LibCore/Process.cpp index 915af3c213d..2c845683800 100644 --- a/Userland/Libraries/LibCore/Process.cpp +++ b/Userland/Libraries/LibCore/Process.cpp @@ -90,6 +90,10 @@ ErrorOr Process::spawn(ProcessSpawnOptions const& options) File::open_mode_to_options(action.mode | Core::File::OpenMode::KeepOnExec), action.permissions)); return {}; + }, + [&](FileAction::CloseFile const& action) -> ErrorOr { + CHECK(posix_spawn_file_actions_addclose(&spawn_actions, action.fd)); + return {}; })); } diff --git a/Userland/Libraries/LibCore/Process.h b/Userland/Libraries/LibCore/Process.h index ba7bef16d68..deca16ac403 100644 --- a/Userland/Libraries/LibCore/Process.h +++ b/Userland/Libraries/LibCore/Process.h @@ -24,6 +24,10 @@ struct OpenFile { mode_t permissions = 0600; }; +struct CloseFile { + int fd { -1 }; +}; + // FIXME: Implement other file actions } @@ -31,9 +35,9 @@ struct OpenFile { struct ProcessSpawnOptions { ByteString executable; bool search_for_executable_in_path { false }; - Vector const& arguments = {}; - Optional working_directory = {}; - Vector> const& file_actions = {}; + Vector const& arguments {}; + Optional working_directory {}; + Vector> const& file_actions {}; }; class Process {