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>
14 //usage:#define sysctl_trivial_usage
15 //usage: "[OPTIONS] [VALUE]..."
16 //usage:#define sysctl_full_usage "\n\n"
17 //usage: "Configure kernel parameters at runtime\n"
18 //usage: "\n -n Don't print key names"
19 //usage: "\n -e Don't warn about unknown keys"
20 //usage: "\n -w Change sysctl setting"
21 //usage: "\n -p FILE Load sysctl settings from FILE (default /etc/sysctl.conf)"
22 //usage: "\n -a Display all values"
23 //usage: "\n -A Display all values in table form"
25 //usage:#define sysctl_example_usage
26 //usage: "sysctl [-n] [-e] variable...\n"
27 //usage: "sysctl [-n] [-e] -w variable=value...\n"
28 //usage: "sysctl [-n] [-e] -a\n"
29 //usage: "sysctl [-n] [-e] -p file (default /etc/sysctl.conf)\n"
30 //usage: "sysctl [-n] [-e] -A\n"
35 FLAG_SHOW_KEYS
= 1 << 0,
36 FLAG_SHOW_KEY_ERRORS
= 1 << 1,
37 FLAG_TABLE_FORMAT
= 1 << 2, /* not implemented */
38 FLAG_SHOW_ALL
= 1 << 3,
39 FLAG_PRELOAD_FILE
= 1 << 4,
42 #define OPTION_STR "neAapw"
44 static void sysctl_dots_to_slashes(char *name
)
46 char *cptr
, *last_good
, *end
;
48 /* Convert minimum number of '.' to '/' so that
49 * we end up with existing file's name.
51 * Example from bug 3894:
52 * net.ipv4.conf.eth0.100.mc_forwarding ->
53 * net/ipv4/conf/eth0.100/mc_forwarding
54 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
55 * therefore we must start from the end, and if
56 * we replaced even one . -> /, start over again,
57 * but never replace dots before the position
58 * where last replacement occurred.
60 * Another bug we later had is that
61 * net.ipv4.conf.eth0.100
62 * (without .mc_forwarding) was mishandled.
64 * To set up testing: modprobe 8021q; vconfig add eth0 100
66 end
= name
+ strlen(name
);
68 *end
= '.'; /* trick the loop into trying full name too */
72 while (cptr
> last_good
) {
75 //bb_error_msg("trying:'%s'", name);
76 if (access(name
, F_OK
) == 0) {
78 //bb_error_msg("replaced:'%s'", name);
89 static int sysctl_act_on_setting(char *setting
)
91 int fd
, retval
= EXIT_SUCCESS
;
93 char *value
= value
; /* for compiler */
95 outname
= xstrdup(setting
);
104 if (option_mask32
& FLAG_WRITE
) {
105 cptr
= strchr(setting
, '=');
107 bb_error_msg("error: '%s' must be of the form name=value",
109 retval
= EXIT_FAILURE
;
112 value
= cptr
+ 1; /* point to the value in name=value */
113 if (setting
== cptr
|| !*value
) {
114 bb_error_msg("error: malformed setting '%s'", outname
);
115 retval
= EXIT_FAILURE
;
119 outname
[cptr
- setting
] = '\0';
120 /* procps 3.2.7 actually uses these flags */
121 fd
= open(setting
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
123 fd
= open(setting
, O_RDONLY
);
129 if (option_mask32
& FLAG_SHOW_KEY_ERRORS
)
130 bb_error_msg("error: '%s' is an unknown key", outname
);
133 bb_perror_msg("error %sing key '%s'",
134 option_mask32
& FLAG_WRITE
?
139 retval
= EXIT_FAILURE
;
143 if (option_mask32
& FLAG_WRITE
) {
144 //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
145 xwrite_str(fd
, value
);
147 if (option_mask32
& FLAG_SHOW_KEYS
)
148 printf("%s = ", outname
);
153 value
= cptr
= xmalloc_read(fd
, NULL
);
156 bb_perror_msg("error reading key '%s'", outname
);
160 /* dev.cdrom.info and sunrpc.transports, for example,
161 * are multi-line. Try "sysctl sunrpc.transports"
163 while ((c
= *cptr
) != '\0') {
164 if (option_mask32
& FLAG_SHOW_KEYS
)
165 printf("%s = ", outname
);
183 static int sysctl_act_recursive(const char *path
)
187 struct dirent
*entry
;
192 if (S_ISDIR(buf
.st_mode
) && !(option_mask32
& FLAG_WRITE
)) {
193 dirp
= opendir(path
);
196 while ((entry
= readdir(dirp
)) != NULL
) {
197 next
= concat_subpath_file(path
, entry
->d_name
);
199 continue; /* d_name is "." or ".." */
200 /* if path was ".", drop "./" prefix: */
201 retval
|= sysctl_act_recursive((next
[0] == '.' && next
[1] == '/') ?
207 char *name
= xstrdup(path
);
208 retval
|= sysctl_act_on_setting(name
);
215 /* Set sysctl's from a conf file. Format example:
216 * # Controls IP packet forwarding
217 * net.ipv4.ip_forward = 0
219 static int sysctl_handle_preload_file(const char *filename
)
224 parser
= config_open(filename
);
225 /* Must do it _after_ config_open(): */
227 /* xchroot("/proc/sys") - if you are paranoid */
229 //TODO: ';' is comment char too
230 //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
231 // (but _whitespace_ from ends should be trimmed first (and we do it right))
232 //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
233 // can it be fixed by removing PARSE_COLLAPSE bit?
234 while (config_read(parser
, token
, 2, 2, "# \t=", PARSE_NORMAL
)) {
236 sysctl_dots_to_slashes(token
[0]);
237 tp
= xasprintf("%s=%s", token
[0], token
[1]);
238 sysctl_act_recursive(tp
);
241 if (ENABLE_FEATURE_CLEAN_UP
)
242 config_close(parser
);
246 int sysctl_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
247 int sysctl_main(int argc UNUSED_PARAM
, char **argv
)
252 opt
= getopt32(argv
, "+" OPTION_STR
); /* '+' - stop on first non-option */
254 opt
^= (FLAG_SHOW_KEYS
| FLAG_SHOW_KEY_ERRORS
);
257 if (opt
& FLAG_PRELOAD_FILE
) {
258 option_mask32
|= FLAG_WRITE
;
259 /* xchdir("/proc/sys") is inside */
260 return sysctl_handle_preload_file(*argv
? *argv
: "/etc/sysctl.conf");
263 /* xchroot("/proc/sys") - if you are paranoid */
264 if (opt
& (FLAG_TABLE_FORMAT
| FLAG_SHOW_ALL
)) {
265 return sysctl_act_recursive(".");
270 sysctl_dots_to_slashes(*argv
);
271 retval
|= sysctl_act_recursive(*argv
);