HelperProcess.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HelperProcess.h"
  7. #include "Utilities.h"
  8. #include <AK/Enumerate.h>
  9. #include <LibCore/Process.h>
  10. #include <LibWebView/ProcessManager.h>
  11. enum class RegisterWithProcessManager {
  12. No,
  13. Yes,
  14. };
  15. template<typename ClientType, typename... ClientArguments>
  16. static ErrorOr<NonnullRefPtr<ClientType>> launch_server_process(
  17. StringView server_name,
  18. ReadonlySpan<ByteString> candidate_server_paths,
  19. Vector<ByteString> arguments,
  20. RegisterWithProcessManager register_with_process_manager,
  21. Ladybird::EnableCallgrindProfiling enable_callgrind_profiling,
  22. ClientArguments&&... client_arguments)
  23. {
  24. if (enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
  25. arguments.prepend({
  26. "--tool=callgrind"sv,
  27. "--instr-atstart=no"sv,
  28. ""sv, // Placeholder for the process path.
  29. });
  30. }
  31. for (auto [i, path] : enumerate(candidate_server_paths)) {
  32. Core::ProcessSpawnOptions options { .name = server_name, .arguments = arguments };
  33. if (enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
  34. options.executable = "valgrind"sv;
  35. options.search_for_executable_in_path = true;
  36. arguments[2] = path;
  37. } else {
  38. options.executable = path;
  39. }
  40. auto result = Core::IPCProcess::spawn<ClientType>(move(options), forward<ClientArguments>(client_arguments)...);
  41. if (!result.is_error()) {
  42. auto process = result.release_value();
  43. if constexpr (requires { process.client->set_pid(pid_t {}); })
  44. process.client->set_pid(process.process.pid());
  45. if (register_with_process_manager == RegisterWithProcessManager::Yes)
  46. WebView::ProcessManager::the().add_process(WebView::process_type_from_name(server_name), process.process.pid());
  47. if (enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
  48. dbgln();
  49. dbgln("\033[1;45mLaunched {} process under callgrind!\033[0m", server_name);
  50. dbgln("\033[100mRun `\033[4mcallgrind_control -i on\033[24m` to start instrumentation and `\033[4mcallgrind_control -i off\033[24m` stop it again.\033[0m");
  51. dbgln();
  52. }
  53. return move(process.client);
  54. }
  55. if (i == candidate_server_paths.size() - 1) {
  56. warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
  57. return result.release_error();
  58. }
  59. }
  60. VERIFY_NOT_REACHED();
  61. }
  62. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
  63. WebView::ViewImplementation& view,
  64. ReadonlySpan<ByteString> candidate_web_content_paths,
  65. Ladybird::WebContentOptions const& web_content_options,
  66. Optional<IPC::File> request_server_socket)
  67. {
  68. Vector<ByteString> arguments {
  69. "--command-line"sv,
  70. web_content_options.command_line.to_byte_string(),
  71. "--executable-path"sv,
  72. web_content_options.executable_path.to_byte_string(),
  73. };
  74. if (web_content_options.is_layout_test_mode == Ladybird::IsLayoutTestMode::Yes)
  75. arguments.append("--layout-test-mode"sv);
  76. if (web_content_options.use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
  77. arguments.append("--use-lagom-networking"sv);
  78. if (web_content_options.enable_gpu_painting == Ladybird::EnableGPUPainting::Yes)
  79. arguments.append("--use-gpu-painting"sv);
  80. if (web_content_options.enable_skia_painting == Ladybird::EnableSkiaPainting::Yes)
  81. arguments.append("--use-skia-painting"sv);
  82. if (web_content_options.wait_for_debugger == Ladybird::WaitForDebugger::Yes)
  83. arguments.append("--wait-for-debugger"sv);
  84. if (web_content_options.log_all_js_exceptions == Ladybird::LogAllJSExceptions::Yes)
  85. arguments.append("--log-all-js-exceptions"sv);
  86. if (web_content_options.enable_idl_tracing == Ladybird::EnableIDLTracing::Yes)
  87. arguments.append("--enable-idl-tracing"sv);
  88. if (web_content_options.enable_http_cache == Ladybird::EnableHTTPCache::Yes)
  89. arguments.append("--enable-http-cache"sv);
  90. if (web_content_options.expose_internals_object == Ladybird::ExposeInternalsObject::Yes)
  91. arguments.append("--expose-internals-object"sv);
  92. if (auto server = mach_server_name(); server.has_value()) {
  93. arguments.append("--mach-server-name"sv);
  94. arguments.append(server.value());
  95. }
  96. if (request_server_socket.has_value()) {
  97. arguments.append("--request-server-socket"sv);
  98. arguments.append(ByteString::number(request_server_socket->fd()));
  99. }
  100. return launch_server_process<WebView::WebContentClient>("WebContent"sv, candidate_web_content_paths, move(arguments), RegisterWithProcessManager::No, web_content_options.enable_callgrind_profiling, view);
  101. }
  102. ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths)
  103. {
  104. return launch_server_process<ImageDecoderClient::Client>("ImageDecoder"sv, candidate_image_decoder_paths, {}, RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
  105. }
  106. ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client)
  107. {
  108. auto socket = TRY(connect_new_request_server_client(move(request_client)));
  109. Vector<ByteString> arguments {
  110. "--request-server-socket"sv,
  111. ByteString::number(socket.fd()),
  112. };
  113. return launch_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
  114. }
  115. ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates)
  116. {
  117. Vector<ByteString> arguments;
  118. if (!serenity_resource_root.is_empty()) {
  119. arguments.append("--serenity-resource-root"sv);
  120. arguments.append(serenity_resource_root);
  121. }
  122. for (auto const& certificate : certificates)
  123. arguments.append(ByteString::formatted("--certificate={}", certificate));
  124. if (auto server = mach_server_name(); server.has_value()) {
  125. arguments.append("--mach-server-name"sv);
  126. arguments.append(server.value());
  127. }
  128. return launch_server_process<Protocol::RequestClient>("RequestServer"sv, candidate_request_server_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
  129. }
  130. ErrorOr<IPC::File> connect_new_request_server_client(Protocol::RequestClient& client)
  131. {
  132. auto new_socket = client.send_sync_but_allow_failure<Messages::RequestServer::ConnectNewClient>();
  133. if (!new_socket)
  134. return Error::from_string_literal("Failed to connect to RequestServer");
  135. auto socket = new_socket->take_client_socket();
  136. // FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding.
  137. // Perhaps we should add an option to IPC::File to allow the receiver to decide whether to
  138. // make it O_CLOEXEC or not. Or an attribute in the .ipc file?
  139. auto fd = socket.fd();
  140. auto fd_flags = MUST(Core::System::fcntl(fd, F_GETFD));
  141. fd_flags &= ~FD_CLOEXEC;
  142. MUST(Core::System::fcntl(fd, F_SETFD, fd_flags));
  143. return socket;
  144. }