blob: 60d9c0afc0c3ed20040080e23d1a0473b4a7b9d2 [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 Vlasenko64309f82007-11-29 06:40:28 +000019static int sysctl_read_setting(const char *setting);
20static int sysctl_write_setting(const char *setting);
21static int sysctl_display_all(const char *path);
22static int sysctl_preload_file_and_exit(const char *filename);
Denis Vlasenko58cc52a2008-10-15 08:22:55 +000023static void sysctl_dots_to_slashes(char *name);
Eric Andersenb9050282003-12-24 06:02:11 +000024
Denis Vlasenko5a28a252007-10-29 19:22:13 +000025static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";
26static const char PROC_SYS[] ALIGN1 = "/proc/sys/";
27enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };
Eric Andersenb9050282003-12-24 06:02:11 +000028
Denis Vlasenkocfdb7112008-10-15 08:29:17 +000029static const char msg_unknown_key[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000030 "error: '%s' is an unknown key";
Eric Andersenb9050282003-12-24 06:02:11 +000031
Glenn L McGrathab821542003-12-26 02:19:34 +000032static void dwrite_str(int fd, const char *buf)
33{
34 write(fd, buf, strlen(buf));
35}
36
Denis Vlasenko64309f82007-11-29 06:40:28 +000037enum {
38 FLAG_SHOW_KEYS = 1 << 0,
39 FLAG_SHOW_KEY_ERRORS = 1 << 1,
40 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
41 FLAG_SHOW_ALL = 1 << 3,
42 FLAG_PRELOAD_FILE = 1 << 4,
43 FLAG_WRITE = 1 << 5,
44};
45
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000046int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000047int sysctl_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenb9050282003-12-24 06:02:11 +000048{
Denis Vlasenko64309f82007-11-29 06:40:28 +000049 int retval;
50 int opt;
Eric Andersenb9050282003-12-24 06:02:11 +000051
Denis Vlasenko64309f82007-11-29 06:40:28 +000052 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
53 argv += optind;
54 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
55 option_mask32 ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
Eric Andersenb9050282003-12-24 06:02:11 +000056
Denis Vlasenko64309f82007-11-29 06:40:28 +000057 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL))
58 return sysctl_display_all(PROC_SYS);
59 if (opt & FLAG_PRELOAD_FILE)
60 return sysctl_preload_file_and_exit(*argv ? *argv : ETC_SYSCTL_CONF);
Eric Andersenb9050282003-12-24 06:02:11 +000061
Denis Vlasenko64309f82007-11-29 06:40:28 +000062 retval = 0;
63 while (*argv) {
64 if (opt & FLAG_WRITE)
65 retval |= sysctl_write_setting(*argv);
66 else
67 retval |= sysctl_read_setting(*argv);
68 argv++;
Eric Andersenb9050282003-12-24 06:02:11 +000069 }
Denis Vlasenko64309f82007-11-29 06:40:28 +000070
Eric Andersenb9050282003-12-24 06:02:11 +000071 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +000072} /* end sysctl_main() */
Eric Andersenb9050282003-12-24 06:02:11 +000073
Denis Vlasenko81944c92008-10-15 08:45:54 +000074/* Set sysctl's from a conf file. Format example:
75 * # Controls IP packet forwarding
76 * net.ipv4.ip_forward = 0
Eric Andersenb9050282003-12-24 06:02:11 +000077 */
Denis Vlasenko64309f82007-11-29 06:40:28 +000078static int sysctl_preload_file_and_exit(const char *filename)
Eric Andersenb9050282003-12-24 06:02:11 +000079{
Denis Vlasenko4a717e02008-07-20 13:01:56 +000080 char *token[2];
81 parser_t *parser;
Eric Andersenb9050282003-12-24 06:02:11 +000082
Denis Vlasenko4a717e02008-07-20 13:01:56 +000083 parser = config_open(filename);
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 Vlasenko4a717e02008-07-20 13:01:56 +000086#if 0
Denis Vlasenko81944c92008-10-15 08:45:54 +000087 char *s = xasprintf("%s=%s", token[0], token[1]);
88 sysctl_write_setting(s);
89 free(s);
90#else /* Save ~4 bytes by using parser internals */
91 sprintf(parser->line, "%s=%s", token[0], token[1]); // must have room by definition
92 sysctl_write_setting(parser->line);
Denis Vlasenko4a717e02008-07-20 13:01:56 +000093#endif
Eric Andersenb9050282003-12-24 06:02:11 +000094 }
Denis Vlasenko64309f82007-11-29 06:40:28 +000095 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko4a717e02008-07-20 13:01:56 +000096 config_close(parser);
Eric Andersenb9050282003-12-24 06:02:11 +000097 return 0;
Denis Vlasenko64309f82007-11-29 06:40:28 +000098} /* end sysctl_preload_file_and_exit() */
Eric Andersenb9050282003-12-24 06:02:11 +000099
Denis Vlasenko64309f82007-11-29 06:40:28 +0000100static int sysctl_write_setting(const char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +0000101{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000102 int retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000103 const char *name;
Eric Andersenb9050282003-12-24 06:02:11 +0000104 const char *value;
105 const char *equals;
106 char *tmpname, *outname, *cptr;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000107 int fd;
Eric Andersenb9050282003-12-24 06:02:11 +0000108
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000109 name = setting;
110 equals = strchr(setting, '=');
111 if (!equals) {
Denis Vlasenkocfdb7112008-10-15 08:29:17 +0000112 bb_error_msg("error: '%s' must be of the form name=value", setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000113 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000114 }
115
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000116 value = equals + 1; /* point to the value in name=value */
117 if (name == equals || !*value) {
Denis Vlasenkocfdb7112008-10-15 08:29:17 +0000118 bb_error_msg("error: malformed setting '%s'", setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000119 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000120 }
121
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000122 tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
123 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000124
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000125 sysctl_dots_to_slashes(tmpname);
Eric Andersenb9050282003-12-24 06:02:11 +0000126
127 while ((cptr = strchr(outname, '/')) != NULL)
128 *cptr = '.';
129
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000130 fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
131 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 Vlasenkocfdb7112008-10-15 08:29:17 +0000135 bb_error_msg(msg_unknown_key, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000136 break;
137 default:
Denis Vlasenkocfdb7112008-10-15 08:29:17 +0000138 bb_perror_msg("error setting key '%s'", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000139 break;
140 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000141 retval = EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000142 } else {
Glenn L McGrathab821542003-12-26 02:19:34 +0000143 dwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000144 close(fd);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000145 if (option_mask32 & FLAG_SHOW_KEYS) {
146 printf("%s = ", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000147 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000148 puts(value);
149 retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000150 }
151
Eric Andersenb9050282003-12-24 06:02:11 +0000152 free(tmpname);
153 free(outname);
154 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000155} /* end sysctl_write_setting() */
Eric Andersenb9050282003-12-24 06:02:11 +0000156
Denis Vlasenko64309f82007-11-29 06:40:28 +0000157static int sysctl_read_setting(const char *name)
Eric Andersenb9050282003-12-24 06:02:11 +0000158{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000159 int retval;
Eric Andersenb9050282003-12-24 06:02:11 +0000160 char *tmpname, *outname, *cptr;
161 char inbuf[1025];
Eric Andersenb9050282003-12-24 06:02:11 +0000162 FILE *fp;
163
Denis Vlasenko64309f82007-11-29 06:40:28 +0000164 if (!*name) {
165 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenkocfdb7112008-10-15 08:29:17 +0000166 bb_error_msg(msg_unknown_key, name);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000167 return -1;
168 }
Eric Andersenb9050282003-12-24 06:02:11 +0000169
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000170 tmpname = concat_path_file(PROC_SYS, name);
171 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000172
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000173 sysctl_dots_to_slashes(tmpname);
174
Eric Andersenb9050282003-12-24 06:02:11 +0000175 while ((cptr = strchr(outname, '/')) != NULL)
176 *cptr = '.';
177
Denis Vlasenko5415c852008-07-21 23:05:26 +0000178 fp = fopen_for_read(tmpname);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000179 if (fp == NULL) {
Eric Andersenb9050282003-12-24 06:02:11 +0000180 switch (errno) {
181 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000182 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
Denis Vlasenkocfdb7112008-10-15 08:29:17 +0000183 bb_error_msg(msg_unknown_key, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000184 break;
185 default:
Denis Vlasenkocfdb7112008-10-15 08:29:17 +0000186 bb_perror_msg("error reading key '%s'", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000187 break;
188 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000189 retval = EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000190 } else {
191 while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000192 if (option_mask32 & FLAG_SHOW_KEYS) {
193 printf("%s = ", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000194 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000195 fputs(inbuf, stdout);
Eric Andersenb9050282003-12-24 06:02:11 +0000196 }
197 fclose(fp);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000198 retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000199 }
200
201 free(tmpname);
202 free(outname);
203 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000204} /* end sysctl_read_setting() */
Eric Andersenb9050282003-12-24 06:02:11 +0000205
Denis Vlasenko64309f82007-11-29 06:40:28 +0000206static int sysctl_display_all(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000207{
Denis Vlasenko81944c92008-10-15 08:45:54 +0000208 int retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000209 DIR *dp;
210 struct dirent *de;
211 char *tmpdir;
212 struct stat ts;
213
Denis Vlasenko51742f42007-04-12 00:32:05 +0000214 dp = opendir(path);
215 if (!dp) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000216 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000217 }
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000218 while ((de = readdir(dp)) != NULL) {
219 tmpdir = concat_subpath_file(path, de->d_name);
220 if (tmpdir == NULL)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000221 continue; /* . or .. */
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000222 if (stat(tmpdir, &ts) != 0) {
223 bb_perror_msg(tmpdir);
224 } else if (S_ISDIR(ts.st_mode)) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000225 retval |= sysctl_display_all(tmpdir);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000226 } else {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000227 retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000228 }
229 free(tmpdir);
230 } /* end while */
231 closedir(dp);
Eric Andersenb9050282003-12-24 06:02:11 +0000232
233 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000234} /* end sysctl_display_all() */
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000235
236static void sysctl_dots_to_slashes(char *name)
237{
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000238 char *cptr, *last_good;
239 char *end = name + strlen(name) - 1;
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000240
241 /* Example from bug 3894:
242 * net.ipv4.conf.eth0.100.mc_forwarding ->
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000243 * net/ipv4/conf/eth0.100/mc_forwarding. NB:
244 * net/ipv4/conf/eth0/mc_forwarding *also exists*,
245 * therefore we must start from the end, and if
246 * we replaced even one . -> /, start over again,
247 * but never replace dots before the position
248 * where replacement occurred. */
249 last_good = name - 1;
250 again:
251 cptr = end;
252 while (cptr > last_good) {
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000253 if (*cptr == '.') {
254 *cptr = '\0';
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000255 if (access(name, F_OK) == 0) {
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000256 *cptr = '/';
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000257 last_good = cptr;
258 goto again;
259 }
260 *cptr = '.';
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000261 }
Denis Vlasenkoe915a1a2008-10-15 09:43:35 +0000262 cptr--;
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000263 }
264} /* end sysctl_dots_to_slashes() */