2020-09-06 18:44:16 +00:00
|
|
|
/*
|
2021-11-23 14:13:16 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
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
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2020-11-15 12:11:21 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-09-06 14:10:27 +00:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DateTime.h>
|
2020-09-06 18:44:16 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 14:13:16 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2020-09-06 14:10:27 +00:00
|
|
|
|
2021-11-23 14:13:16 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-09-06 14:10:27 +00:00
|
|
|
{
|
2021-11-27 22:26:34 +00:00
|
|
|
TRY(Core::System::pledge("stdio wpath cpath"));
|
2021-11-23 14:13:16 +00:00
|
|
|
TRY(Core::System::unveil("/var/run/utmp", "rwc"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-09-06 14:14:27 +00:00
|
|
|
|
2020-09-06 14:10:27 +00:00
|
|
|
pid_t pid = 0;
|
|
|
|
bool flag_create = false;
|
|
|
|
bool flag_delete = false;
|
|
|
|
const char* tty_name = nullptr;
|
|
|
|
const char* from = nullptr;
|
|
|
|
|
|
|
|
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 14:13:16 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-09-06 14:10:27 +00:00
|
|
|
|
|
|
|
if (flag_create && flag_delete) {
|
2020-10-25 16:46:16 +00:00
|
|
|
warnln("-c and -d are mutually exclusive");
|
2020-09-06 14:10:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Updating utmp from UID={} GID={} EGID={} PID={}", getuid(), getgid(), getegid(), pid);
|
2020-09-06 14:10:27 +00:00
|
|
|
|
2021-11-23 14:13:16 +00:00
|
|
|
auto file = TRY(Core::File::open("/var/run/utmp", Core::OpenMode::ReadWrite));
|
2020-09-06 14:10:27 +00:00
|
|
|
|
2021-11-23 14:13:16 +00:00
|
|
|
auto file_contents = file->read_all();
|
|
|
|
auto previous_json = TRY(JsonValue::from_string(file_contents));
|
2020-09-06 14:10:27 +00:00
|
|
|
|
|
|
|
JsonObject json;
|
|
|
|
|
2021-02-21 09:56:26 +00:00
|
|
|
if (!file_contents.is_empty()) {
|
2021-11-23 14:13:16 +00:00
|
|
|
if (!previous_json.is_object()) {
|
2021-02-21 09:56:26 +00:00
|
|
|
dbgln("Error: Could not parse JSON");
|
|
|
|
} else {
|
2021-11-23 14:13:16 +00:00
|
|
|
json = previous_json.as_object();
|
2021-02-21 09:56:26 +00:00
|
|
|
}
|
2020-09-06 14:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flag_create) {
|
|
|
|
JsonObject entry;
|
|
|
|
entry.set("pid", pid);
|
|
|
|
entry.set("uid", getuid());
|
|
|
|
entry.set("from", from);
|
2020-09-06 17:07:51 +00:00
|
|
|
entry.set("login_at", time(nullptr));
|
2020-09-06 14:10:27 +00:00
|
|
|
json.set(tty_name, move(entry));
|
|
|
|
} else {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(flag_delete);
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Removing {} from utmp", tty_name);
|
2020-09-06 14:10:27 +00:00
|
|
|
json.remove(tty_name);
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:13:16 +00:00
|
|
|
if (!file->seek(0)) {
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Seek failed");
|
2020-09-06 14:10:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:13:16 +00:00
|
|
|
if (!file->truncate(0)) {
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Truncation failed");
|
2020-09-06 14:10:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:13:16 +00:00
|
|
|
if (!file->write(json.to_string())) {
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Write failed");
|
2020-09-06 14:10:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|