From 6e7ff8cf1c5498f4448e631dba3793a522a84d95 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 29 Nov 2021 21:26:35 +0100 Subject: [PATCH] netstat: Port to LibMain :^) --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/netstat.cpp | 44 ++++++++----------------------- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 958321f8e5..1d774e242c 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -89,6 +89,7 @@ target_link_libraries(man LibMarkdown LibMain) target_link_libraries(markdown-check LibMarkdown) target_link_libraries(matroska LibVideo) target_link_libraries(md LibMarkdown) +target_link_libraries(netstat LibMain) target_link_libraries(notify LibGUI) target_link_libraries(nproc LibMain) target_link_libraries(open LibDesktop) diff --git a/Userland/Utilities/netstat.cpp b/Userland/Utilities/netstat.cpp index 02f63def2b..03bd277e9f 100644 --- a/Userland/Utilities/netstat.cpp +++ b/Userland/Utilities/netstat.cpp @@ -12,34 +12,17 @@ #include #include #include +#include +#include #include -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } - - if (unveil("/proc/net", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/proc/all", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/etc/passwd", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil(nullptr, nullptr) < 0) { - perror("unveil"); - return 1; - } + TRY(Core::System::pledge("stdio rpath")); + TRY(Core::System::unveil("/proc/net", "r")); + TRY(Core::System::unveil("/proc/all", "r")); + TRY(Core::System::unveil("/etc/passwd", "r")); + TRY(Core::System::unveil(nullptr, nullptr)); bool flag_all = false; bool flag_list = false; @@ -54,7 +37,7 @@ int main(int argc, char** argv) args_parser.add_option(flag_tcp, "Display only TCP network connections", "tcp", 't'); args_parser.add_option(flag_udp, "Display only UDP network connections", "udp", 'u'); args_parser.add_option(flag_program, "Show the PID and name of the program to which each socket belongs", "program", 'p'); - args_parser.parse(argc, argv); + args_parser.parse(arguments); bool has_protocol_flag = (flag_tcp || flag_udp); @@ -202,14 +185,9 @@ int main(int argc, char** argv) } if (!has_protocol_flag || flag_udp) { - auto file = Core::File::construct("/proc/net/udp"); - if (!file->open(Core::OpenMode::ReadOnly)) { - warnln("Error: {}", file->error_string()); - return 1; - } - + auto file = TRY(Core::File::open("/proc/net/udp", Core::OpenMode::ReadOnly)); auto file_contents = file->read_all(); - auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors(); + auto json = TRY(JsonValue::from_string(file_contents)); Vector sorted_regions = json.as_array().values(); quick_sort(sorted_regions, [](auto& a, auto& b) {