summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-12-06 16:27:57 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-06 19:22:16 +0100
commit81047d8f9cb092fa3ef83c4e06f1dfd8f65173be (patch)
tree944ba53a9b84332c2b3e32b682ebe6505ac4db3f
parent229a45ab1471e5bdcf532189cdd6a45027ebbfd5 (diff)
downloadserenity-81047d8f9cb092fa3ef83c4e06f1dfd8f65173be.zip
LibCore: Make LocalServer::take_over_from_system_server() return ErrorOr
This allows us to use TRY() or MUST() when calling it.
-rw-r--r--Userland/Libraries/LibCore/LocalServer.cpp50
-rw-r--r--Userland/Libraries/LibCore/LocalServer.h2
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp5
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaQueryList.h2
-rw-r--r--Userland/Services/AudioServer/main.cpp3
-rw-r--r--Userland/Services/Clipboard/main.cpp3
-rw-r--r--Userland/Services/ConfigServer/main.cpp5
-rw-r--r--Userland/Services/InspectorServer/main.cpp6
-rw-r--r--Userland/Services/LaunchServer/main.cpp3
-rw-r--r--Userland/Services/LookupServer/LookupServer.cpp3
-rw-r--r--Userland/Services/NotificationServer/main.cpp3
-rw-r--r--Userland/Services/SQLServer/main.cpp5
-rw-r--r--Userland/Services/WindowServer/EventLoop.cpp6
13 files changed, 42 insertions, 54 deletions
diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp
index d1362b41ef..aaba58baf9 100644
--- a/Userland/Libraries/LibCore/LocalServer.cpp
+++ b/Userland/Libraries/LibCore/LocalServer.cpp
@@ -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);
-
- m_listening = true;
- setup_notifier();
- return true;
- } else {
- if (rc != 0)
- perror("fstat");
- dbgln("It's not a socket, what the heck??");
- }
- }
+ if (fd < 0)
+ return Error::from_string_literal("Core::LocalServer: No file descriptor for socket takeover"sv);
+
+ // 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()
diff --git a/Userland/Libraries/LibCore/LocalServer.h b/Userland/Libraries/LibCore/LocalServer.h
index 7974d3aa28..ba7a56d7d2 100644
--- a/Userland/Libraries/LibCore/LocalServer.h
+++ b/Userland/Libraries/LibCore/LocalServer.h
@@ -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);
diff --git a/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp b/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp
index f354be13dc..2bde03a70e 100644
--- a/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp
+++ b/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp
@@ -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;
diff --git a/Userland/Libraries/LibWeb/CSS/MediaQueryList.h b/Userland/Libraries/LibWeb/CSS/MediaQueryList.h
index ced43fe3d2..0585e759e1 100644
--- a/Userland/Libraries/LibWeb/CSS/MediaQueryList.h
+++ b/Userland/Libraries/LibWeb/CSS/MediaQueryList.h
@@ -54,7 +54,7 @@ public:
private:
MediaQueryList(DOM::Document&, NonnullRefPtrVector<MediaQuery>&&);
- DOM::Document& m_document;
+ WeakPtr<DOM::Document> m_document;
NonnullRefPtrVector<MediaQuery> m_media;
};
diff --git a/Userland/Services/AudioServer/main.cpp b/Userland/Services/AudioServer/main.cpp
index 72593f9d97..3e00d15e8a 100644
--- a/Userland/Services/AudioServer/main.cpp
+++ b/Userland/Services/AudioServer/main.cpp
@@ -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;
diff --git a/Userland/Services/Clipboard/main.cpp b/Userland/Services/Clipboard/main.cpp
index 5e95d376b0..ea4ddc4855 100644
--- a/Userland/Services/Clipboard/main.cpp
+++ b/Userland/Services/Clipboard/main.cpp
@@ -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;
diff --git a/Userland/Services/ConfigServer/main.cpp b/Userland/Services/ConfigServer/main.cpp
index 65bbab3ae1..cde261b8a7 100644
--- a/Userland/Services/ConfigServer/main.cpp
+++ b/Userland/Services/ConfigServer/main.cpp
@@ -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;
diff --git a/Userland/Services/InspectorServer/main.cpp b/Userland/Services/InspectorServer/main.cpp
index 79d7f0fe60..5029ffdc90 100644
--- a/Userland/Services/InspectorServer/main.cpp
+++ b/Userland/Services/InspectorServer/main.cpp
@@ -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();
diff --git a/Userland/Services/LaunchServer/main.cpp b/Userland/Services/LaunchServer/main.cpp
index 3b0d23bfc9..00d6137fb2 100644
--- a/Userland/Services/LaunchServer/main.cpp
+++ b/Userland/Services/LaunchServer/main.cpp
@@ -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;
diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp
index 8d5f02163a..9cc5115117 100644
--- a/Userland/Services/LookupServer/LookupServer.cpp
+++ b/Userland/Services/LookupServer/LookupServer.cpp
@@ -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()
diff --git a/Userland/Services/NotificationServer/main.cpp b/Userland/Services/NotificationServer/main.cpp
index 7a3182166e..ef74de1a2e 100644
--- a/Userland/Services/NotificationServer/main.cpp
+++ b/Userland/Services/NotificationServer/main.cpp
@@ -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;
diff --git a/Userland/Services/SQLServer/main.cpp b/Userland/Services/SQLServer/main.cpp
index cb387f13eb..814d3e342c 100644
--- a/Userland/Services/SQLServer/main.cpp
+++ b/Userland/Services/SQLServer/main.cpp
@@ -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;
diff --git a/Userland/Services/WindowServer/EventLoop.cpp b/Userland/Services/WindowServer/EventLoop.cpp
index 7f9c21c63d..15a4f3d701 100644
--- a/Userland/Services/WindowServer/EventLoop.cpp
+++ b/Userland/Services/WindowServer/EventLoop.cpp
@@ -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;