Process.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/Environment.h>
  7. #include <LibCore/Process.h>
  8. #include <LibCore/Socket.h>
  9. #include <LibCore/StandardPaths.h>
  10. #include <LibWebView/Process.h>
  11. namespace WebView {
  12. Process::Process(ProcessType type, RefPtr<IPC::ConnectionBase> connection, Core::Process process)
  13. : m_process(move(process))
  14. , m_type(type)
  15. , m_connection(move(connection))
  16. {
  17. }
  18. Process::~Process()
  19. {
  20. if (m_connection)
  21. m_connection->shutdown();
  22. }
  23. ErrorOr<Process::ProcessAndIPCTransport> Process::spawn_and_connect_to_process(Core::ProcessSpawnOptions const& options)
  24. {
  25. static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
  26. int socket_fds[2] {};
  27. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  28. ArmedScopeGuard guard_fd_0 { [&] { MUST(Core::System::close(socket_fds[0])); } };
  29. ArmedScopeGuard guard_fd_1 { [&] { MUST(Core::System::close(socket_fds[1])); } };
  30. auto& file_actions = const_cast<Core::ProcessSpawnOptions&>(options).file_actions;
  31. file_actions.append(Core::FileAction::CloseFile { socket_fds[0] });
  32. auto takeover_string = MUST(String::formatted("{}:{}", options.name, socket_fds[1]));
  33. TRY(Core::Environment::set("SOCKET_TAKEOVER"sv, takeover_string, Core::Environment::Overwrite::Yes));
  34. auto process = TRY(Core::Process::spawn(options));
  35. auto ipc_socket = TRY(Core::LocalSocket::adopt_fd(socket_fds[0]));
  36. guard_fd_0.disarm();
  37. TRY(ipc_socket->set_blocking(true));
  38. return ProcessAndIPCTransport { move(process), IPC::Transport(move(ipc_socket)) };
  39. }
  40. ErrorOr<Optional<pid_t>> Process::get_process_pid(StringView process_name, StringView pid_path)
  41. {
  42. if (Core::System::stat(pid_path).is_error())
  43. return OptionalNone {};
  44. Optional<pid_t> pid;
  45. {
  46. auto pid_file = Core::File::open(pid_path, Core::File::OpenMode::Read);
  47. if (pid_file.is_error()) {
  48. warnln("Could not open {} PID file '{}': {}", process_name, pid_path, pid_file.error());
  49. return pid_file.release_error();
  50. }
  51. auto contents = pid_file.value()->read_until_eof();
  52. if (contents.is_error()) {
  53. warnln("Could not read {} PID file '{}': {}", process_name, pid_path, contents.error());
  54. return contents.release_error();
  55. }
  56. pid = StringView { contents.value() }.to_number<pid_t>();
  57. }
  58. if (!pid.has_value()) {
  59. warnln("{} PID file '{}' exists, but with an invalid PID", process_name, pid_path);
  60. TRY(Core::System::unlink(pid_path));
  61. return OptionalNone {};
  62. }
  63. if (kill(*pid, 0) < 0) {
  64. warnln("{} PID file '{}' exists with PID {}, but process cannot be found", process_name, pid_path, *pid);
  65. TRY(Core::System::unlink(pid_path));
  66. return OptionalNone {};
  67. }
  68. return pid;
  69. }
  70. // This is heavily based on how SystemServer's Service creates its socket.
  71. ErrorOr<int> Process::create_ipc_socket(ByteString const& socket_path)
  72. {
  73. if (!Core::System::stat(socket_path).is_error())
  74. TRY(Core::System::unlink(socket_path));
  75. #ifdef SOCK_NONBLOCK
  76. auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
  77. #else
  78. auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM, 0));
  79. int option = 1;
  80. TRY(Core::System::ioctl(socket_fd, FIONBIO, &option));
  81. TRY(Core::System::fcntl(socket_fd, F_SETFD, FD_CLOEXEC));
  82. #endif
  83. #if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_GNU_HURD)
  84. TRY(Core::System::fchmod(socket_fd, 0600));
  85. #endif
  86. auto socket_address = Core::SocketAddress::local(socket_path);
  87. auto socket_address_un = socket_address.to_sockaddr_un().release_value();
  88. TRY(Core::System::bind(socket_fd, reinterpret_cast<sockaddr*>(&socket_address_un), sizeof(socket_address_un)));
  89. TRY(Core::System::listen(socket_fd, 16));
  90. return socket_fd;
  91. }
  92. ErrorOr<Process::ProcessPaths> Process::paths_for_process(StringView process_name)
  93. {
  94. auto runtime_directory = TRY(Core::StandardPaths::runtime_directory());
  95. auto socket_path = ByteString::formatted("{}/{}.socket", runtime_directory, process_name);
  96. auto pid_path = ByteString::formatted("{}/{}.pid", runtime_directory, process_name);
  97. return ProcessPaths { move(socket_path), move(pid_path) };
  98. }
  99. }