2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-15 12:11:21 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2019-01-18 14:35:38 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
|
|
|
#include <LibCore/File.h>
|
2019-01-18 14:35:38 +00:00
|
|
|
|
2019-05-17 13:01:41 +00:00
|
|
|
static String read_var(const String& name)
|
2019-01-18 14:35:38 +00:00
|
|
|
{
|
2019-05-17 13:01:41 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("/proc/sys/");
|
|
|
|
builder.append(name);
|
|
|
|
auto path = builder.to_string();
|
2020-02-02 11:34:39 +00:00
|
|
|
auto f = Core::File::construct(path);
|
2021-05-12 09:26:43 +00:00
|
|
|
if (!f->open(Core::OpenMode::ReadOnly)) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Failed to open {}: {}", f->name(), f->error_string());
|
2019-05-17 13:01:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-09-21 18:50:06 +00:00
|
|
|
const auto& b = f->read_all();
|
|
|
|
if (f->error() < 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Failed to read: {}", f->error_string());
|
2019-05-17 13:01:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-09-30 06:57:01 +00:00
|
|
|
return String((const char*)b.data(), b.size(), Chomp);
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 13:01:41 +00:00
|
|
|
static void write_var(const String& name, const String& value)
|
2019-01-18 14:35:38 +00:00
|
|
|
{
|
2019-05-17 13:01:41 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("/proc/sys/");
|
|
|
|
builder.append(name);
|
|
|
|
auto path = builder.to_string();
|
2020-02-02 11:34:39 +00:00
|
|
|
auto f = Core::File::construct(path);
|
2021-05-12 09:26:43 +00:00
|
|
|
if (!f->open(Core::OpenMode::WriteOnly)) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Failed to open: {}", f->error_string());
|
2019-05-17 13:01:41 +00:00
|
|
|
exit(1);
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
2019-09-21 18:50:06 +00:00
|
|
|
f->write(value);
|
|
|
|
if (f->error() < 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Failed to write: {}", f->error_string());
|
2019-05-17 13:01:41 +00:00
|
|
|
exit(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
|
|
|
{
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::DirIterator di("/proc/sys", Core::DirIterator::SkipDots);
|
2019-05-27 07:26:54 +00:00
|
|
|
if (di.has_error()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("DirIterator: {}", di.error_string());
|
2019-01-18 14:35:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-27 07:26:54 +00:00
|
|
|
while (di.has_next()) {
|
2019-06-02 10:05:06 +00:00
|
|
|
String variable_name = di.next_path();
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{} = {}", variable_name, read_var(variable_name));
|
2019-01-18 14:35:38 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-17 13:01:41 +00:00
|
|
|
static int handle_var(const String& var)
|
2019-01-18 14:35:38 +00:00
|
|
|
{
|
2019-05-17 13:01:41 +00:00
|
|
|
String spec(var.characters(), Chomp);
|
2019-01-18 14:35:38 +00:00
|
|
|
auto parts = spec.split('=');
|
|
|
|
String variable_name = parts[0];
|
|
|
|
bool is_write = parts.size() > 1;
|
|
|
|
|
|
|
|
if (!is_write) {
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{} = {}", variable_name, read_var(variable_name));
|
2019-01-18 14:35:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
out("{} = {}", variable_name, read_var(variable_name));
|
2019-01-18 14:35:38 +00:00
|
|
|
write_var(variable_name, parts[1]);
|
2021-05-31 14:43:25 +00:00
|
|
|
outln(" -> {}", read_var(variable_name));
|
2019-01-18 14:35:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-17 13:01:41 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
bool show_all = false;
|
|
|
|
const char* var = nullptr;
|
2019-05-17 13:01:41 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help(
|
|
|
|
"Show or modify system-internal values. This requires root, and can crash your system.");
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(show_all, "Show all variables", nullptr, 'a');
|
2021-03-08 08:45:34 +00:00
|
|
|
args_parser.add_positional_argument(var, "variable[=value]", "variable", Core::ArgsParser::Required::No);
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.parse(argc, argv);
|
2019-05-17 13:01:41 +00:00
|
|
|
|
2020-05-04 00:26:17 +00:00
|
|
|
if (var == nullptr) {
|
|
|
|
// Not supplied; assume `-a`.
|
|
|
|
show_all = true;
|
|
|
|
}
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
if (show_all) {
|
2020-05-04 00:26:17 +00:00
|
|
|
// Ignore `var`, even if it was supplied. Just like the real procps does.
|
2019-05-17 13:01:41 +00:00
|
|
|
return handle_show_all();
|
|
|
|
}
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
return handle_var(var);
|
2019-05-17 13:01:41 +00:00
|
|
|
}
|