diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-05 13:45:22 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-05 13:45:22 +0200 |
| commit | 9cf89cdf84fb20154088145980b676d2b28fc55d (patch) | |
| tree | 436a0d02edc588782ca00796e14469288c3baa8d | |
| parent | feb79e8742eb3cef211804dadcc7f3ddfd154c72 (diff) | |
| download | busybox-w32-9cf89cdf84fb20154088145980b676d2b28fc55d.tar.gz busybox-w32-9cf89cdf84fb20154088145980b676d2b28fc55d.tar.bz2 busybox-w32-9cf89cdf84fb20154088145980b676d2b28fc55d.zip | |
sysctl: fix file parsing, do not require -w for VAR=VAL
function old new delta
sysctl_act_on_setting - 451 +451
sysctl_main 222 282 +60
packed_usage 31744 31793 +49
config_read 604 639 +35
sysctl_act_recursive 612 163 -449
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/1 up/down: 595/-449) Total: 146 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | libbb/parse_config.c | 10 | ||||
| -rw-r--r-- | procps/sysctl.c | 51 | ||||
| -rwxr-xr-x | testsuite/mdev.tests | 4 | ||||
| -rwxr-xr-x | testsuite/parse.tests | 44 |
4 files changed, 77 insertions, 32 deletions
diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 307ae2cd2..da7482c6d 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c | |||
| @@ -201,10 +201,10 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
| 201 | /* Combine remaining arguments? */ | 201 | /* Combine remaining arguments? */ |
| 202 | if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) { | 202 | if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) { |
| 203 | /* Vanilla token, find next delimiter */ | 203 | /* Vanilla token, find next delimiter */ |
| 204 | line += strcspn(line, delims[0] ? delims : delims + 1); | 204 | line += strcspn(line, (delims[0] && (flags & PARSE_EOL_COMMENTS)) ? delims : delims + 1); |
| 205 | } else { | 205 | } else { |
| 206 | /* Combining, find comment char if any */ | 206 | /* Combining, find comment char if any */ |
| 207 | line = strchrnul(line, PARSE_EOL_COMMENTS ? delims[0] : '\0'); | 207 | line = strchrnul(line, (flags & PARSE_EOL_COMMENTS) ? delims[0] : '\0'); |
| 208 | 208 | ||
| 209 | /* Trim any extra delimiters from the end */ | 209 | /* Trim any extra delimiters from the end */ |
| 210 | if (flags & PARSE_TRIM) { | 210 | if (flags & PARSE_TRIM) { |
| @@ -214,10 +214,10 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | /* Token not terminated? */ | 216 | /* Token not terminated? */ |
| 217 | if (*line == delims[0]) | 217 | if ((flags & PARSE_EOL_COMMENTS) && *line == delims[0]) |
| 218 | *line = '\0'; | 218 | *line = '\0'; /* ends with comment char: this line is done */ |
| 219 | else if (*line != '\0') | 219 | else if (*line != '\0') |
| 220 | *line++ = '\0'; | 220 | *line++ = '\0'; /* token is done, continue parsing line */ |
| 221 | 221 | ||
| 222 | #if 0 /* unused so far */ | 222 | #if 0 /* unused so far */ |
| 223 | if (flags & PARSE_ESCAPE) { | 223 | if (flags & PARSE_ESCAPE) { |
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 | } |
diff --git a/testsuite/mdev.tests b/testsuite/mdev.tests index 8515aff31..8e53ec564 100755 --- a/testsuite/mdev.tests +++ b/testsuite/mdev.tests | |||
| @@ -168,7 +168,7 @@ SKIP= | |||
| 168 | # continuing to use directory structure from prev test | 168 | # continuing to use directory structure from prev test |
| 169 | rm -rf mdev.testdir/dev/* | 169 | rm -rf mdev.testdir/dev/* |
| 170 | echo "sda 0:0 644 @echo @echo TEST" >mdev.testdir/etc/mdev.conf | 170 | echo "sda 0:0 644 @echo @echo TEST" >mdev.testdir/etc/mdev.conf |
| 171 | optional STATIC FEATURE_MDEV_CONF FEATURE_MDEV_EXEC FEATURE_LS_RECURSIVE FEATURE_LS_TIMESTAMPS FEATURE_LS_USERNAME FEATURE_SH_IS_ASH ASH_ECHO | 171 | optional STATIC FEATURE_MDEV_CONF FEATURE_MDEV_EXEC FEATURE_LS_RECURSIVE FEATURE_LS_TIMESTAMPS FEATURE_LS_USERNAME SH_IS_ASH ASH_ECHO |
| 172 | testing "mdev command" \ | 172 | testing "mdev command" \ |
| 173 | "env - PATH=$PATH ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1; | 173 | "env - PATH=$PATH ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1; |
| 174 | ls -lnR mdev.testdir/dev | $FILTER_LS" \ | 174 | ls -lnR mdev.testdir/dev | $FILTER_LS" \ |
| @@ -183,7 +183,7 @@ SKIP= | |||
| 183 | # continuing to use directory structure from prev test | 183 | # continuing to use directory structure from prev test |
| 184 | rm -rf mdev.testdir/dev/* | 184 | rm -rf mdev.testdir/dev/* |
| 185 | echo "sda 0:0 644 =block/ @echo @echo TEST:\$MDEV" >mdev.testdir/etc/mdev.conf | 185 | echo "sda 0:0 644 =block/ @echo @echo TEST:\$MDEV" >mdev.testdir/etc/mdev.conf |
| 186 | optional STATIC FEATURE_MDEV_CONF FEATURE_MDEV_RENAME FEATURE_MDEV_EXEC FEATURE_LS_RECURSIVE FEATURE_LS_TIMESTAMPS FEATURE_LS_USERNAME FEATURE_SH_IS_ASH | 186 | optional STATIC FEATURE_MDEV_CONF FEATURE_MDEV_RENAME FEATURE_MDEV_EXEC FEATURE_LS_RECURSIVE FEATURE_LS_TIMESTAMPS FEATURE_LS_USERNAME SH_IS_ASH |
| 187 | testing "mdev move and command" \ | 187 | testing "mdev move and command" \ |
| 188 | "env - PATH=$PATH ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1; | 188 | "env - PATH=$PATH ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1; |
| 189 | ls -lnR mdev.testdir/dev | $FILTER_LS2" \ | 189 | ls -lnR mdev.testdir/dev | $FILTER_LS2" \ |
diff --git a/testsuite/parse.tests b/testsuite/parse.tests index 904e1a17a..2cbed6f31 100755 --- a/testsuite/parse.tests +++ b/testsuite/parse.tests | |||
| @@ -5,13 +5,13 @@ | |||
| 5 | 5 | ||
| 6 | . ./testing.sh | 6 | . ./testing.sh |
| 7 | 7 | ||
| 8 | COLLAPSE=$(( 0x00010000)) | 8 | COLLAPSE=$(( 0x00010000)) |
| 9 | TRIM=$(( 0x00020000)) | 9 | TRIM=$(( 0x00020000)) |
| 10 | GREEDY=$(( 0x00040000)) | 10 | GREEDY=$(( 0x00040000)) |
| 11 | MIN_DIE=$(( 0x00100000)) | 11 | MIN_DIE=$(( 0x00100000)) |
| 12 | KEEP_COPY=$((0x00200000)) | 12 | KEEP_COPY=$(( 0x00200000)) |
| 13 | ESCAPE=$(( 0x00400000)) | 13 | EOL_COMMENTS=$((0x00400000)) |
| 14 | NORMAL=$(( COLLAPSE | TRIM | GREEDY)) | 14 | NORMAL=$(( COLLAPSE | TRIM | GREEDY | EOL_COMMENTS)) |
| 15 | 15 | ||
| 16 | # testing "description" "command" "result" "infile" "stdin" | 16 | # testing "description" "command" "result" "infile" "stdin" |
| 17 | 17 | ||
| @@ -27,6 +27,34 @@ testing "parse notrim" \ | |||
| 27 | "-" \ | 27 | "-" \ |
| 28 | " sda 0:0 644 @echo @echo TEST \n" | 28 | " sda 0:0 644 @echo @echo TEST \n" |
| 29 | 29 | ||
| 30 | testing "parse comments" \ | ||
| 31 | "parse -n 4 -m 3 -f $((NORMAL - EOL_COMMENTS)) -" \ | ||
| 32 | "[sda][0:0][644][@echo @echo TEST #this is not eaten]\n" \ | ||
| 33 | "-" \ | ||
| 34 | "\ | ||
| 35 | # sda 0:0 644 @echo @echo TEST - this gets eaten | ||
| 36 | sda 0:0 644 @echo @echo TEST #this is not eaten | ||
| 37 | " | ||
| 38 | |||
| 39 | testing "parse bad comment" \ | ||
| 40 | "parse -n 2 -m 2 -d '#=' -f $((GREEDY)) - 2>&1" \ | ||
| 41 | "\ | ||
| 42 | [var][val] | ||
| 43 | parse: bad line 3: 1 tokens found, 2 needed | ||
| 44 | [ #this][ok] | ||
| 45 | [ #this][=ok] | ||
| 46 | [ #this][=ok=ok=ok=] | ||
| 47 | " \ | ||
| 48 | "-" \ | ||
| 49 | "\ | ||
| 50 | # this gets eaten | ||
| 51 | var=val | ||
| 52 | #this causes error msg | ||
| 53 | #this=ok | ||
| 54 | #this==ok | ||
| 55 | #this==ok=ok=ok= | ||
| 56 | " | ||
| 57 | |||
| 30 | FILE=__parse | 58 | FILE=__parse |
| 31 | cat >$FILE <<EOF | 59 | cat >$FILE <<EOF |
| 32 | # | 60 | # |
| @@ -96,6 +124,8 @@ cat >$FILE.res <<EOF | |||
| 96 | [option][dns][129.219.13.81] | 124 | [option][dns][129.219.13.81] |
| 97 | [option][domain][local] | 125 | [option][domain][local] |
| 98 | [option][lease][864000] | 126 | [option][lease][864000] |
| 127 | [option][msstaticroutes][10.0.0.0/8][10.127.0.1] | ||
| 128 | [option][staticroutes][10.0.0.0/8][10.127.0.1,][10.11.12.0/24][10.11.12.1] | ||
| 99 | [option][0x08][01020304] | 129 | [option][0x08][01020304] |
| 100 | EOF | 130 | EOF |
| 101 | 131 | ||
