blob: 18caa2c1139ed9d13776b6463ba30cef16339263 [file] [log] [blame]
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +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
29/* error messages */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000030static const char ERR_MALFORMED_SETTING[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000031 "error: malformed setting '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000032static const char ERR_NO_EQUALS[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000033 "error: '%s' must be of the form name=value";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000034static const char ERR_INVALID_KEY[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000035 "error: '%s' is an unknown key";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000036static const char ERR_UNKNOWN_WRITING[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000037 "error setting key '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000038static const char ERR_UNKNOWN_READING[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000039 "error reading key '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000040static const char ERR_PERMISSION_DENIED[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000041 "error: permission denied on key '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000042static const char WARN_BAD_LINE[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000043 "warning: %s(%d): invalid syntax, continuing";
Eric Andersenb9050282003-12-24 06:02:11 +000044
45
Glenn L McGrathab821542003-12-26 02:19:34 +000046static void dwrite_str(int fd, const char *buf)
47{
48 write(fd, buf, strlen(buf));
49}
50
Denis Vlasenko64309f82007-11-29 06:40:28 +000051enum {
52 FLAG_SHOW_KEYS = 1 << 0,
53 FLAG_SHOW_KEY_ERRORS = 1 << 1,
54 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
55 FLAG_SHOW_ALL = 1 << 3,
56 FLAG_PRELOAD_FILE = 1 << 4,
57 FLAG_WRITE = 1 << 5,
58};
59
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061int sysctl_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenb9050282003-12-24 06:02:11 +000062{
Denis Vlasenko64309f82007-11-29 06:40:28 +000063 int retval;
64 int opt;
Eric Andersenb9050282003-12-24 06:02:11 +000065
Denis Vlasenko64309f82007-11-29 06:40:28 +000066 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
67 argv += optind;
68 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
69 option_mask32 ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
Eric Andersenb9050282003-12-24 06:02:11 +000070
Denis Vlasenko64309f82007-11-29 06:40:28 +000071 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL))
72 return sysctl_display_all(PROC_SYS);
73 if (opt & FLAG_PRELOAD_FILE)
74 return sysctl_preload_file_and_exit(*argv ? *argv : ETC_SYSCTL_CONF);
Eric Andersenb9050282003-12-24 06:02:11 +000075
Denis Vlasenko64309f82007-11-29 06:40:28 +000076 retval = 0;
77 while (*argv) {
78 if (opt & FLAG_WRITE)
79 retval |= sysctl_write_setting(*argv);
80 else
81 retval |= sysctl_read_setting(*argv);
82 argv++;
Eric Andersenb9050282003-12-24 06:02:11 +000083 }
Denis Vlasenko64309f82007-11-29 06:40:28 +000084
Eric Andersenb9050282003-12-24 06:02:11 +000085 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +000086} /* end sysctl_main() */
Eric Andersenb9050282003-12-24 06:02:11 +000087
88/*
Denis Vlasenko5a28a252007-10-29 19:22:13 +000089 * preload the sysctl's from a conf file
90 * - we parse the file and then reform it (strip out whitespace)
Eric Andersenb9050282003-12-24 06:02:11 +000091 */
Glenn L McGrathab821542003-12-26 02:19:34 +000092
Denis Vlasenko64309f82007-11-29 06:40:28 +000093static int sysctl_preload_file_and_exit(const char *filename)
Eric Andersenb9050282003-12-24 06:02:11 +000094{
Denis Vlasenko4a717e02008-07-20 13:01:56 +000095 char *token[2];
96 parser_t *parser;
Eric Andersenb9050282003-12-24 06:02:11 +000097
Denis Vlasenko4a717e02008-07-20 13:01:56 +000098 parser = config_open(filename);
Denis Vlasenko084266e2008-07-26 23:08:31 +000099 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) { // TODO: ';' is comment char too
Denis Vlasenko5415c852008-07-21 23:05:26 +0000100// if (!token[1]) {
101// bb_error_msg(WARN_BAD_LINE, filename, parser->lineno);
102// } else {
103 {
Denis Vlasenko4a717e02008-07-20 13:01:56 +0000104#if 0
105 char *s = xasprintf("%s=%s", token[0], token[1]);
106 sysctl_write_setting(s);
107 free(s);
108#else // PLAY_WITH_FIRE for -4 bytes?
109 sprintf(parser->line, "%s=%s", token[0], token[1]); // must have room by definition
110 sysctl_write_setting(parser->line);
111#endif
Eric Andersenb9050282003-12-24 06:02:11 +0000112 }
Eric Andersenb9050282003-12-24 06:02:11 +0000113 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000114 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko4a717e02008-07-20 13:01:56 +0000115 config_close(parser);
Eric Andersenb9050282003-12-24 06:02:11 +0000116 return 0;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000117} /* end sysctl_preload_file_and_exit() */
Eric Andersenb9050282003-12-24 06:02:11 +0000118
119/*
120 * Write a single sysctl setting
121 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000122static int sysctl_write_setting(const char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +0000123{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000124 int retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000125 const char *name;
Eric Andersenb9050282003-12-24 06:02:11 +0000126 const char *value;
127 const char *equals;
128 char *tmpname, *outname, *cptr;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000129 int fd;
Eric Andersenb9050282003-12-24 06:02:11 +0000130
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000131 name = setting;
132 equals = strchr(setting, '=');
133 if (!equals) {
Eric Andersenb9050282003-12-24 06:02:11 +0000134 bb_error_msg(ERR_NO_EQUALS, setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000135 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000136 }
137
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000138 value = equals + 1; /* point to the value in name=value */
139 if (name == equals || !*value) {
Eric Andersenb9050282003-12-24 06:02:11 +0000140 bb_error_msg(ERR_MALFORMED_SETTING, setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000141 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000142 }
143
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000144 tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
145 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000146
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000147 sysctl_dots_to_slashes(tmpname);
Eric Andersenb9050282003-12-24 06:02:11 +0000148
149 while ((cptr = strchr(outname, '/')) != NULL)
150 *cptr = '.';
151
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000152 fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
153 if (fd < 0) {
Eric Andersenb9050282003-12-24 06:02:11 +0000154 switch (errno) {
155 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000156 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
157 bb_error_msg(ERR_INVALID_KEY, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000158 break;
159 case EACCES:
160 bb_perror_msg(ERR_PERMISSION_DENIED, outname);
161 break;
162 default:
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000163 bb_perror_msg(ERR_UNKNOWN_WRITING, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000164 break;
165 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000166 retval = EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000167 } else {
Glenn L McGrathab821542003-12-26 02:19:34 +0000168 dwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000169 close(fd);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000170 if (option_mask32 & FLAG_SHOW_KEYS) {
171 printf("%s = ", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000172 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000173 puts(value);
174 retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000175 }
176
Eric Andersenb9050282003-12-24 06:02:11 +0000177 free(tmpname);
178 free(outname);
179 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000180} /* end sysctl_write_setting() */
Eric Andersenb9050282003-12-24 06:02:11 +0000181
182/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000183 * Read a sysctl setting
Eric Andersenb9050282003-12-24 06:02:11 +0000184 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000185static int sysctl_read_setting(const char *name)
Eric Andersenb9050282003-12-24 06:02:11 +0000186{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000187 int retval;
Eric Andersenb9050282003-12-24 06:02:11 +0000188 char *tmpname, *outname, *cptr;
189 char inbuf[1025];
Eric Andersenb9050282003-12-24 06:02:11 +0000190 FILE *fp;
191
Denis Vlasenko64309f82007-11-29 06:40:28 +0000192 if (!*name) {
193 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
194 bb_error_msg(ERR_INVALID_KEY, name);
195 return -1;
196 }
Eric Andersenb9050282003-12-24 06:02:11 +0000197
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000198 tmpname = concat_path_file(PROC_SYS, name);
199 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000200
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000201 sysctl_dots_to_slashes(tmpname);
202
Eric Andersenb9050282003-12-24 06:02:11 +0000203 while ((cptr = strchr(outname, '/')) != NULL)
204 *cptr = '.';
205
Denis Vlasenko5415c852008-07-21 23:05:26 +0000206 fp = fopen_for_read(tmpname);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000207 if (fp == NULL) {
Eric Andersenb9050282003-12-24 06:02:11 +0000208 switch (errno) {
209 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000210 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
211 bb_error_msg(ERR_INVALID_KEY, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000212 break;
213 case EACCES:
214 bb_error_msg(ERR_PERMISSION_DENIED, outname);
215 break;
216 default:
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000217 bb_perror_msg(ERR_UNKNOWN_READING, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000218 break;
219 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000220 retval = EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000221 } else {
222 while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000223 if (option_mask32 & FLAG_SHOW_KEYS) {
224 printf("%s = ", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000225 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000226 fputs(inbuf, stdout);
Eric Andersenb9050282003-12-24 06:02:11 +0000227 }
228 fclose(fp);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000229 retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000230 }
231
232 free(tmpname);
233 free(outname);
234 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000235} /* end sysctl_read_setting() */
Eric Andersenb9050282003-12-24 06:02:11 +0000236
237/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000238 * Display all the sysctl settings
Eric Andersenb9050282003-12-24 06:02:11 +0000239 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000240static int sysctl_display_all(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000241{
242 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000243 DIR *dp;
244 struct dirent *de;
245 char *tmpdir;
246 struct stat ts;
247
Denis Vlasenko51742f42007-04-12 00:32:05 +0000248 dp = opendir(path);
249 if (!dp) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000250 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000251 }
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000252 while ((de = readdir(dp)) != NULL) {
253 tmpdir = concat_subpath_file(path, de->d_name);
254 if (tmpdir == NULL)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000255 continue; /* . or .. */
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000256 if (stat(tmpdir, &ts) != 0) {
257 bb_perror_msg(tmpdir);
258 } else if (S_ISDIR(ts.st_mode)) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000259 retval |= sysctl_display_all(tmpdir);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000260 } else {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000261 retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000262 }
263 free(tmpdir);
264 } /* end while */
265 closedir(dp);
Eric Andersenb9050282003-12-24 06:02:11 +0000266
267 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000268} /* end sysctl_display_all() */
Denis Vlasenko58cc52a2008-10-15 08:22:55 +0000269
270static void sysctl_dots_to_slashes(char *name)
271{
272 char *cptr = name;
273
274 /* Example from bug 3894:
275 * net.ipv4.conf.eth0.100.mc_forwarding ->
276 * net/ipv4/conf/eth0.100/mc_forwarding */
277 while (*cptr != '\0') {
278 if (*cptr == '.') {
279 *cptr = '\0';
280 if (access(name, F_OK) == 0)
281 *cptr = '/';
282 else
283 *cptr = '.';
284 }
285 cptr++;
286 }
287} /* end sysctl_dots_to_slashes() */