2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-25 02:03:21 +00:00
|
|
|
* Copyright (c) 2022, Alex Major
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-01-25 02:03:21 +00:00
|
|
|
#include <LibMain/Main.h>
|
2019-01-18 14:35:38 +00:00
|
|
|
|
2021-10-16 22:18:38 +00:00
|
|
|
static bool s_set_variable = false;
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString get_variable(StringView name)
|
2019-01-18 14:35:38 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
auto path = DeprecatedString::formatted("/sys/kernel/variables/{}", name);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = Core::File::open(path, Core::File::OpenMode::Read);
|
2022-09-14 16:24:17 +00:00
|
|
|
if (file.is_error()) {
|
|
|
|
warnln("Failed to open {}: {}", path, file.error());
|
2021-10-16 22:18:38 +00:00
|
|
|
return {};
|
2019-05-17 13:01:41 +00:00
|
|
|
}
|
2022-12-11 16:49:00 +00:00
|
|
|
auto buffer = file.value()->read_until_eof();
|
2022-09-14 16:24:17 +00:00
|
|
|
if (buffer.is_error()) {
|
|
|
|
warnln("Failed to read {}: {}", path, buffer.error());
|
2021-10-16 22:18:38 +00:00
|
|
|
return {};
|
2019-05-17 13:01:41 +00:00
|
|
|
}
|
2022-09-14 16:24:17 +00:00
|
|
|
return { (char const*)buffer.value().data(), buffer.value().size(), Chomp };
|
2021-10-16 22:18:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
static bool read_variable(StringView name)
|
2021-10-16 22:18:38 +00:00
|
|
|
{
|
|
|
|
auto value = get_variable(name);
|
|
|
|
if (value.is_null())
|
|
|
|
return false;
|
|
|
|
outln("{} = {}", name, value);
|
|
|
|
return true;
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
static bool write_variable(StringView name, StringView value)
|
2019-01-18 14:35:38 +00:00
|
|
|
{
|
2021-10-16 22:18:38 +00:00
|
|
|
auto old_value = get_variable(name);
|
|
|
|
if (old_value.is_null())
|
|
|
|
return false;
|
2022-12-04 18:02:33 +00:00
|
|
|
auto path = DeprecatedString::formatted("/sys/kernel/variables/{}", name);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = Core::File::open(path, Core::File::OpenMode::Write);
|
2022-09-14 16:24:17 +00:00
|
|
|
if (file.is_error()) {
|
|
|
|
warnln("Failed to open {}: {}", path, file.error());
|
2021-10-16 22:18:38 +00:00
|
|
|
return false;
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
2022-09-14 16:24:17 +00:00
|
|
|
if (auto result = file.value()->write(value.bytes()); result.is_error()) {
|
|
|
|
warnln("Failed to write {}: {}", path, result.error());
|
2021-10-16 22:18:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
outln("{}: {} -> {}", name, old_value, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-26 21:32:37 +00:00
|
|
|
static int handle_variables(Vector<StringView> const& variables)
|
2021-10-16 22:18:38 +00:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
for (auto const& variable : variables) {
|
|
|
|
auto maybe_index = variable.find('=');
|
|
|
|
if (!maybe_index.has_value()) {
|
|
|
|
success = read_variable(variable);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto equal_index = maybe_index.release_value();
|
|
|
|
auto name = variable.substring_view(0, equal_index);
|
|
|
|
auto value = variable.substring_view(equal_index + 1, variable.length() - equal_index - 1);
|
|
|
|
if (name.is_empty())
|
|
|
|
warnln("Malformed setting '{}'", variable);
|
|
|
|
else if (!s_set_variable)
|
|
|
|
warnln("Must specify '-w' to set variables");
|
|
|
|
else
|
|
|
|
success = write_variable(name, value);
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
2021-10-16 22:18:38 +00:00
|
|
|
return success ? 0 : 1;
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 13:01:41 +00:00
|
|
|
static int handle_show_all()
|
2019-01-18 14:35:38 +00:00
|
|
|
{
|
2022-10-14 14:36:52 +00:00
|
|
|
Core::DirIterator di("/sys/kernel/variables", Core::DirIterator::SkipDots);
|
2019-05-27 07:26:54 +00:00
|
|
|
if (di.has_error()) {
|
2023-03-01 15:55:15 +00:00
|
|
|
outln("DirIterator: {}", di.error());
|
2019-01-18 14:35:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:18:38 +00:00
|
|
|
bool success = false;
|
2019-05-27 07:26:54 +00:00
|
|
|
while (di.has_next()) {
|
2021-10-16 22:18:38 +00:00
|
|
|
auto name = di.next_path();
|
|
|
|
success = read_variable(name);
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
2021-10-16 22:18:38 +00:00
|
|
|
return success ? 0 : 1;
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
2019-05-17 13:01:41 +00:00
|
|
|
|
2022-01-25 02:03:21 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-05-17 13:01:41 +00:00
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
bool show_all = false;
|
2021-11-26 21:32:37 +00:00
|
|
|
Vector<StringView> variables;
|
2019-05-17 13:01:41 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2021-10-16 22:18:38 +00:00
|
|
|
args_parser.set_general_help("Show or modify system-internal values. This requires root, and can crash your system.");
|
|
|
|
args_parser.add_option(show_all, "Show all variables", "all", 'a');
|
|
|
|
args_parser.add_option(s_set_variable, "Set variables", "write", 'w');
|
|
|
|
args_parser.add_positional_argument(variables, "variable[=value]", "variables", Core::ArgsParser::Required::No);
|
2022-01-25 02:03:21 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-05-17 13:01:41 +00:00
|
|
|
|
2021-10-16 22:18:38 +00:00
|
|
|
if (!show_all && variables.is_empty()) {
|
2023-02-21 11:44:41 +00:00
|
|
|
args_parser.print_usage(stdout, arguments.strings[0]);
|
2021-10-16 22:18:38 +00:00
|
|
|
return 1;
|
2020-05-04 00:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
if (show_all) {
|
2021-10-16 22:18:38 +00:00
|
|
|
// Ignore `variables`, even if they are supplied. Just like the real procps does.
|
2019-05-17 13:01:41 +00:00
|
|
|
return handle_show_all();
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:18:38 +00:00
|
|
|
return handle_variables(variables);
|
2019-05-17 13:01:41 +00:00
|
|
|
}
|