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

LibCore+JSSpecCompiler: Add option for Process::spawn() to use spawnp()

Add a boolean to ProcessSpawnOptions, `search_for_executable_in_path`,
which when true, calls into posix_spawnp() instead of posix_spawn().
This defaults to false to maintain the existing behavior.

The `path` field is renamed to `executable` because having two fields
refer to "path" and mean different things seemed unnecessarily
confusing.
This commit is contained in:
Sam Atkins 2024-01-16 13:42:30 +00:00 committed by Andrew Kaster
parent 48983a43b1
commit 071f7fd818
3 changed files with 13 additions and 8 deletions

View file

@ -152,7 +152,7 @@ TEST_CASE(test_regression)
auto path_to_expectation = LexicalPath::join(path_to_tests_directory.string(), ByteString::formatted("{}.expectation", source)); auto path_to_expectation = LexicalPath::join(path_to_tests_directory.string(), ByteString::formatted("{}.expectation", source));
auto process = MUST(Core::Process::spawn({ auto process = MUST(Core::Process::spawn({
.path = path_to_compiler_binary.string(), .executable = path_to_compiler_binary.string(),
.arguments = build_command_line_arguments(path_to_test, test_description), .arguments = build_command_line_arguments(path_to_test, test_description),
.file_actions = { .file_actions = {
Core::FileAction::OpenFile { Core::FileAction::OpenFile {

View file

@ -94,19 +94,23 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)
#undef CHECK #undef CHECK
ArgvList argv_list(options.path, options.arguments.size()); ArgvList argv_list(options.executable, options.arguments.size());
for (auto const& argument : options.arguments) for (auto const& argument : options.arguments)
argv_list.append(argument.characters()); argv_list.append(argument.characters());
auto pid = TRY(System::posix_spawn(options.path.view(), &spawn_actions, nullptr, const_cast<char**>(argv_list.get().data()), System::environment())); pid_t pid;
if (options.search_for_executable_in_path) {
pid = TRY(System::posix_spawnp(options.executable.view(), &spawn_actions, nullptr, const_cast<char**>(argv_list.get().data()), System::environment()));
} else {
pid = TRY(System::posix_spawn(options.executable.view(), &spawn_actions, nullptr, const_cast<char**>(argv_list.get().data()), System::environment()));
}
return Process { pid }; return Process { pid };
} }
ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<ByteString> arguments, ByteString working_directory, KeepAsChild keep_as_child) ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<ByteString> arguments, ByteString working_directory, KeepAsChild keep_as_child)
{ {
auto process = TRY(spawn({ auto process = TRY(spawn({
.path = path, .executable = path,
.arguments = Vector<ByteString> { arguments }, .arguments = Vector<ByteString> { arguments },
.working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory }, .working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory },
})); }));
@ -128,7 +132,7 @@ ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<StringView> argument
backing_strings.append(argument); backing_strings.append(argument);
auto process = TRY(spawn({ auto process = TRY(spawn({
.path = path, .executable = path,
.arguments = backing_strings, .arguments = backing_strings,
.working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory }, .working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory },
})); }));
@ -148,7 +152,7 @@ ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<char const*> argumen
backing_strings.append(argument); backing_strings.append(argument);
auto process = TRY(spawn({ auto process = TRY(spawn({
.path = path, .executable = path,
.arguments = backing_strings, .arguments = backing_strings,
.working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory }, .working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory },
})); }));

View file

@ -29,7 +29,8 @@ struct OpenFile {
} }
struct ProcessSpawnOptions { struct ProcessSpawnOptions {
ByteString path; ByteString executable;
bool search_for_executable_in_path { false };
Vector<ByteString> const& arguments = {}; Vector<ByteString> const& arguments = {};
Optional<ByteString> working_directory = {}; Optional<ByteString> working_directory = {};
Vector<Variant<FileAction::OpenFile>> const& file_actions = {}; Vector<Variant<FileAction::OpenFile>> const& file_actions = {};