blob: 262574e61e63723ee2775868371197fede43cb50 [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:
10 * v1.01:
11 * - added -p <preload> to preload values from a file
12 * v1.01.1
13 * - busybox applet aware by <solar@gentoo.org>
Eric Andersenc7bda1c2004-03-15 08:29:22 +000014 *
Eric Andersenb9050282003-12-24 06:02:11 +000015 */
16
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Eric Andersenb9050282003-12-24 06:02:11 +000018
Denis Vlasenko54d10052008-12-24 03:11:43 +000019static int sysctl_act_on_setting(char *setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +000020static int sysctl_display_all(const char *path);
Denis Vlasenko54d10052008-12-24 03:11:43 +000021static int sysctl_handle_preload_file(const char *filename);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +000022static void sysctl_dots_to_slashes(char *name);
Eric Andersenb9050282003-12-24 06:02:11 +000023
Glenn L McGrathab821542003-12-26 02:19:34 +000024static void dwrite_str(int fd, const char *buf)
25{
26 write(fd, buf, strlen(buf));
27}
28
Denis Vlasenko64309f82007-11-29 06:40:28 +000029enum {
30 FLAG_SHOW_KEYS = 1 << 0,
31 FLAG_SHOW_KEY_ERRORS = 1 << 1,
32 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
33 FLAG_SHOW_ALL = 1 << 3,
34 FLAG_PRELOAD_FILE = 1 << 4,
35 FLAG_WRITE = 1 << 5,
36};
37
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000038int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000039int sysctl_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenb9050282003-12-24 06:02:11 +000040{
Denis Vlasenko64309f82007-11-29 06:40:28 +000041 int retval;
42 int opt;
Eric Andersenb9050282003-12-24 06:02:11 +000043
Denis Vlasenko64309f82007-11-29 06:40:28 +000044 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
45 argv += optind;
46 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
Denis Vlasenko54d10052008-12-24 03:11:43 +000047 option_mask32 = opt;
Eric Andersenb9050282003-12-24 06:02:11 +000048
Denis Vlasenko54d10052008-12-24 03:11:43 +000049 if (opt & FLAG_PRELOAD_FILE) {
50 option_mask32 |= FLAG_WRITE;
51 /* xchdir("/proc/sys") is inside */
52 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
53 }
54 xchdir("/proc/sys");
55 /* xchroot(".") - if you are paranoid */
56 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
57 return sysctl_display_all(".");
58 }
Eric Andersenb9050282003-12-24 06:02:11 +000059
Denis Vlasenko64309f82007-11-29 06:40:28 +000060 retval = 0;
61 while (*argv) {
Denis Vlasenko54d10052008-12-24 03:11:43 +000062 sysctl_dots_to_slashes(*argv);
63 retval |= sysctl_display_all(*argv);
Denis Vlasenko64309f82007-11-29 06:40:28 +000064 argv++;
Eric Andersenb9050282003-12-24 06:02:11 +000065 }
Denis Vlasenko64309f82007-11-29 06:40:28 +000066
Eric Andersenb9050282003-12-24 06:02:11 +000067 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +000068}
Eric Andersenb9050282003-12-24 06:02:11 +000069
Denis Vlasenko81944c92008-10-15 08:45:54 +000070/* Set sysctl's from a conf file. Format example:
71 * # Controls IP packet forwarding
72 * net.ipv4.ip_forward = 0
Eric Andersenb9050282003-12-24 06:02:11 +000073 */
Denis Vlasenko54d10052008-12-24 03:11:43 +000074static int sysctl_handle_preload_file(const char *filename)
Eric Andersenb9050282003-12-24 06:02:11 +000075{
Denis Vlasenko4a717e02008-07-20 13:01:56 +000076 char *token[2];
77 parser_t *parser;
Eric Andersenb9050282003-12-24 06:02:11 +000078
Denis Vlasenko4a717e02008-07-20 13:01:56 +000079 parser = config_open(filename);
Denis Vlasenko54d10052008-12-24 03:11:43 +000080 /* Must do it _after_ config_open(): */
81 xchdir("/proc/sys");
82 /* xchroot(".") - if you are paranoid */
83
Denis Vlasenko81944c92008-10-15 08:45:54 +000084// TODO: ';' is comment char too
85 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
Denis Vlasenko54d10052008-12-24 03:11:43 +000086 /* Save ~4 bytes by using parser internals */
87 /* parser->line is big enough for sprintf */
88 sprintf(parser->line, "%s=%s", token[0], token[1]);
89 sysctl_dots_to_slashes(parser->line);
90 sysctl_display_all(parser->line);
Eric Andersenb9050282003-12-24 06:02:11 +000091 }
Denis Vlasenko64309f82007-11-29 06:40:28 +000092 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko4a717e02008-07-20 13:01:56 +000093 config_close(parser);
Eric Andersenb9050282003-12-24 06:02:11 +000094 return 0;
Denis Vlasenko54d10052008-12-24 03:11:43 +000095}
Eric Andersenb9050282003-12-24 06:02:11 +000096
Denis Vlasenko54d10052008-12-24 03:11:43 +000097static int sysctl_act_on_setting(char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +000098{
Denis Vlasenko54d10052008-12-24 03:11:43 +000099 int fd, retval = EXIT_SUCCESS;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +0000100 char *cptr, *outname;
101 char *value = value; /* for compiler */
Eric Andersenb9050282003-12-24 06:02:11 +0000102
Denis Vlasenko54d10052008-12-24 03:11:43 +0000103 outname = xstrdup(setting);
104
105 cptr = outname;
106 while (*cptr) {
107 if (*cptr == '/')
108 *cptr = '.';
109 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +0000110 }
111
Denis Vlasenko54d10052008-12-24 03:11:43 +0000112 if (option_mask32 & FLAG_WRITE) {
113 cptr = strchr(setting, '=');
114 if (cptr == NULL) {
115 bb_error_msg("error: '%s' must be of the form name=value",
116 outname);
117 retval = EXIT_FAILURE;
118 goto end;
119 }
120 value = cptr + 1; /* point to the value in name=value */
121 if (setting == cptr || !*value) {
122 bb_error_msg("error: malformed setting '%s'", outname);
123 retval = EXIT_FAILURE;
124 goto end;
125 }
126 *cptr = '\0';
127 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
128 } else {
129 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000130 }
131
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000132 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000133 switch (errno) {
134 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000135 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000136 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000137 break;
138 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000139 bb_perror_msg("error %sing key '%s'",
140 option_mask32 & FLAG_WRITE ?
141 "sett" : "read",
142 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000143 break;
144 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000145 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000146 goto end;
147 }
148
149 if (option_mask32 & FLAG_WRITE) {
Glenn L McGrathab821542003-12-26 02:19:34 +0000150 dwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000151 close(fd);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000152 if (option_mask32 & FLAG_SHOW_KEYS)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000153 printf("%s = ", outname);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000154 puts(value);
Eric Andersenb9050282003-12-24 06:02:11 +0000155 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000156 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000157
Denis Vlasenko54d10052008-12-24 03:11:43 +0000158 value = cptr = xmalloc_read(fd, NULL);
159 close(fd);
160 if (value == NULL) {
161 bb_perror_msg("error reading key '%s'", outname);
162 goto end;
163 }
164
165 /* dev.cdrom.info and sunrpc.transports, for example,
166 * are multi-line. Try "sysctl sunrpc.transports"
167 */
168 while ((c = *cptr) != '\0') {
169 if (option_mask32 & FLAG_SHOW_KEYS)
170 printf("%s = ", outname);
171 while (1) {
172 fputc(c, stdout);
173 cptr++;
174 if (c == '\n')
175 break;
176 c = *cptr;
177 if (c == '\0')
178 break;
179 }
180 }
181 free(value);
182 }
183 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000184 free(outname);
185 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000186}
Eric Andersenb9050282003-12-24 06:02:11 +0000187
Denis Vlasenko64309f82007-11-29 06:40:28 +0000188static int sysctl_display_all(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000189{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000190 DIR *dirp;
191 struct stat buf;
192 struct dirent *entry;
193 char *next;
194 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000195
Denis Vlasenko54d10052008-12-24 03:11:43 +0000196 stat(path, &buf);
197 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
198 dirp = opendir(path);
199 if (dirp == NULL)
200 return -1;
201 while ((entry = readdir(dirp)) != NULL) {
202 next = concat_subpath_file(
203 path, entry->d_name);
204 if (next == NULL)
205 continue; /* d_name is "." or ".." */
206 /* if path was ".", drop "./" prefix: */
207 retval |= sysctl_display_all((next[0] == '.' && next[1] == '/') ?
208 next + 2 : next);
209 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000210 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000211 closedir(dirp);
212 } else {
213 char *name = xstrdup(path);
214 retval |= sysctl_act_on_setting(name);
215 free(name);
216 }
Eric Andersenb9050282003-12-24 06:02:11 +0000217
218 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000219}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000220
221static void sysctl_dots_to_slashes(char *name)
222{
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000223 char *cptr, *last_good, *end;
224
Denis Vlasenko54d10052008-12-24 03:11:43 +0000225 /* Convert minimum number of '.' to '/' so that
226 * we end up with existing file's name.
227 *
228 * Example from bug 3894:
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000229 * net.ipv4.conf.eth0.100.mc_forwarding ->
Denis Vlasenko54d10052008-12-24 03:11:43 +0000230 * net/ipv4/conf/eth0.100/mc_forwarding
231 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000232 * therefore we must start from the end, and if
233 * we replaced even one . -> /, start over again,
234 * but never replace dots before the position
Denis Vlasenko54d10052008-12-24 03:11:43 +0000235 * where last replacement occurred.
236 */
237 end = name + strlen(name);
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000238 last_good = name - 1;
Denis Vlasenkod6e8f942008-12-29 01:03:17 +0000239 *end = '.'; /* trick the loop into trying full name too */
Denis Vlasenko54d10052008-12-24 03:11:43 +0000240
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000241 again:
242 cptr = end;
243 while (cptr > last_good) {
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000244 if (*cptr == '.') {
245 *cptr = '\0';
Denis Vlasenko54d10052008-12-24 03:11:43 +0000246 //bb_error_msg("trying:'%s'", name);
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000247 if (access(name, F_OK) == 0) {
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000248 *cptr = '/';
Denis Vlasenko54d10052008-12-24 03:11:43 +0000249 *end = '\0'; /* prevent trailing '/' */
250 //bb_error_msg("replaced:'%s'", name);
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000251 last_good = cptr;
252 goto again;
253 }
254 *cptr = '.';
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000255 }
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000256 cptr--;
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000257 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000258 *end = '\0';
259}