blob: 23fb16e9eaada9d396bd9f9eb0be719f4f4b80fc [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;
100 char *cptr, *outname, *value;
Eric Andersenb9050282003-12-24 06:02:11 +0000101
Denis Vlasenko54d10052008-12-24 03:11:43 +0000102 outname = xstrdup(setting);
103
104 cptr = outname;
105 while (*cptr) {
106 if (*cptr == '/')
107 *cptr = '.';
108 cptr++;
Eric Andersenb9050282003-12-24 06:02:11 +0000109 }
110
Denis Vlasenko54d10052008-12-24 03:11:43 +0000111 if (option_mask32 & FLAG_WRITE) {
112 cptr = strchr(setting, '=');
113 if (cptr == NULL) {
114 bb_error_msg("error: '%s' must be of the form name=value",
115 outname);
116 retval = EXIT_FAILURE;
117 goto end;
118 }
119 value = cptr + 1; /* point to the value in name=value */
120 if (setting == cptr || !*value) {
121 bb_error_msg("error: malformed setting '%s'", outname);
122 retval = EXIT_FAILURE;
123 goto end;
124 }
125 *cptr = '\0';
126 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
127 } else {
128 fd = open(setting, O_RDONLY);
Eric Andersenb9050282003-12-24 06:02:11 +0000129 }
130
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000131 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000132 switch (errno) {
133 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000134 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenko54d10052008-12-24 03:11:43 +0000135 bb_error_msg("error: '%s' is an unknown key", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000136 break;
137 default:
Denis Vlasenko54d10052008-12-24 03:11:43 +0000138 bb_perror_msg("error %sing key '%s'",
139 option_mask32 & FLAG_WRITE ?
140 "sett" : "read",
141 outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000142 break;
143 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000144 retval = EXIT_FAILURE;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000145 goto end;
146 }
147
148 if (option_mask32 & FLAG_WRITE) {
Glenn L McGrathab821542003-12-26 02:19:34 +0000149 dwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000150 close(fd);
Denis Vlasenko54d10052008-12-24 03:11:43 +0000151 if (option_mask32 & FLAG_SHOW_KEYS)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000152 printf("%s = ", outname);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000153 puts(value);
Eric Andersenb9050282003-12-24 06:02:11 +0000154 } else {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000155 char c;
Eric Andersenb9050282003-12-24 06:02:11 +0000156
Denis Vlasenko54d10052008-12-24 03:11:43 +0000157 value = cptr = xmalloc_read(fd, NULL);
158 close(fd);
159 if (value == NULL) {
160 bb_perror_msg("error reading key '%s'", outname);
161 goto end;
162 }
163
164 /* dev.cdrom.info and sunrpc.transports, for example,
165 * are multi-line. Try "sysctl sunrpc.transports"
166 */
167 while ((c = *cptr) != '\0') {
168 if (option_mask32 & FLAG_SHOW_KEYS)
169 printf("%s = ", outname);
170 while (1) {
171 fputc(c, stdout);
172 cptr++;
173 if (c == '\n')
174 break;
175 c = *cptr;
176 if (c == '\0')
177 break;
178 }
179 }
180 free(value);
181 }
182 end:
Eric Andersenb9050282003-12-24 06:02:11 +0000183 free(outname);
184 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000185}
Eric Andersenb9050282003-12-24 06:02:11 +0000186
Denis Vlasenko64309f82007-11-29 06:40:28 +0000187static int sysctl_display_all(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000188{
Denis Vlasenko54d10052008-12-24 03:11:43 +0000189 DIR *dirp;
190 struct stat buf;
191 struct dirent *entry;
192 char *next;
193 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000194
Denis Vlasenko54d10052008-12-24 03:11:43 +0000195 stat(path, &buf);
196 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
197 dirp = opendir(path);
198 if (dirp == NULL)
199 return -1;
200 while ((entry = readdir(dirp)) != NULL) {
201 next = concat_subpath_file(
202 path, entry->d_name);
203 if (next == NULL)
204 continue; /* d_name is "." or ".." */
205 /* if path was ".", drop "./" prefix: */
206 retval |= sysctl_display_all((next[0] == '.' && next[1] == '/') ?
207 next + 2 : next);
208 free(next);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000209 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000210 closedir(dirp);
211 } else {
212 char *name = xstrdup(path);
213 retval |= sysctl_act_on_setting(name);
214 free(name);
215 }
Eric Andersenb9050282003-12-24 06:02:11 +0000216
217 return retval;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000218}
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000219
220static void sysctl_dots_to_slashes(char *name)
221{
Denis Vlasenkoa9c3f7a2008-10-15 13:50:24 +0000222 char *cptr, *last_good, *end;
223
Denis Vlasenko54d10052008-12-24 03:11:43 +0000224 /* Convert minimum number of '.' to '/' so that
225 * we end up with existing file's name.
226 *
227 * Example from bug 3894:
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000228 * net.ipv4.conf.eth0.100.mc_forwarding ->
Denis Vlasenko54d10052008-12-24 03:11:43 +0000229 * net/ipv4/conf/eth0.100/mc_forwarding
230 * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000231 * therefore we must start from the end, and if
232 * we replaced even one . -> /, start over again,
233 * but never replace dots before the position
Denis Vlasenko54d10052008-12-24 03:11:43 +0000234 * where last replacement occurred.
235 */
236 end = name + strlen(name);
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000237 last_good = name - 1;
Denis Vlasenko54d10052008-12-24 03:11:43 +0000238 *end = '.'; /* trick the loop in trying full name too */
239
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000240 again:
241 cptr = end;
242 while (cptr > last_good) {
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000243 if (*cptr == '.') {
244 *cptr = '\0';
Denis Vlasenko54d10052008-12-24 03:11:43 +0000245 //bb_error_msg("trying:'%s'", name);
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000246 if (access(name, F_OK) == 0) {
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000247 *cptr = '/';
Denis Vlasenko54d10052008-12-24 03:11:43 +0000248 *end = '\0'; /* prevent trailing '/' */
249 //bb_error_msg("replaced:'%s'", name);
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000250 last_good = cptr;
251 goto again;
252 }
253 *cptr = '.';
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000254 }
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000255 cptr--;
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000256 }
Denis Vlasenko54d10052008-12-24 03:11:43 +0000257 *end = '\0';
258}