From c1a3968c666343149519f32d59cec78fc83bec50 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Nov 2021 11:14:40 +0100 Subject: [PATCH] LibCore: Make LocalSocket takeover mechanism return ErrorOr --- .../HackStudio/LanguageServers/Cpp/main.cpp | 4 +-- .../HackStudio/LanguageServers/Shell/main.cpp | 4 +-- Userland/Libraries/LibCore/LocalSocket.cpp | 26 ++++++++----------- Userland/Libraries/LibCore/LocalSocket.h | 2 +- .../Services/FileSystemAccessServer/main.cpp | 4 +-- Userland/Services/ImageDecoder/main.cpp | 4 +-- Userland/Services/RequestServer/main.cpp | 5 ++-- Userland/Services/WebContent/main.cpp | 5 ++-- Userland/Services/WebSocket/main.cpp | 5 ++-- 9 files changed, 26 insertions(+), 33 deletions(-) diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp index 6b3b3100f57..5a3b0e3cff2 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp @@ -36,8 +36,8 @@ ErrorOr mode_server() Core::EventLoop event_loop; TRY(Core::System::pledge("stdio unix recvfd rpath ", nullptr)); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); TRY(Core::System::pledge("stdio recvfd rpath", nullptr)); TRY(Core::System::unveil("/usr/include", "r")); diff --git a/Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp b/Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp index ef7800e9f64..bfad0eb1fd8 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp @@ -16,8 +16,8 @@ ErrorOr serenity_main(Main::Arguments) Core::EventLoop event_loop; TRY(Core::System::pledge("stdio unix rpath recvfd", nullptr)); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); TRY(Core::System::pledge("stdio rpath recvfd", nullptr)); TRY(Core::System::unveil("/etc/passwd", "r")); diff --git a/Userland/Libraries/LibCore/LocalSocket.cpp b/Userland/Libraries/LibCore/LocalSocket.cpp index 94547fa9809..af496613025 100644 --- a/Userland/Libraries/LibCore/LocalSocket.cpp +++ b/Userland/Libraries/LibCore/LocalSocket.cpp @@ -5,12 +5,13 @@ */ #include +#include #include #include #include #include #include -#include + #if defined(__FreeBSD__) # include #endif @@ -114,7 +115,7 @@ void LocalSocket::parse_sockets_from_system_server() unsetenv(socket_takeover); } -RefPtr LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path) +ErrorOr> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path) { if (!s_overtaken_sockets_parsed) parse_sockets_from_system_server(); @@ -126,29 +127,24 @@ RefPtr LocalSocket::take_over_accepted_socket_from_system_server(St fd = s_overtaken_sockets.begin()->value; } else { auto it = s_overtaken_sockets.find(socket_path); - if (it == s_overtaken_sockets.end()) { - dbgln("Non-existent socket requested"); - return nullptr; - } + if (it == s_overtaken_sockets.end()) + return Error::from_string_literal("Non-existent socket requested"sv); fd = it->value; } // Sanity check: it has to be a socket. - struct stat stat; - int rc = fstat(fd, &stat); - if (rc < 0 || !S_ISSOCK(stat.st_mode)) { - if (rc != 0) - perror("fstat"); - dbgln("ERROR: The fd we got from SystemServer is not a socket"); - return nullptr; - } + auto stat = TRY(Core::System::fstat(fd)); + + if (!S_ISSOCK(stat.st_mode)) + return Error::from_string_literal("The fd we got from SystemServer is not a socket"sv); auto socket = LocalSocket::construct(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(fd, F_SETFD, FD_CLOEXEC); + TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC)); + return socket; } diff --git a/Userland/Libraries/LibCore/LocalSocket.h b/Userland/Libraries/LibCore/LocalSocket.h index 05b293b0ee1..ab08018eb82 100644 --- a/Userland/Libraries/LibCore/LocalSocket.h +++ b/Userland/Libraries/LibCore/LocalSocket.h @@ -15,7 +15,7 @@ class LocalSocket final : public Socket { public: virtual ~LocalSocket() override; - static RefPtr take_over_accepted_socket_from_system_server(String const& socket_path = String()); + static ErrorOr> take_over_accepted_socket_from_system_server(String const& socket_path = String()); pid_t peer_pid() const; private: diff --git a/Userland/Services/FileSystemAccessServer/main.cpp b/Userland/Services/FileSystemAccessServer/main.cpp index f25b369052e..2e37422f192 100644 --- a/Userland/Services/FileSystemAccessServer/main.cpp +++ b/Userland/Services/FileSystemAccessServer/main.cpp @@ -18,7 +18,7 @@ ErrorOr serenity_main(Main::Arguments) auto app = GUI::Application::construct(0, nullptr); app->set_quit_when_last_window_deleted(false); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); return app->exec(); } diff --git a/Userland/Services/ImageDecoder/main.cpp b/Userland/Services/ImageDecoder/main.cpp index 7f4194cd4eb..cab1292c0dd 100644 --- a/Userland/Services/ImageDecoder/main.cpp +++ b/Userland/Services/ImageDecoder/main.cpp @@ -17,8 +17,8 @@ ErrorOr serenity_main(Main::Arguments) TRY(Core::System::pledge("stdio recvfd sendfd unix", nullptr)); TRY(Core::System::unveil(nullptr, nullptr)); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); TRY(Core::System::pledge("stdio recvfd sendfd", nullptr)); return event_loop.exec(); } diff --git a/Userland/Services/RequestServer/main.cpp b/Userland/Services/RequestServer/main.cpp index c1fc7fa925d..5f4a26b0dd9 100644 --- a/Userland/Services/RequestServer/main.cpp +++ b/Userland/Services/RequestServer/main.cpp @@ -36,9 +36,8 @@ ErrorOr serenity_main(Main::Arguments) [[maybe_unused]] auto http = make(); [[maybe_unused]] auto https = make(); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - VERIFY(socket); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); auto result = event_loop.exec(); // FIXME: We exit instead of returning, so that protocol destructors don't get called. diff --git a/Userland/Services/WebContent/main.cpp b/Userland/Services/WebContent/main.cpp index 350995f5183..d7d5782d997 100644 --- a/Userland/Services/WebContent/main.cpp +++ b/Userland/Services/WebContent/main.cpp @@ -21,8 +21,7 @@ ErrorOr serenity_main(Main::Arguments) TRY(Core::System::unveil("/tmp/portal/websocket", "rw")); TRY(Core::System::unveil(nullptr, nullptr)); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - VERIFY(socket); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); return event_loop.exec(); } diff --git a/Userland/Services/WebSocket/main.cpp b/Userland/Services/WebSocket/main.cpp index 5fc28d3c202..438bba72ed7 100644 --- a/Userland/Services/WebSocket/main.cpp +++ b/Userland/Services/WebSocket/main.cpp @@ -25,8 +25,7 @@ ErrorOr serenity_main(Main::Arguments) TRY(Core::System::unveil("/tmp/portal/lookup", "rw")); TRY(Core::System::unveil(nullptr, nullptr)); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - VERIFY(socket); - IPC::new_client_connection(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection(move(socket), 1); return event_loop.exec(); }