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_read_setting(const char *setting
);
20 static int sysctl_write_setting(const char *setting
);
21 static int sysctl_display_all(const char *path
);
22 static int sysctl_preload_file_and_exit(const char *filename
);
23 static void sysctl_dots_to_slashes(char *name
);
25 static const char ETC_SYSCTL_CONF
[] ALIGN1
= "/etc/sysctl.conf";
26 static const char PROC_SYS
[] ALIGN1
= "/proc/sys/";
27 enum { strlen_PROC_SYS
= sizeof(PROC_SYS
) - 1 };
29 static const char msg_unknown_key
[] ALIGN1
=
30 "error: '%s' is an unknown key";
32 static void dwrite_str(int fd
, const char *buf
)
34 write(fd
, buf
, strlen(buf
));
38 FLAG_SHOW_KEYS
= 1 << 0,
39 FLAG_SHOW_KEY_ERRORS
= 1 << 1,
40 FLAG_TABLE_FORMAT
= 1 << 2, /* not implemented */
41 FLAG_SHOW_ALL
= 1 << 3,
42 FLAG_PRELOAD_FILE
= 1 << 4,
46 int sysctl_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
47 int sysctl_main(int argc UNUSED_PARAM
, char **argv
)
52 opt
= getopt32(argv
, "+neAapw"); /* '+' - stop on first non-option */
54 opt
^= (FLAG_SHOW_KEYS
| FLAG_SHOW_KEY_ERRORS
);
55 option_mask32
^= (FLAG_SHOW_KEYS
| FLAG_SHOW_KEY_ERRORS
);
57 if (opt
& (FLAG_TABLE_FORMAT
| FLAG_SHOW_ALL
))
58 return sysctl_display_all(PROC_SYS
);
59 if (opt
& FLAG_PRELOAD_FILE
)
60 return sysctl_preload_file_and_exit(*argv
? *argv
: ETC_SYSCTL_CONF
);
65 retval
|= sysctl_write_setting(*argv
);
67 retval
|= sysctl_read_setting(*argv
);
72 } /* end sysctl_main() */
74 /* Set sysctl's from a conf file. Format example:
75 * # Controls IP packet forwarding
76 * net.ipv4.ip_forward = 0
78 static int sysctl_preload_file_and_exit(const char *filename
)
83 parser
= config_open(filename
);
84 // TODO: ';' is comment char too
85 while (config_read(parser
, token
, 2, 2, "# \t=", PARSE_NORMAL
)) {
87 char *s
= xasprintf("%s=%s", token
[0], token
[1]);
88 sysctl_write_setting(s
);
90 #else /* Save ~4 bytes by using parser internals */
91 sprintf(parser
->line
, "%s=%s", token
[0], token
[1]); // must have room by definition
92 sysctl_write_setting(parser
->line
);
95 if (ENABLE_FEATURE_CLEAN_UP
)
98 } /* end sysctl_preload_file_and_exit() */
100 static int sysctl_write_setting(const char *setting
)
106 char *tmpname
, *outname
, *cptr
;
110 equals
= strchr(setting
, '=');
112 bb_error_msg("error: '%s' must be of the form name=value", setting
);
116 value
= equals
+ 1; /* point to the value in name=value */
117 if (name
== equals
|| !*value
) {
118 bb_error_msg("error: malformed setting '%s'", setting
);
122 tmpname
= xasprintf("%s%.*s", PROC_SYS
, (int)(equals
- name
), name
);
123 outname
= xstrdup(tmpname
+ strlen_PROC_SYS
);
125 sysctl_dots_to_slashes(tmpname
);
127 while ((cptr
= strchr(outname
, '/')) != NULL
)
130 fd
= open(tmpname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
134 if (option_mask32
& FLAG_SHOW_KEY_ERRORS
)
135 bb_error_msg(msg_unknown_key
, outname
);
138 bb_perror_msg("error setting key '%s'", outname
);
141 retval
= EXIT_FAILURE
;
143 dwrite_str(fd
, value
);
145 if (option_mask32
& FLAG_SHOW_KEYS
) {
146 printf("%s = ", outname
);
149 retval
= EXIT_SUCCESS
;
155 } /* end sysctl_write_setting() */
157 static int sysctl_read_setting(const char *name
)
160 char *tmpname
, *outname
, *cptr
;
165 if (option_mask32
& FLAG_SHOW_KEY_ERRORS
)
166 bb_error_msg(msg_unknown_key
, name
);
170 tmpname
= concat_path_file(PROC_SYS
, name
);
171 outname
= xstrdup(tmpname
+ strlen_PROC_SYS
);
173 sysctl_dots_to_slashes(tmpname
);
175 while ((cptr
= strchr(outname
, '/')) != NULL
)
178 fp
= fopen_for_read(tmpname
);
182 if (option_mask32
& FLAG_SHOW_KEY_ERRORS
)
183 bb_error_msg(msg_unknown_key
, outname
);
186 bb_perror_msg("error reading key '%s'", outname
);
189 retval
= EXIT_FAILURE
;
191 while (fgets(inbuf
, sizeof(inbuf
) - 1, fp
)) {
192 if (option_mask32
& FLAG_SHOW_KEYS
) {
193 printf("%s = ", outname
);
195 fputs(inbuf
, stdout
);
198 retval
= EXIT_SUCCESS
;
204 } /* end sysctl_read_setting() */
206 static int sysctl_display_all(const char *path
)
208 int retval
= EXIT_SUCCESS
;
218 while ((de
= readdir(dp
)) != NULL
) {
219 tmpdir
= concat_subpath_file(path
, de
->d_name
);
221 continue; /* . or .. */
222 if (stat(tmpdir
, &ts
) != 0) {
223 bb_perror_msg(tmpdir
);
224 } else if (S_ISDIR(ts
.st_mode
)) {
225 retval
|= sysctl_display_all(tmpdir
);
227 retval
|= sysctl_read_setting(tmpdir
+ strlen_PROC_SYS
);
234 } /* end sysctl_display_all() */
236 static void sysctl_dots_to_slashes(char *name
)
238 char *cptr
, *last_good
, *end
;
240 /* It can be good as-is! */
241 if (access(name
, F_OK
) == 0)
244 /* Example from bug 3894:
245 * net.ipv4.conf.eth0.100.mc_forwarding ->
246 * net/ipv4/conf/eth0.100/mc_forwarding. NB:
247 * net/ipv4/conf/eth0/mc_forwarding *also exists*,
248 * therefore we must start from the end, and if
249 * we replaced even one . -> /, start over again,
250 * but never replace dots before the position
251 * where replacement occurred. */
252 end
= name
+ strlen(name
) - 1;
253 last_good
= name
- 1;
256 while (cptr
> last_good
) {
259 if (access(name
, F_OK
) == 0) {
268 } /* end sysctl_dots_to_slashes() */