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);
24 static void dwrite_str(int fd, const char *buf)
26 write(fd, buf, strlen(buf));
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,
38 int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
39 int sysctl_main(int argc UNUSED_PARAM, char **argv)
44 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
46 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
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");
55 /* xchroot(".") - if you are paranoid */
56 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
57 return sysctl_display_all(".");
62 sysctl_dots_to_slashes(*argv);
63 retval |= sysctl_display_all(*argv);
70 /* Set sysctl's from a conf file. Format example:
71 * # Controls IP packet forwarding
72 * net.ipv4.ip_forward = 0
74 static int sysctl_handle_preload_file(const char *filename)
79 parser = config_open(filename);
80 /* Must do it _after_ config_open(): */
82 /* xchroot(".") - if you are paranoid */
84 // TODO: ';' is comment char too
85 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
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);
92 if (ENABLE_FEATURE_CLEAN_UP)
97 static int sysctl_act_on_setting(char *setting)
99 int fd, retval = EXIT_SUCCESS;
100 char *cptr, *outname, *value;
102 outname = xstrdup(setting);
111 if (option_mask32 & FLAG_WRITE) {
112 cptr = strchr(setting, '=');
114 bb_error_msg("error: '%s' must be of the form name=value",
116 retval = EXIT_FAILURE;
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;
126 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
128 fd = open(setting, O_RDONLY);
134 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
135 bb_error_msg("error: '%s' is an unknown key", outname);
138 bb_perror_msg("error %sing key '%s'",
139 option_mask32 & FLAG_WRITE ?
144 retval = EXIT_FAILURE;
148 if (option_mask32 & FLAG_WRITE) {
149 dwrite_str(fd, value);
151 if (option_mask32 & FLAG_SHOW_KEYS)
152 printf("%s = ", outname);
157 value = cptr = xmalloc_read(fd, NULL);
160 bb_perror_msg("error reading key '%s'", outname);
164 /* dev.cdrom.info and sunrpc.transports, for example,
165 * are multi-line. Try "sysctl sunrpc.transports"
167 while ((c = *cptr) != '\0') {
168 if (option_mask32 & FLAG_SHOW_KEYS)
169 printf("%s = ", outname);
187 static int sysctl_display_all(const char *path)
191 struct dirent *entry;
196 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
197 dirp = opendir(path);
200 while ((entry = readdir(dirp)) != NULL) {
201 next = concat_subpath_file(
202 path, entry->d_name);
204 continue; /* d_name is "." or ".." */
205 /* if path was ".", drop "./" prefix: */
206 retval |= sysctl_display_all((next[0] == '.' && next[1] == '/') ?
212 char *name = xstrdup(path);
213 retval |= sysctl_act_on_setting(name);
220 static void sysctl_dots_to_slashes(char *name)
222 char *cptr, *last_good, *end;
224 /* Convert minimum number of '.' to '/' so that
225 * we end up with existing file's name.
227 * Example from bug 3894:
228 * net.ipv4.conf.eth0.100.mc_forwarding ->
229 * net/ipv4/conf/eth0.100/mc_forwarding
230 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
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
234 * where last replacement occurred.
236 end = name + strlen(name);
237 last_good = name - 1;
238 *end = '.'; /* trick the loop in trying full name too */
242 while (cptr > last_good) {
245 //bb_error_msg("trying:'%s'", name);
246 if (access(name, F_OK) == 0) {
248 *end = '\0'; /* prevent trailing '/' */
249 //bb_error_msg("replaced:'%s'", name);