aboutsummaryrefslogtreecommitdiff
path: root/procps/sysctl.c
diff options
context:
space:
mode:
Diffstat (limited to 'procps/sysctl.c')
-rw-r--r--procps/sysctl.c64
1 files changed, 42 insertions, 22 deletions
diff --git a/procps/sysctl.c b/procps/sysctl.c
index b17f5e896..827e09cce 100644
--- a/procps/sysctl.c
+++ b/procps/sysctl.c
@@ -16,22 +16,22 @@
16//config: help 16//config: help
17//config: Configure kernel parameters at runtime. 17//config: Configure kernel parameters at runtime.
18 18
19//applet:IF_BB_SYSCTL(APPLET(sysctl, BB_DIR_SBIN, BB_SUID_DROP)) 19//applet:IF_BB_SYSCTL(APPLET_NOEXEC(sysctl, sysctl, BB_DIR_SBIN, BB_SUID_DROP, sysctl))
20 20
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,23 +241,33 @@ 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 parse_flags = 0;
248//TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value 251 parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
249// (but _whitespace_ from ends should be trimmed first (and we do it right)) 252 parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
250//TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1") 253 parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
251// can it be fixed by removing PARSE_COLLAPSE bit? 254 parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
252 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) { 255 parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
256 parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#'
257 /* <space><tab><space>#comment is also comment, not strictly 1st char only */
258 parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before
259 while (config_read(parser, token, 2, 2, ";#=", parse_flags)) {
253 char *tp; 260 char *tp;
261
262 trim(token[1]);
263 tp = trim(token[0]);
254 sysctl_dots_to_slashes(token[0]); 264 sysctl_dots_to_slashes(token[0]);
255 tp = xasprintf("%s=%s", token[0], token[1]); 265 /* ^^^converted in-place. tp still points to NUL */
256 sysctl_act_recursive(tp); 266 /* now, add "=TOKEN1" */
257 free(tp); 267 *tp++ = '=';
268 overlapping_strcpy(tp, token[1]);
269
270 sysctl_act_on_setting(token[0]);
258 } 271 }
259 if (ENABLE_FEATURE_CLEAN_UP) 272 if (ENABLE_FEATURE_CLEAN_UP)
260 config_close(parser); 273 config_close(parser);
@@ -273,12 +286,19 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv)
273 option_mask32 = opt; 286 option_mask32 = opt;
274 287
275 if (opt & FLAG_PRELOAD_FILE) { 288 if (opt & FLAG_PRELOAD_FILE) {
289 int cur_dir_fd;
276 option_mask32 |= FLAG_WRITE; 290 option_mask32 |= FLAG_WRITE;
277 /* xchdir("/proc/sys") is inside */ 291 if (!*argv)
278 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf"); 292 *--argv = (char*)"/etc/sysctl.conf";
293 cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
294 do {
295 /* xchdir("/proc/sys") is inside */
296 sysctl_handle_preload_file(*argv);
297 xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
298 } while (*++argv);
299 return 0; /* procps-ng 3.3.10 does not flag parse errors */
279 } 300 }
280 xchdir("/proc/sys"); 301 xchdir("/proc/sys");
281 /* xchroot("/proc/sys") - if you are paranoid */
282 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) { 302 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
283 return sysctl_act_recursive("."); 303 return sysctl_act_recursive(".");
284 } 304 }