blob: 5296d0f5813c10dfe8fd326177e18e88cf368418 [file] [log] [blame]
Denis Vlasenko92993052008-10-15 09:44:37 +00001/* vi: set sw=4 ts=4: */
Eric Andersenb9050282003-12-24 06:02:11 +00002/*
3 * Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
4 *
Rob Landley8b1f11d2006-04-17 21:49:34 +00005 * Copyright 1999 George Staikos
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenb9050282003-12-24 06:02:11 +00008 *
9 * Changelog:
Denis Vlasenko038fe442009-03-29 02:23:16 +000010 * v1.01 - added -p <preload> to preload values from a file
11 * v1.01.1 - busybox applet aware by <solar@gentoo.org>
Eric Andersenb9050282003-12-24 06:02:11 +000012 */
13
Pere Orga5bc8c002011-04-11 03:29:49 +020014//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"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//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"
24//usage:
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"
31
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000032#include "libbb.h"
Eric Andersenb9050282003-12-24 06:02:11 +000033
Denis Vlasenko64309f82007-11-29 06:40:28 +000034enum {
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,
40 FLAG_WRITE = 1 << 5,
41};
Denis Vlasenko038fe442009-03-29 02:23:16 +000042#define OPTION_STR "neAapw"
Denis Vlasenko64309f82007-11-29 06:40:28 +000043
Denis Vlasenko038fe442009-03-29 02:23:16 +000044static void sysctl_dots_to_slashes(char *name)
Eric Andersenb9050282003-12-24 06:02:11 +000045{
Denis Vlasenko038fe442009-03-29 02:23:16 +000046 char *cptr, *last_good, *end;
Eric Andersenb9050282003-12-24 06:02:11 +000047
Denis Vlasenko038fe442009-03-29 02:23:16 +000048 /* Convert minimum number of '.' to '/' so that
49 * we end up with existing file's name.
50 *
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.
59 *
60 * Another bug we later had is that
61 * net.ipv4.conf.eth0.100
62 * (without .mc_forwarding) was mishandled.
63 *
64 * To set up testing: modprobe 8021q; vconfig add eth0 100
65 */
66 end = name + strlen(name);
67 last_good = name - 1;
68 *end = '.'; /* trick the loop into trying full name too */
Eric Andersenb9050282003-12-24 06:02:11 +000069
Denis Vlasenko038fe442009-03-29 02:23:16 +000070 again:
71 cptr = end;
72 while (cptr > last_good) {
73 if (*cptr == '.') {
74 *cptr = '\0';
75 //bb_error_msg("trying:'%s'", name);
76 if (access(name, F_OK) == 0) {
Denys Vlasenkoc2fdd412010-03-27 05:02:00 +010077 *cptr = '/';
Denis Vlasenko038fe442009-03-29 02:23:16 +000078 //bb_error_msg("replaced:'%s'", name);
79 last_good = cptr;
80 goto again;
81 }
82 *cptr = '.';
83 }
84 cptr--;
Denis Vlasenko54d10052008-12-24 03:11:43 +000085 }
Denis Vlasenko038fe442009-03-29 02:23:16 +000086 *end = '\0';
Denis Vlasenko54d10052008-12-24 03:11:43 +000087}
Eric Andersenb9050282003-12-24 06:02:11 +000088
Denis Vlasenko54d10052008-12-24 03:11:43 +000089static int sysctl_act_on_setting(char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +000090{
Denis Vlasenko54d10052008-12-24 03:11:43 +000091 int fd, retval = EXIT_SUCCESS;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +000092 char *cptr, *outname;
93 char *value = value; /* for compiler */
Eric Andersenb9050282003-12-24 06:02:11 +000094
Denis Vlasenko54d10052008-12-24 03:11:43 +000095 outname = xstrdup(setting);
96
97 cptr = outname;
98 while (*cptr) {
99 if (*cptr == '/')
100 *cptr = '.';
101 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +0000102 }
103
Denis Vlasenko54d10052008-12-24 03:11:43 +0000104 if (option_mask32 & FLAG_WRITE) {
105 cptr = strchr(setting, '=');
106 if (cptr == NULL) {
107 bb_error_msg("error: '%s' must be of the form name=value",
108 outname);
109 retval = EXIT_FAILURE;
110 goto end;
111 }
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200112 value = cptr + 1; /* point to the value in name=value */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000113 if (setting == cptr || !*value) {
114 bb_error_msg("error: malformed setting '%s'", outname);
115 retval = EXIT_FAILURE;
116 goto end;
117 }
118 *cptr = '\0';
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000119 outname[cptr - setting] = '\0';
120 /* procps 3.2.7 actually uses these flags */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000121 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
122 } else {
123 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000124 }
125
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000126 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000127 switch (errno) {
128 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000129 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000130 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000131 break;
132 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000133 bb_perror_msg("error %sing key '%s'",
134 option_mask32 & FLAG_WRITE ?
135 "sett" : "read",
136 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000137 break;
138 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000139 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000140 goto end;
141 }
142
143 if (option_mask32 & FLAG_WRITE) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000144//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000145 xwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000146 close(fd);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000147 if (option_mask32 & FLAG_SHOW_KEYS)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000148 printf("%s = ", outname);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000149 puts(value);
Eric Andersenb9050282003-12-24 06:02:11 +0000150 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000151 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000152
Denis Vlasenko54d10052008-12-24 03:11:43 +0000153 value = cptr = xmalloc_read(fd, NULL);
154 close(fd);
155 if (value == NULL) {
156 bb_perror_msg("error reading key '%s'", outname);
157 goto end;
158 }
159
160 /* dev.cdrom.info and sunrpc.transports, for example,
161 * are multi-line. Try "sysctl sunrpc.transports"
162 */
163 while ((c = *cptr) != '\0') {
164 if (option_mask32 & FLAG_SHOW_KEYS)
165 printf("%s = ", outname);
166 while (1) {
167 fputc(c, stdout);
168 cptr++;
169 if (c == '\n')
170 break;
171 c = *cptr;
172 if (c == '\0')
173 break;
174 }
175 }
176 free(value);
177 }
178 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000179 free(outname);
180 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000181}
Eric Andersenb9050282003-12-24 06:02:11 +0000182
Denis Vlasenko038fe442009-03-29 02:23:16 +0000183static int sysctl_act_recursive(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000184{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000185 DIR *dirp;
186 struct stat buf;
187 struct dirent *entry;
188 char *next;
189 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000190
Denis Vlasenko54d10052008-12-24 03:11:43 +0000191 stat(path, &buf);
192 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
193 dirp = opendir(path);
194 if (dirp == NULL)
195 return -1;
196 while ((entry = readdir(dirp)) != NULL) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000197 next = concat_subpath_file(path, entry->d_name);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000198 if (next == NULL)
199 continue; /* d_name is "." or ".." */
200 /* if path was ".", drop "./" prefix: */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000201 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
Denis Vlasenko54d10052008-12-24 03:11:43 +0000202 next + 2 : next);
203 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000204 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000205 closedir(dirp);
206 } else {
207 char *name = xstrdup(path);
208 retval |= sysctl_act_on_setting(name);
209 free(name);
210 }
Eric Andersenb9050282003-12-24 06:02:11 +0000211
212 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000213}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000214
Denis Vlasenko038fe442009-03-29 02:23:16 +0000215/* Set sysctl's from a conf file. Format example:
216 * # Controls IP packet forwarding
217 * net.ipv4.ip_forward = 0
218 */
219static int sysctl_handle_preload_file(const char *filename)
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000220{
Denis Vlasenko038fe442009-03-29 02:23:16 +0000221 char *token[2];
222 parser_t *parser;
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000223
Denis Vlasenko038fe442009-03-29 02:23:16 +0000224 parser = config_open(filename);
225 /* Must do it _after_ config_open(): */
226 xchdir("/proc/sys");
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100227 /* xchroot("/proc/sys") - if you are paranoid */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000228
Denis Vlasenko038fe442009-03-29 02:23:16 +0000229//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")
Denys Vlasenkoadbbee42010-06-21 07:17:23 +0200233// can it be fixed by removing PARSE_COLLAPSE bit?
Denis Vlasenko038fe442009-03-29 02:23:16 +0000234 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
Denys Vlasenkof427c802009-05-10 23:41:29 +0200235 char *tp;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000236 sysctl_dots_to_slashes(token[0]);
Denys Vlasenkof427c802009-05-10 23:41:29 +0200237 tp = xasprintf("%s=%s", token[0], token[1]);
238 sysctl_act_recursive(tp);
239 free(tp);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000240 }
Denis Vlasenko038fe442009-03-29 02:23:16 +0000241 if (ENABLE_FEATURE_CLEAN_UP)
242 config_close(parser);
243 return 0;
244}
245
246int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
247int sysctl_main(int argc UNUSED_PARAM, char **argv)
248{
249 int retval;
250 int opt;
251
252 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
253 argv += optind;
254 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
255 option_mask32 = opt;
256
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");
261 }
262 xchdir("/proc/sys");
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100263 /* xchroot("/proc/sys") - if you are paranoid */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000264 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
265 return sysctl_act_recursive(".");
266 }
267
268 retval = 0;
269 while (*argv) {
270 sysctl_dots_to_slashes(*argv);
271 retval |= sysctl_act_recursive(*argv);
272 argv++;
273 }
274
275 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000276}