2020-09-06 20:44:16 +02:00
|
|
|
/*
|
2021-11-23 15:13:16 +01:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-09-06 20:44:16 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-06 20:44:16 +02:00
|
|
|
*/
|
|
|
|
|
|
2021-05-15 12:34:40 +02:00
|
|
|
#include <AK/Assertions.h>
|
2020-11-15 13:11:21 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-09-06 16:10:27 +02:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
|
#include <LibCore/DateTime.h>
|
2023-02-09 03:02:46 +01:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 15:13:16 +01:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2021-03-12 17:29:37 +01:00
|
|
|
#include <unistd.h>
|
2020-09-06 16:10:27 +02:00
|
|
|
|
2021-11-23 15:13:16 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-09-06 16:10:27 +02:00
|
|
|
{
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio wpath cpath"));
|
2021-11-23 15:13:16 +01:00
|
|
|
TRY(Core::System::unveil("/var/run/utmp", "rwc"));
|
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-09-06 16:14:27 +02:00
|
|
|
|
2020-09-06 16:10:27 +02:00
|
|
|
pid_t pid = 0;
|
|
|
|
|
bool flag_create = false;
|
|
|
|
|
bool flag_delete = false;
|
2022-07-11 20:42:03 +00:00
|
|
|
StringView tty_name;
|
|
|
|
|
StringView from;
|
2020-09-06 16:10:27 +02:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
args_parser.add_option(flag_create, "Create entry", "create", 'c');
|
|
|
|
|
args_parser.add_option(flag_delete, "Delete entry", "delete", 'd');
|
|
|
|
|
args_parser.add_option(pid, "PID", "PID", 'p', "PID");
|
|
|
|
|
args_parser.add_option(from, "From", "from", 'f', "From");
|
|
|
|
|
args_parser.add_positional_argument(tty_name, "TTY name", "tty");
|
2021-11-23 15:13:16 +01:00
|
|
|
args_parser.parse(arguments);
|
2020-09-06 16:10:27 +02:00
|
|
|
|
|
|
|
|
if (flag_create && flag_delete) {
|
2020-10-25 17:46:16 +01:00
|
|
|
warnln("-c and -d are mutually exclusive");
|
2020-09-06 16:10:27 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 15:09:40 +01:00
|
|
|
dbgln("Updating utmp from UID={} GID={} EGID={} PID={}", getuid(), getgid(), getegid(), pid);
|
2020-09-06 16:10:27 +02:00
|
|
|
|
2023-02-09 03:02:46 +01:00
|
|
|
auto file = TRY(Core::File::open("/var/run/utmp"sv, Core::File::OpenMode::ReadWrite));
|
2020-09-06 16:10:27 +02:00
|
|
|
|
2022-12-11 17:49:00 +01:00
|
|
|
auto file_contents = TRY(file->read_until_eof());
|
2021-11-23 15:13:16 +01:00
|
|
|
auto previous_json = TRY(JsonValue::from_string(file_contents));
|
2020-09-06 16:10:27 +02:00
|
|
|
|
|
|
|
|
JsonObject json;
|
|
|
|
|
|
2021-02-21 10:56:26 +01:00
|
|
|
if (!file_contents.is_empty()) {
|
2021-11-23 15:13:16 +01:00
|
|
|
if (!previous_json.is_object()) {
|
2021-02-21 10:56:26 +01:00
|
|
|
dbgln("Error: Could not parse JSON");
|
|
|
|
|
} else {
|
2021-11-23 15:13:16 +01:00
|
|
|
json = previous_json.as_object();
|
2021-02-21 10:56:26 +01:00
|
|
|
}
|
2020-09-06 16:10:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag_create) {
|
|
|
|
|
JsonObject entry;
|
|
|
|
|
entry.set("pid", pid);
|
|
|
|
|
entry.set("uid", getuid());
|
|
|
|
|
entry.set("from", from);
|
2020-09-06 19:07:51 +02:00
|
|
|
entry.set("login_at", time(nullptr));
|
2020-09-06 16:10:27 +02:00
|
|
|
json.set(tty_name, move(entry));
|
|
|
|
|
} else {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(flag_delete);
|
2021-01-09 15:09:40 +01:00
|
|
|
dbgln("Removing {} from utmp", tty_name);
|
2020-09-06 16:10:27 +02:00
|
|
|
json.remove(tty_name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 05:09:11 +01:00
|
|
|
TRY(file->seek(0, SeekMode::SetPosition));
|
2022-09-14 17:36:26 +01:00
|
|
|
TRY(file->truncate(0));
|
2023-03-01 17:24:50 +01:00
|
|
|
TRY(file->write_until_depleted(json.to_deprecated_string().bytes()));
|
2020-09-06 16:10:27 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|