blob: f0883f05405a0a2d92c37d7b9211e28596061f20 [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
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020015//usage: "[OPTIONS] [KEY[=VALUE]]..."
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage:#define sysctl_full_usage "\n\n"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020017//usage: "Show/set kernel parameters\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage: "\n -e Don't warn about unknown keys"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020019//usage: "\n -n Don't show key names"
20//usage: "\n -a Show all values"
21/* Same as -a, no need to show it */
22/* //usage: "\n -A Show all values in table form" */
23//usage: "\n -w Set values"
24//usage: "\n -p FILE Set values from FILE (default /etc/sysctl.conf)"
25//usage: "\n -q Set values silently"
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage:
27//usage:#define sysctl_example_usage
28//usage: "sysctl [-n] [-e] variable...\n"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020029//usage: "sysctl [-n] [-e] [-q] -w variable=value...\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage: "sysctl [-n] [-e] -a\n"
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020031//usage: "sysctl [-n] [-e] [-q] -p file (default /etc/sysctl.conf)\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage: "sysctl [-n] [-e] -A\n"
33
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Eric Andersenb9050282003-12-24 06:02:11 +000035
Denis Vlasenko64309f82007-11-29 06:40:28 +000036enum {
37 FLAG_SHOW_KEYS = 1 << 0,
38 FLAG_SHOW_KEY_ERRORS = 1 << 1,
39 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
40 FLAG_SHOW_ALL = 1 << 3,
41 FLAG_PRELOAD_FILE = 1 << 4,
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020042/* TODO: procps 3.2.8 seems to not require -w for KEY=VAL to work: */
Denis Vlasenko64309f82007-11-29 06:40:28 +000043 FLAG_WRITE = 1 << 5,
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020044 FLAG_QUIET = 1 << 6,
Denis Vlasenko64309f82007-11-29 06:40:28 +000045};
Joshua Kahlenbergc4398512012-09-05 18:15:12 +020046#define OPTION_STR "neAapwq"
Denis Vlasenko64309f82007-11-29 06:40:28 +000047
Denis Vlasenko038fe442009-03-29 02:23:16 +000048static void sysctl_dots_to_slashes(char *name)
Eric Andersenb9050282003-12-24 06:02:11 +000049{
Denis Vlasenko038fe442009-03-29 02:23:16 +000050 char *cptr, *last_good, *end;
Eric Andersenb9050282003-12-24 06:02:11 +000051
Denis Vlasenko038fe442009-03-29 02:23:16 +000052 /* Convert minimum number of '.' to '/' so that
53 * we end up with existing file's name.
54 *
55 * Example from bug 3894:
56 * net.ipv4.conf.eth0.100.mc_forwarding ->
57 * net/ipv4/conf/eth0.100/mc_forwarding
58 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
59 * therefore we must start from the end, and if
60 * we replaced even one . -> /, start over again,
61 * but never replace dots before the position
62 * where last replacement occurred.
63 *
64 * Another bug we later had is that
65 * net.ipv4.conf.eth0.100
66 * (without .mc_forwarding) was mishandled.
67 *
68 * To set up testing: modprobe 8021q; vconfig add eth0 100
69 */
70 end = name + strlen(name);
71 last_good = name - 1;
72 *end = '.'; /* trick the loop into trying full name too */
Eric Andersenb9050282003-12-24 06:02:11 +000073
Denis Vlasenko038fe442009-03-29 02:23:16 +000074 again:
75 cptr = end;
76 while (cptr > last_good) {
77 if (*cptr == '.') {
78 *cptr = '\0';
79 //bb_error_msg("trying:'%s'", name);
80 if (access(name, F_OK) == 0) {
Denys Vlasenkoc2fdd412010-03-27 05:02:00 +010081 *cptr = '/';
Denis Vlasenko038fe442009-03-29 02:23:16 +000082 //bb_error_msg("replaced:'%s'", name);
83 last_good = cptr;
84 goto again;
85 }
86 *cptr = '.';
87 }
88 cptr--;
Denis Vlasenko54d10052008-12-24 03:11:43 +000089 }
Denis Vlasenko038fe442009-03-29 02:23:16 +000090 *end = '\0';
Denis Vlasenko54d10052008-12-24 03:11:43 +000091}
Eric Andersenb9050282003-12-24 06:02:11 +000092
Denis Vlasenko54d10052008-12-24 03:11:43 +000093static int sysctl_act_on_setting(char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +000094{
Denis Vlasenko54d10052008-12-24 03:11:43 +000095 int fd, retval = EXIT_SUCCESS;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +000096 char *cptr, *outname;
97 char *value = value; /* for compiler */
Eric Andersenb9050282003-12-24 06:02:11 +000098
Denis Vlasenko54d10052008-12-24 03:11:43 +000099 outname = xstrdup(setting);
100
101 cptr = outname;
102 while (*cptr) {
103 if (*cptr == '/')
104 *cptr = '.';
105 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +0000106 }
107
Denis Vlasenko54d10052008-12-24 03:11:43 +0000108 if (option_mask32 & FLAG_WRITE) {
109 cptr = strchr(setting, '=');
110 if (cptr == NULL) {
111 bb_error_msg("error: '%s' must be of the form name=value",
112 outname);
113 retval = EXIT_FAILURE;
114 goto end;
115 }
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200116 value = cptr + 1; /* point to the value in name=value */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000117 if (setting == cptr || !*value) {
118 bb_error_msg("error: malformed setting '%s'", outname);
119 retval = EXIT_FAILURE;
120 goto end;
121 }
122 *cptr = '\0';
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000123 outname[cptr - setting] = '\0';
124 /* procps 3.2.7 actually uses these flags */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000125 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
126 } else {
127 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000128 }
129
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000130 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000131 switch (errno) {
Denys Vlasenko6554d032014-02-24 17:28:43 +0100132 case EACCES:
133 /* Happens for write-only settings, e.g. net.ipv6.route.flush */
134 goto end;
Eric Andersenb9050282003-12-24 06:02:11 +0000135 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000136 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000137 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000138 break;
139 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000140 bb_perror_msg("error %sing key '%s'",
141 option_mask32 & FLAG_WRITE ?
142 "sett" : "read",
143 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000144 break;
145 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000146 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000147 goto end;
148 }
149
150 if (option_mask32 & FLAG_WRITE) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000151//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000152 xwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000153 close(fd);
Joshua Kahlenbergc4398512012-09-05 18:15:12 +0200154 if (!(option_mask32 & FLAG_QUIET)) {
155 if (option_mask32 & FLAG_SHOW_KEYS)
156 printf("%s = ", outname);
157 puts(value);
158 }
Eric Andersenb9050282003-12-24 06:02:11 +0000159 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000160 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000161
Denis Vlasenko54d10052008-12-24 03:11:43 +0000162 value = cptr = xmalloc_read(fd, NULL);
163 close(fd);
164 if (value == NULL) {
165 bb_perror_msg("error reading key '%s'", outname);
166 goto end;
167 }
168
169 /* dev.cdrom.info and sunrpc.transports, for example,
170 * are multi-line. Try "sysctl sunrpc.transports"
171 */
172 while ((c = *cptr) != '\0') {
173 if (option_mask32 & FLAG_SHOW_KEYS)
174 printf("%s = ", outname);
175 while (1) {
176 fputc(c, stdout);
177 cptr++;
178 if (c == '\n')
179 break;
180 c = *cptr;
181 if (c == '\0')
182 break;
183 }
184 }
185 free(value);
186 }
187 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000188 free(outname);
189 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000190}
Eric Andersenb9050282003-12-24 06:02:11 +0000191
Denis Vlasenko038fe442009-03-29 02:23:16 +0000192static int sysctl_act_recursive(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000193{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000194 DIR *dirp;
195 struct stat buf;
196 struct dirent *entry;
197 char *next;
198 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000199
Denis Vlasenko54d10052008-12-24 03:11:43 +0000200 stat(path, &buf);
201 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
202 dirp = opendir(path);
203 if (dirp == NULL)
204 return -1;
205 while ((entry = readdir(dirp)) != NULL) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000206 next = concat_subpath_file(path, entry->d_name);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000207 if (next == NULL)
208 continue; /* d_name is "." or ".." */
209 /* if path was ".", drop "./" prefix: */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000210 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
Denys Vlasenko69675782013-01-14 01:34:48 +0100211 next + 2 : next);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000212 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000213 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000214 closedir(dirp);
215 } else {
216 char *name = xstrdup(path);
217 retval |= sysctl_act_on_setting(name);
218 free(name);
219 }
Eric Andersenb9050282003-12-24 06:02:11 +0000220
221 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000222}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000223
Denis Vlasenko038fe442009-03-29 02:23:16 +0000224/* Set sysctl's from a conf file. Format example:
225 * # Controls IP packet forwarding
226 * net.ipv4.ip_forward = 0
227 */
228static int sysctl_handle_preload_file(const char *filename)
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000229{
Denis Vlasenko038fe442009-03-29 02:23:16 +0000230 char *token[2];
231 parser_t *parser;
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000232
Denis Vlasenko038fe442009-03-29 02:23:16 +0000233 parser = config_open(filename);
234 /* Must do it _after_ config_open(): */
235 xchdir("/proc/sys");
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100236 /* xchroot("/proc/sys") - if you are paranoid */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000237
Denis Vlasenko038fe442009-03-29 02:23:16 +0000238//TODO: ';' is comment char too
239//TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
240// (but _whitespace_ from ends should be trimmed first (and we do it right))
241//TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
Denys Vlasenkoadbbee42010-06-21 07:17:23 +0200242// can it be fixed by removing PARSE_COLLAPSE bit?
Denis Vlasenko038fe442009-03-29 02:23:16 +0000243 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
Denys Vlasenkof427c802009-05-10 23:41:29 +0200244 char *tp;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000245 sysctl_dots_to_slashes(token[0]);
Denys Vlasenkof427c802009-05-10 23:41:29 +0200246 tp = xasprintf("%s=%s", token[0], token[1]);
247 sysctl_act_recursive(tp);
248 free(tp);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000249 }
Denis Vlasenko038fe442009-03-29 02:23:16 +0000250 if (ENABLE_FEATURE_CLEAN_UP)
251 config_close(parser);
252 return 0;
253}
254
255int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
256int sysctl_main(int argc UNUSED_PARAM, char **argv)
257{
258 int retval;
259 int opt;
260
261 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
262 argv += optind;
263 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
264 option_mask32 = opt;
265
266 if (opt & FLAG_PRELOAD_FILE) {
267 option_mask32 |= FLAG_WRITE;
268 /* xchdir("/proc/sys") is inside */
269 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
270 }
271 xchdir("/proc/sys");
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100272 /* xchroot("/proc/sys") - if you are paranoid */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000273 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
274 return sysctl_act_recursive(".");
275 }
276
277 retval = 0;
278 while (*argv) {
279 sysctl_dots_to_slashes(*argv);
280 retval |= sysctl_act_recursive(*argv);
281 argv++;
282 }
283
284 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000285}