Denis Vlasenko | 9299305 | 2008-10-15 09:44:37 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Sysctl 1.01 - A utility to read and manipulate the sysctl parameters |
| 4 | * |
Rob Landley | 8b1f11d | 2006-04-17 21:49:34 +0000 | [diff] [blame] | 5 | * Copyright 1999 George Staikos |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 8 | * |
| 9 | * Changelog: |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 10 | * v1.01 - added -p <preload> to preload values from a file |
| 11 | * v1.01.1 - busybox applet aware by <solar@gentoo.org> |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 12 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 13 | //config:config BB_SYSCTL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 14 | //config: bool "sysctl (7.4 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 15 | //config: default y |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 17 | //config: Configure kernel parameters at runtime. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 18 | |
Denys Vlasenko | caf26b3 | 2017-08-05 18:23:10 +0200 | [diff] [blame] | 19 | //applet:IF_BB_SYSCTL(APPLET_NOEXEC(sysctl, sysctl, BB_DIR_SBIN, BB_SUID_DROP, sysctl)) |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 20 | |
| 21 | //kbuild:lib-$(CONFIG_BB_SYSCTL) += sysctl.o |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 22 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define sysctl_trivial_usage |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 24 | //usage: "-p [-enq] [FILE...] / [-enqaw] [KEY[=VALUE]]..." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define sysctl_full_usage "\n\n" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 26 | //usage: "Show/set kernel parameters\n" |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 27 | //usage: "\n -p Set values from FILEs (default /etc/sysctl.conf)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage: "\n -e Don't warn about unknown keys" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 29 | //usage: "\n -n Don't show key names" |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 30 | //usage: "\n -q Quiet" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 31 | //usage: "\n -a Show all values" |
| 32 | /* Same as -a, no need to show it */ |
| 33 | /* //usage: "\n -A Show all values in table form" */ |
| 34 | //usage: "\n -w Set values" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 35 | //usage: |
| 36 | //usage:#define sysctl_example_usage |
| 37 | //usage: "sysctl [-n] [-e] variable...\n" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 38 | //usage: "sysctl [-n] [-e] [-q] -w variable=value...\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 39 | //usage: "sysctl [-n] [-e] -a\n" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 40 | //usage: "sysctl [-n] [-e] [-q] -p file (default /etc/sysctl.conf)\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 41 | //usage: "sysctl [-n] [-e] -A\n" |
| 42 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 45 | enum { |
| 46 | FLAG_SHOW_KEYS = 1 << 0, |
| 47 | FLAG_SHOW_KEY_ERRORS = 1 << 1, |
| 48 | FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */ |
| 49 | FLAG_SHOW_ALL = 1 << 3, |
| 50 | FLAG_PRELOAD_FILE = 1 << 4, |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 51 | /* NB: procps 3.2.8 does not require -w for KEY=VAL to work, it only rejects non-KEY=VAL form */ |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 52 | FLAG_WRITE = 1 << 5, |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 53 | FLAG_QUIET = 1 << 6, |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 54 | }; |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 55 | #define OPTION_STR "neAapwq" |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 57 | static void sysctl_dots_to_slashes(char *name) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 58 | { |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 59 | char *cptr, *last_good, *end; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 61 | /* Convert minimum number of '.' to '/' so that |
| 62 | * we end up with existing file's name. |
| 63 | * |
| 64 | * Example from bug 3894: |
| 65 | * net.ipv4.conf.eth0.100.mc_forwarding -> |
| 66 | * net/ipv4/conf/eth0.100/mc_forwarding |
| 67 | * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*, |
| 68 | * therefore we must start from the end, and if |
| 69 | * we replaced even one . -> /, start over again, |
| 70 | * but never replace dots before the position |
| 71 | * where last replacement occurred. |
| 72 | * |
| 73 | * Another bug we later had is that |
| 74 | * net.ipv4.conf.eth0.100 |
| 75 | * (without .mc_forwarding) was mishandled. |
| 76 | * |
| 77 | * To set up testing: modprobe 8021q; vconfig add eth0 100 |
| 78 | */ |
| 79 | end = name + strlen(name); |
| 80 | last_good = name - 1; |
| 81 | *end = '.'; /* trick the loop into trying full name too */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 83 | again: |
| 84 | cptr = end; |
| 85 | while (cptr > last_good) { |
| 86 | if (*cptr == '.') { |
| 87 | *cptr = '\0'; |
| 88 | //bb_error_msg("trying:'%s'", name); |
| 89 | if (access(name, F_OK) == 0) { |
Denys Vlasenko | c2fdd41 | 2010-03-27 05:02:00 +0100 | [diff] [blame] | 90 | *cptr = '/'; |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 91 | //bb_error_msg("replaced:'%s'", name); |
| 92 | last_good = cptr; |
| 93 | goto again; |
| 94 | } |
| 95 | *cptr = '.'; |
| 96 | } |
| 97 | cptr--; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 98 | } |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 99 | *end = '\0'; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 100 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 102 | static int sysctl_act_on_setting(char *setting) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 103 | { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 104 | int fd, retval = EXIT_SUCCESS; |
Denis Vlasenko | d6e8f94 | 2008-12-29 01:03:17 +0000 | [diff] [blame] | 105 | char *cptr, *outname; |
| 106 | char *value = value; /* for compiler */ |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 107 | bool writing = (option_mask32 & FLAG_WRITE); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 109 | outname = xstrdup(setting); |
| 110 | |
| 111 | cptr = outname; |
| 112 | while (*cptr) { |
| 113 | if (*cptr == '/') |
| 114 | *cptr = '.'; |
| 115 | cptr++; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 118 | cptr = strchr(setting, '='); |
| 119 | if (cptr) |
| 120 | writing = 1; |
| 121 | if (writing) { |
Denys Vlasenko | 317498f | 2019-02-08 14:02:59 +0100 | [diff] [blame] | 122 | if (!cptr) { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 123 | bb_error_msg("error: '%s' must be of the form name=value", |
| 124 | outname); |
| 125 | retval = EXIT_FAILURE; |
| 126 | goto end; |
| 127 | } |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 128 | value = cptr + 1; /* point to the value in name=value */ |
Denys Vlasenko | 317498f | 2019-02-08 14:02:59 +0100 | [diff] [blame] | 129 | if (setting == cptr /* "name" can't be empty */ |
| 130 | /* || !*value - WRONG: "sysctl net.ipv4.ip_local_reserved_ports=" is a valid syntax (clears the value) */ |
| 131 | ) { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 132 | bb_error_msg("error: malformed setting '%s'", outname); |
| 133 | retval = EXIT_FAILURE; |
| 134 | goto end; |
| 135 | } |
| 136 | *cptr = '\0'; |
Denis Vlasenko | 5a6617a | 2009-03-29 02:22:19 +0000 | [diff] [blame] | 137 | outname[cptr - setting] = '\0'; |
| 138 | /* procps 3.2.7 actually uses these flags */ |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 139 | fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 140 | } else { |
| 141 | fd = open(setting, O_RDONLY); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 144 | if (fd < 0) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 145 | switch (errno) { |
Denys Vlasenko | 6554d03 | 2014-02-24 17:28:43 +0100 | [diff] [blame] | 146 | case EACCES: |
| 147 | /* Happens for write-only settings, e.g. net.ipv6.route.flush */ |
| 148 | goto end; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 149 | case ENOENT: |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 150 | if (option_mask32 & FLAG_SHOW_KEY_ERRORS) |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 151 | bb_error_msg("error: '%s' is an unknown key", outname); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 152 | break; |
| 153 | default: |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 154 | bb_perror_msg("error %sing key '%s'", |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 155 | writing ? |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 156 | "sett" : "read", |
| 157 | outname); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 158 | break; |
| 159 | } |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 160 | retval = EXIT_FAILURE; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 161 | goto end; |
| 162 | } |
| 163 | |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 164 | if (writing) { |
Denis Vlasenko | 5a6617a | 2009-03-29 02:22:19 +0000 | [diff] [blame] | 165 | //TODO: procps 3.2.7 writes "value\n", note trailing "\n" |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 166 | xwrite_str(fd, value); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 167 | close(fd); |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 168 | if (!(option_mask32 & FLAG_QUIET)) { |
| 169 | if (option_mask32 & FLAG_SHOW_KEYS) |
| 170 | printf("%s = ", outname); |
| 171 | puts(value); |
| 172 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 173 | } else { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 174 | char c; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 176 | value = cptr = xmalloc_read(fd, NULL); |
| 177 | close(fd); |
| 178 | if (value == NULL) { |
| 179 | bb_perror_msg("error reading key '%s'", outname); |
Denys Vlasenko | 1422ba6 | 2019-02-08 14:59:06 +0100 | [diff] [blame^] | 180 | retval = EXIT_FAILURE; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | /* dev.cdrom.info and sunrpc.transports, for example, |
| 185 | * are multi-line. Try "sysctl sunrpc.transports" |
| 186 | */ |
| 187 | while ((c = *cptr) != '\0') { |
| 188 | if (option_mask32 & FLAG_SHOW_KEYS) |
| 189 | printf("%s = ", outname); |
| 190 | while (1) { |
| 191 | fputc(c, stdout); |
| 192 | cptr++; |
| 193 | if (c == '\n') |
| 194 | break; |
| 195 | c = *cptr; |
| 196 | if (c == '\0') |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | free(value); |
| 201 | } |
| 202 | end: |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 203 | free(outname); |
| 204 | return retval; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 205 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 206 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 207 | static int sysctl_act_recursive(const char *path) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 208 | { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 209 | struct stat buf; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 210 | int retval = 0; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 211 | |
Denys Vlasenko | 679c30e | 2019-02-08 14:27:21 +0100 | [diff] [blame] | 212 | if (!(option_mask32 & FLAG_WRITE) |
| 213 | && stat(path, &buf) == 0 |
| 214 | && S_ISDIR(buf.st_mode) |
| 215 | ) { |
| 216 | struct dirent *entry; |
| 217 | DIR *dirp; |
| 218 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 219 | dirp = opendir(path); |
| 220 | if (dirp == NULL) |
| 221 | return -1; |
| 222 | while ((entry = readdir(dirp)) != NULL) { |
Denys Vlasenko | 679c30e | 2019-02-08 14:27:21 +0100 | [diff] [blame] | 223 | char *next = concat_subpath_file(path, entry->d_name); |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 224 | if (next == NULL) |
| 225 | continue; /* d_name is "." or ".." */ |
| 226 | /* if path was ".", drop "./" prefix: */ |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 227 | retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ? |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 228 | next + 2 : next); |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 229 | free(next); |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame] | 230 | } |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 231 | closedir(dirp); |
| 232 | } else { |
| 233 | char *name = xstrdup(path); |
| 234 | retval |= sysctl_act_on_setting(name); |
| 235 | free(name); |
| 236 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 237 | |
| 238 | return retval; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 239 | } |
Denis Vlasenko | 58cc52a | 2008-10-15 08:22:55 +0000 | [diff] [blame] | 240 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 241 | /* Set sysctl's from a conf file. Format example: |
| 242 | * # Controls IP packet forwarding |
| 243 | * net.ipv4.ip_forward = 0 |
| 244 | */ |
| 245 | static int sysctl_handle_preload_file(const char *filename) |
Denis Vlasenko | 58cc52a | 2008-10-15 08:22:55 +0000 | [diff] [blame] | 246 | { |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 247 | char *token[2]; |
| 248 | parser_t *parser; |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 249 | int parse_flags; |
Denis Vlasenko | a9c3f7a | 2008-10-15 13:50:24 +0000 | [diff] [blame] | 250 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 251 | parser = config_open(filename); |
| 252 | /* Must do it _after_ config_open(): */ |
| 253 | xchdir("/proc/sys"); |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 254 | |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 255 | parse_flags = 0; |
| 256 | parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one |
| 257 | parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters |
| 258 | parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line |
| 259 | parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found |
| 260 | parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char |
Denys Vlasenko | 50db1f2 | 2017-08-05 18:20:34 +0200 | [diff] [blame] | 261 | parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#' |
| 262 | /* <space><tab><space>#comment is also comment, not strictly 1st char only */ |
| 263 | parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before |
| 264 | while (config_read(parser, token, 2, 2, ";#=", parse_flags)) { |
Denys Vlasenko | f427c80 | 2009-05-10 23:41:29 +0200 | [diff] [blame] | 265 | char *tp; |
Denys Vlasenko | 20077c1 | 2017-08-05 17:50:35 +0200 | [diff] [blame] | 266 | |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 267 | trim(token[1]); |
Denys Vlasenko | 20077c1 | 2017-08-05 17:50:35 +0200 | [diff] [blame] | 268 | tp = trim(token[0]); |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 269 | sysctl_dots_to_slashes(token[0]); |
Denys Vlasenko | 20077c1 | 2017-08-05 17:50:35 +0200 | [diff] [blame] | 270 | /* ^^^converted in-place. tp still points to NUL */ |
| 271 | /* now, add "=TOKEN1" */ |
| 272 | *tp++ = '='; |
| 273 | overlapping_strcpy(tp, token[1]); |
| 274 | |
| 275 | sysctl_act_on_setting(token[0]); |
Denis Vlasenko | 58cc52a | 2008-10-15 08:22:55 +0000 | [diff] [blame] | 276 | } |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 277 | if (ENABLE_FEATURE_CLEAN_UP) |
| 278 | config_close(parser); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 283 | int sysctl_main(int argc UNUSED_PARAM, char **argv) |
| 284 | { |
| 285 | int retval; |
| 286 | int opt; |
| 287 | |
| 288 | opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */ |
| 289 | argv += optind; |
| 290 | opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS); |
| 291 | option_mask32 = opt; |
| 292 | |
| 293 | if (opt & FLAG_PRELOAD_FILE) { |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 294 | int cur_dir_fd; |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 295 | option_mask32 |= FLAG_WRITE; |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 296 | if (!*argv) |
| 297 | *--argv = (char*)"/etc/sysctl.conf"; |
| 298 | cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY); |
| 299 | do { |
| 300 | /* xchdir("/proc/sys") is inside */ |
| 301 | sysctl_handle_preload_file(*argv); |
| 302 | xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */ |
| 303 | } while (*++argv); |
| 304 | return 0; /* procps-ng 3.3.10 does not flag parse errors */ |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 305 | } |
| 306 | xchdir("/proc/sys"); |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 307 | if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) { |
| 308 | return sysctl_act_recursive("."); |
| 309 | } |
| 310 | |
Denys Vlasenko | 679c30e | 2019-02-08 14:27:21 +0100 | [diff] [blame] | 311 | //TODO: if(!argv[0]) bb_show_usage() ? |
| 312 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 313 | retval = 0; |
| 314 | while (*argv) { |
| 315 | sysctl_dots_to_slashes(*argv); |
| 316 | retval |= sysctl_act_recursive(*argv); |
| 317 | argv++; |
| 318 | } |
| 319 | |
| 320 | return retval; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 321 | } |