diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-23 11:14:40 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-23 11:33:36 +0100 |
commit | c1a3968c666343149519f32d59cec78fc83bec50 (patch) | |
tree | 1d4b19454236133b6449cae26f9c2f011c7e9636 | |
parent | c37a02341b61f00f0097a93d566ab61859f15a10 (diff) | |
download | serenity-c1a3968c666343149519f32d59cec78fc83bec50.zip |
LibCore: Make LocalSocket takeover mechanism return ErrorOr<T>
-rw-r--r-- | Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp | 4 | ||||
-rw-r--r-- | Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalSocket.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalSocket.h | 2 | ||||
-rw-r--r-- | Userland/Services/FileSystemAccessServer/main.cpp | 4 | ||||
-rw-r--r-- | Userland/Services/ImageDecoder/main.cpp | 4 | ||||
-rw-r--r-- | Userland/Services/RequestServer/main.cpp | 5 | ||||
-rw-r--r-- | Userland/Services/WebContent/main.cpp | 5 | ||||
-rw-r--r-- | 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 6b3b3100f5..5a3b0e3cff 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/main.cpp @@ -36,8 +36,8 @@ ErrorOr<int> 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<LanguageServers::Cpp::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<LanguageServers::Cpp::ClientConnection>(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 ef7800e9f6..bfad0eb1fd 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Shell/main.cpp @@ -16,8 +16,8 @@ ErrorOr<int> 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<LanguageServers::Shell::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<LanguageServers::Shell::ClientConnection>(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 94547fa980..af49661302 100644 --- a/Userland/Libraries/LibCore/LocalSocket.cpp +++ b/Userland/Libraries/LibCore/LocalSocket.cpp @@ -5,12 +5,13 @@ */ #include <LibCore/LocalSocket.h> +#include <LibCore/System.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> -#include <sys/stat.h> + #if defined(__FreeBSD__) # include <sys/ucred.h> #endif @@ -114,7 +115,7 @@ void LocalSocket::parse_sockets_from_system_server() unsetenv(socket_takeover); } -RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path) +ErrorOr<NonnullRefPtr<LocalSocket>> 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> 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 05b293b0ee..ab08018eb8 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<LocalSocket> take_over_accepted_socket_from_system_server(String const& socket_path = String()); + static ErrorOr<NonnullRefPtr<LocalSocket>> 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 f25b369052..2e37422f19 100644 --- a/Userland/Services/FileSystemAccessServer/main.cpp +++ b/Userland/Services/FileSystemAccessServer/main.cpp @@ -18,7 +18,7 @@ ErrorOr<int> 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<FileSystemAccessServer::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<FileSystemAccessServer::ClientConnection>(move(socket), 1); return app->exec(); } diff --git a/Userland/Services/ImageDecoder/main.cpp b/Userland/Services/ImageDecoder/main.cpp index 7f4194cd4e..cab1292c0d 100644 --- a/Userland/Services/ImageDecoder/main.cpp +++ b/Userland/Services/ImageDecoder/main.cpp @@ -17,8 +17,8 @@ ErrorOr<int> 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<ImageDecoder::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<ImageDecoder::ClientConnection>(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 c1fc7fa925..5f4a26b0dd 100644 --- a/Userland/Services/RequestServer/main.cpp +++ b/Userland/Services/RequestServer/main.cpp @@ -36,9 +36,8 @@ ErrorOr<int> serenity_main(Main::Arguments) [[maybe_unused]] auto http = make<RequestServer::HttpProtocol>(); [[maybe_unused]] auto https = make<RequestServer::HttpsProtocol>(); - auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); - VERIFY(socket); - IPC::new_client_connection<RequestServer::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<RequestServer::ClientConnection>(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 350995f518..d7d5782d99 100644 --- a/Userland/Services/WebContent/main.cpp +++ b/Userland/Services/WebContent/main.cpp @@ -21,8 +21,7 @@ ErrorOr<int> 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<WebContent::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<WebContent::ClientConnection>(move(socket), 1); return event_loop.exec(); } diff --git a/Userland/Services/WebSocket/main.cpp b/Userland/Services/WebSocket/main.cpp index 5fc28d3c20..438bba72ed 100644 --- a/Userland/Services/WebSocket/main.cpp +++ b/Userland/Services/WebSocket/main.cpp @@ -25,8 +25,7 @@ ErrorOr<int> 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<WebSocket::ClientConnection>(socket.release_nonnull(), 1); + auto socket = TRY(Core::LocalSocket::take_over_accepted_socket_from_system_server()); + IPC::new_client_connection<WebSocket::ClientConnection>(move(socket), 1); return event_loop.exec(); } |