LibCore: Remove now-unused singleton process utilities

This commit is contained in:
Timothy Flynn 2024-06-04 16:54:43 -04:00 committed by Tim Flynn
parent 8362c073f3
commit d61770c457
Notes: sideshowbarker 2024-07-16 23:52:22 +09:00
3 changed files with 8 additions and 98 deletions

View file

@ -15,14 +15,14 @@ enum class RegisterWithProcessManager {
Yes,
};
template<typename ClientType, typename SpawnFunction>
static ErrorOr<NonnullRefPtr<ClientType>> launch_server_process_impl(
template<typename ClientType, typename... ClientArguments>
static ErrorOr<NonnullRefPtr<ClientType>> launch_server_process(
StringView server_name,
ReadonlySpan<ByteString> candidate_server_paths,
Vector<ByteString> arguments,
RegisterWithProcessManager register_with_process_manager,
Ladybird::EnableCallgrindProfiling enable_callgrind_profiling,
SpawnFunction&& spawn_function)
ClientArguments&&... client_arguments)
{
if (enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
arguments.prepend({
@ -43,7 +43,7 @@ static ErrorOr<NonnullRefPtr<ClientType>> launch_server_process_impl(
options.executable = path;
}
auto result = spawn_function(options);
auto result = Core::IPCProcess::spawn<ClientType>(move(options), forward<ClientArguments>(client_arguments)...);
if (!result.is_error()) {
auto process = result.release_value();
@ -70,33 +70,6 @@ static ErrorOr<NonnullRefPtr<ClientType>> launch_server_process_impl(
VERIFY_NOT_REACHED();
}
template<typename ClientType, typename... ClientArguments>
static ErrorOr<NonnullRefPtr<ClientType>> launch_generic_server_process(
StringView server_name,
ReadonlySpan<ByteString> candidate_server_paths,
Vector<ByteString> arguments,
RegisterWithProcessManager register_with_process_manager,
Ladybird::EnableCallgrindProfiling enable_callgrind_profiling,
ClientArguments&&... client_arguments)
{
return launch_server_process_impl<ClientType>(server_name, candidate_server_paths, move(arguments), register_with_process_manager, enable_callgrind_profiling, [&](auto options) {
return Core::IPCProcess::spawn<ClientType>(move(options), forward<ClientArguments>(client_arguments)...);
});
}
template<typename ClientType, typename... ClientArguments>
static ErrorOr<NonnullRefPtr<ClientType>> launch_singleton_server_process(
StringView server_name,
ReadonlySpan<ByteString> candidate_server_paths,
Vector<ByteString> arguments,
RegisterWithProcessManager register_with_process_manager,
ClientArguments&&... client_arguments)
{
return launch_server_process_impl<ClientType>(server_name, candidate_server_paths, move(arguments), register_with_process_manager, Ladybird::EnableCallgrindProfiling::No, [&](auto options) {
return Core::IPCProcess::spawn_singleton<ClientType>(move(options), forward<ClientArguments>(client_arguments)...);
});
}
ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
WebView::ViewImplementation& view,
ReadonlySpan<ByteString> candidate_web_content_paths,
@ -135,12 +108,12 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
arguments.append(ByteString::number(request_server_socket->fd()));
}
return launch_generic_server_process<WebView::WebContentClient>("WebContent"sv, candidate_web_content_paths, move(arguments), RegisterWithProcessManager::No, web_content_options.enable_callgrind_profiling, view);
return launch_server_process<WebView::WebContentClient>("WebContent"sv, candidate_web_content_paths, move(arguments), RegisterWithProcessManager::No, web_content_options.enable_callgrind_profiling, view);
}
ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths)
{
return launch_generic_server_process<ImageDecoderClient::Client>("ImageDecoder"sv, candidate_image_decoder_paths, {}, RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
return launch_server_process<ImageDecoderClient::Client>("ImageDecoder"sv, candidate_image_decoder_paths, {}, RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
}
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client)
@ -152,7 +125,7 @@ ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(Rea
ByteString::number(socket.fd()),
};
return launch_generic_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
return launch_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
}
ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates)
@ -172,7 +145,7 @@ ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(Re
arguments.append(server.value());
}
return launch_generic_server_process<Protocol::RequestClient>("RequestServer"sv, candidate_request_server_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
return launch_server_process<Protocol::RequestClient>("RequestServer"sv, candidate_request_server_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
}
ErrorOr<IPC::File> connect_new_request_server_client(Protocol::RequestClient& client)

View file

@ -453,57 +453,4 @@ ErrorOr<IPCProcess::ProcessPaths> IPCProcess::paths_for_process(StringView proce
return ProcessPaths { move(socket_path), move(pid_path) };
}
ErrorOr<IPCProcess::ProcessAndIPCSocket> IPCProcess::spawn_singleton_and_connect_to_process(ProcessSpawnOptions const& options)
{
auto [socket_path, pid_path] = TRY(paths_for_process(options.name));
Process process { -1 };
if (auto existing_pid = TRY(get_process_pid(options.name, pid_path)); existing_pid.has_value()) {
process = Process { *existing_pid };
} else {
auto ipc_fd = TRY(create_ipc_socket(socket_path));
sigset_t original_set;
sigset_t setting_set;
sigfillset(&setting_set);
(void)pthread_sigmask(SIG_BLOCK, &setting_set, &original_set);
// FIXME: Roll this daemon implementation into `Process::disown`.
if (auto pid = TRY(System::fork()); pid == 0) {
(void)pthread_sigmask(SIG_SETMASK, &original_set, nullptr);
TRY(System::setsid());
TRY(System::signal(SIGCHLD, SIG_IGN));
auto& arguments = const_cast<Vector<ByteString>&>(options.arguments);
arguments.append("--pid-file"sv);
arguments.append(pid_path);
auto takeover_string = ByteString::formatted("{}:{}", options.name, TRY(System::dup(ipc_fd)));
TRY(Environment::set("SOCKET_TAKEOVER"sv, takeover_string, Environment::Overwrite::Yes));
auto process = TRY(Process::spawn(options));
{
auto pid_file = TRY(File::open(pid_path, File::OpenMode::Write));
TRY(pid_file->write_until_depleted(ByteString::number(process.pid())));
}
TRY(System::kill(getpid(), SIGTERM));
} else {
auto wait_err = System::waitpid(pid);
(void)pthread_sigmask(SIG_SETMASK, &original_set, nullptr);
TRY(wait_err);
}
auto pid = TRY(get_process_pid(options.name, pid_path));
VERIFY(pid.has_value());
process = Process { *pid };
}
auto ipc_socket = TRY(LocalSocket::connect(socket_path));
TRY(ipc_socket->set_blocking(true));
return ProcessAndIPCSocket { move(process), move(ipc_socket) };
}
}

View file

@ -129,15 +129,6 @@ public:
return ProcessAndIPCClient<ClientType> { move(process), move(client) };
}
template<typename ClientType, typename... ClientArguments>
static ErrorOr<ProcessAndIPCClient<ClientType>> spawn_singleton(ProcessSpawnOptions const& options, ClientArguments&&... client_arguments)
{
auto [process, socket] = TRY(spawn_singleton_and_connect_to_process(options));
auto client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ClientType { move(socket), forward<ClientArguments>(client_arguments)... }));
return ProcessAndIPCClient<ClientType> { move(process), move(client) };
}
struct ProcessPaths {
ByteString socket_path;
ByteString pid_path;
@ -154,7 +145,6 @@ private:
NonnullOwnPtr<Core::LocalSocket> m_ipc_socket;
};
static ErrorOr<ProcessAndIPCSocket> spawn_and_connect_to_process(ProcessSpawnOptions const& options);
static ErrorOr<ProcessAndIPCSocket> spawn_singleton_and_connect_to_process(ProcessSpawnOptions const& options);
Process m_process;
};