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.
11 * - added -p <preload> to preload values from a file
13 * - busybox applet aware by <solar@gentoo.org>
19 static int sysctl_act_on_setting(char *setting);
20 static int sysctl_display_all(const char *path);
21 static int sysctl_handle_preload_file(const char *filename);
22 static void sysctl_dots_to_slashes(char *name);
25 FLAG_SHOW_KEYS = 1 << 0,
26 FLAG_SHOW_KEY_ERRORS = 1 << 1,
27 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
28 FLAG_SHOW_ALL = 1 << 3,
29 FLAG_PRELOAD_FILE = 1 << 4,
33 int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34 int sysctl_main(int argc UNUSED_PARAM, char **argv)
39 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
41 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
44 if (opt & FLAG_PRELOAD_FILE) {
45 option_mask32 |= FLAG_WRITE;
46 /* xchdir("/proc/sys") is inside */
47 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
50 /* xchroot(".") - if you are paranoid */
51 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
52 return sysctl_display_all(".");
57 sysctl_dots_to_slashes(*argv);
58 retval |= sysctl_display_all(*argv);
65 /* Set sysctl's from a conf file. Format example:
66 * # Controls IP packet forwarding
67 * net.ipv4.ip_forward = 0
69 static int sysctl_handle_preload_file(const char *filename)
74 parser = config_open(filename);
75 /* Must do it _after_ config_open(): */
77 /* xchroot(".") - if you are paranoid */
79 // TODO: ';' is comment char too
80 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
81 /* Save ~4 bytes by using parser internals */
82 /* parser->line is big enough for sprintf */
83 sprintf(parser->line, "%s=%s", token[0], token[1]);
84 sysctl_dots_to_slashes(parser->line);
85 sysctl_display_all(parser->line);
87 if (ENABLE_FEATURE_CLEAN_UP)
92 static int sysctl_act_on_setting(char *setting)
94 int fd, retval = EXIT_SUCCESS;
96 char *value = value; /* for compiler */
98 outname = xstrdup(setting);
107 if (option_mask32 & FLAG_WRITE) {
108 cptr = strchr(setting, '=');
110 bb_error_msg("error: '%s' must be of the form name=value",
112 retval = EXIT_FAILURE;
115 value = cptr + 1; /* point to the value in name=value */
116 if (setting == cptr || !*value) {
117 bb_error_msg("error: malformed setting '%s'", outname);
118 retval = EXIT_FAILURE;
122 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
124 fd = open(setting, O_RDONLY);
130 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
131 bb_error_msg("error: '%s' is an unknown key", outname);
134 bb_perror_msg("error %sing key '%s'",
135 option_mask32 & FLAG_WRITE ?
140 retval = EXIT_FAILURE;
144 if (option_mask32 & FLAG_WRITE) {
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_display_all(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(
198 path, entry->d_name);
200 continue; /* d_name is "." or ".." */
201 /* if path was ".", drop "./" prefix: */
202 retval |= sysctl_display_all((next[0] == '.' && next[1] == '/') ?
208 char *name = xstrdup(path);
209 retval |= sysctl_act_on_setting(name);
216 static void sysctl_dots_to_slashes(char *name)
218 char *cptr, *last_good, *end;
220 /* Convert minimum number of '.' to '/' so that
221 * we end up with existing file's name.
223 * Example from bug 3894:
224 * net.ipv4.conf.eth0.100.mc_forwarding ->
225 * net/ipv4/conf/eth0.100/mc_forwarding
226 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
227 * therefore we must start from the end, and if
228 * we replaced even one . -> /, start over again,
229 * but never replace dots before the position
230 * where last replacement occurred.
232 * Another bug we later had is that
233 * net.ipv4.conf.eth0.100
234 * (without .mc_forwarding) was mishandled.
236 * To set up testing: modprobe 8021q; vconfig add eth0 100
238 end = name + strlen(name);
239 last_good = name - 1;
240 *end = '.'; /* trick the loop into trying full name too */
244 while (cptr > last_good) {
247 //bb_error_msg("trying:'%s'", name);
248 if (access(name, F_OK) == 0) {
249 if (cptr != end) /* prevent trailing '/' */
251 //bb_error_msg("replaced:'%s'", name);