HelperProcess.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. WebView::UseJavaScriptBytecode use_javascript_bytecode,
  12. Ladybird::UseLagomNetworking use_lagom_networking)
  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_javascript_bytecode == WebView::UseJavaScriptBytecode::Yes)
  46. arguments.append("--use-bytecode"sv);
  47. if (use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
  48. arguments.append("--use-lagom-networking"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. ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<String> candidate_request_server_paths, StringView serenity_resource_root)
  72. {
  73. int socket_fds[2] {};
  74. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  75. int ui_fd = socket_fds[0];
  76. int rc_fd = socket_fds[1];
  77. int fd_passing_socket_fds[2] {};
  78. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  79. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  80. int rc_fd_passing_fd = fd_passing_socket_fds[1];
  81. if (auto child_pid = TRY(Core::System::fork()); child_pid == 0) {
  82. TRY(Core::System::close(ui_fd));
  83. TRY(Core::System::close(ui_fd_passing_fd));
  84. auto takeover_string = TRY(String::formatted("RequestServer:{}", rc_fd));
  85. TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
  86. auto fd_passing_socket_string = TRY(String::number(rc_fd_passing_fd));
  87. ErrorOr<void> result;
  88. for (auto const& path : candidate_request_server_paths) {
  89. if (Core::System::access(path, X_OK).is_error())
  90. continue;
  91. auto arguments = Vector {
  92. path.bytes_as_string_view(),
  93. "--fd-passing-socket"sv,
  94. fd_passing_socket_string,
  95. "--serenity-resource-root"sv,
  96. serenity_resource_root,
  97. };
  98. result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
  99. if (!result.is_error())
  100. break;
  101. }
  102. if (result.is_error())
  103. warnln("Could not launch any of {}: {}", candidate_request_server_paths, result.error());
  104. VERIFY_NOT_REACHED();
  105. }
  106. TRY(Core::System::close(rc_fd));
  107. TRY(Core::System::close(rc_fd_passing_fd));
  108. auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
  109. TRY(socket->set_blocking(true));
  110. auto new_client = TRY(try_make_ref_counted<Protocol::RequestClient>(move(socket)));
  111. new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  112. return new_client;
  113. }