1 /* vi: set sw=4 ts=4: */
3 * Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
5 * Copyright 1999 George Staikos
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 * v1.01 - added -p <preload> to preload values from a file
11 * v1.01.1 - busybox applet aware by <solar@gentoo.org>
13 //config:config BB_SYSCTL
14 //config: bool "sysctl (6.9 kb)"
17 //config: Configure kernel parameters at runtime.
19 //applet:IF_BB_SYSCTL(APPLET(sysctl, BB_DIR_SBIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_BB_SYSCTL) += sysctl.o
23 //usage:#define sysctl_trivial_usage
24 //usage: "-p [-enq] [FILE...] / [-enqaw] [KEY[=VALUE]]..."
25 //usage:#define sysctl_full_usage "\n\n"
26 //usage: "Show/set kernel parameters\n"
27 //usage: "\n -p Set values from FILEs (default /etc/sysctl.conf)"
28 //usage: "\n -e Don't warn about unknown keys"
29 //usage: "\n -n Don't show key names"
30 //usage: "\n -q Quiet"
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"
36 //usage:#define sysctl_example_usage
37 //usage: "sysctl [-n] [-e] variable...\n"
38 //usage: "sysctl [-n] [-e] [-q] -w variable=value...\n"
39 //usage: "sysctl [-n] [-e] -a\n"
40 //usage: "sysctl [-n] [-e] [-q] -p file (default /etc/sysctl.conf)\n"
41 //usage: "sysctl [-n] [-e] -A\n"
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,
51 /* NB: procps 3.2.8 does not require -w for KEY=VAL to work, it only rejects non-KEY=VAL form */
55 #define OPTION_STR "neAapwq"
57 static void sysctl_dots_to_slashes(char *name)
59 char *cptr, *last_good, *end;
61 /* Convert minimum number of '.' to '/' so that
62 * we end up with existing file's name.
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.
73 * Another bug we later had is that
74 * net.ipv4.conf.eth0.100
75 * (without .mc_forwarding) was mishandled.
77 * To set up testing: modprobe 8021q; vconfig add eth0 100
79 end = name + strlen(name);
81 *end = '.'; /* trick the loop into trying full name too */
85 while (cptr > last_good) {
88 //bb_error_msg("trying:'%s'", name);
89 if (access(name, F_OK) == 0) {
91 //bb_error_msg("replaced:'%s'", name);
102 static int sysctl_act_on_setting(char *setting)
104 int fd, retval = EXIT_SUCCESS;
105 char *cptr, *outname;
106 char *value = value; /* for compiler */
107 bool writing = (option_mask32 & FLAG_WRITE);
109 outname = xstrdup(setting);
118 cptr = strchr(setting, '=');
123 bb_error_msg("error: '%s' must be of the form name=value",
125 retval = EXIT_FAILURE;
128 value = cptr + 1; /* point to the value in name=value */
129 if (setting == cptr || !*value) {
130 bb_error_msg("error: malformed setting '%s'", outname);
131 retval = EXIT_FAILURE;
135 outname[cptr - setting] = '\0';
136 /* procps 3.2.7 actually uses these flags */
137 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
139 fd = open(setting, O_RDONLY);
145 /* Happens for write-only settings, e.g. net.ipv6.route.flush */
148 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
149 bb_error_msg("error: '%s' is an unknown key", outname);
152 bb_perror_msg("error %sing key '%s'",
158 retval = EXIT_FAILURE;
163 //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
164 xwrite_str(fd, value);
166 if (!(option_mask32 & FLAG_QUIET)) {
167 if (option_mask32 & FLAG_SHOW_KEYS)
168 printf("%s = ", outname);
174 value = cptr = xmalloc_read(fd, NULL);
177 bb_perror_msg("error reading key '%s'", outname);
181 /* dev.cdrom.info and sunrpc.transports, for example,
182 * are multi-line. Try "sysctl sunrpc.transports"
184 while ((c = *cptr) != '\0') {
185 if (option_mask32 & FLAG_SHOW_KEYS)
186 printf("%s = ", outname);
204 static int sysctl_act_recursive(const char *path)
208 struct dirent *entry;
213 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
214 dirp = opendir(path);
217 while ((entry = readdir(dirp)) != NULL) {
218 next = concat_subpath_file(path, entry->d_name);
220 continue; /* d_name is "." or ".." */
221 /* if path was ".", drop "./" prefix: */
222 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
228 char *name = xstrdup(path);
229 retval |= sysctl_act_on_setting(name);
236 /* Set sysctl's from a conf file. Format example:
237 * # Controls IP packet forwarding
238 * net.ipv4.ip_forward = 0
240 static int sysctl_handle_preload_file(const char *filename)
246 parser = config_open(filename);
247 /* Must do it _after_ config_open(): */
250 //TODO: ';' is comment char too
251 //TODO: <space><tab><space>#comment is also comment, not strictly 1st char only
253 parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
254 parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
255 parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
256 parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
257 parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
258 while (config_read(parser, token, 2, 2, "#=", parse_flags)) {
263 sysctl_dots_to_slashes(token[0]);
264 /* ^^^converted in-place. tp still points to NUL */
265 /* now, add "=TOKEN1" */
267 overlapping_strcpy(tp, token[1]);
269 sysctl_act_on_setting(token[0]);
271 if (ENABLE_FEATURE_CLEAN_UP)
272 config_close(parser);
276 int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
277 int sysctl_main(int argc UNUSED_PARAM, char **argv)
282 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
284 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
287 if (opt & FLAG_PRELOAD_FILE) {
289 option_mask32 |= FLAG_WRITE;
291 *--argv = (char*)"/etc/sysctl.conf";
292 cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
294 /* xchdir("/proc/sys") is inside */
295 sysctl_handle_preload_file(*argv);
296 xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
298 return 0; /* procps-ng 3.3.10 does not flag parse errors */
301 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
302 return sysctl_act_recursive(".");
307 sysctl_dots_to_slashes(*argv);
308 retval |= sysctl_act_recursive(*argv);