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