HelperProcess.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (register_with_process_manager == RegisterWithProcessManager::Yes)
  44. WebView::ProcessManager::the().add_process(WebView::process_type_from_name(server_name), process.process.pid());
  45. if (enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
  46. dbgln();
  47. dbgln("\033[1;45mLaunched {} process under callgrind!\033[0m", server_name);
  48. 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");
  49. dbgln();
  50. }
  51. return move(process.client);
  52. }
  53. if (i == candidate_server_paths.size() - 1) {
  54. warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
  55. return result.release_error();
  56. }
  57. }
  58. VERIFY_NOT_REACHED();
  59. }
  60. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
  61. WebView::ViewImplementation& view,
  62. ReadonlySpan<ByteString> candidate_web_content_paths,
  63. Ladybird::WebContentOptions const& web_content_options,
  64. Optional<IPC::File> request_server_socket)
  65. {
  66. Vector<ByteString> arguments {
  67. "--command-line"sv,
  68. web_content_options.command_line.to_byte_string(),
  69. "--executable-path"sv,
  70. web_content_options.executable_path.to_byte_string(),
  71. };
  72. if (web_content_options.is_layout_test_mode == Ladybird::IsLayoutTestMode::Yes)
  73. arguments.append("--layout-test-mode"sv);
  74. if (web_content_options.use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
  75. arguments.append("--use-lagom-networking"sv);
  76. if (web_content_options.enable_gpu_painting == Ladybird::EnableGPUPainting::Yes)
  77. arguments.append("--use-gpu-painting"sv);
  78. if (web_content_options.enable_skia_painting == Ladybird::EnableSkiaPainting::Yes)
  79. arguments.append("--use-skia-painting"sv);
  80. if (web_content_options.wait_for_debugger == Ladybird::WaitForDebugger::Yes)
  81. arguments.append("--wait-for-debugger"sv);
  82. if (web_content_options.log_all_js_exceptions == Ladybird::LogAllJSExceptions::Yes)
  83. arguments.append("--log-all-js-exceptions"sv);
  84. if (web_content_options.enable_idl_tracing == Ladybird::EnableIDLTracing::Yes)
  85. arguments.append("--enable-idl-tracing"sv);
  86. if (web_content_options.expose_internals_object == Ladybird::ExposeInternalsObject::Yes)
  87. arguments.append("--expose-internals-object"sv);
  88. if (auto server = mach_server_name(); server.has_value()) {
  89. arguments.append("--mach-server-name"sv);
  90. arguments.append(server.value());
  91. }
  92. if (request_server_socket.has_value()) {
  93. arguments.append("--request-server-socket"sv);
  94. arguments.append(ByteString::number(request_server_socket->fd()));
  95. }
  96. return launch_server_process<WebView::WebContentClient>("WebContent"sv, candidate_web_content_paths, move(arguments), RegisterWithProcessManager::No, web_content_options.enable_callgrind_profiling, view);
  97. }
  98. ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths)
  99. {
  100. return launch_server_process<ImageDecoderClient::Client>("ImageDecoder"sv, candidate_image_decoder_paths, {}, RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
  101. }
  102. ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client)
  103. {
  104. auto socket = TRY(connect_new_request_server_client(move(request_client)));
  105. Vector<ByteString> arguments {
  106. "--request-server-socket"sv,
  107. ByteString::number(socket.fd()),
  108. };
  109. return launch_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
  110. }
  111. ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates)
  112. {
  113. Vector<ByteString> arguments;
  114. if (!serenity_resource_root.is_empty()) {
  115. arguments.append("--serenity-resource-root"sv);
  116. arguments.append(serenity_resource_root);
  117. }
  118. for (auto const& certificate : certificates)
  119. arguments.append(ByteString::formatted("--certificate={}", certificate));
  120. if (auto server = mach_server_name(); server.has_value()) {
  121. arguments.append("--mach-server-name"sv);
  122. arguments.append(server.value());
  123. }
  124. return launch_server_process<Protocol::RequestClient>("RequestServer"sv, candidate_request_server_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
  125. }
  126. ErrorOr<IPC::File> connect_new_request_server_client(Protocol::RequestClient& client)
  127. {
  128. auto new_socket = client.send_sync_but_allow_failure<Messages::RequestServer::ConnectNewClient>();
  129. if (!new_socket)
  130. return Error::from_string_literal("Failed to connect to RequestServer");
  131. auto socket = new_socket->take_client_socket();
  132. // FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding.
  133. // Perhaps we should add an option to IPC::File to allow the receiver to decide whether to
  134. // make it O_CLOEXEC or not. Or an attribute in the .ipc file?
  135. auto fd = socket.fd();
  136. auto fd_flags = MUST(Core::System::fcntl(fd, F_GETFD));
  137. fd_flags &= ~FD_CLOEXEC;
  138. MUST(Core::System::fcntl(fd, F_SETFD, fd_flags));
  139. return socket;
  140. }