1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:47:37 +00:00

w: Port to LibMain :^)

This unlocked a bunch of TRY() opportunities. Not just system calls, but
also Core::File::open() and JsonValue::from_string().
This commit is contained in:
Andreas Kling 2021-11-22 19:40:27 +01:00
parent e388782f60
commit 561e50108d
2 changed files with 14 additions and 37 deletions

View file

@ -102,5 +102,6 @@ target_link_libraries(zip LibArchive LibCompress LibCrypto)
target_link_libraries(cpp-lexer LibCpp)
target_link_libraries(cpp-parser LibCpp LibGUI)
target_link_libraries(cpp-preprocessor LibCpp LibGUI)
target_link_libraries(w LibMain)
target_link_libraries(wasm LibWasm LibLine)
target_link_libraries(wsctl LibGUI)

View file

@ -9,48 +9,24 @@
#include <LibCore/DateTime.h>
#include <LibCore/File.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <LibMain/Main.h>
#include <LibSystem/Wrappers.h>
#include <pwd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
int main()
ErrorOr<int> serenity_main(Main::Arguments)
{
if (pledge("stdio rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(System::pledge("stdio rpath", nullptr));
TRY(System::unveil("/dev", "r"));
TRY(System::unveil("/etc/passwd", "r"));
TRY(System::unveil("/var/run/utmp", "r"));
TRY(System::unveil("/proc", "r"));
TRY(System::unveil(nullptr, nullptr));
if (unveil("/dev", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/etc/passwd", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/var/run/utmp", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/proc", "r") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
auto file_or_error = Core::File::open("/var/run/utmp", Core::OpenMode::ReadOnly);
if (file_or_error.is_error()) {
warnln("Error: {}", file_or_error.error());
return 1;
}
auto& file = *file_or_error.value();
auto json = JsonValue::from_string(file.read_all());
if (json.is_error() || !json.value().is_object()) {
auto file = TRY(Core::File::open("/var/run/utmp", Core::OpenMode::ReadOnly));
auto json = TRY(JsonValue::from_string(file->read_all()));
if (!json.is_object()) {
warnln("Error: Could not parse /var/run/utmp");
return 1;
}
@ -64,7 +40,7 @@ int main()
auto now = time(nullptr);
outln("\033[1m{:10} {:12} {:16} {:6} {}\033[0m", "USER", "TTY", "LOGIN@", "IDLE", "WHAT");
json.value().as_object().for_each_member([&](auto& tty, auto& value) {
json.as_object().for_each_member([&](auto& tty, auto& value) {
const JsonObject& entry = value.as_object();
auto uid = entry.get("uid").to_u32();
[[maybe_unused]] auto pid = entry.get("pid").to_i32();