From 96e4077c38fdc51033ee2c612cb86d8f85a411f4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 3 Dec 2021 00:28:36 +0100 Subject: [PATCH] SystemMonitor: Port to LibMain :^) There's a lot more work to do on this application to take full advantage of TRY() etc. This patch just scratches the surface. --- .../Applications/SystemMonitor/CMakeLists.txt | 2 +- Userland/Applications/SystemMonitor/main.cpp | 75 +++++-------------- 2 files changed, 20 insertions(+), 57 deletions(-) diff --git a/Userland/Applications/SystemMonitor/CMakeLists.txt b/Userland/Applications/SystemMonitor/CMakeLists.txt index 4ed531fca5c..058afb42046 100644 --- a/Userland/Applications/SystemMonitor/CMakeLists.txt +++ b/Userland/Applications/SystemMonitor/CMakeLists.txt @@ -18,4 +18,4 @@ set(SOURCES ) serenity_app(SystemMonitor ICON app-system-monitor) -target_link_libraries(SystemMonitor LibGUI LibSymbolication LibPCIDB) +target_link_libraries(SystemMonitor LibGUI LibSymbolication LibPCIDB LibMain) diff --git a/Userland/Applications/SystemMonitor/main.cpp b/Userland/Applications/SystemMonitor/main.cpp index 68d5a3d34bc..6f3e4567137 100644 --- a/Userland/Applications/SystemMonitor/main.cpp +++ b/Userland/Applications/SystemMonitor/main.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -89,7 +91,7 @@ static bool can_access_pid(pid_t pid) return rc == 0; } -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { { // Before we do anything else, boost our process priority to the maximum allowed. @@ -101,72 +103,33 @@ int main(int argc, char** argv) sched_setparam(0, ¶m); } - if (pledge("stdio thread proc recvfd sendfd rpath exec unix", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio thread proc recvfd sendfd rpath exec unix")); - auto app = GUI::Application::construct(argc, argv); + auto app = TRY(GUI::Application::try_create(arguments)); Config::pledge_domains("SystemMonitor"); - if (unveil("/etc/passwd", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/res", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/proc", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/dev", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/bin", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/usr/lib", "r") < 0) { - perror("unveil"); - return 1; - } + TRY(Core::System::unveil("/etc/passwd", "r")); + TRY(Core::System::unveil("/res", "r")); + TRY(Core::System::unveil("/proc", "r")); + TRY(Core::System::unveil("/dev", "r")); + TRY(Core::System::unveil("/bin", "r")); + TRY(Core::System::unveil("/usr/lib", "r")); // This directory only exists if ports are installed - if (unveil("/usr/local/bin", "r") < 0 && errno != ENOENT) { - perror("unveil"); - return 1; - } + if (auto result = Core::System::unveil("/usr/local/bin", "r"); result.is_error() && result.error().code() != ENOENT) + return result.release_error(); - if (unveil("/usr/local/lib", "r") < 0 && errno != ENOENT) { - perror("unveil"); - return 1; - } + if (auto result = Core::System::unveil("/usr/local/lib", "r"); result.is_error() && result.error().code() != ENOENT) + return result.release_error(); - if (unveil("/bin/Profiler", "rx") < 0) { - perror("unveil"); - return 1; - } + TRY(Core::System::unveil("/bin/Profiler", "rx")); + TRY(Core::System::unveil("/bin/Inspector", "rx")); - if (unveil("/bin/Inspector", "rx") < 0) { - perror("unveil"); - return 1; - } - - unveil(nullptr, nullptr); - - const char* args_tab = "processes"; + StringView args_tab = "processes"sv; Core::ArgsParser parser; parser.add_option(args_tab, "Tab, one of 'processes', 'graphs', 'fs', 'hardware', or 'network'", "open-tab", 't', "tab"); - parser.parse(argc, argv); + parser.parse(arguments); StringView args_tab_view = args_tab; auto app_icon = GUI::Icon::default_icon("app-system-monitor");