aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/sysctl.c51
1 files changed, 33 insertions, 18 deletions
diff --git a/procps/sysctl.c b/procps/sysctl.c
index b17f5e896..619f4f1e4 100644
--- a/procps/sysctl.c
+++ b/procps/sysctl.c
@@ -21,17 +21,17 @@
21//kbuild:lib-$(CONFIG_BB_SYSCTL) += sysctl.o 21//kbuild:lib-$(CONFIG_BB_SYSCTL) += sysctl.o
22 22
23//usage:#define sysctl_trivial_usage 23//usage:#define sysctl_trivial_usage
24//usage: "[OPTIONS] [KEY[=VALUE]]..." 24//usage: "-p [-enq] [FILE...] / [-enqaw] [KEY[=VALUE]]..."
25//usage:#define sysctl_full_usage "\n\n" 25//usage:#define sysctl_full_usage "\n\n"
26//usage: "Show/set kernel parameters\n" 26//usage: "Show/set kernel parameters\n"
27//usage: "\n -p Set values from FILEs (default /etc/sysctl.conf)"
27//usage: "\n -e Don't warn about unknown keys" 28//usage: "\n -e Don't warn about unknown keys"
28//usage: "\n -n Don't show key names" 29//usage: "\n -n Don't show key names"
30//usage: "\n -q Quiet"
29//usage: "\n -a Show all values" 31//usage: "\n -a Show all values"
30/* Same as -a, no need to show it */ 32/* Same as -a, no need to show it */
31/* //usage: "\n -A Show all values in table form" */ 33/* //usage: "\n -A Show all values in table form" */
32//usage: "\n -w Set values" 34//usage: "\n -w Set values"
33//usage: "\n -p FILE Set values from FILE (default /etc/sysctl.conf)"
34//usage: "\n -q Set values silently"
35//usage: 35//usage:
36//usage:#define sysctl_example_usage 36//usage:#define sysctl_example_usage
37//usage: "sysctl [-n] [-e] variable...\n" 37//usage: "sysctl [-n] [-e] variable...\n"
@@ -48,7 +48,7 @@ enum {
48 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */ 48 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
49 FLAG_SHOW_ALL = 1 << 3, 49 FLAG_SHOW_ALL = 1 << 3,
50 FLAG_PRELOAD_FILE = 1 << 4, 50 FLAG_PRELOAD_FILE = 1 << 4,
51/* TODO: procps 3.2.8 seems to not require -w for KEY=VAL to work: */ 51 /* NB: procps 3.2.8 does not require -w for KEY=VAL to work, it only rejects non-KEY=VAL form */
52 FLAG_WRITE = 1 << 5, 52 FLAG_WRITE = 1 << 5,
53 FLAG_QUIET = 1 << 6, 53 FLAG_QUIET = 1 << 6,
54}; 54};
@@ -104,6 +104,7 @@ static int sysctl_act_on_setting(char *setting)
104 int fd, retval = EXIT_SUCCESS; 104 int fd, retval = EXIT_SUCCESS;
105 char *cptr, *outname; 105 char *cptr, *outname;
106 char *value = value; /* for compiler */ 106 char *value = value; /* for compiler */
107 bool writing = (option_mask32 & FLAG_WRITE);
107 108
108 outname = xstrdup(setting); 109 outname = xstrdup(setting);
109 110
@@ -114,8 +115,10 @@ static int sysctl_act_on_setting(char *setting)
114 cptr++; 115 cptr++;
115 } 116 }
116 117
117 if (option_mask32 & FLAG_WRITE) { 118 cptr = strchr(setting, '=');
118 cptr = strchr(setting, '='); 119 if (cptr)
120 writing = 1;
121 if (writing) {
119 if (cptr == NULL) { 122 if (cptr == NULL) {
120 bb_error_msg("error: '%s' must be of the form name=value", 123 bb_error_msg("error: '%s' must be of the form name=value",
121 outname); 124 outname);
@@ -147,7 +150,7 @@ static int sysctl_act_on_setting(char *setting)
147 break; 150 break;
148 default: 151 default:
149 bb_perror_msg("error %sing key '%s'", 152 bb_perror_msg("error %sing key '%s'",
150 option_mask32 & FLAG_WRITE ? 153 writing ?
151 "sett" : "read", 154 "sett" : "read",
152 outname); 155 outname);
153 break; 156 break;
@@ -156,7 +159,7 @@ static int sysctl_act_on_setting(char *setting)
156 goto end; 159 goto end;
157 } 160 }
158 161
159 if (option_mask32 & FLAG_WRITE) { 162 if (writing) {
160//TODO: procps 3.2.7 writes "value\n", note trailing "\n" 163//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
161 xwrite_str(fd, value); 164 xwrite_str(fd, value);
162 close(fd); 165 close(fd);
@@ -238,22 +241,27 @@ static int sysctl_handle_preload_file(const char *filename)
238{ 241{
239 char *token[2]; 242 char *token[2];
240 parser_t *parser; 243 parser_t *parser;
244 int parse_flags;
241 245
242 parser = config_open(filename); 246 parser = config_open(filename);
243 /* Must do it _after_ config_open(): */ 247 /* Must do it _after_ config_open(): */
244 xchdir("/proc/sys"); 248 xchdir("/proc/sys");
245 /* xchroot("/proc/sys") - if you are paranoid */
246 249
247//TODO: ';' is comment char too 250//TODO: ';' is comment char too
248//TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value 251//TODO: <space><tab><space>#comment is also comment, not strictly 1st char only
249// (but _whitespace_ from ends should be trimmed first (and we do it right)) 252 parse_flags = 0;
250//TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1") 253 parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
251// can it be fixed by removing PARSE_COLLAPSE bit? 254 parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
252 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) { 255 parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
256 parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
257 parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
258 while (config_read(parser, token, 2, 2, "#=", parse_flags)) {
253 char *tp; 259 char *tp;
260 trim(token[0]);
261 trim(token[1]);
254 sysctl_dots_to_slashes(token[0]); 262 sysctl_dots_to_slashes(token[0]);
255 tp = xasprintf("%s=%s", token[0], token[1]); 263 tp = xasprintf("%s=%s", token[0], token[1]);
256 sysctl_act_recursive(tp); 264 sysctl_act_on_setting(tp);
257 free(tp); 265 free(tp);
258 } 266 }
259 if (ENABLE_FEATURE_CLEAN_UP) 267 if (ENABLE_FEATURE_CLEAN_UP)
@@ -273,12 +281,19 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv)
273 option_mask32 = opt; 281 option_mask32 = opt;
274 282
275 if (opt & FLAG_PRELOAD_FILE) { 283 if (opt & FLAG_PRELOAD_FILE) {
284 int cur_dir_fd;
276 option_mask32 |= FLAG_WRITE; 285 option_mask32 |= FLAG_WRITE;
277 /* xchdir("/proc/sys") is inside */ 286 if (!*argv)
278 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf"); 287 *--argv = (char*)"/etc/sysctl.conf";
288 cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
289 do {
290 /* xchdir("/proc/sys") is inside */
291 sysctl_handle_preload_file(*argv);
292 xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
293 } while (*++argv);
294 return 0; /* procps-ng 3.3.10 does not flag parse errors */
279 } 295 }
280 xchdir("/proc/sys"); 296 xchdir("/proc/sys");
281 /* xchroot("/proc/sys") - if you are paranoid */
282 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) { 297 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
283 return sysctl_act_recursive("."); 298 return sysctl_act_recursive(".");
284 } 299 }