HelperProcess.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HelperProcess.h"
  7. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(WebView::ViewImplementation& view,
  8. ReadonlySpan<String> candidate_web_content_paths,
  9. WebView::EnableCallgrindProfiling enable_callgrind_profiling,
  10. WebView::IsLayoutTestMode is_layout_test_mode,
  11. Ladybird::UseLagomNetworking use_lagom_networking,
  12. WebView::EnableGPUPainting enable_gpu_painting)
  13. {
  14. int socket_fds[2] {};
  15. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  16. int ui_fd = socket_fds[0];
  17. int wc_fd = socket_fds[1];
  18. int fd_passing_socket_fds[2] {};
  19. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  20. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  21. int wc_fd_passing_fd = fd_passing_socket_fds[1];
  22. if (auto child_pid = TRY(Core::System::fork()); child_pid == 0) {
  23. TRY(Core::System::close(ui_fd_passing_fd));
  24. TRY(Core::System::close(ui_fd));
  25. auto takeover_string = TRY(String::formatted("WebContent:{}", wc_fd));
  26. TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
  27. auto webcontent_fd_passing_socket_string = TRY(String::number(wc_fd_passing_fd));
  28. ErrorOr<void> result;
  29. for (auto const& path : candidate_web_content_paths) {
  30. constexpr auto callgrind_prefix_length = 3;
  31. if (Core::System::access(path, X_OK).is_error())
  32. continue;
  33. auto arguments = Vector {
  34. "valgrind"sv,
  35. "--tool=callgrind"sv,
  36. "--instr-atstart=no"sv,
  37. path.bytes_as_string_view(),
  38. "--webcontent-fd-passing-socket"sv,
  39. webcontent_fd_passing_socket_string
  40. };
  41. if (enable_callgrind_profiling == WebView::EnableCallgrindProfiling::No)
  42. arguments.remove(0, callgrind_prefix_length);
  43. if (is_layout_test_mode == WebView::IsLayoutTestMode::Yes)
  44. arguments.append("--layout-test-mode"sv);
  45. if (use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
  46. arguments.append("--use-lagom-networking"sv);
  47. if (enable_gpu_painting == WebView::EnableGPUPainting::Yes)
  48. arguments.append("--use-gpu-painting"sv);
  49. result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
  50. if (!result.is_error())
  51. break;
  52. }
  53. if (result.is_error())
  54. warnln("Could not launch any of {}: {}", candidate_web_content_paths, result.error());
  55. VERIFY_NOT_REACHED();
  56. }
  57. TRY(Core::System::close(wc_fd_passing_fd));
  58. TRY(Core::System::close(wc_fd));
  59. auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
  60. TRY(socket->set_blocking(true));
  61. auto new_client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(move(socket), view)));
  62. new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  63. if (enable_callgrind_profiling == WebView::EnableCallgrindProfiling::Yes) {
  64. dbgln();
  65. dbgln("\033[1;45mLaunched WebContent process under callgrind!\033[0m");
  66. 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");
  67. dbgln();
  68. }
  69. return new_client;
  70. }
  71. template<typename Client>
  72. ErrorOr<NonnullRefPtr<Client>> launch_generic_server_process(ReadonlySpan<String> candidate_server_paths, StringView serenity_resource_root, StringView server_name)
  73. {
  74. int socket_fds[2] {};
  75. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  76. int ui_fd = socket_fds[0];
  77. int server_fd = socket_fds[1];
  78. int fd_passing_socket_fds[2] {};
  79. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  80. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  81. int server_fd_passing_fd = fd_passing_socket_fds[1];
  82. if (auto child_pid = TRY(Core::System::fork()); child_pid == 0) {
  83. TRY(Core::System::close(ui_fd));
  84. TRY(Core::System::close(ui_fd_passing_fd));
  85. auto takeover_string = TRY(String::formatted("{}:{}", server_name, server_fd));
  86. TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
  87. auto fd_passing_socket_string = TRY(String::number(server_fd_passing_fd));
  88. ErrorOr<void> result;
  89. for (auto const& path : candidate_server_paths) {
  90. if (Core::System::access(path, X_OK).is_error())
  91. continue;
  92. auto arguments = Vector<StringView, 5> {
  93. path.bytes_as_string_view(),
  94. "--fd-passing-socket"sv,
  95. fd_passing_socket_string,
  96. "--serenity-resource-root"sv,
  97. serenity_resource_root,
  98. };
  99. result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
  100. if (!result.is_error())
  101. break;
  102. }
  103. if (result.is_error())
  104. warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
  105. VERIFY_NOT_REACHED();
  106. }
  107. TRY(Core::System::close(server_fd));
  108. TRY(Core::System::close(server_fd_passing_fd));
  109. auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
  110. TRY(socket->set_blocking(true));
  111. auto new_client = TRY(try_make_ref_counted<Client>(move(socket)));
  112. new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  113. return new_client;
  114. }
  115. ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<String> candidate_image_decoder_paths)
  116. {
  117. return launch_generic_server_process<ImageDecoderClient::Client>(candidate_image_decoder_paths, ""sv, "ImageDecoder"sv);
  118. }
  119. ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<String> candidate_request_server_paths, StringView serenity_resource_root)
  120. {
  121. return launch_generic_server_process<Protocol::RequestClient>(candidate_request_server_paths, serenity_resource_root, "RequestServer"sv);
  122. }
  123. ErrorOr<NonnullRefPtr<Protocol::WebSocketClient>> launch_web_socket_process(ReadonlySpan<String> candidate_web_socket_paths, StringView serenity_resource_root)
  124. {
  125. return launch_generic_server_process<Protocol::WebSocketClient>(candidate_web_socket_paths, serenity_resource_root, "WebSocket"sv);
  126. }