blob: 93d7c3418f39c15638d4147fd7740a1bc6819a35 [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 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010013//config:config BB_SYSCTL
14//config: bool "sysctl"
15//config: default y
16//config: help
17//config: Configure kernel parameters at runtime.
18
19//applet:IF_BB_SYSCTL(APPLET(sysctl, BB_DIR_SBIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_BB_SYSCTL) += sysctl.o
Eric Andersenb9050282003-12-24 06:02:11 +000022
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define sysctl_trivial_usage
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020024//usage: "[OPTIONS] [KEY[=VALUE]]..."
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define sysctl_full_usage "\n\n"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020026//usage: "Show/set kernel parameters\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage: "\n -e Don't warn about unknown keys"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020028//usage: "\n -n Don't show key names"
29//usage: "\n -a Show all values"
30/* Same as -a, no need to show it */
31/* //usage: "\n -A Show all values in table form" */
32//usage: "\n -w Set values"
33//usage: "\n -p FILE Set values from FILE (default /etc/sysctl.conf)"
34//usage: "\n -q Set values silently"
Pere Orga5bc8c002011-04-11 03:29:49 +020035//usage:
36//usage:#define sysctl_example_usage
37//usage: "sysctl [-n] [-e] variable...\n"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020038//usage: "sysctl [-n] [-e] [-q] -w variable=value...\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020039//usage: "sysctl [-n] [-e] -a\n"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020040//usage: "sysctl [-n] [-e] [-q] -p file (default /etc/sysctl.conf)\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage: "sysctl [-n] [-e] -A\n"
42
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000043#include "libbb.h"
Eric Andersenb9050282003-12-24 06:02:11 +000044
Denis Vlasenko64309f82007-11-29 06:40:28 +000045enum {
46 FLAG_SHOW_KEYS = 1 << 0,
47 FLAG_SHOW_KEY_ERRORS = 1 << 1,
48 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
49 FLAG_SHOW_ALL = 1 << 3,
50 FLAG_PRELOAD_FILE = 1 << 4,
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020051/* TODO: procps 3.2.8 seems to not require -w for KEY=VAL to work: */
Denis Vlasenko64309f82007-11-29 06:40:28 +000052 FLAG_WRITE = 1 << 5,
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020053 FLAG_QUIET = 1 << 6,
Denis Vlasenko64309f82007-11-29 06:40:28 +000054};
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020055#define OPTION_STR "neAapwq"
Denis Vlasenko64309f82007-11-29 06:40:28 +000056
Denis Vlasenko038fe442009-03-29 02:23:16 +000057static void sysctl_dots_to_slashes(char *name)
Eric Andersenb9050282003-12-24 06:02:11 +000058{
Denis Vlasenko038fe442009-03-29 02:23:16 +000059 char *cptr, *last_good, *end;
Eric Andersenb9050282003-12-24 06:02:11 +000060
Denis Vlasenko038fe442009-03-29 02:23:16 +000061 /* Convert minimum number of '.' to '/' so that
62 * we end up with existing file's name.
63 *
64 * Example from bug 3894:
65 * net.ipv4.conf.eth0.100.mc_forwarding ->
66 * net/ipv4/conf/eth0.100/mc_forwarding
67 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
68 * therefore we must start from the end, and if
69 * we replaced even one . -> /, start over again,
70 * but never replace dots before the position
71 * where last replacement occurred.
72 *
73 * Another bug we later had is that
74 * net.ipv4.conf.eth0.100
75 * (without .mc_forwarding) was mishandled.
76 *
77 * To set up testing: modprobe 8021q; vconfig add eth0 100
78 */
79 end = name + strlen(name);
80 last_good = name - 1;
81 *end = '.'; /* trick the loop into trying full name too */
Eric Andersenb9050282003-12-24 06:02:11 +000082
Denis Vlasenko038fe442009-03-29 02:23:16 +000083 again:
84 cptr = end;
85 while (cptr > last_good) {
86 if (*cptr == '.') {
87 *cptr = '\0';
88 //bb_error_msg("trying:'%s'", name);
89 if (access(name, F_OK) == 0) {
Denys Vlasenkoc2fdd412010-03-27 05:02:00 +010090 *cptr = '/';
Denis Vlasenko038fe442009-03-29 02:23:16 +000091 //bb_error_msg("replaced:'%s'", name);
92 last_good = cptr;
93 goto again;
94 }
95 *cptr = '.';
96 }
97 cptr--;
Denis Vlasenko54d10052008-12-24 03:11:43 +000098 }
Denis Vlasenko038fe442009-03-29 02:23:16 +000099 *end = '\0';
Denis Vlasenko54d10052008-12-24 03:11:43 +0000100}
Eric Andersenb9050282003-12-24 06:02:11 +0000101
Denis Vlasenko54d10052008-12-24 03:11:43 +0000102static int sysctl_act_on_setting(char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +0000103{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000104 int fd, retval = EXIT_SUCCESS;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +0000105 char *cptr, *outname;
106 char *value = value; /* for compiler */
Eric Andersenb9050282003-12-24 06:02:11 +0000107
Denis Vlasenko54d10052008-12-24 03:11:43 +0000108 outname = xstrdup(setting);
109
110 cptr = outname;
111 while (*cptr) {
112 if (*cptr == '/')
113 *cptr = '.';
114 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +0000115 }
116
Denis Vlasenko54d10052008-12-24 03:11:43 +0000117 if (option_mask32 & FLAG_WRITE) {
118 cptr = strchr(setting, '=');
119 if (cptr == NULL) {
120 bb_error_msg("error: '%s' must be of the form name=value",
121 outname);
122 retval = EXIT_FAILURE;
123 goto end;
124 }
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200125 value = cptr + 1; /* point to the value in name=value */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000126 if (setting == cptr || !*value) {
127 bb_error_msg("error: malformed setting '%s'", outname);
128 retval = EXIT_FAILURE;
129 goto end;
130 }
131 *cptr = '\0';
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000132 outname[cptr - setting] = '\0';
133 /* procps 3.2.7 actually uses these flags */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000134 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
135 } else {
136 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000137 }
138
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000139 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000140 switch (errno) {
Denys Vlasenko6554d032014-02-24 17:28:43 +0100141 case EACCES:
142 /* Happens for write-only settings, e.g. net.ipv6.route.flush */
143 goto end;
Eric Andersenb9050282003-12-24 06:02:11 +0000144 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000145 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000146 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000147 break;
148 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000149 bb_perror_msg("error %sing key '%s'",
150 option_mask32 & FLAG_WRITE ?
151 "sett" : "read",
152 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000153 break;
154 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000155 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000156 goto end;
157 }
158
159 if (option_mask32 & FLAG_WRITE) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000160//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000161 xwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000162 close(fd);
Joshua Kahlenbergc4398512012-09-05 18:15:12 +0200163 if (!(option_mask32 & FLAG_QUIET)) {
164 if (option_mask32 & FLAG_SHOW_KEYS)
165 printf("%s = ", outname);
166 puts(value);
167 }
Eric Andersenb9050282003-12-24 06:02:11 +0000168 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000169 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000170
Denis Vlasenko54d10052008-12-24 03:11:43 +0000171 value = cptr = xmalloc_read(fd, NULL);
172 close(fd);
173 if (value == NULL) {
174 bb_perror_msg("error reading key '%s'", outname);
175 goto end;
176 }
177
178 /* dev.cdrom.info and sunrpc.transports, for example,
179 * are multi-line. Try "sysctl sunrpc.transports"
180 */
181 while ((c = *cptr) != '\0') {
182 if (option_mask32 & FLAG_SHOW_KEYS)
183 printf("%s = ", outname);
184 while (1) {
185 fputc(c, stdout);
186 cptr++;
187 if (c == '\n')
188 break;
189 c = *cptr;
190 if (c == '\0')
191 break;
192 }
193 }
194 free(value);
195 }
196 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000197 free(outname);
198 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000199}
Eric Andersenb9050282003-12-24 06:02:11 +0000200
Denis Vlasenko038fe442009-03-29 02:23:16 +0000201static int sysctl_act_recursive(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000202{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000203 DIR *dirp;
204 struct stat buf;
205 struct dirent *entry;
206 char *next;
207 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000208
Denis Vlasenko54d10052008-12-24 03:11:43 +0000209 stat(path, &buf);
210 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
211 dirp = opendir(path);
212 if (dirp == NULL)
213 return -1;
214 while ((entry = readdir(dirp)) != NULL) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000215 next = concat_subpath_file(path, entry->d_name);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000216 if (next == NULL)
217 continue; /* d_name is "." or ".." */
218 /* if path was ".", drop "./" prefix: */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000219 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
Denys Vlasenko69675782013-01-14 01:34:48 +0100220 next + 2 : next);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000221 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000222 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000223 closedir(dirp);
224 } else {
225 char *name = xstrdup(path);
226 retval |= sysctl_act_on_setting(name);
227 free(name);
228 }
Eric Andersenb9050282003-12-24 06:02:11 +0000229
230 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000231}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000232
Denis Vlasenko038fe442009-03-29 02:23:16 +0000233/* Set sysctl's from a conf file. Format example:
234 * # Controls IP packet forwarding
235 * net.ipv4.ip_forward = 0
236 */
237static int sysctl_handle_preload_file(const char *filename)
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000238{
Denis Vlasenko038fe442009-03-29 02:23:16 +0000239 char *token[2];
240 parser_t *parser;
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000241
Denis Vlasenko038fe442009-03-29 02:23:16 +0000242 parser = config_open(filename);
243 /* Must do it _after_ config_open(): */
244 xchdir("/proc/sys");
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100245 /* xchroot("/proc/sys") - if you are paranoid */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000246
Denis Vlasenko038fe442009-03-29 02:23:16 +0000247//TODO: ';' is comment char too
248//TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
249// (but _whitespace_ from ends should be trimmed first (and we do it right))
250//TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
Denys Vlasenkoadbbee42010-06-21 07:17:23 +0200251// can it be fixed by removing PARSE_COLLAPSE bit?
Denis Vlasenko038fe442009-03-29 02:23:16 +0000252 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
Denys Vlasenkof427c802009-05-10 23:41:29 +0200253 char *tp;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000254 sysctl_dots_to_slashes(token[0]);
Denys Vlasenkof427c802009-05-10 23:41:29 +0200255 tp = xasprintf("%s=%s", token[0], token[1]);
256 sysctl_act_recursive(tp);
257 free(tp);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000258 }
Denis Vlasenko038fe442009-03-29 02:23:16 +0000259 if (ENABLE_FEATURE_CLEAN_UP)
260 config_close(parser);
261 return 0;
262}
263
264int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
265int sysctl_main(int argc UNUSED_PARAM, char **argv)
266{
267 int retval;
268 int opt;
269
270 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
271 argv += optind;
272 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
273 option_mask32 = opt;
274
275 if (opt & FLAG_PRELOAD_FILE) {
276 option_mask32 |= FLAG_WRITE;
277 /* xchdir("/proc/sys") is inside */
278 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
279 }
280 xchdir("/proc/sys");
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100281 /* xchroot("/proc/sys") - if you are paranoid */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000282 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
283 return sysctl_act_recursive(".");
284 }
285
286 retval = 0;
287 while (*argv) {
288 sysctl_dots_to_slashes(*argv);
289 retval |= sysctl_act_recursive(*argv);
290 argv++;
291 }
292
293 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000294}