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);
24 static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";
25 static const char PROC_SYS[] ALIGN1 = "/proc/sys/";
26 enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };
29 static const char ERR_MALFORMED_SETTING[] ALIGN1 =
30 "error: malformed setting '%s'";
31 static const char ERR_NO_EQUALS[] ALIGN1 =
32 "error: '%s' must be of the form name=value";
33 static const char ERR_INVALID_KEY[] ALIGN1 =
34 "error: '%s' is an unknown key";
35 static const char ERR_UNKNOWN_WRITING[] ALIGN1 =
36 "error setting key '%s'";
37 static const char ERR_UNKNOWN_READING[] ALIGN1 =
38 "error reading key '%s'";
39 static const char ERR_PERMISSION_DENIED[] ALIGN1 =
40 "error: permission denied on key '%s'";
41 static const char WARN_BAD_LINE[] ALIGN1 =
42 "warning: %s(%d): invalid syntax, continuing";
45 static void dwrite_str(int fd, const char *buf)
47 write(fd, buf, strlen(buf));
51 FLAG_SHOW_KEYS = 1 << 0,
52 FLAG_SHOW_KEY_ERRORS = 1 << 1,
53 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
54 FLAG_SHOW_ALL = 1 << 3,
55 FLAG_PRELOAD_FILE = 1 << 4,
59 int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60 int sysctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
65 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
67 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
68 option_mask32 ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
70 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL))
71 return sysctl_display_all(PROC_SYS);
72 if (opt & FLAG_PRELOAD_FILE)
73 return sysctl_preload_file_and_exit(*argv ? *argv : ETC_SYSCTL_CONF);
78 retval |= sysctl_write_setting(*argv);
80 retval |= sysctl_read_setting(*argv);
85 } /* end sysctl_main() */
88 * preload the sysctl's from a conf file
89 * - we parse the file and then reform it (strip out whitespace)
91 #define PRELOAD_BUF 256
93 static int sysctl_preload_file_and_exit(const char *filename)
96 char oneline[PRELOAD_BUF];
97 char buffer[PRELOAD_BUF];
101 fp = xfopen(filename, "r");
104 while (fgets(oneline, sizeof(oneline) - 1, fp)) {
107 if (oneline[0] == '#' || oneline[0] == ';')
109 if (!oneline[0] || !oneline[1])
112 name = strtok(oneline, "=");
114 bb_error_msg(WARN_BAD_LINE, filename, lineno);
119 bb_error_msg(WARN_BAD_LINE, filename, lineno);
123 value = strtok(NULL, "\n\r");
125 bb_error_msg(WARN_BAD_LINE, filename, lineno);
128 while (*value == ' ' || *value == '\t')
131 bb_error_msg(WARN_BAD_LINE, filename, lineno);
135 /* safe because sizeof(oneline) == sizeof(buffer) */
136 sprintf(buffer, "%s=%s", name, value);
137 sysctl_write_setting(buffer);
139 if (ENABLE_FEATURE_CLEAN_UP)
142 } /* end sysctl_preload_file_and_exit() */
145 * Write a single sysctl setting
147 static int sysctl_write_setting(const char *setting)
153 char *tmpname, *outname, *cptr;
157 equals = strchr(setting, '=');
159 bb_error_msg(ERR_NO_EQUALS, setting);
163 value = equals + 1; /* point to the value in name=value */
164 if (name == equals || !*value) {
165 bb_error_msg(ERR_MALFORMED_SETTING, setting);
169 tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
170 outname = xstrdup(tmpname + strlen_PROC_SYS);
172 while ((cptr = strchr(tmpname, '.')) != NULL)
175 while ((cptr = strchr(outname, '/')) != NULL)
178 fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
182 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
183 bb_error_msg(ERR_INVALID_KEY, outname);
186 bb_perror_msg(ERR_PERMISSION_DENIED, outname);
189 bb_perror_msg(ERR_UNKNOWN_WRITING, outname);
192 retval = EXIT_FAILURE;
194 dwrite_str(fd, value);
196 if (option_mask32 & FLAG_SHOW_KEYS) {
197 printf("%s = ", outname);
200 retval = EXIT_SUCCESS;
206 } /* end sysctl_write_setting() */
209 * Read a sysctl setting
211 static int sysctl_read_setting(const char *name)
214 char *tmpname, *outname, *cptr;
219 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
220 bb_error_msg(ERR_INVALID_KEY, name);
224 tmpname = concat_path_file(PROC_SYS, name);
225 outname = xstrdup(tmpname + strlen_PROC_SYS);
227 while ((cptr = strchr(tmpname, '.')) != NULL)
229 while ((cptr = strchr(outname, '/')) != NULL)
232 fp = fopen(tmpname, "r");
236 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
237 bb_error_msg(ERR_INVALID_KEY, outname);
240 bb_error_msg(ERR_PERMISSION_DENIED, outname);
243 bb_perror_msg(ERR_UNKNOWN_READING, outname);
246 retval = EXIT_FAILURE;
248 while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
249 if (option_mask32 & FLAG_SHOW_KEYS) {
250 printf("%s = ", outname);
252 fputs(inbuf, stdout);
255 retval = EXIT_SUCCESS;
261 } /* end sysctl_read_setting() */
264 * Display all the sysctl settings
266 static int sysctl_display_all(const char *path)
278 while ((de = readdir(dp)) != NULL) {
279 tmpdir = concat_subpath_file(path, de->d_name);
281 continue; /* . or .. */
282 if (stat(tmpdir, &ts) != 0) {
283 bb_perror_msg(tmpdir);
284 } else if (S_ISDIR(ts.st_mode)) {
285 retval |= sysctl_display_all(tmpdir);
287 retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);
294 } /* end sysctl_display_all() */