|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <LibCore/Directory.h> |
| 8 | +#include <LibCore/Environment.h> |
| 9 | +#include <LibCore/SingletonProcess.h> |
| 10 | +#include <LibCore/SocketAddress.h> |
| 11 | +#include <LibCore/StandardPaths.h> |
| 12 | +#include <LibCore/System.h> |
| 13 | +#include <signal.h> |
| 14 | + |
| 15 | +namespace Core::Detail { |
| 16 | + |
| 17 | +static ErrorOr<bool> should_launch_process(StringView process_name, ByteString const& pid_path) |
| 18 | +{ |
| 19 | + if (Core::System::stat(pid_path).is_error()) |
| 20 | + return true; |
| 21 | + |
| 22 | + Optional<pid_t> pid; |
| 23 | + { |
| 24 | + auto pid_file = Core::File::open(pid_path, Core::File::OpenMode::Read); |
| 25 | + if (pid_file.is_error()) { |
| 26 | + warnln("Could not open {} PID file '{}': {}", process_name, pid_path, pid_file.error()); |
| 27 | + return pid_file.release_error(); |
| 28 | + } |
| 29 | + |
| 30 | + auto contents = pid_file.value()->read_until_eof(); |
| 31 | + if (contents.is_error()) { |
| 32 | + warnln("Could not read {} PID file '{}': {}", process_name, pid_path, contents.error()); |
| 33 | + return contents.release_error(); |
| 34 | + } |
| 35 | + |
| 36 | + pid = StringView { contents.value() }.to_number<pid_t>(); |
| 37 | + } |
| 38 | + |
| 39 | + if (!pid.has_value()) { |
| 40 | + warnln("{} PID file '{}' exists, but with an invalid PID", process_name, pid_path); |
| 41 | + TRY(Core::System::unlink(pid_path)); |
| 42 | + return true; |
| 43 | + } |
| 44 | + if (kill(*pid, 0) < 0) { |
| 45 | + warnln("{} PID file '{}' exists with PID {}, but process cannot be found", process_name, pid_path, *pid); |
| 46 | + TRY(Core::System::unlink(pid_path)); |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +// This is heavily based on how SystemServer's Service creates its socket. |
| 54 | +static ErrorOr<int> create_ipc_socket(ByteString const& socket_path) |
| 55 | +{ |
| 56 | + if (!Core::System::stat(socket_path).is_error()) |
| 57 | + TRY(Core::System::unlink(socket_path)); |
| 58 | + |
| 59 | +#ifdef SOCK_NONBLOCK |
| 60 | + auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); |
| 61 | +#else |
| 62 | + auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM, 0)); |
| 63 | + |
| 64 | + int option = 1; |
| 65 | + TRY(Core::System::ioctl(socket_fd, FIONBIO, &option)); |
| 66 | + TRY(Core::System::fcntl(socket_fd, F_SETFD, FD_CLOEXEC)); |
| 67 | +#endif |
| 68 | + |
| 69 | +#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_GNU_HURD) |
| 70 | + TRY(Core::System::fchmod(socket_fd, 0600)); |
| 71 | +#endif |
| 72 | + |
| 73 | + auto socket_address = Core::SocketAddress::local(socket_path); |
| 74 | + auto socket_address_un = socket_address.to_sockaddr_un().release_value(); |
| 75 | + |
| 76 | + TRY(Core::System::bind(socket_fd, reinterpret_cast<sockaddr*>(&socket_address_un), sizeof(socket_address_un))); |
| 77 | + TRY(Core::System::listen(socket_fd, 16)); |
| 78 | + |
| 79 | + return socket_fd; |
| 80 | +} |
| 81 | + |
| 82 | +static ErrorOr<void> launch_process(StringView process_name, ByteString const& socket_path, ByteString const& pid_path, ReadonlySpan<ByteString> candidate_process_paths, ReadonlySpan<ByteString> command_line_arguments) |
| 83 | +{ |
| 84 | + auto ipc_fd_or_error = create_ipc_socket(socket_path); |
| 85 | + if (ipc_fd_or_error.is_error()) { |
| 86 | + warnln("Failed to create an IPC socket for {} at {}: {}", process_name, socket_path, ipc_fd_or_error.error()); |
| 87 | + return ipc_fd_or_error.release_error(); |
| 88 | + } |
| 89 | + |
| 90 | + sigset_t original_set; |
| 91 | + sigset_t setting_set; |
| 92 | + sigfillset(&setting_set); |
| 93 | + (void)pthread_sigmask(SIG_BLOCK, &setting_set, &original_set); |
| 94 | + |
| 95 | + auto ipc_fd = ipc_fd_or_error.value(); |
| 96 | + auto pid = TRY(Core::System::fork()); |
| 97 | + |
| 98 | + if (pid == 0) { |
| 99 | + (void)pthread_sigmask(SIG_SETMASK, &original_set, nullptr); |
| 100 | + TRY(Core::System::setsid()); |
| 101 | + TRY(Core::System::signal(SIGCHLD, SIG_IGN)); |
| 102 | + |
| 103 | + pid = TRY(Core::System::fork()); |
| 104 | + |
| 105 | + if (pid != 0) { |
| 106 | + auto pid_file = TRY(Core::File::open(pid_path, Core::File::OpenMode::Write)); |
| 107 | + TRY(pid_file->write_until_depleted(ByteString::number(pid))); |
| 108 | + |
| 109 | + TRY(Core::System::kill(getpid(), SIGTERM)); |
| 110 | + } |
| 111 | + |
| 112 | + ipc_fd = TRY(Core::System::dup(ipc_fd)); |
| 113 | + |
| 114 | + auto takeover_string = ByteString::formatted("{}:{}", process_name, ipc_fd); |
| 115 | + TRY(Core::Environment::set("SOCKET_TAKEOVER"sv, takeover_string, Core::Environment::Overwrite::Yes)); |
| 116 | + |
| 117 | + ErrorOr<void> result; |
| 118 | + |
| 119 | + Vector<StringView> arguments { |
| 120 | + ""sv, // placeholder for the candidate path |
| 121 | + "--pid-file"sv, |
| 122 | + pid_path, |
| 123 | + }; |
| 124 | + |
| 125 | + for (auto const& argument : command_line_arguments) |
| 126 | + arguments.append(argument); |
| 127 | + |
| 128 | + for (auto const& process_path : candidate_process_paths) { |
| 129 | + arguments[0] = process_path; |
| 130 | + |
| 131 | + result = Core::System::exec(arguments[0], arguments, Core::System::SearchInPath::Yes); |
| 132 | + if (!result.is_error()) |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + if (result.is_error()) { |
| 137 | + warnln("Could not launch any of {}: {}", candidate_process_paths, result.error()); |
| 138 | + TRY(Core::System::unlink(pid_path)); |
| 139 | + } |
| 140 | + |
| 141 | + VERIFY_NOT_REACHED(); |
| 142 | + } |
| 143 | + |
| 144 | + VERIFY(pid > 0); |
| 145 | + |
| 146 | + auto wait_err = Core::System::waitpid(pid); |
| 147 | + (void)pthread_sigmask(SIG_SETMASK, &original_set, nullptr); |
| 148 | + TRY(wait_err); |
| 149 | + |
| 150 | + return {}; |
| 151 | +} |
| 152 | + |
| 153 | +struct ProcessPaths { |
| 154 | + ByteString socket_path; |
| 155 | + ByteString pid_path; |
| 156 | +}; |
| 157 | +static ErrorOr<ProcessPaths> paths_for_process(StringView process_name) |
| 158 | +{ |
| 159 | + auto runtime_directory = TRY(Core::StandardPaths::runtime_directory()); |
| 160 | + auto socket_path = ByteString::formatted("{}/{}.socket", runtime_directory, process_name); |
| 161 | + auto pid_path = ByteString::formatted("{}/{}.pid", runtime_directory, process_name); |
| 162 | + |
| 163 | + return ProcessPaths { move(socket_path), move(pid_path) }; |
| 164 | +} |
| 165 | + |
| 166 | +ErrorOr<NonnullOwnPtr<Core::LocalSocket>> launch_and_connect_to_process(StringView process_name, ReadonlySpan<ByteString> candidate_process_paths, ReadonlySpan<ByteString> command_line_arguments) |
| 167 | +{ |
| 168 | + auto [socket_path, pid_path] = TRY(paths_for_process(process_name)); |
| 169 | + |
| 170 | + if (TRY(Detail::should_launch_process(process_name, pid_path))) |
| 171 | + TRY(Detail::launch_process(process_name, socket_path, pid_path, candidate_process_paths, command_line_arguments)); |
| 172 | + |
| 173 | + auto socket = TRY(Core::LocalSocket::connect(socket_path)); |
| 174 | + TRY(socket->set_blocking(true)); |
| 175 | + |
| 176 | + return socket; |
| 177 | +} |
| 178 | + |
| 179 | +} |
0 commit comments