2020-09-06 18:44:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2023-05-23 21:09:48 +00:00
|
|
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
2020-09-06 18:44:16 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-06 18:44:16 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-06 14:16:10 +00:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2023-05-23 21:09:48 +00:00
|
|
|
#include <AK/Time.h>
|
|
|
|
#include <LibCore/Account.h>
|
2020-09-06 17:12:21 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2020-09-06 17:05:08 +00:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2021-11-23 09:59:50 +00:00
|
|
|
#include <LibCore/System.h>
|
2021-11-22 18:40:27 +00:00
|
|
|
#include <LibMain/Main.h>
|
2020-09-06 14:16:10 +00:00
|
|
|
|
2021-11-22 18:40:27 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments)
|
2020-09-06 14:16:10 +00:00
|
|
|
{
|
2021-11-27 22:26:34 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-11-23 09:59:50 +00:00
|
|
|
TRY(Core::System::unveil("/dev", "r"));
|
2023-05-23 21:09:48 +00:00
|
|
|
TRY(Core::System::unveil("/etc/group", "r"));
|
2021-11-23 09:59:50 +00:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
2022-01-20 17:27:56 +00:00
|
|
|
TRY(Core::System::unveil("/etc/timezone", "r"));
|
2021-11-23 09:59:50 +00:00
|
|
|
TRY(Core::System::unveil("/var/run/utmp", "r"));
|
2022-10-14 18:56:19 +00:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2021-11-23 09:59:50 +00:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-11-22 18:40:27 +00:00
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = TRY(Core::File::open("/var/run/utmp"sv, Core::File::OpenMode::Read));
|
2022-12-11 16:49:00 +00:00
|
|
|
auto file_contents = TRY(file->read_until_eof());
|
2022-09-14 16:40:25 +00:00
|
|
|
auto json = TRY(JsonValue::from_string(file_contents));
|
2021-11-22 18:40:27 +00:00
|
|
|
if (!json.is_object()) {
|
2020-10-25 16:46:16 +00:00
|
|
|
warnln("Error: Could not parse /var/run/utmp");
|
2020-09-06 14:16:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
auto process_statistics = TRY(Core::ProcessStatisticsReader::get_all());
|
2020-09-06 17:05:08 +00:00
|
|
|
|
2023-05-23 21:09:48 +00:00
|
|
|
auto now = Time::now_realtime().to_seconds();
|
2020-09-06 16:58:58 +00:00
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("\033[1m{:10} {:12} {:16} {:6} {}\033[0m", "USER", "TTY", "LOGIN@", "IDLE", "WHAT");
|
2021-11-22 18:40:27 +00:00
|
|
|
json.as_object().for_each_member([&](auto& tty, auto& value) {
|
2020-09-06 14:16:10 +00:00
|
|
|
const JsonObject& entry = value.as_object();
|
2022-12-22 14:28:48 +00:00
|
|
|
auto uid = entry.get_u32("uid"sv).value_or(0);
|
|
|
|
[[maybe_unused]] auto pid = entry.get_i32("pid"sv).value_or(0);
|
2020-09-06 17:12:21 +00:00
|
|
|
|
2022-12-22 14:28:48 +00:00
|
|
|
auto login_time = Core::DateTime::from_timestamp(entry.get_integer<time_t>("login_at"sv).value_or(0));
|
2022-12-06 01:12:49 +00:00
|
|
|
auto login_at = login_time.to_deprecated_string("%b%d %H:%M:%S"sv);
|
2020-09-06 14:16:10 +00:00
|
|
|
|
2023-05-23 21:09:48 +00:00
|
|
|
auto maybe_account = Core::Account::from_uid(uid, Core::Account::Read::PasswdOnly);
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString username;
|
2023-05-23 21:09:48 +00:00
|
|
|
if (!maybe_account.is_error())
|
|
|
|
username = maybe_account.release_value().username();
|
2020-09-06 14:16:10 +00:00
|
|
|
else
|
2022-12-04 18:02:33 +00:00
|
|
|
username = DeprecatedString::number(uid);
|
2020-09-06 14:16:10 +00:00
|
|
|
|
2020-09-06 16:58:58 +00:00
|
|
|
StringBuilder builder;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString idle_string = "n/a";
|
2023-05-23 21:09:48 +00:00
|
|
|
auto maybe_stat = Core::System::stat(tty);
|
|
|
|
if (!maybe_stat.is_error()) {
|
|
|
|
auto stat = maybe_stat.release_value();
|
|
|
|
auto idle_time = now - stat.st_mtime;
|
2020-09-06 16:58:58 +00:00
|
|
|
if (idle_time >= 0) {
|
2021-05-07 18:48:20 +00:00
|
|
|
builder.appendff("{}s", idle_time);
|
2022-12-06 01:12:49 +00:00
|
|
|
idle_string = builder.to_deprecated_string();
|
2020-09-06 16:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString what = "n/a";
|
2020-09-06 17:05:08 +00:00
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
for (auto& process : process_statistics.processes) {
|
2021-05-23 09:08:32 +00:00
|
|
|
if (process.tty == tty && process.pid == process.pgid)
|
|
|
|
what = process.name;
|
2020-09-06 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{:10} {:12} {:16} {:6} {}", username, tty, login_at, idle_string, what);
|
2020-09-06 14:16:10 +00:00
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}
|