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 | */ |
| 13 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | //usage:#define sysctl_trivial_usage |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 15 | //usage: "[OPTIONS] [KEY[=VALUE]]..." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 16 | //usage:#define sysctl_full_usage "\n\n" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 17 | //usage: "Show/set kernel parameters\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 18 | //usage: "\n -e Don't warn about unknown keys" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 19 | //usage: "\n -n Don't show key names" |
| 20 | //usage: "\n -a Show all values" |
| 21 | /* Same as -a, no need to show it */ |
| 22 | /* //usage: "\n -A Show all values in table form" */ |
| 23 | //usage: "\n -w Set values" |
| 24 | //usage: "\n -p FILE Set values from FILE (default /etc/sysctl.conf)" |
| 25 | //usage: "\n -q Set values silently" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 26 | //usage: |
| 27 | //usage:#define sysctl_example_usage |
| 28 | //usage: "sysctl [-n] [-e] variable...\n" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 29 | //usage: "sysctl [-n] [-e] [-q] -w variable=value...\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage: "sysctl [-n] [-e] -a\n" |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 31 | //usage: "sysctl [-n] [-e] [-q] -p file (default /etc/sysctl.conf)\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | //usage: "sysctl [-n] [-e] -A\n" |
| 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 36 | enum { |
| 37 | FLAG_SHOW_KEYS = 1 << 0, |
| 38 | FLAG_SHOW_KEY_ERRORS = 1 << 1, |
| 39 | FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */ |
| 40 | FLAG_SHOW_ALL = 1 << 3, |
| 41 | FLAG_PRELOAD_FILE = 1 << 4, |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 42 | /* TODO: procps 3.2.8 seems to not require -w for KEY=VAL to work: */ |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 43 | FLAG_WRITE = 1 << 5, |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 44 | FLAG_QUIET = 1 << 6, |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 45 | }; |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 46 | #define OPTION_STR "neAapwq" |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 48 | static void sysctl_dots_to_slashes(char *name) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 49 | { |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 50 | char *cptr, *last_good, *end; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 52 | /* Convert minimum number of '.' to '/' so that |
| 53 | * we end up with existing file's name. |
| 54 | * |
| 55 | * Example from bug 3894: |
| 56 | * net.ipv4.conf.eth0.100.mc_forwarding -> |
| 57 | * net/ipv4/conf/eth0.100/mc_forwarding |
| 58 | * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*, |
| 59 | * therefore we must start from the end, and if |
| 60 | * we replaced even one . -> /, start over again, |
| 61 | * but never replace dots before the position |
| 62 | * where last replacement occurred. |
| 63 | * |
| 64 | * Another bug we later had is that |
| 65 | * net.ipv4.conf.eth0.100 |
| 66 | * (without .mc_forwarding) was mishandled. |
| 67 | * |
| 68 | * To set up testing: modprobe 8021q; vconfig add eth0 100 |
| 69 | */ |
| 70 | end = name + strlen(name); |
| 71 | last_good = name - 1; |
| 72 | *end = '.'; /* trick the loop into trying full name too */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 74 | again: |
| 75 | cptr = end; |
| 76 | while (cptr > last_good) { |
| 77 | if (*cptr == '.') { |
| 78 | *cptr = '\0'; |
| 79 | //bb_error_msg("trying:'%s'", name); |
| 80 | if (access(name, F_OK) == 0) { |
Denys Vlasenko | c2fdd41 | 2010-03-27 05:02:00 +0100 | [diff] [blame] | 81 | *cptr = '/'; |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 82 | //bb_error_msg("replaced:'%s'", name); |
| 83 | last_good = cptr; |
| 84 | goto again; |
| 85 | } |
| 86 | *cptr = '.'; |
| 87 | } |
| 88 | cptr--; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 89 | } |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 90 | *end = '\0'; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 91 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 93 | static int sysctl_act_on_setting(char *setting) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 94 | { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 95 | int fd, retval = EXIT_SUCCESS; |
Denis Vlasenko | d6e8f94 | 2008-12-29 01:03:17 +0000 | [diff] [blame] | 96 | char *cptr, *outname; |
| 97 | char *value = value; /* for compiler */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 99 | outname = xstrdup(setting); |
| 100 | |
| 101 | cptr = outname; |
| 102 | while (*cptr) { |
| 103 | if (*cptr == '/') |
| 104 | *cptr = '.'; |
| 105 | cptr++; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 108 | if (option_mask32 & FLAG_WRITE) { |
| 109 | cptr = strchr(setting, '='); |
| 110 | if (cptr == NULL) { |
| 111 | bb_error_msg("error: '%s' must be of the form name=value", |
| 112 | outname); |
| 113 | retval = EXIT_FAILURE; |
| 114 | goto end; |
| 115 | } |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 116 | value = cptr + 1; /* point to the value in name=value */ |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 117 | if (setting == cptr || !*value) { |
| 118 | bb_error_msg("error: malformed setting '%s'", outname); |
| 119 | retval = EXIT_FAILURE; |
| 120 | goto end; |
| 121 | } |
| 122 | *cptr = '\0'; |
Denis Vlasenko | 5a6617a | 2009-03-29 02:22:19 +0000 | [diff] [blame] | 123 | outname[cptr - setting] = '\0'; |
| 124 | /* procps 3.2.7 actually uses these flags */ |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 125 | fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 126 | } else { |
| 127 | fd = open(setting, O_RDONLY); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 130 | if (fd < 0) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 131 | switch (errno) { |
| 132 | case ENOENT: |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 133 | if (option_mask32 & FLAG_SHOW_KEY_ERRORS) |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 134 | bb_error_msg("error: '%s' is an unknown key", outname); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 135 | break; |
| 136 | default: |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 137 | bb_perror_msg("error %sing key '%s'", |
| 138 | option_mask32 & FLAG_WRITE ? |
| 139 | "sett" : "read", |
| 140 | outname); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 141 | break; |
| 142 | } |
Denis Vlasenko | 64309f8 | 2007-11-29 06:40:28 +0000 | [diff] [blame] | 143 | retval = EXIT_FAILURE; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | if (option_mask32 & FLAG_WRITE) { |
Denis Vlasenko | 5a6617a | 2009-03-29 02:22:19 +0000 | [diff] [blame] | 148 | //TODO: procps 3.2.7 writes "value\n", note trailing "\n" |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 149 | xwrite_str(fd, value); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 150 | close(fd); |
Joshua Kahlenberg | c439851 | 2012-09-05 18:15:12 +0200 | [diff] [blame] | 151 | if (!(option_mask32 & FLAG_QUIET)) { |
| 152 | if (option_mask32 & FLAG_SHOW_KEYS) |
| 153 | printf("%s = ", outname); |
| 154 | puts(value); |
| 155 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 156 | } else { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 157 | char c; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 159 | value = cptr = xmalloc_read(fd, NULL); |
| 160 | close(fd); |
| 161 | if (value == NULL) { |
| 162 | bb_perror_msg("error reading key '%s'", outname); |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | /* dev.cdrom.info and sunrpc.transports, for example, |
| 167 | * are multi-line. Try "sysctl sunrpc.transports" |
| 168 | */ |
| 169 | while ((c = *cptr) != '\0') { |
| 170 | if (option_mask32 & FLAG_SHOW_KEYS) |
| 171 | printf("%s = ", outname); |
| 172 | while (1) { |
| 173 | fputc(c, stdout); |
| 174 | cptr++; |
| 175 | if (c == '\n') |
| 176 | break; |
| 177 | c = *cptr; |
| 178 | if (c == '\0') |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | free(value); |
| 183 | } |
| 184 | end: |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 185 | free(outname); |
| 186 | return retval; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 187 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 189 | static int sysctl_act_recursive(const char *path) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 190 | { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 191 | DIR *dirp; |
| 192 | struct stat buf; |
| 193 | struct dirent *entry; |
| 194 | char *next; |
| 195 | int retval = 0; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 197 | stat(path, &buf); |
| 198 | if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) { |
| 199 | dirp = opendir(path); |
| 200 | if (dirp == NULL) |
| 201 | return -1; |
| 202 | while ((entry = readdir(dirp)) != NULL) { |
Denis Vlasenko | 5a6617a | 2009-03-29 02:22:19 +0000 | [diff] [blame] | 203 | next = concat_subpath_file(path, entry->d_name); |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 204 | if (next == NULL) |
| 205 | continue; /* d_name is "." or ".." */ |
| 206 | /* if path was ".", drop "./" prefix: */ |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 207 | retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ? |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame^] | 208 | next + 2 : next); |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 209 | free(next); |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame] | 210 | } |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 211 | closedir(dirp); |
| 212 | } else { |
| 213 | char *name = xstrdup(path); |
| 214 | retval |= sysctl_act_on_setting(name); |
| 215 | free(name); |
| 216 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 217 | |
| 218 | return retval; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 219 | } |
Denis Vlasenko | 58cc52a | 2008-10-15 08:22:55 +0000 | [diff] [blame] | 220 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 221 | /* Set sysctl's from a conf file. Format example: |
| 222 | * # Controls IP packet forwarding |
| 223 | * net.ipv4.ip_forward = 0 |
| 224 | */ |
| 225 | static int sysctl_handle_preload_file(const char *filename) |
Denis Vlasenko | 58cc52a | 2008-10-15 08:22:55 +0000 | [diff] [blame] | 226 | { |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 227 | char *token[2]; |
| 228 | parser_t *parser; |
Denis Vlasenko | a9c3f7a | 2008-10-15 13:50:24 +0000 | [diff] [blame] | 229 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 230 | parser = config_open(filename); |
| 231 | /* Must do it _after_ config_open(): */ |
| 232 | xchdir("/proc/sys"); |
Denys Vlasenko | 0687a5b | 2012-03-08 00:28:24 +0100 | [diff] [blame] | 233 | /* xchroot("/proc/sys") - if you are paranoid */ |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 234 | |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 235 | //TODO: ';' is comment char too |
| 236 | //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value |
| 237 | // (but _whitespace_ from ends should be trimmed first (and we do it right)) |
| 238 | //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1") |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 239 | // can it be fixed by removing PARSE_COLLAPSE bit? |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 240 | while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) { |
Denys Vlasenko | f427c80 | 2009-05-10 23:41:29 +0200 | [diff] [blame] | 241 | char *tp; |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 242 | sysctl_dots_to_slashes(token[0]); |
Denys Vlasenko | f427c80 | 2009-05-10 23:41:29 +0200 | [diff] [blame] | 243 | tp = xasprintf("%s=%s", token[0], token[1]); |
| 244 | sysctl_act_recursive(tp); |
| 245 | free(tp); |
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 | if (ENABLE_FEATURE_CLEAN_UP) |
| 248 | config_close(parser); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 253 | int sysctl_main(int argc UNUSED_PARAM, char **argv) |
| 254 | { |
| 255 | int retval; |
| 256 | int opt; |
| 257 | |
| 258 | opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */ |
| 259 | argv += optind; |
| 260 | opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS); |
| 261 | option_mask32 = opt; |
| 262 | |
| 263 | if (opt & FLAG_PRELOAD_FILE) { |
| 264 | option_mask32 |= FLAG_WRITE; |
| 265 | /* xchdir("/proc/sys") is inside */ |
| 266 | return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf"); |
| 267 | } |
| 268 | xchdir("/proc/sys"); |
Denys Vlasenko | 0687a5b | 2012-03-08 00:28:24 +0100 | [diff] [blame] | 269 | /* xchroot("/proc/sys") - if you are paranoid */ |
Denis Vlasenko | 038fe44 | 2009-03-29 02:23:16 +0000 | [diff] [blame] | 270 | if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) { |
| 271 | return sysctl_act_recursive("."); |
| 272 | } |
| 273 | |
| 274 | retval = 0; |
| 275 | while (*argv) { |
| 276 | sysctl_dots_to_slashes(*argv); |
| 277 | retval |= sysctl_act_recursive(*argv); |
| 278 | argv++; |
| 279 | } |
| 280 | |
| 281 | return retval; |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 282 | } |