mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
utmpupdate: Port to LibMain :^)
This commit is contained in:
parent
adc83e5802
commit
03b6ff2bf0
2 changed files with 17 additions and 29 deletions
|
@ -100,6 +100,7 @@ target_link_libraries(test-pthread LibThreading)
|
||||||
target_link_libraries(truncate LibMain)
|
target_link_libraries(truncate LibMain)
|
||||||
target_link_libraries(tt LibPthread)
|
target_link_libraries(tt LibPthread)
|
||||||
target_link_libraries(unzip LibArchive LibCompress)
|
target_link_libraries(unzip LibArchive LibCompress)
|
||||||
|
target_link_libraries(utmpupdate LibMain)
|
||||||
target_link_libraries(zip LibArchive LibCompress LibCrypto)
|
target_link_libraries(zip LibArchive LibCompress LibCrypto)
|
||||||
target_link_libraries(cpp-lexer LibCpp)
|
target_link_libraries(cpp-lexer LibCpp)
|
||||||
target_link_libraries(cpp-parser LibCpp LibGUI)
|
target_link_libraries(cpp-parser LibCpp LibGUI)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -11,21 +11,15 @@
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/DateTime.h>
|
#include <LibCore/DateTime.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
if (pledge("stdio wpath cpath", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio wpath cpath", nullptr));
|
||||||
perror("pledge");
|
TRY(Core::System::unveil("/var/run/utmp", "rwc"));
|
||||||
return 1;
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
}
|
|
||||||
|
|
||||||
if (unveil("/var/run/utmp", "rwc") < 0) {
|
|
||||||
perror("unveil");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
unveil(nullptr, nullptr);
|
|
||||||
|
|
||||||
pid_t pid = 0;
|
pid_t pid = 0;
|
||||||
bool flag_create = false;
|
bool flag_create = false;
|
||||||
|
@ -39,8 +33,7 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_option(pid, "PID", "PID", 'p', "PID");
|
args_parser.add_option(pid, "PID", "PID", 'p', "PID");
|
||||||
args_parser.add_option(from, "From", "from", 'f', "From");
|
args_parser.add_option(from, "From", "from", 'f', "From");
|
||||||
args_parser.add_positional_argument(tty_name, "TTY name", "tty");
|
args_parser.add_positional_argument(tty_name, "TTY name", "tty");
|
||||||
|
args_parser.parse(arguments);
|
||||||
args_parser.parse(argc, argv);
|
|
||||||
|
|
||||||
if (flag_create && flag_delete) {
|
if (flag_create && flag_delete) {
|
||||||
warnln("-c and -d are mutually exclusive");
|
warnln("-c and -d are mutually exclusive");
|
||||||
|
@ -49,24 +42,18 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
dbgln("Updating utmp from UID={} GID={} EGID={} PID={}", getuid(), getgid(), getegid(), pid);
|
dbgln("Updating utmp from UID={} GID={} EGID={} PID={}", getuid(), getgid(), getegid(), pid);
|
||||||
|
|
||||||
auto file_or_error = Core::File::open("/var/run/utmp", Core::OpenMode::ReadWrite);
|
auto file = TRY(Core::File::open("/var/run/utmp", Core::OpenMode::ReadWrite));
|
||||||
if (file_or_error.is_error()) {
|
|
||||||
dbgln("Error: {}", file_or_error.error());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& file = *file_or_error.value();
|
auto file_contents = file->read_all();
|
||||||
|
auto previous_json = TRY(JsonValue::from_string(file_contents));
|
||||||
auto file_contents = file.read_all();
|
|
||||||
auto previous_json = JsonValue::from_string(file_contents);
|
|
||||||
|
|
||||||
JsonObject json;
|
JsonObject json;
|
||||||
|
|
||||||
if (!file_contents.is_empty()) {
|
if (!file_contents.is_empty()) {
|
||||||
if (previous_json.is_error() || !previous_json.value().is_object()) {
|
if (!previous_json.is_object()) {
|
||||||
dbgln("Error: Could not parse JSON");
|
dbgln("Error: Could not parse JSON");
|
||||||
} else {
|
} else {
|
||||||
json = previous_json.value().as_object();
|
json = previous_json.as_object();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,17 +70,17 @@ int main(int argc, char** argv)
|
||||||
json.remove(tty_name);
|
json.remove(tty_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file.seek(0)) {
|
if (!file->seek(0)) {
|
||||||
dbgln("Seek failed");
|
dbgln("Seek failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file.truncate(0)) {
|
if (!file->truncate(0)) {
|
||||||
dbgln("Truncation failed");
|
dbgln("Truncation failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file.write(json.to_string())) {
|
if (!file->write(json.to_string())) {
|
||||||
dbgln("Write failed");
|
dbgln("Write failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue