Ladybird: Use Core::Process facilities to spawn helper processes

Note that WebContent now also uses launch_generic_server_process()
instead of its own implementation.
This commit is contained in:
Timothy Flynn 2024-04-23 16:39:04 -04:00 committed by Andrew Kaster
parent 9986350e97
commit 9fb84e4693
Notes: sideshowbarker 2024-07-17 00:57:24 +09:00

View file

@ -6,89 +6,104 @@
#include "HelperProcess.h" #include "HelperProcess.h"
#include "Utilities.h" #include "Utilities.h"
#include <LibCore/Environment.h> #include <AK/Enumerate.h>
#include <LibCore/Process.h>
#include <LibCore/SingletonProcess.h> #include <LibCore/SingletonProcess.h>
#include <LibWebView/ProcessManager.h> #include <LibWebView/ProcessManager.h>
enum class RegisterWithProcessManager {
No,
Yes,
};
template<typename ClientType, typename SpawnFunction>
static ErrorOr<NonnullRefPtr<ClientType>> launch_server_process_impl(
StringView server_name,
ReadonlySpan<ByteString> candidate_server_paths,
Vector<ByteString> const& arguments,
RegisterWithProcessManager register_with_process_manager,
SpawnFunction&& spawn_function)
{
for (auto [i, path] : enumerate(candidate_server_paths)) {
auto result = spawn_function(Core::ProcessSpawnOptions {
.name = server_name,
.executable = path,
.arguments = arguments,
});
if (!result.is_error()) {
auto process = result.release_value();
if (register_with_process_manager == RegisterWithProcessManager::Yes)
WebView::ProcessManager::the().add_process(WebView::process_type_from_name(server_name), process.process.pid());
return move(process.client);
}
if (i == candidate_server_paths.size() - 1) {
warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
return result.release_error();
}
}
VERIFY_NOT_REACHED();
}
template<typename ClientType, typename... ClientArguments>
static ErrorOr<NonnullRefPtr<ClientType>> launch_generic_server_process(
StringView server_name,
ReadonlySpan<ByteString> candidate_server_paths,
Vector<ByteString> const& arguments,
RegisterWithProcessManager register_with_process_manager,
ClientArguments&&... client_arguments)
{
return launch_server_process_impl<ClientType>(server_name, candidate_server_paths, arguments, register_with_process_manager, [&](auto options) {
return Core::IPCProcess::spawn<ClientType>(move(options), forward<ClientArguments>(client_arguments)...);
});
}
ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process( ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
WebView::ViewImplementation& view, WebView::ViewImplementation& view,
ReadonlySpan<ByteString> candidate_web_content_paths, ReadonlySpan<ByteString> candidate_web_content_paths,
Ladybird::WebContentOptions const& web_content_options, Ladybird::WebContentOptions const& web_content_options,
Optional<IPC::File> request_server_socket) Optional<IPC::File> request_server_socket)
{ {
int socket_fds[2] {}; Vector<ByteString> arguments {
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds)); "--command-line"sv,
web_content_options.command_line.to_byte_string(),
"--executable-path"sv,
web_content_options.executable_path.to_byte_string(),
};
int ui_fd = socket_fds[0]; if (web_content_options.enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
int wc_fd = socket_fds[1]; arguments.prepend("--instr-atstart=no"sv);
arguments.prepend("--tool=callgrind"sv);
auto child_pid = TRY(Core::System::fork()); arguments.prepend("valgrind"sv);
if (child_pid == 0) { }
TRY(Core::System::close(ui_fd)); if (web_content_options.is_layout_test_mode == Ladybird::IsLayoutTestMode::Yes)
arguments.append("--layout-test-mode"sv);
auto takeover_string = TRY(String::formatted("WebContent:{}", wc_fd)); if (web_content_options.use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
TRY(Core::Environment::set("SOCKET_TAKEOVER"sv, takeover_string, Core::Environment::Overwrite::Yes)); arguments.append("--use-lagom-networking"sv);
if (web_content_options.enable_gpu_painting == Ladybird::EnableGPUPainting::Yes)
ErrorOr<void> result; arguments.append("--use-gpu-painting"sv);
for (auto const& path : candidate_web_content_paths) { if (web_content_options.wait_for_debugger == Ladybird::WaitForDebugger::Yes)
constexpr auto callgrind_prefix_length = 3; arguments.append("--wait-for-debugger"sv);
if (web_content_options.log_all_js_exceptions == Ladybird::LogAllJSExceptions::Yes)
if (Core::System::access(path, X_OK).is_error()) arguments.append("--log-all-js-exceptions"sv);
continue; if (web_content_options.enable_idl_tracing == Ladybird::EnableIDLTracing::Yes)
arguments.append("--enable-idl-tracing"sv);
auto arguments = Vector { if (web_content_options.expose_internals_object == Ladybird::ExposeInternalsObject::Yes)
"valgrind"sv, arguments.append("--expose-internals-object"sv);
"--tool=callgrind"sv, if (auto server = mach_server_name(); server.has_value()) {
"--instr-atstart=no"sv, arguments.append("--mach-server-name"sv);
path.view(), arguments.append(server.value());
"--command-line"sv, }
web_content_options.command_line, if (request_server_socket.has_value()) {
"--executable-path"sv, arguments.append("--request-server-socket"sv);
web_content_options.executable_path, arguments.append(ByteString::number(request_server_socket->fd()));
};
if (web_content_options.enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::No)
arguments.remove(0, callgrind_prefix_length);
if (web_content_options.is_layout_test_mode == Ladybird::IsLayoutTestMode::Yes)
arguments.append("--layout-test-mode"sv);
if (web_content_options.use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
arguments.append("--use-lagom-networking"sv);
if (web_content_options.enable_gpu_painting == Ladybird::EnableGPUPainting::Yes)
arguments.append("--use-gpu-painting"sv);
if (web_content_options.wait_for_debugger == Ladybird::WaitForDebugger::Yes)
arguments.append("--wait-for-debugger"sv);
if (web_content_options.log_all_js_exceptions == Ladybird::LogAllJSExceptions::Yes)
arguments.append("--log-all-js-exceptions"sv);
if (web_content_options.enable_idl_tracing == Ladybird::EnableIDLTracing::Yes)
arguments.append("--enable-idl-tracing"sv);
if (web_content_options.expose_internals_object == Ladybird::ExposeInternalsObject::Yes)
arguments.append("--expose-internals-object"sv);
if (auto server = mach_server_name(); server.has_value()) {
arguments.append("--mach-server-name"sv);
arguments.append(server.value());
}
String fd_string;
if (request_server_socket.has_value()) {
arguments.append("--request-server-socket"sv);
fd_string = MUST(String::number(request_server_socket->fd()));
arguments.append(fd_string.bytes_as_string_view());
}
result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
if (!result.is_error())
break;
}
if (result.is_error())
warnln("Could not launch any of {}: {}", candidate_web_content_paths, result.error());
VERIFY_NOT_REACHED();
} }
TRY(Core::System::close(wc_fd)); auto web_content_client = TRY(launch_generic_server_process<WebView::WebContentClient>("WebContent"sv, candidate_web_content_paths, arguments, RegisterWithProcessManager::No, view));
auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
TRY(socket->set_blocking(true));
auto new_client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(move(socket), view)));
if (web_content_options.enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) { if (web_content_options.enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
dbgln(); dbgln();
@ -97,98 +112,44 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
dbgln(); dbgln();
} }
return new_client; return web_content_client;
}
template<typename Client>
ErrorOr<NonnullRefPtr<Client>> launch_generic_server_process(ReadonlySpan<ByteString> candidate_server_paths, StringView server_name, Vector<StringView> extra_arguments = {})
{
int socket_fds[2] {};
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
int ui_fd = socket_fds[0];
int server_fd = socket_fds[1];
auto child_pid = TRY(Core::System::fork());
if (child_pid == 0) {
TRY(Core::System::close(ui_fd));
auto takeover_string = TRY(String::formatted("{}:{}", server_name, server_fd));
TRY(Core::Environment::set("SOCKET_TAKEOVER"sv, takeover_string, Core::Environment::Overwrite::Yes));
ErrorOr<void> result;
for (auto const& path : candidate_server_paths) {
if (Core::System::access(path, X_OK).is_error())
continue;
auto arguments = Vector<StringView> {
path.view(),
};
if (!extra_arguments.is_empty())
arguments.extend(extra_arguments);
result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
if (!result.is_error())
break;
}
if (result.is_error())
warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
VERIFY_NOT_REACHED();
}
TRY(Core::System::close(server_fd));
auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
TRY(socket->set_blocking(true));
auto new_client = TRY(try_make_ref_counted<Client>(move(socket)));
WebView::ProcessManager::the().add_process(WebView::process_type_from_name(server_name), child_pid);
return new_client;
} }
ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths) ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths)
{ {
return launch_generic_server_process<ImageDecoderClient::Client>(candidate_image_decoder_paths, "ImageDecoder"sv); return launch_generic_server_process<ImageDecoderClient::Client>("ImageDecoder"sv, candidate_image_decoder_paths, {}, RegisterWithProcessManager::Yes);
} }
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client) ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client)
{ {
auto socket = TRY(connect_new_request_server_client(move(request_client))); auto socket = TRY(connect_new_request_server_client(move(request_client)));
Vector<StringView> arguments; Vector<ByteString> arguments {
String fd_string = MUST(String::number(socket.fd())); "--request-server-socket"sv,
ByteString::number(socket.fd()),
};
arguments.append("--request-server-socket"sv); return launch_generic_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, arguments, RegisterWithProcessManager::Yes);
arguments.append(fd_string.bytes_as_string_view());
return launch_generic_server_process<Web::HTML::WebWorkerClient>(candidate_web_worker_paths, "WebWorker"sv, move(arguments));
} }
ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates) ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates)
{ {
Vector<StringView> arguments; Vector<ByteString> arguments;
if (!serenity_resource_root.is_empty()) { if (!serenity_resource_root.is_empty()) {
arguments.append("--serenity-resource-root"sv); arguments.append("--serenity-resource-root"sv);
arguments.append(serenity_resource_root); arguments.append(serenity_resource_root);
} }
Vector<ByteString> certificate_args; for (auto const& certificate : certificates)
for (auto const& certificate : certificates) { arguments.append(ByteString::formatted("--certificate={}", certificate));
certificate_args.append(ByteString::formatted("--certificate={}", certificate));
arguments.append(certificate_args.last().view());
}
if (auto server = mach_server_name(); server.has_value()) { if (auto server = mach_server_name(); server.has_value()) {
arguments.append("--mach-server-name"sv); arguments.append("--mach-server-name"sv);
arguments.append(server.value()); arguments.append(server.value());
} }
return launch_generic_server_process<Protocol::RequestClient>(candidate_request_server_paths, "RequestServer"sv, move(arguments)); return launch_generic_server_process<Protocol::RequestClient>("RequestServer"sv, candidate_request_server_paths, arguments, RegisterWithProcessManager::Yes);
} }
ErrorOr<NonnullRefPtr<SQL::SQLClient>> launch_sql_server_process(ReadonlySpan<ByteString> candidate_sql_server_paths) ErrorOr<NonnullRefPtr<SQL::SQLClient>> launch_sql_server_process(ReadonlySpan<ByteString> candidate_sql_server_paths)