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