Truly independent web browser
LibCore: Pass a descriptor duplicated onto itself to a macOS child
Shannon Booth79f35dae7e42
diff --git a/Libraries/LibCore/Process.cpp b/Libraries/LibCore/Process.cppindex 151845fcd5b..9be7e41b0be 100644--- a/Libraries/LibCore/Process.cpp+++ b/Libraries/LibCore/Process.cpp@@ -419,6 +419,14 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)return {};},[&](FileAction::DupFd const& action) -> ErrorOr<void> {+#if defined(AK_OS_MACOS)+ // NB: macOS does not clear close-on-exec for a descriptor duplicated onto itself, so it is inherited+ // instead.+ if (action.write_fd == action.fd) {+ CHECK(posix_spawn_file_actions_addinherit_np(&spawn_actions, action.fd));+ return {};+ }+#endifCHECK(posix_spawn_file_actions_adddup2(&spawn_actions, action.write_fd, action.fd));return {};}));diff --git a/Tests/LibCore/TestLibCoreProcess.cpp b/Tests/LibCore/TestLibCoreProcess.cppindex 42c8e232808..85af91dfc45 100644--- a/Tests/LibCore/TestLibCoreProcess.cpp+++ b/Tests/LibCore/TestLibCoreProcess.cpp@@ -80,6 +80,23 @@ TEST_CASE(spawned_process_only_inherits_the_standard_streams_and_the_given_descrfor (auto fd : { unrelated[0], unrelated[1], given[0], given[1], given_child_fd })MUST(Core::System::close(fd));}++TEST_CASE(spawned_process_gets_a_close_on_exec_descriptor_duplicated_onto_itself)+{+ auto given = MUST(Core::System::pipe2(O_CLOEXEC));++ auto script = ByteString::formatted("if {{ true >&{}; }} 2>/dev/null; then echo open; else echo closed; fi", given[1]);+ auto output = run_and_read_output({+ .executable = "/bin/sh"sv,+ .arguments = { "-c"sv, script },+ .file_actions = { Core::FileAction::DupFd { .write_fd = given[1], .fd = given[1] } },+ });++ EXPECT_EQ(output, "open\n"sv);++ for (auto fd : { given[0], given[1] })+ MUST(Core::System::close(fd));+}#endifTEST_CASE(duplicated_descriptor_is_close_on_exec)