blob: e16b119e98d4b8e5ea5058ad48877d9157ffb518 [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
Denys Vlasenkob097a842018-12-28 03:20:17 +010014//config: bool "sysctl (7.4 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010015//config: default y
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: Configure kernel parameters at runtime.
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010018
Denys Vlasenkocaf26b32017-08-05 18:23:10 +020019//applet:IF_BB_SYSCTL(APPLET_NOEXEC(sysctl, sysctl, BB_DIR_SBIN, BB_SUID_DROP, sysctl))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010020
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
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +020024//usage: "-p [-enq] [FILE...] / [-enqaw] [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"
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +020027//usage: "\n -p Set values from FILEs (default /etc/sysctl.conf)"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -e Don't warn about unknown keys"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020029//usage: "\n -n Don't show key names"
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +020030//usage: "\n -q Quiet"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020031//usage: "\n -a Show all values"
32/* Same as -a, no need to show it */
33/* //usage: "\n -A Show all values in table form" */
34//usage: "\n -w Set values"
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,
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +020051 /* NB: procps 3.2.8 does not require -w for KEY=VAL to work, it only rejects non-KEY=VAL form */
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{
Aaro Koskinenc89764c2019-02-08 16:30:24 +010059 char *cptr, *last_good, *end, *slash;
Denys Vlasenko78301862019-02-08 16:02:39 +010060 char end_ch;
Eric Andersenb9050282003-12-24 06:02:11 +000061
Aaro Koskinenc89764c2019-02-08 16:30:24 +010062 end = strchrnul(name, '=');
63
64 slash = strchrnul(name, '/');
65 if (slash < end
66 && strchrnul(name, '.') < slash
67 ) {
68 /* There are both dots and slashes, and 1st dot is
69 * before 1st slash.
70 * (IOW: not raw, unmangled a/b/c.d format)
71 *
72 * procps supports this syntax for names with dots:
73 * net.ipv4.conf.eth0/100.mc_forwarding
74 * (dots and slashes are simply swapped)
75 */
76 while (end != name) {
77 end--;
78 if (*end == '.') *end = '/';
79 else if (*end == '/') *end = '.';
80 }
81 return;
82 }
83 /* else: use our old behavior: */
84
Denis Vlasenko038fe442009-03-29 02:23:16 +000085 /* Convert minimum number of '.' to '/' so that
86 * we end up with existing file's name.
87 *
88 * Example from bug 3894:
89 * net.ipv4.conf.eth0.100.mc_forwarding ->
90 * net/ipv4/conf/eth0.100/mc_forwarding
91 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
92 * therefore we must start from the end, and if
93 * we replaced even one . -> /, start over again,
94 * but never replace dots before the position
95 * where last replacement occurred.
96 *
97 * Another bug we later had is that
98 * net.ipv4.conf.eth0.100
99 * (without .mc_forwarding) was mishandled.
100 *
101 * To set up testing: modprobe 8021q; vconfig add eth0 100
102 */
Denys Vlasenko78301862019-02-08 16:02:39 +0100103 end_ch = *end;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000104 *end = '.'; /* trick the loop into trying full name too */
Eric Andersenb9050282003-12-24 06:02:11 +0000105
Denys Vlasenko78301862019-02-08 16:02:39 +0100106 last_good = name - 1;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000107 again:
108 cptr = end;
109 while (cptr > last_good) {
110 if (*cptr == '.') {
111 *cptr = '\0';
112 //bb_error_msg("trying:'%s'", name);
113 if (access(name, F_OK) == 0) {
Denys Vlasenkoc2fdd412010-03-27 05:02:00 +0100114 *cptr = '/';
Denis Vlasenko038fe442009-03-29 02:23:16 +0000115 //bb_error_msg("replaced:'%s'", name);
116 last_good = cptr;
117 goto again;
118 }
119 *cptr = '.';
120 }
121 cptr--;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000122 }
Denys Vlasenko78301862019-02-08 16:02:39 +0100123 *end = end_ch;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000124}
Eric Andersenb9050282003-12-24 06:02:11 +0000125
Denis Vlasenko54d10052008-12-24 03:11:43 +0000126static int sysctl_act_on_setting(char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +0000127{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000128 int fd, retval = EXIT_SUCCESS;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +0000129 char *cptr, *outname;
130 char *value = value; /* for compiler */
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200131 bool writing = (option_mask32 & FLAG_WRITE);
Eric Andersenb9050282003-12-24 06:02:11 +0000132
Denis Vlasenko54d10052008-12-24 03:11:43 +0000133 outname = xstrdup(setting);
134
135 cptr = outname;
136 while (*cptr) {
137 if (*cptr == '/')
138 *cptr = '.';
Aaro Koskinenc89764c2019-02-08 16:30:24 +0100139 else if (*cptr == '.')
140 *cptr = '/';
Denis Vlasenko54d10052008-12-24 03:11:43 +0000141 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +0000142 }
143
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200144 cptr = strchr(setting, '=');
145 if (cptr)
146 writing = 1;
147 if (writing) {
Denys Vlasenko317498f2019-02-08 14:02:59 +0100148 if (!cptr) {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000149 bb_error_msg("error: '%s' must be of the form name=value",
150 outname);
151 retval = EXIT_FAILURE;
152 goto end;
153 }
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200154 value = cptr + 1; /* point to the value in name=value */
Denys Vlasenko317498f2019-02-08 14:02:59 +0100155 if (setting == cptr /* "name" can't be empty */
156 /* || !*value - WRONG: "sysctl net.ipv4.ip_local_reserved_ports=" is a valid syntax (clears the value) */
157 ) {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000158 bb_error_msg("error: malformed setting '%s'", outname);
159 retval = EXIT_FAILURE;
160 goto end;
161 }
162 *cptr = '\0';
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000163 outname[cptr - setting] = '\0';
164 /* procps 3.2.7 actually uses these flags */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000165 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
166 } else {
167 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000168 }
169
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000170 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000171 switch (errno) {
172 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000173 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000174 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000175 break;
Denys Vlasenko002be6e2020-06-09 15:58:32 +0200176 case EACCES:
177 /* Happens for write-only settings, e.g. net.ipv6.route.flush */
178 if (!writing)
179 goto end;
180 /* fall through */
Eric Andersenb9050282003-12-24 06:02:11 +0000181 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000182 bb_perror_msg("error %sing key '%s'",
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200183 writing ?
Denis Vlasenko54d10052008-12-24 03:11:43 +0000184 "sett" : "read",
185 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000186 break;
187 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000188 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000189 goto end;
190 }
191
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200192 if (writing) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000193//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000194 xwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000195 close(fd);
Joshua Kahlenbergc4398512012-09-05 18:15:12 +0200196 if (!(option_mask32 & FLAG_QUIET)) {
197 if (option_mask32 & FLAG_SHOW_KEYS)
198 printf("%s = ", outname);
199 puts(value);
200 }
Eric Andersenb9050282003-12-24 06:02:11 +0000201 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000202 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000203
Denis Vlasenko54d10052008-12-24 03:11:43 +0000204 value = cptr = xmalloc_read(fd, NULL);
205 close(fd);
206 if (value == NULL) {
207 bb_perror_msg("error reading key '%s'", outname);
Denys Vlasenko1422ba62019-02-08 14:59:06 +0100208 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000209 goto end;
210 }
211
212 /* dev.cdrom.info and sunrpc.transports, for example,
213 * are multi-line. Try "sysctl sunrpc.transports"
214 */
215 while ((c = *cptr) != '\0') {
216 if (option_mask32 & FLAG_SHOW_KEYS)
217 printf("%s = ", outname);
218 while (1) {
219 fputc(c, stdout);
220 cptr++;
221 if (c == '\n')
222 break;
223 c = *cptr;
224 if (c == '\0')
225 break;
226 }
227 }
228 free(value);
229 }
230 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000231 free(outname);
232 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000233}
Eric Andersenb9050282003-12-24 06:02:11 +0000234
Denis Vlasenko038fe442009-03-29 02:23:16 +0000235static int sysctl_act_recursive(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000236{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000237 struct stat buf;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000238 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000239
Denys Vlasenko679c30e2019-02-08 14:27:21 +0100240 if (!(option_mask32 & FLAG_WRITE)
Denys Vlasenko002be6e2020-06-09 15:58:32 +0200241 && !strchr(path, '=') /* do not try to resurse on "var=val" */
Denys Vlasenko679c30e2019-02-08 14:27:21 +0100242 && stat(path, &buf) == 0
243 && S_ISDIR(buf.st_mode)
244 ) {
245 struct dirent *entry;
246 DIR *dirp;
247
Denis Vlasenko54d10052008-12-24 03:11:43 +0000248 dirp = opendir(path);
249 if (dirp == NULL)
250 return -1;
251 while ((entry = readdir(dirp)) != NULL) {
Denys Vlasenko679c30e2019-02-08 14:27:21 +0100252 char *next = concat_subpath_file(path, entry->d_name);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000253 if (next == NULL)
254 continue; /* d_name is "." or ".." */
255 /* if path was ".", drop "./" prefix: */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000256 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
Denys Vlasenko69675782013-01-14 01:34:48 +0100257 next + 2 : next);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000258 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000259 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000260 closedir(dirp);
261 } else {
262 char *name = xstrdup(path);
263 retval |= sysctl_act_on_setting(name);
264 free(name);
265 }
Eric Andersenb9050282003-12-24 06:02:11 +0000266
267 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000268}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000269
Denis Vlasenko038fe442009-03-29 02:23:16 +0000270/* Set sysctl's from a conf file. Format example:
271 * # Controls IP packet forwarding
272 * net.ipv4.ip_forward = 0
273 */
274static int sysctl_handle_preload_file(const char *filename)
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000275{
Denis Vlasenko038fe442009-03-29 02:23:16 +0000276 char *token[2];
277 parser_t *parser;
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200278 int parse_flags;
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000279
Denis Vlasenko038fe442009-03-29 02:23:16 +0000280 parser = config_open(filename);
281 /* Must do it _after_ config_open(): */
282 xchdir("/proc/sys");
Denis Vlasenko54d10052008-12-24 03:11:43 +0000283
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200284 parse_flags = 0;
285 parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
286 parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
287 parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
288 parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
289 parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
Denys Vlasenko50db1f22017-08-05 18:20:34 +0200290 parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#'
291 /* <space><tab><space>#comment is also comment, not strictly 1st char only */
292 parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before
293 while (config_read(parser, token, 2, 2, ";#=", parse_flags)) {
Denys Vlasenkof427c802009-05-10 23:41:29 +0200294 char *tp;
Denys Vlasenko20077c12017-08-05 17:50:35 +0200295
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200296 trim(token[1]);
Denys Vlasenko20077c12017-08-05 17:50:35 +0200297 tp = trim(token[0]);
Denis Vlasenko038fe442009-03-29 02:23:16 +0000298 sysctl_dots_to_slashes(token[0]);
Denys Vlasenko20077c12017-08-05 17:50:35 +0200299 /* ^^^converted in-place. tp still points to NUL */
300 /* now, add "=TOKEN1" */
301 *tp++ = '=';
302 overlapping_strcpy(tp, token[1]);
303
304 sysctl_act_on_setting(token[0]);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000305 }
Denis Vlasenko038fe442009-03-29 02:23:16 +0000306 if (ENABLE_FEATURE_CLEAN_UP)
307 config_close(parser);
308 return 0;
309}
310
311int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
312int sysctl_main(int argc UNUSED_PARAM, char **argv)
313{
314 int retval;
315 int opt;
316
317 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
318 argv += optind;
319 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
320 option_mask32 = opt;
321
322 if (opt & FLAG_PRELOAD_FILE) {
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200323 int cur_dir_fd;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000324 option_mask32 |= FLAG_WRITE;
Denys Vlasenko9cf89cd2017-08-05 13:45:22 +0200325 if (!*argv)
326 *--argv = (char*)"/etc/sysctl.conf";
327 cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
328 do {
329 /* xchdir("/proc/sys") is inside */
330 sysctl_handle_preload_file(*argv);
331 xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
332 } while (*++argv);
333 return 0; /* procps-ng 3.3.10 does not flag parse errors */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000334 }
335 xchdir("/proc/sys");
Denis Vlasenko038fe442009-03-29 02:23:16 +0000336 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
337 return sysctl_act_recursive(".");
338 }
339
Denys Vlasenko679c30e2019-02-08 14:27:21 +0100340//TODO: if(!argv[0]) bb_show_usage() ?
341
Denis Vlasenko038fe442009-03-29 02:23:16 +0000342 retval = 0;
343 while (*argv) {
344 sysctl_dots_to_slashes(*argv);
345 retval |= sysctl_act_recursive(*argv);
346 argv++;
347 }
348
349 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000350}