summaryrefslogtreecommitdiff
path: root/Userland/Services/SystemServer/Service.cpp
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-06-08 18:29:08 +0200
committerAndreas Kling <kling@serenityos.org>2022-06-09 22:08:04 +0200
commit23fa6b1f7bcd823bab4640082d909370f73b0054 (patch)
treecd24c4f7952b7ac4d63f48463a2b520b6dc9e511 /Userland/Services/SystemServer/Service.cpp
parentdd3b65c762c0fb203b88d342d29f25f7a3a960d2 (diff)
downloadserenity-23fa6b1f7bcd823bab4640082d909370f73b0054.zip
SystemServer: Add Service::try_create to propagate errors
This static method is used to propagate errors at the creation of the object.
Diffstat (limited to 'Userland/Services/SystemServer/Service.cpp')
-rw-r--r--Userland/Services/SystemServer/Service.cpp33
1 files changed, 18 insertions, 15 deletions
diff --git a/Userland/Services/SystemServer/Service.cpp b/Userland/Services/SystemServer/Service.cpp
index 54a12cd52b..6b93db32fc 100644
--- a/Userland/Services/SystemServer/Service.cpp
+++ b/Userland/Services/SystemServer/Service.cpp
@@ -31,26 +31,24 @@ Service* Service::find_by_pid(pid_t pid)
return (*it).value;
}
-void Service::setup_socket(SocketDescriptor& socket)
+ErrorOr<void> Service::setup_socket(SocketDescriptor& socket)
{
VERIFY(socket.fd == -1);
- MUST(Core::Directory::create(LexicalPath(socket.path).parent(), Core::Directory::CreateDirectories::Yes));
+ TRY(Core::Directory::create(LexicalPath(socket.path).parent(), Core::Directory::CreateDirectories::Yes));
// Note: we use SOCK_CLOEXEC here to make sure we don't leak every socket to
// all the clients. We'll make the one we do need to pass down !CLOEXEC later
// after forking off the process.
- int socket_fd = Core::System::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0).release_value_but_fixme_should_propagate_errors();
+ int const socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
socket.fd = socket_fd;
if (m_account.has_value()) {
auto& account = m_account.value();
- // FIXME: Propagate errors
- MUST(Core::System::fchown(socket_fd, account.uid(), account.gid()));
+ TRY(Core::System::fchown(socket_fd, account.uid(), account.gid()));
}
- // FIXME: Propagate errors
- MUST(Core::System::fchmod(socket_fd, socket.permissions));
+ TRY(Core::System::fchmod(socket_fd, socket.permissions));
auto socket_address = Core::SocketAddress::local(socket.path);
auto un_optional = socket_address.to_sockaddr_un();
@@ -60,16 +58,16 @@ void Service::setup_socket(SocketDescriptor& socket)
}
auto un = un_optional.value();
- // FIXME: Propagate errors
- MUST(Core::System::bind(socket_fd, (sockaddr const*)&un, sizeof(un)));
- // FIXME: Propagate errors
- MUST(Core::System::listen(socket_fd, 16));
+ TRY(Core::System::bind(socket_fd, (sockaddr const*)&un, sizeof(un)));
+ TRY(Core::System::listen(socket_fd, 16));
+ return {};
}
-void Service::setup_sockets()
+ErrorOr<void> Service::setup_sockets()
{
for (SocketDescriptor& socket : m_sockets)
- setup_socket(socket);
+ TRY(setup_socket(socket));
+ return {};
}
void Service::setup_notifier()
@@ -338,9 +336,14 @@ Service::Service(Core::ConfigFile const& config, StringView name)
VERIFY(!m_accept_socket_connections || (m_sockets.size() == 1 && m_lazy && m_multi_instance));
// MultiInstance doesn't work with KeepAlive.
VERIFY(!m_multi_instance || !m_keep_alive);
+}
- if (is_enabled())
- setup_sockets();
+ErrorOr<NonnullRefPtr<Service>> Service::try_create(Core::ConfigFile const& config, StringView name)
+{
+ auto service = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Service(config, name)));
+ if (service->is_enabled())
+ TRY(service->setup_sockets());
+ return service;
}
void Service::save_to(JsonObject& json)