2020-09-06 20:44:16 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-06 20:44:16 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-09-06 16:16:10 +02:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
|
#include <AK/JsonValue.h>
|
2020-09-06 19:12:21 +02:00
|
|
|
#include <LibCore/DateTime.h>
|
2023-02-09 03:02:46 +01:00
|
|
|
#include <LibCore/File.h>
|
2020-09-06 19:05:08 +02:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2021-11-23 10:59:50 +01:00
|
|
|
#include <LibCore/System.h>
|
2021-11-22 19:40:27 +01:00
|
|
|
#include <LibMain/Main.h>
|
2020-09-06 16:16:10 +02:00
|
|
|
#include <pwd.h>
|
2020-09-06 18:58:58 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <time.h>
|
2020-09-06 16:16:10 +02:00
|
|
|
|
2021-11-22 19:40:27 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments)
|
2020-09-06 16:16:10 +02:00
|
|
|
{
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-11-23 10:59:50 +01:00
|
|
|
TRY(Core::System::unveil("/dev", "r"));
|
|
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
2022-01-20 12:27:56 -05:00
|
|
|
TRY(Core::System::unveil("/etc/timezone", "r"));
|
2021-11-23 10:59:50 +01:00
|
|
|
TRY(Core::System::unveil("/var/run/utmp", "r"));
|
2022-10-14 21:56:19 +03:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2021-11-23 10:59:50 +01:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-11-22 19:40:27 +01:00
|
|
|
|
2023-02-09 03:02:46 +01:00
|
|
|
auto file = TRY(Core::File::open("/var/run/utmp"sv, Core::File::OpenMode::Read));
|
2022-12-11 17:49:00 +01:00
|
|
|
auto file_contents = TRY(file->read_until_eof());
|
2022-09-14 17:40:25 +01:00
|
|
|
auto json = TRY(JsonValue::from_string(file_contents));
|
2021-11-22 19:40:27 +01:00
|
|
|
if (!json.is_object()) {
|
2020-10-25 17:46:16 +01:00
|
|
|
warnln("Error: Could not parse /var/run/utmp");
|
2020-09-06 16:16:10 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 14:50:31 +01:00
|
|
|
auto process_statistics = TRY(Core::ProcessStatisticsReader::get_all());
|
2020-09-06 19:05:08 +02:00
|
|
|
|
2020-09-06 18:58:58 +02:00
|
|
|
auto now = time(nullptr);
|
|
|
|
|
|
2021-05-31 15:43:25 +01:00
|
|
|
outln("\033[1m{:10} {:12} {:16} {:6} {}\033[0m", "USER", "TTY", "LOGIN@", "IDLE", "WHAT");
|
2021-11-22 19:40:27 +01:00
|
|
|
json.as_object().for_each_member([&](auto& tty, auto& value) {
|
2020-09-06 16:16:10 +02: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 19:12:21 +02: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 16:16:10 +02:00
|
|
|
|
|
|
|
|
auto* pw = getpwuid(uid);
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString username;
|
2020-09-06 16:16:10 +02:00
|
|
|
if (pw)
|
|
|
|
|
username = pw->pw_name;
|
|
|
|
|
else
|
2022-12-04 18:02:33 +00:00
|
|
|
username = DeprecatedString::number(uid);
|
2020-09-06 16:16:10 +02:00
|
|
|
|
2020-09-06 18:58:58 +02:00
|
|
|
StringBuilder builder;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString idle_string = "n/a";
|
2020-09-06 18:58:58 +02:00
|
|
|
struct stat st;
|
|
|
|
|
if (stat(tty.characters(), &st) == 0) {
|
|
|
|
|
auto idle_time = now - st.st_mtime;
|
|
|
|
|
if (idle_time >= 0) {
|
2021-05-07 20:48:20 +02:00
|
|
|
builder.appendff("{}s", idle_time);
|
2022-12-06 01:12:49 +00:00
|
|
|
idle_string = builder.to_deprecated_string();
|
2020-09-06 18:58:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString what = "n/a";
|
2020-09-06 19:05:08 +02:00
|
|
|
|
2022-12-08 14:50:31 +01:00
|
|
|
for (auto& process : process_statistics.processes) {
|
2021-05-23 11:08:32 +02:00
|
|
|
if (process.tty == tty && process.pid == process.pgid)
|
|
|
|
|
what = process.name;
|
2020-09-06 19:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-31 15:43:25 +01:00
|
|
|
outln("{:10} {:12} {:16} {:6} {}", username, tty, login_at, idle_string, what);
|
2020-09-06 16:16:10 +02:00
|
|
|
});
|
|
|
|
|
return 0;
|
|
|
|
|
}
|