HelperProcess.cpp 6.3 KB

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