blob: 1995382ad4baa86db91ec354942d78fe30d2196a [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);
Eric Andersenb9050282003-12-24 06:02:11 +000023
Denis Vlasenko5a28a252007-10-29 19:22:13 +000024static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";
25static const char PROC_SYS[] ALIGN1 = "/proc/sys/";
26enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };
Eric Andersenb9050282003-12-24 06:02:11 +000027
28/* error messages */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000029static const char ERR_MALFORMED_SETTING[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000030 "error: malformed setting '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000031static const char ERR_NO_EQUALS[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000032 "error: '%s' must be of the form name=value";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000033static const char ERR_INVALID_KEY[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000034 "error: '%s' is an unknown key";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000035static const char ERR_UNKNOWN_WRITING[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000036 "error setting key '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000037static const char ERR_UNKNOWN_READING[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000038 "error reading key '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000039static const char ERR_PERMISSION_DENIED[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000040 "error: permission denied on key '%s'";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000041static const char WARN_BAD_LINE[] ALIGN1 =
Denis Vlasenko5a28a252007-10-29 19:22:13 +000042 "warning: %s(%d): invalid syntax, continuing";
Eric Andersenb9050282003-12-24 06:02:11 +000043
44
Glenn L McGrathab821542003-12-26 02:19:34 +000045static void dwrite_str(int fd, const char *buf)
46{
47 write(fd, buf, strlen(buf));
48}
49
Denis Vlasenko64309f82007-11-29 06:40:28 +000050enum {
51 FLAG_SHOW_KEYS = 1 << 0,
52 FLAG_SHOW_KEY_ERRORS = 1 << 1,
53 FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
54 FLAG_SHOW_ALL = 1 << 3,
55 FLAG_PRELOAD_FILE = 1 << 4,
56 FLAG_WRITE = 1 << 5,
57};
58
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000059int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000060int sysctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersenb9050282003-12-24 06:02:11 +000061{
Denis Vlasenko64309f82007-11-29 06:40:28 +000062 int retval;
63 int opt;
Eric Andersenb9050282003-12-24 06:02:11 +000064
Denis Vlasenko64309f82007-11-29 06:40:28 +000065 opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
66 argv += optind;
67 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
68 option_mask32 ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
Eric Andersenb9050282003-12-24 06:02:11 +000069
Denis Vlasenko64309f82007-11-29 06:40:28 +000070 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL))
71 return sysctl_display_all(PROC_SYS);
72 if (opt & FLAG_PRELOAD_FILE)
73 return sysctl_preload_file_and_exit(*argv ? *argv : ETC_SYSCTL_CONF);
Eric Andersenb9050282003-12-24 06:02:11 +000074
Denis Vlasenko64309f82007-11-29 06:40:28 +000075 retval = 0;
76 while (*argv) {
77 if (opt & FLAG_WRITE)
78 retval |= sysctl_write_setting(*argv);
79 else
80 retval |= sysctl_read_setting(*argv);
81 argv++;
Eric Andersenb9050282003-12-24 06:02:11 +000082 }
Denis Vlasenko64309f82007-11-29 06:40:28 +000083
Eric Andersenb9050282003-12-24 06:02:11 +000084 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +000085} /* end sysctl_main() */
Eric Andersenb9050282003-12-24 06:02:11 +000086
87/*
Denis Vlasenko5a28a252007-10-29 19:22:13 +000088 * preload the sysctl's from a conf file
89 * - we parse the file and then reform it (strip out whitespace)
Eric Andersenb9050282003-12-24 06:02:11 +000090 */
Glenn L McGrathab821542003-12-26 02:19:34 +000091#define PRELOAD_BUF 256
92
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 Vlasenko5a28a252007-10-29 19:22:13 +000095 int lineno;
Glenn L McGrathab821542003-12-26 02:19:34 +000096 char oneline[PRELOAD_BUF];
97 char buffer[PRELOAD_BUF];
Denis Vlasenko5a28a252007-10-29 19:22:13 +000098 char *name, *value;
99 FILE *fp;
Eric Andersenb9050282003-12-24 06:02:11 +0000100
Denis Vlasenko64309f82007-11-29 06:40:28 +0000101 fp = xfopen(filename, "r");
Eric Andersenb9050282003-12-24 06:02:11 +0000102
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000103 lineno = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000104 while (fgets(oneline, sizeof(oneline) - 1, fp)) {
Eric Andersenb9050282003-12-24 06:02:11 +0000105 lineno++;
106 trim(oneline);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000107 if (oneline[0] == '#' || oneline[0] == ';')
108 continue;
109 if (!oneline[0] || !oneline[1])
Eric Andersenb9050282003-12-24 06:02:11 +0000110 continue;
111
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000112 name = strtok(oneline, "=");
113 if (!name) {
Eric Andersenb9050282003-12-24 06:02:11 +0000114 bb_error_msg(WARN_BAD_LINE, filename, lineno);
115 continue;
116 }
Eric Andersenb9050282003-12-24 06:02:11 +0000117 trim(name);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000118 if (!*name) {
119 bb_error_msg(WARN_BAD_LINE, filename, lineno);
120 continue;
121 }
Eric Andersenb9050282003-12-24 06:02:11 +0000122
123 value = strtok(NULL, "\n\r");
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000124 if (!value) {
125 bb_error_msg(WARN_BAD_LINE, filename, lineno);
126 continue;
127 }
128 while (*value == ' ' || *value == '\t')
129 value++;
130 if (!*value) {
Eric Andersenb9050282003-12-24 06:02:11 +0000131 bb_error_msg(WARN_BAD_LINE, filename, lineno);
132 continue;
133 }
134
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000135 /* safe because sizeof(oneline) == sizeof(buffer) */
136 sprintf(buffer, "%s=%s", name, value);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000137 sysctl_write_setting(buffer);
Eric Andersenb9050282003-12-24 06:02:11 +0000138 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000139 if (ENABLE_FEATURE_CLEAN_UP)
140 fclose(fp);
Eric Andersenb9050282003-12-24 06:02:11 +0000141 return 0;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000142} /* end sysctl_preload_file_and_exit() */
Eric Andersenb9050282003-12-24 06:02:11 +0000143
144/*
145 * Write a single sysctl setting
146 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000147static int sysctl_write_setting(const char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +0000148{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000149 int retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000150 const char *name;
Eric Andersenb9050282003-12-24 06:02:11 +0000151 const char *value;
152 const char *equals;
153 char *tmpname, *outname, *cptr;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000154 int fd;
Eric Andersenb9050282003-12-24 06:02:11 +0000155
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000156 name = setting;
157 equals = strchr(setting, '=');
158 if (!equals) {
Eric Andersenb9050282003-12-24 06:02:11 +0000159 bb_error_msg(ERR_NO_EQUALS, setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000160 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000161 }
162
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000163 value = equals + 1; /* point to the value in name=value */
164 if (name == equals || !*value) {
Eric Andersenb9050282003-12-24 06:02:11 +0000165 bb_error_msg(ERR_MALFORMED_SETTING, setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000166 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000167 }
168
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000169 tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
170 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000171
172 while ((cptr = strchr(tmpname, '.')) != NULL)
173 *cptr = '/';
174
175 while ((cptr = strchr(outname, '/')) != NULL)
176 *cptr = '.';
177
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000178 fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
179 if (fd < 0) {
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)
183 bb_error_msg(ERR_INVALID_KEY, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000184 break;
185 case EACCES:
186 bb_perror_msg(ERR_PERMISSION_DENIED, outname);
187 break;
188 default:
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000189 bb_perror_msg(ERR_UNKNOWN_WRITING, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000190 break;
191 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000192 retval = EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000193 } else {
Glenn L McGrathab821542003-12-26 02:19:34 +0000194 dwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000195 close(fd);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000196 if (option_mask32 & FLAG_SHOW_KEYS) {
197 printf("%s = ", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000198 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000199 puts(value);
200 retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000201 }
202
Eric Andersenb9050282003-12-24 06:02:11 +0000203 free(tmpname);
204 free(outname);
205 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000206} /* end sysctl_write_setting() */
Eric Andersenb9050282003-12-24 06:02:11 +0000207
208/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000209 * Read a sysctl setting
Eric Andersenb9050282003-12-24 06:02:11 +0000210 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000211static int sysctl_read_setting(const char *name)
Eric Andersenb9050282003-12-24 06:02:11 +0000212{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000213 int retval;
Eric Andersenb9050282003-12-24 06:02:11 +0000214 char *tmpname, *outname, *cptr;
215 char inbuf[1025];
Eric Andersenb9050282003-12-24 06:02:11 +0000216 FILE *fp;
217
Denis Vlasenko64309f82007-11-29 06:40:28 +0000218 if (!*name) {
219 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
220 bb_error_msg(ERR_INVALID_KEY, name);
221 return -1;
222 }
Eric Andersenb9050282003-12-24 06:02:11 +0000223
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000224 tmpname = concat_path_file(PROC_SYS, name);
225 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000226
227 while ((cptr = strchr(tmpname, '.')) != NULL)
228 *cptr = '/';
229 while ((cptr = strchr(outname, '/')) != NULL)
230 *cptr = '.';
231
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000232 fp = fopen(tmpname, "r");
233 if (fp == NULL) {
Eric Andersenb9050282003-12-24 06:02:11 +0000234 switch (errno) {
235 case ENOENT:
Denis Vlasenko64309f82007-11-29 06:40:28 +0000236 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
237 bb_error_msg(ERR_INVALID_KEY, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000238 break;
239 case EACCES:
240 bb_error_msg(ERR_PERMISSION_DENIED, outname);
241 break;
242 default:
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000243 bb_perror_msg(ERR_UNKNOWN_READING, outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000244 break;
245 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000246 retval = EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000247 } else {
248 while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000249 if (option_mask32 & FLAG_SHOW_KEYS) {
250 printf("%s = ", outname);
Eric Andersenb9050282003-12-24 06:02:11 +0000251 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000252 fputs(inbuf, stdout);
Eric Andersenb9050282003-12-24 06:02:11 +0000253 }
254 fclose(fp);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000255 retval = EXIT_SUCCESS;
Eric Andersenb9050282003-12-24 06:02:11 +0000256 }
257
258 free(tmpname);
259 free(outname);
260 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000261} /* end sysctl_read_setting() */
Eric Andersenb9050282003-12-24 06:02:11 +0000262
263/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000264 * Display all the sysctl settings
Eric Andersenb9050282003-12-24 06:02:11 +0000265 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000266static int sysctl_display_all(const char *path)
Eric Andersenb9050282003-12-24 06:02:11 +0000267{
268 int retval = 0;
Eric Andersenb9050282003-12-24 06:02:11 +0000269 DIR *dp;
270 struct dirent *de;
271 char *tmpdir;
272 struct stat ts;
273
Denis Vlasenko51742f42007-04-12 00:32:05 +0000274 dp = opendir(path);
275 if (!dp) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000276 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000277 }
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000278 while ((de = readdir(dp)) != NULL) {
279 tmpdir = concat_subpath_file(path, de->d_name);
280 if (tmpdir == NULL)
Denis Vlasenko64309f82007-11-29 06:40:28 +0000281 continue; /* . or .. */
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000282 if (stat(tmpdir, &ts) != 0) {
283 bb_perror_msg(tmpdir);
284 } else if (S_ISDIR(ts.st_mode)) {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000285 retval |= sysctl_display_all(tmpdir);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000286 } else {
Denis Vlasenko64309f82007-11-29 06:40:28 +0000287 retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000288 }
289 free(tmpdir);
290 } /* end while */
291 closedir(dp);
Eric Andersenb9050282003-12-24 06:02:11 +0000292
293 return retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000294} /* end sysctl_display_all() */