mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:17:45 +00:00
SystemServer: Add Service::try_create to propagate errors
This static method is used to propagate errors at the creation of the object.
This commit is contained in:
parent
dd3b65c762
commit
23fa6b1f7b
3 changed files with 25 additions and 20 deletions
|
@ -31,26 +31,24 @@ Service* Service::find_by_pid(pid_t pid)
|
||||||
return (*it).value;
|
return (*it).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Service::setup_socket(SocketDescriptor& socket)
|
ErrorOr<void> Service::setup_socket(SocketDescriptor& socket)
|
||||||
{
|
{
|
||||||
VERIFY(socket.fd == -1);
|
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
|
// 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
|
// all the clients. We'll make the one we do need to pass down !CLOEXEC later
|
||||||
// after forking off the process.
|
// 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;
|
socket.fd = socket_fd;
|
||||||
|
|
||||||
if (m_account.has_value()) {
|
if (m_account.has_value()) {
|
||||||
auto& account = m_account.value();
|
auto& account = m_account.value();
|
||||||
// FIXME: Propagate errors
|
TRY(Core::System::fchown(socket_fd, account.uid(), account.gid()));
|
||||||
MUST(Core::System::fchown(socket_fd, account.uid(), account.gid()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Propagate errors
|
TRY(Core::System::fchmod(socket_fd, socket.permissions));
|
||||||
MUST(Core::System::fchmod(socket_fd, socket.permissions));
|
|
||||||
|
|
||||||
auto socket_address = Core::SocketAddress::local(socket.path);
|
auto socket_address = Core::SocketAddress::local(socket.path);
|
||||||
auto un_optional = socket_address.to_sockaddr_un();
|
auto un_optional = socket_address.to_sockaddr_un();
|
||||||
|
@ -60,16 +58,16 @@ void Service::setup_socket(SocketDescriptor& socket)
|
||||||
}
|
}
|
||||||
auto un = un_optional.value();
|
auto un = un_optional.value();
|
||||||
|
|
||||||
// FIXME: Propagate errors
|
TRY(Core::System::bind(socket_fd, (sockaddr const*)&un, sizeof(un)));
|
||||||
MUST(Core::System::bind(socket_fd, (sockaddr const*)&un, sizeof(un)));
|
TRY(Core::System::listen(socket_fd, 16));
|
||||||
// FIXME: Propagate errors
|
return {};
|
||||||
MUST(Core::System::listen(socket_fd, 16));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Service::setup_sockets()
|
ErrorOr<void> Service::setup_sockets()
|
||||||
{
|
{
|
||||||
for (SocketDescriptor& socket : m_sockets)
|
for (SocketDescriptor& socket : m_sockets)
|
||||||
setup_socket(socket);
|
TRY(setup_socket(socket));
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Service::setup_notifier()
|
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));
|
VERIFY(!m_accept_socket_connections || (m_sockets.size() == 1 && m_lazy && m_multi_instance));
|
||||||
// MultiInstance doesn't work with KeepAlive.
|
// MultiInstance doesn't work with KeepAlive.
|
||||||
VERIFY(!m_multi_instance || !m_keep_alive);
|
VERIFY(!m_multi_instance || !m_keep_alive);
|
||||||
|
}
|
||||||
|
|
||||||
if (is_enabled())
|
ErrorOr<NonnullRefPtr<Service>> Service::try_create(Core::ConfigFile const& config, StringView name)
|
||||||
setup_sockets();
|
{
|
||||||
|
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)
|
void Service::save_to(JsonObject& json)
|
||||||
|
|
|
@ -14,9 +14,11 @@
|
||||||
#include <LibCore/Object.h>
|
#include <LibCore/Object.h>
|
||||||
|
|
||||||
class Service final : public Core::Object {
|
class Service final : public Core::Object {
|
||||||
C_OBJECT(Service)
|
C_OBJECT_ABSTRACT(Service)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static ErrorOr<NonnullRefPtr<Service>> try_create(Core::ConfigFile const& config, StringView name);
|
||||||
|
|
||||||
bool is_enabled() const;
|
bool is_enabled() const;
|
||||||
void activate();
|
void activate();
|
||||||
void did_exit(int exit_code);
|
void did_exit(int exit_code);
|
||||||
|
@ -83,8 +85,8 @@ private:
|
||||||
// times where it has exited unsuccessfully and too quickly.
|
// times where it has exited unsuccessfully and too quickly.
|
||||||
int m_restart_attempts { 0 };
|
int m_restart_attempts { 0 };
|
||||||
|
|
||||||
void setup_socket(SocketDescriptor&);
|
ErrorOr<void> setup_socket(SocketDescriptor&);
|
||||||
void setup_sockets();
|
ErrorOr<void> setup_sockets();
|
||||||
void setup_notifier();
|
void setup_notifier();
|
||||||
void handle_socket_connection();
|
void handle_socket_connection();
|
||||||
};
|
};
|
||||||
|
|
|
@ -487,8 +487,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto config = (user)
|
auto config = (user)
|
||||||
? TRY(Core::ConfigFile::open_for_app("SystemServer"))
|
? TRY(Core::ConfigFile::open_for_app("SystemServer"))
|
||||||
: TRY(Core::ConfigFile::open_for_system("SystemServer"));
|
: TRY(Core::ConfigFile::open_for_system("SystemServer"));
|
||||||
for (auto name : config->groups()) {
|
for (auto const& name : config->groups()) {
|
||||||
auto service = Service::construct(*config, name);
|
auto service = TRY(Service::try_create(*config, name));
|
||||||
if (service->is_enabled())
|
if (service->is_enabled())
|
||||||
services.append(service);
|
services.append(service);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue