blob: 7a5bf14358b160c9042c7799739786aadcceef51 [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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Eric Andersenb9050282003-12-24 06:02:11 +000015
Denis Vlasenko64309f82007-11-29 06:40:28 +000016enum {
17 FLAG_SHOW_KEYS = 1 << 0,
18 FLAG_SHOW_KEY_ERRORS = 1 << 1,
19 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
20 FLAG_SHOW_ALL = 1 << 3,
21 FLAG_PRELOAD_FILE = 1 << 4,
22 FLAG_WRITE = 1 << 5,
23};
Denis Vlasenko038fe442009-03-29 02:23:16 +000024#define OPTION_STR "neAapw"
Denis Vlasenko64309f82007-11-29 06:40:28 +000025
Denis Vlasenko038fe442009-03-29 02:23:16 +000026static void sysctl_dots_to_slashes(char *name)
Eric Andersenb9050282003-12-24 06:02:11 +000027{
Denis Vlasenko038fe442009-03-29 02:23:16 +000028 char *cptr, *last_good, *end;
Eric Andersenb9050282003-12-24 06:02:11 +000029
Denis Vlasenko038fe442009-03-29 02:23:16 +000030 /* Convert minimum number of '.' to '/' so that
31 * we end up with existing file's name.
32 *
33 * Example from bug 3894:
34 * net.ipv4.conf.eth0.100.mc_forwarding ->
35 * net/ipv4/conf/eth0.100/mc_forwarding
36 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
37 * therefore we must start from the end, and if
38 * we replaced even one . -> /, start over again,
39 * but never replace dots before the position
40 * where last replacement occurred.
41 *
42 * Another bug we later had is that
43 * net.ipv4.conf.eth0.100
44 * (without .mc_forwarding) was mishandled.
45 *
46 * To set up testing: modprobe 8021q; vconfig add eth0 100
47 */
48 end = name + strlen(name);
49 last_good = name - 1;
50 *end = '.'; /* trick the loop into trying full name too */
Eric Andersenb9050282003-12-24 06:02:11 +000051
Denis Vlasenko038fe442009-03-29 02:23:16 +000052 again:
53 cptr = end;
54 while (cptr > last_good) {
55 if (*cptr == '.') {
56 *cptr = '\0';
57 //bb_error_msg("trying:'%s'", name);
58 if (access(name, F_OK) == 0) {
Denys Vlasenkoc2fdd412010-03-27 05:02:00 +010059 *cptr = '/';
Denis Vlasenko038fe442009-03-29 02:23:16 +000060 //bb_error_msg("replaced:'%s'", name);
61 last_good = cptr;
62 goto again;
63 }
64 *cptr = '.';
65 }
66 cptr--;
Denis Vlasenko54d10052008-12-24 03:11:43 +000067 }
Denis Vlasenko038fe442009-03-29 02:23:16 +000068 *end = '\0';
Denis Vlasenko54d10052008-12-24 03:11:43 +000069}
Eric Andersenb9050282003-12-24 06:02:11 +000070
Denis Vlasenko54d10052008-12-24 03:11:43 +000071static int sysctl_act_on_setting(char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +000072{
Denis Vlasenko54d10052008-12-24 03:11:43 +000073 int fd, retval = EXIT_SUCCESS;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +000074 char *cptr, *outname;
75 char *value = value; /* for compiler */
Eric Andersenb9050282003-12-24 06:02:11 +000076
Denis Vlasenko54d10052008-12-24 03:11:43 +000077 outname = xstrdup(setting);
78
79 cptr = outname;
80 while (*cptr) {
81 if (*cptr == '/')
82 *cptr = '.';
83 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +000084 }
85
Denis Vlasenko54d10052008-12-24 03:11:43 +000086 if (option_mask32 & FLAG_WRITE) {
87 cptr = strchr(setting, '=');
88 if (cptr == NULL) {
89 bb_error_msg("error: '%s' must be of the form name=value",
90 outname);
91 retval = EXIT_FAILURE;
92 goto end;
93 }
94 value = cptr + 1; /* point to the value in name=value */
95 if (setting == cptr || !*value) {
96 bb_error_msg("error: malformed setting '%s'", outname);
97 retval = EXIT_FAILURE;
98 goto end;
99 }
100 *cptr = '\0';
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000101 outname[cptr - setting] = '\0';
102 /* procps 3.2.7 actually uses these flags */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000103 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
104 } else {
105 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000106 }
107
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000108 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000109 switch (errno) {
110 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000111 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000112 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000113 break;
114 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000115 bb_perror_msg("error %sing key '%s'",
116 option_mask32 & FLAG_WRITE ?
117 "sett" : "read",
118 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000119 break;
120 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000121 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000122 goto end;
123 }
124
125 if (option_mask32 & FLAG_WRITE) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000126//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000127 xwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000128 close(fd);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000129 if (option_mask32 & FLAG_SHOW_KEYS)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000130 printf("%s = ", outname);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000131 puts(value);
Eric Andersenb9050282003-12-24 06:02:11 +0000132 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000133 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000134
Denis Vlasenko54d10052008-12-24 03:11:43 +0000135 value = cptr = xmalloc_read(fd, NULL);
136 close(fd);
137 if (value == NULL) {
138 bb_perror_msg("error reading key '%s'", outname);
139 goto end;
140 }
141
142 /* dev.cdrom.info and sunrpc.transports, for example,
143 * are multi-line. Try "sysctl sunrpc.transports"
144 */
145 while ((c = *cptr) != '\0') {
146 if (option_mask32 & FLAG_SHOW_KEYS)
147 printf("%s = ", outname);
148 while (1) {
149 fputc(c, stdout);
150 cptr++;
151 if (c == '\n')
152 break;
153 c = *cptr;
154 if (c == '\0')
155 break;
156 }
157 }
158 free(value);
159 }
160 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000161 free(outname);
162 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000163}
Eric Andersenb9050282003-12-24 06:02:11 +0000164
Denis Vlasenko038fe442009-03-29 02:23:16 +0000165static int sysctl_act_recursive(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000166{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000167 DIR *dirp;
168 struct stat buf;
169 struct dirent *entry;
170 char *next;
171 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000172
Denis Vlasenko54d10052008-12-24 03:11:43 +0000173 stat(path, &buf);
174 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
175 dirp = opendir(path);
176 if (dirp == NULL)
177 return -1;
178 while ((entry = readdir(dirp)) != NULL) {
Denis Vlasenko5a6617a2009-03-29 02:22:19 +0000179 next = concat_subpath_file(path, entry->d_name);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000180 if (next == NULL)
181 continue; /* d_name is "." or ".." */
182 /* if path was ".", drop "./" prefix: */
Denis Vlasenko038fe442009-03-29 02:23:16 +0000183 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
Denis Vlasenko54d10052008-12-24 03:11:43 +0000184 next + 2 : next);
185 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000186 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000187 closedir(dirp);
188 } else {
189 char *name = xstrdup(path);
190 retval |= sysctl_act_on_setting(name);
191 free(name);
192 }
Eric Andersenb9050282003-12-24 06:02:11 +0000193
194 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000195}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000196
Denis Vlasenko038fe442009-03-29 02:23:16 +0000197/* Set sysctl's from a conf file. Format example:
198 * # Controls IP packet forwarding
199 * net.ipv4.ip_forward = 0
200 */
201static int sysctl_handle_preload_file(const char *filename)
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000202{
Denis Vlasenko038fe442009-03-29 02:23:16 +0000203 char *token[2];
204 parser_t *parser;
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000205
Denis Vlasenko038fe442009-03-29 02:23:16 +0000206 parser = config_open(filename);
207 /* Must do it _after_ config_open(): */
208 xchdir("/proc/sys");
209 /* xchroot(".") - if you are paranoid */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000210
Denis Vlasenko038fe442009-03-29 02:23:16 +0000211//TODO: ';' is comment char too
212//TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
213// (but _whitespace_ from ends should be trimmed first (and we do it right))
214//TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
215 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
Denys Vlasenkof427c802009-05-10 23:41:29 +0200216 char *tp;
Denis Vlasenko038fe442009-03-29 02:23:16 +0000217 sysctl_dots_to_slashes(token[0]);
Denys Vlasenkof427c802009-05-10 23:41:29 +0200218 tp = xasprintf("%s=%s", token[0], token[1]);
219 sysctl_act_recursive(tp);
220 free(tp);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000221 }
Denis Vlasenko038fe442009-03-29 02:23:16 +0000222 if (ENABLE_FEATURE_CLEAN_UP)
223 config_close(parser);
224 return 0;
225}
226
227int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
228int sysctl_main(int argc UNUSED_PARAM, char **argv)
229{
230 int retval;
231 int opt;
232
233 opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
234 argv += optind;
235 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
236 option_mask32 = opt;
237
238 if (opt & FLAG_PRELOAD_FILE) {
239 option_mask32 |= FLAG_WRITE;
240 /* xchdir("/proc/sys") is inside */
241 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
242 }
243 xchdir("/proc/sys");
244 /* xchroot(".") - if you are paranoid */
245 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
246 return sysctl_act_recursive(".");
247 }
248
249 retval = 0;
250 while (*argv) {
251 sysctl_dots_to_slashes(*argv);
252 retval |= sysctl_act_recursive(*argv);
253 argv++;
254 }
255
256 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000257}