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 tarball for details.
10 * v1.01 - added -p <preload> to preload values from a file
11 * v1.01.1 - busybox applet aware by <solar@gentoo.org>
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,
24 #define OPTION_STR "neAapw"
26 static void sysctl_dots_to_slashes(char *name)
28 char *cptr, *last_good, *end;
30 /* Convert minimum number of '.' to '/' so that
31 * we end up with existing file's name.
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.
42 * Another bug we later had is that
43 * net.ipv4.conf.eth0.100
44 * (without .mc_forwarding) was mishandled.
46 * To set up testing: modprobe 8021q; vconfig add eth0 100
48 end = name + strlen(name);
50 *end = '.'; /* trick the loop into trying full name too */
54 while (cptr > last_good) {
57 //bb_error_msg("trying:'%s'", name);
58 if (access(name, F_OK) == 0) {
59 if (cptr != end) /* prevent trailing '/' */
61 //bb_error_msg("replaced:'%s'", name);
72 static int sysctl_act_on_setting(char *setting)
74 int fd, retval = EXIT_SUCCESS;
76 char *value = value; /* for compiler */
78 outname = xstrdup(setting);
87 if (option_mask32 & FLAG_WRITE) {
88 cptr = strchr(setting, '=');
90 bb_error_msg("error: '%s' must be of the form name=value",
92 retval = EXIT_FAILURE;
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;
102 outname[cptr - setting] = '\0';
103 /* procps 3.2.7 actually uses these flags */
104 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
106 fd = open(setting, O_RDONLY);
112 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
113 bb_error_msg("error: '%s' is an unknown key", outname);
116 bb_perror_msg("error %sing key '%s'",
117 option_mask32 & FLAG_WRITE ?
122 retval = EXIT_FAILURE;
126 if (option_mask32 & FLAG_WRITE) {
127 //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
128 xwrite_str(fd, value);
130 if (option_mask32 & FLAG_SHOW_KEYS)
131 printf("%s = ", outname);
136 value = cptr = xmalloc_read(fd, NULL);
139 bb_perror_msg("error reading key '%s'", outname);
143 /* dev.cdrom.info and sunrpc.transports, for example,
144 * are multi-line. Try "sysctl sunrpc.transports"
146 while ((c = *cptr) != '\0') {
147 if (option_mask32 & FLAG_SHOW_KEYS)
148 printf("%s = ", outname);
166 static int sysctl_act_recursive(const char *path)
170 struct dirent *entry;
175 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
176 dirp = opendir(path);
179 while ((entry = readdir(dirp)) != NULL) {
180 next = concat_subpath_file(path, entry->d_name);
182 continue; /* d_name is "." or ".." */
183 /* if path was ".", drop "./" prefix: */
184 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
190 char *name = xstrdup(path);
191 retval |= sysctl_act_on_setting(name);
198 /* Set sysctl's from a conf file. Format example:
199 * # Controls IP packet forwarding
200 * net.ipv4.ip_forward = 0
202 static int sysctl_handle_preload_file(const char *filename)
207 parser = config_open(filename);
208 /* Must do it _after_ config_open(): */
210 /* xchroot(".") - if you are paranoid */
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)) {
218 sysctl_dots_to_slashes(token[0]);
219 tp = xasprintf("%s=%s", token[0], token[1]);
220 sysctl_act_recursive(tp);
223 if (ENABLE_FEATURE_CLEAN_UP)
224 config_close(parser);
228 int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
229 int sysctl_main(int argc UNUSED_PARAM, char **argv)
234 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
236 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
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");
245 /* xchroot(".") - if you are paranoid */
246 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
247 return sysctl_act_recursive(".");
252 sysctl_dots_to_slashes(*argv);
253 retval |= sysctl_act_recursive(*argv);