LibCore: Make LocalServer::take_over_from_system_server() return ErrorOr

This allows us to use TRY() or MUST() when calling it.
This commit is contained in:
Andreas Kling 2021-12-06 16:27:57 +01:00
parent 229a45ab14
commit 81047d8f9c
13 changed files with 41 additions and 53 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +7,7 @@
#include <LibCore/LocalServer.h>
#include <LibCore/LocalSocket.h>
#include <LibCore/Notifier.h>
#include <LibCore/System.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/socket.h>
@ -30,10 +31,10 @@ LocalServer::~LocalServer()
::close(m_fd);
}
bool LocalServer::take_over_from_system_server(String const& socket_path)
ErrorOr<void> LocalServer::take_over_from_system_server(String const& socket_path)
{
if (m_listening)
return false;
return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening"sv);
if (!LocalSocket::s_overtaken_sockets_parsed)
LocalSocket::parse_sockets_from_system_server();
@ -51,32 +52,27 @@ bool LocalServer::take_over_from_system_server(String const& socket_path)
}
}
if (fd >= 0) {
// Sanity check: it has to be a socket.
struct stat stat;
int rc = fstat(fd, &stat);
if (rc == 0 && S_ISSOCK(stat.st_mode)) {
// The SystemServer has passed us the socket, so use that instead of
// creating our own.
m_fd = fd;
// It had to be !CLOEXEC for obvious reasons, but we
// don't need it to be !CLOEXEC anymore, so set the
// CLOEXEC flag now.
fcntl(m_fd, F_SETFD, FD_CLOEXEC);
if (fd < 0)
return Error::from_string_literal("Core::LocalServer: No file descriptor for socket takeover"sv);
m_listening = true;
setup_notifier();
return true;
} else {
if (rc != 0)
perror("fstat");
dbgln("It's not a socket, what the heck??");
}
}
// Sanity check: it has to be a socket.
auto stat = TRY(Core::System::fstat(fd));
dbgln("Failed to take the socket over from SystemServer");
if (!S_ISSOCK(stat.st_mode))
return Error::from_string_literal("Core::LocalServer: Attempted socket takeover with non-socket file descriptor"sv);
return false;
// It had to be !CLOEXEC for obvious reasons, but we
// don't need it to be !CLOEXEC anymore, so set the
// CLOEXEC flag now.
TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
// The SystemServer has passed us the socket, so use that instead of
// creating our own.
m_fd = fd;
m_listening = true;
setup_notifier();
return {};
}
void LocalServer::setup_notifier()

View file

@ -16,7 +16,7 @@ class LocalServer : public Object {
public:
virtual ~LocalServer() override;
bool take_over_from_system_server(String const& path = String());
ErrorOr<void> take_over_from_system_server(String const& path = String());
bool is_listening() const { return m_listening; }
bool listen(const String& address);

View file

@ -44,9 +44,12 @@ bool MediaQueryList::matches() const
bool MediaQueryList::evaluate()
{
if (!m_document)
return false;
bool now_matches = false;
for (auto& media : m_media) {
now_matches = now_matches || media.evaluate(m_document.window());
now_matches = now_matches || media.evaluate(m_document->window());
}
return now_matches;

View file

@ -54,7 +54,7 @@ public:
private:
MediaQueryList(DOM::Document&, NonnullRefPtrVector<MediaQuery>&&);
DOM::Document& m_document;
WeakPtr<DOM::Document> m_document;
NonnullRefPtrVector<MediaQuery> m_media;
};

View file

@ -23,8 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
Core::EventLoop event_loop;
auto mixer = TRY(AudioServer::Mixer::try_create(config));
auto server = TRY(Core::LocalServer::try_create());
bool ok = server->take_over_from_system_server();
VERIFY(ok);
TRY(server->take_over_from_system_server());
server->on_accept = [&](NonnullRefPtr<Core::LocalSocket> client_socket) {
static int s_next_client_id = 0;

View file

@ -19,8 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::unveil(nullptr, nullptr));
auto server = TRY(Core::LocalServer::try_create());
bool ok = server->take_over_from_system_server();
VERIFY(ok);
TRY(server->take_over_from_system_server());
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;

View file

@ -17,10 +17,9 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::unveil(nullptr, nullptr));
Core::EventLoop event_loop;
auto server = TRY(Core::LocalServer::try_create());
bool ok = server->take_over_from_system_server();
VERIFY(ok);
auto server = TRY(Core::LocalServer::try_create());
TRY(server->take_over_from_system_server());
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;

View file

@ -19,8 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::pledge("stdio unix accept"));
bool ok = server->take_over_from_system_server("/tmp/portal/inspector");
VERIFY(ok);
TRY(server->take_over_from_system_server("/tmp/portal/inspector"));
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
@ -28,8 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
};
auto inspectables_server = TRY(Core::LocalServer::try_create());
if (!inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"))
VERIFY_NOT_REACHED();
TRY(inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"));
inspectables_server->on_accept = [&](auto client_socket) {
auto pid = client_socket->peer_pid();

View file

@ -23,8 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::pledge("stdio accept rpath proc exec"));
bool ok = server->take_over_from_system_server();
VERIFY(ok);
TRY(server->take_over_from_system_server());
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;

View file

@ -78,8 +78,7 @@ LookupServer::LookupServer()
int client_id = ++s_next_client_id;
(void)IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
};
bool ok = m_local_server->take_over_from_system_server();
VERIFY(ok);
MUST(m_local_server->take_over_from_system_server());
}
void LookupServer::load_etc_hosts()

View file

@ -18,8 +18,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app = TRY(GUI::Application::try_create(arguments));
auto server = TRY(Core::LocalServer::try_create());
bool ok = server->take_over_from_system_server();
VERIFY(ok);
TRY(server->take_over_from_system_server());
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;

View file

@ -25,10 +25,9 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::unveil(nullptr, nullptr));
Core::EventLoop event_loop;
auto server = TRY(Core::LocalServer::try_create());
bool ok = server->take_over_from_system_server();
VERIFY(ok);
auto server = TRY(Core::LocalServer::try_create());
TRY(server->take_over_from_system_server());
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;

View file

@ -25,10 +25,8 @@ EventLoop::EventLoop()
m_keyboard_fd = open("/dev/keyboard0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
m_mouse_fd = open("/dev/mouse0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
bool ok = m_window_server->take_over_from_system_server("/tmp/portal/window");
VERIFY(ok);
ok = m_wm_server->take_over_from_system_server("/tmp/portal/wm");
VERIFY(ok);
MUST(m_window_server->take_over_from_system_server("/tmp/portal/window"));
MUST(m_wm_server->take_over_from_system_server("/tmp/portal/wm"));
m_window_server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;