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