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