-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysctl.cpp
More file actions
189 lines (169 loc) · 5.81 KB
/
sysctl.cpp
File metadata and controls
189 lines (169 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <cstdio>
#include <cstring>
#include <string>
#include <string_view>
#include <filesystem>
#include <cfbox/applet.hpp>
#include <cfbox/help.hpp>
#include <cfbox/args.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "sysctl",
.version = CFBOX_VERSION_STRING,
.one_line = "configure kernel parameters at runtime",
.usage = "sysctl [-a] [-n] [-w KEY=VALUE] [-p FILE] [KEY]",
.options = " -a display all values\n"
" -n show only values (no keys)\n"
" -w set a value\n"
" -p load values from file",
.extra = "",
};
auto key_to_path(std::string_view key) -> std::string {
std::string path = "/proc/sys/";
for (auto c : key) {
if (c == '.') path += '/';
else path += c;
}
return path;
}
auto path_to_key(std::string_view path) -> std::string {
// Strip /proc/sys/ prefix and convert / to .
const char* prefix = "/proc/sys/";
if (path.size() > 10 && path.substr(0, 10) == prefix)
path = path.substr(10);
std::string key;
for (auto c : path) {
if (c == '/') key += '.';
else key += c;
}
return key;
}
auto read_sysctl_value(const std::string& path) -> std::string {
auto* f = std::fopen(path.c_str(), "r");
if (!f) return {};
char buf[4096];
if (!std::fgets(buf, sizeof(buf), f)) {
std::fclose(f);
return {};
}
std::fclose(f);
auto len = std::strlen(buf);
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) {
buf[--len] = '\0';
}
return buf;
}
auto write_sysctl_value(const std::string& path, std::string_view value) -> bool {
auto* f = std::fopen(path.c_str(), "w");
if (!f) return false;
std::fprintf(f, "%.*s\n", static_cast<int>(value.size()), value.data());
return std::fclose(f) == 0;
}
auto show_key(std::string_view key, bool no_name) -> bool {
auto path = key_to_path(key);
auto val = read_sysctl_value(path);
if (val.empty()) return false;
if (no_name) std::printf("%s\n", val.c_str());
else std::printf("%.*s = %s\n", static_cast<int>(key.size()), key.data(), val.c_str());
return true;
}
auto show_all(bool no_name) -> void {
std::error_code ec;
for (const auto& entry : std::filesystem::recursive_directory_iterator("/proc/sys", ec)) {
if (!entry.is_regular_file()) continue;
auto key = path_to_key(entry.path().string());
auto val = read_sysctl_value(entry.path().string());
if (val.empty()) continue;
if (no_name) std::printf("%s\n", val.c_str());
else std::printf("%s = %s\n", key.c_str(), val.c_str());
}
}
auto load_file(const std::string& filepath, bool no_name) -> int {
auto* f = std::fopen(filepath.c_str(), "r");
if (!f) {
CFBOX_ERR("sysctl", "cannot open %s", filepath.c_str());
return 1;
}
int errors = 0;
char line[4096];
while (std::fgets(line, sizeof(line), f)) {
auto len = std::strlen(line);
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
line[--len] = '\0';
}
// Skip comments and empty lines
if (len == 0 || line[0] == '#' || line[0] == ';') continue;
auto* eq = std::strchr(line, '=');
if (!eq) continue;
*eq = '\0';
std::string key(line);
std::string val(eq + 1);
// Trim whitespace
while (!key.empty() && (key.back() == ' ' || key.back() == '\t')) key.pop_back();
while (!val.empty() && (val.front() == ' ' || val.front() == '\t')) val.erase(val.begin());
auto path = key_to_path(key);
if (!write_sysctl_value(path, val)) {
CFBOX_ERR("sysctl", "cannot set %s", key.c_str());
++errors;
} else if (!no_name) {
std::printf("%s = %s\n", key.c_str(), val.c_str());
}
}
std::fclose(f);
return errors > 0 ? 1 : 0;
}
} // anonymous namespace
auto sysctl_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'a', false, "all"},
cfbox::args::OptSpec{'n', false},
cfbox::args::OptSpec{'w', false},
cfbox::args::OptSpec{'e', false},
cfbox::args::OptSpec{'p', true, "load"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool no_name = parsed.has('n');
bool do_write = parsed.has('w');
if (parsed.has('a') || parsed.has_long("all")) {
show_all(no_name);
return 0;
}
if (auto file = parsed.get_any('p', "load")) {
return load_file(std::string(*file), no_name);
}
const auto& pos = parsed.positional();
if (pos.empty()) {
// Default: show all (like sysctl without args on some systems)
show_all(no_name);
return 0;
}
int errors = 0;
for (const auto& arg : pos) {
auto s = std::string(arg);
if (do_write) {
auto eq = s.find('=');
if (eq == std::string::npos) {
CFBOX_ERR("sysctl", "invalid setting: %s", s.c_str());
++errors;
continue;
}
auto key = s.substr(0, eq);
auto val = s.substr(eq + 1);
auto path = key_to_path(key);
if (!write_sysctl_value(path, val)) {
CFBOX_ERR("sysctl", "cannot set %s", key.c_str());
++errors;
} else if (!no_name) {
std::printf("%s = %s\n", key.c_str(), val.c_str());
}
} else {
if (!show_key(s, no_name)) {
CFBOX_ERR("sysctl", "cannot stat %s", s.c_str());
++errors;
}
}
}
return errors > 0 ? 1 : 0;
}