mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:17:44 +00:00
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:
parent
229a45ab14
commit
81047d8f9c
13 changed files with 41 additions and 53 deletions
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -7,6 +7,7 @@
|
||||||
#include <LibCore/LocalServer.h>
|
#include <LibCore/LocalServer.h>
|
||||||
#include <LibCore/LocalSocket.h>
|
#include <LibCore/LocalSocket.h>
|
||||||
#include <LibCore/Notifier.h>
|
#include <LibCore/Notifier.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
@ -30,10 +31,10 @@ LocalServer::~LocalServer()
|
||||||
::close(m_fd);
|
::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)
|
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)
|
if (!LocalSocket::s_overtaken_sockets_parsed)
|
||||||
LocalSocket::parse_sockets_from_system_server();
|
LocalSocket::parse_sockets_from_system_server();
|
||||||
|
@ -51,32 +52,27 @@ bool LocalServer::take_over_from_system_server(String const& socket_path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd >= 0) {
|
if (fd < 0)
|
||||||
// Sanity check: it has to be a socket.
|
return Error::from_string_literal("Core::LocalServer: No file descriptor for socket takeover"sv);
|
||||||
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;
|
// Sanity check: it has to be a socket.
|
||||||
setup_notifier();
|
auto stat = TRY(Core::System::fstat(fd));
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
if (rc != 0)
|
|
||||||
perror("fstat");
|
|
||||||
dbgln("It's not a socket, what the heck??");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
void LocalServer::setup_notifier()
|
||||||
|
|
|
@ -16,7 +16,7 @@ class LocalServer : public Object {
|
||||||
public:
|
public:
|
||||||
virtual ~LocalServer() override;
|
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 is_listening() const { return m_listening; }
|
||||||
bool listen(const String& address);
|
bool listen(const String& address);
|
||||||
|
|
||||||
|
|
|
@ -44,9 +44,12 @@ bool MediaQueryList::matches() const
|
||||||
|
|
||||||
bool MediaQueryList::evaluate()
|
bool MediaQueryList::evaluate()
|
||||||
{
|
{
|
||||||
|
if (!m_document)
|
||||||
|
return false;
|
||||||
|
|
||||||
bool now_matches = false;
|
bool now_matches = false;
|
||||||
for (auto& media : m_media) {
|
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;
|
return now_matches;
|
||||||
|
|
|
@ -54,7 +54,7 @@ public:
|
||||||
private:
|
private:
|
||||||
MediaQueryList(DOM::Document&, NonnullRefPtrVector<MediaQuery>&&);
|
MediaQueryList(DOM::Document&, NonnullRefPtrVector<MediaQuery>&&);
|
||||||
|
|
||||||
DOM::Document& m_document;
|
WeakPtr<DOM::Document> m_document;
|
||||||
NonnullRefPtrVector<MediaQuery> m_media;
|
NonnullRefPtrVector<MediaQuery> m_media;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
Core::EventLoop event_loop;
|
Core::EventLoop event_loop;
|
||||||
auto mixer = TRY(AudioServer::Mixer::try_create(config));
|
auto mixer = TRY(AudioServer::Mixer::try_create(config));
|
||||||
auto server = TRY(Core::LocalServer::try_create());
|
auto server = TRY(Core::LocalServer::try_create());
|
||||||
bool ok = server->take_over_from_system_server();
|
TRY(server->take_over_from_system_server());
|
||||||
VERIFY(ok);
|
|
||||||
|
|
||||||
server->on_accept = [&](NonnullRefPtr<Core::LocalSocket> client_socket) {
|
server->on_accept = [&](NonnullRefPtr<Core::LocalSocket> client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
|
|
|
@ -19,8 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
TRY(Core::System::unveil(nullptr, nullptr));
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
|
|
||||||
auto server = TRY(Core::LocalServer::try_create());
|
auto server = TRY(Core::LocalServer::try_create());
|
||||||
bool ok = server->take_over_from_system_server();
|
TRY(server->take_over_from_system_server());
|
||||||
VERIFY(ok);
|
|
||||||
|
|
||||||
server->on_accept = [&](auto client_socket) {
|
server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
|
|
|
@ -17,10 +17,9 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
TRY(Core::System::unveil(nullptr, nullptr));
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
|
|
||||||
Core::EventLoop event_loop;
|
Core::EventLoop event_loop;
|
||||||
auto server = TRY(Core::LocalServer::try_create());
|
|
||||||
|
|
||||||
bool ok = server->take_over_from_system_server();
|
auto server = TRY(Core::LocalServer::try_create());
|
||||||
VERIFY(ok);
|
TRY(server->take_over_from_system_server());
|
||||||
server->on_accept = [&](auto client_socket) {
|
server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
int client_id = ++s_next_client_id;
|
int client_id = ++s_next_client_id;
|
||||||
|
|
|
@ -19,8 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
|
|
||||||
TRY(Core::System::pledge("stdio unix accept"));
|
TRY(Core::System::pledge("stdio unix accept"));
|
||||||
|
|
||||||
bool ok = server->take_over_from_system_server("/tmp/portal/inspector");
|
TRY(server->take_over_from_system_server("/tmp/portal/inspector"));
|
||||||
VERIFY(ok);
|
|
||||||
server->on_accept = [&](auto client_socket) {
|
server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
int client_id = ++s_next_client_id;
|
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());
|
auto inspectables_server = TRY(Core::LocalServer::try_create());
|
||||||
if (!inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"))
|
TRY(inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"));
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
|
|
||||||
inspectables_server->on_accept = [&](auto client_socket) {
|
inspectables_server->on_accept = [&](auto client_socket) {
|
||||||
auto pid = client_socket->peer_pid();
|
auto pid = client_socket->peer_pid();
|
||||||
|
|
|
@ -23,8 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
|
|
||||||
TRY(Core::System::pledge("stdio accept rpath proc exec"));
|
TRY(Core::System::pledge("stdio accept rpath proc exec"));
|
||||||
|
|
||||||
bool ok = server->take_over_from_system_server();
|
TRY(server->take_over_from_system_server());
|
||||||
VERIFY(ok);
|
|
||||||
server->on_accept = [&](auto client_socket) {
|
server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
int client_id = ++s_next_client_id;
|
int client_id = ++s_next_client_id;
|
||||||
|
|
|
@ -78,8 +78,7 @@ LookupServer::LookupServer()
|
||||||
int client_id = ++s_next_client_id;
|
int client_id = ++s_next_client_id;
|
||||||
(void)IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
|
(void)IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
|
||||||
};
|
};
|
||||||
bool ok = m_local_server->take_over_from_system_server();
|
MUST(m_local_server->take_over_from_system_server());
|
||||||
VERIFY(ok);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LookupServer::load_etc_hosts()
|
void LookupServer::load_etc_hosts()
|
||||||
|
|
|
@ -18,8 +18,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto app = TRY(GUI::Application::try_create(arguments));
|
auto app = TRY(GUI::Application::try_create(arguments));
|
||||||
auto server = TRY(Core::LocalServer::try_create());
|
auto server = TRY(Core::LocalServer::try_create());
|
||||||
|
|
||||||
bool ok = server->take_over_from_system_server();
|
TRY(server->take_over_from_system_server());
|
||||||
VERIFY(ok);
|
|
||||||
server->on_accept = [&](auto client_socket) {
|
server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
int client_id = ++s_next_client_id;
|
int client_id = ++s_next_client_id;
|
||||||
|
|
|
@ -25,10 +25,9 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
TRY(Core::System::unveil(nullptr, nullptr));
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
|
|
||||||
Core::EventLoop event_loop;
|
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) {
|
server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
int client_id = ++s_next_client_id;
|
int client_id = ++s_next_client_id;
|
||||||
|
|
|
@ -25,10 +25,8 @@ EventLoop::EventLoop()
|
||||||
m_keyboard_fd = open("/dev/keyboard0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
m_keyboard_fd = open("/dev/keyboard0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
||||||
m_mouse_fd = open("/dev/mouse0", 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");
|
MUST(m_window_server->take_over_from_system_server("/tmp/portal/window"));
|
||||||
VERIFY(ok);
|
MUST(m_wm_server->take_over_from_system_server("/tmp/portal/wm"));
|
||||||
ok = m_wm_server->take_over_from_system_server("/tmp/portal/wm");
|
|
||||||
VERIFY(ok);
|
|
||||||
|
|
||||||
m_window_server->on_accept = [&](auto client_socket) {
|
m_window_server->on_accept = [&](auto client_socket) {
|
||||||
static int s_next_client_id = 0;
|
static int s_next_client_id = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue