blob: 49754489b97ba6a27b4e24f4aa46b153730d8ea8 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000017#include "busybox.h"
Eric Andersenb9050282003-12-24 06:02:11 +000018
19/*
20 * Function Prototypes
21 */
Eric Andersenb9050282003-12-24 06:02:11 +000022static int sysctl_read_setting(const char *setting, int output);
23static int sysctl_write_setting(const char *setting, int output);
24static int sysctl_preload_file(const char *filename, int output);
25static int sysctl_display_all(const char *path, int output, int show_table);
26
27/*
28 * Globals...
29 */
Glenn L McGrathab821542003-12-26 02:19:34 +000030static const char PROC_PATH[] = "/proc/sys/";
31static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf";
Eric Andersenb9050282003-12-24 06:02:11 +000032
33/* error messages */
Glenn L McGrathab821542003-12-26 02:19:34 +000034static const char ERR_UNKNOWN_PARAMETER[] = "error: Unknown parameter '%s'\n";
35static const char ERR_MALFORMED_SETTING[] = "error: Malformed setting '%s'\n";
36static const char ERR_NO_EQUALS[] =
Eric Andersenb9050282003-12-24 06:02:11 +000037 "error: '%s' must be of the form name=value\n";
Glenn L McGrathab821542003-12-26 02:19:34 +000038static const char ERR_INVALID_KEY[] = "error: '%s' is an unknown key\n";
39static const char ERR_UNKNOWN_WRITING[] =
Eric Andersenb9050282003-12-24 06:02:11 +000040 "error: unknown error %d setting key '%s'\n";
Glenn L McGrathab821542003-12-26 02:19:34 +000041static const char ERR_UNKNOWN_READING[] =
Eric Andersenb9050282003-12-24 06:02:11 +000042 "error: unknown error %d reading key '%s'\n";
Glenn L McGrathab821542003-12-26 02:19:34 +000043static const char ERR_PERMISSION_DENIED[] =
Eric Andersenb9050282003-12-24 06:02:11 +000044 "error: permission denied on key '%s'\n";
Glenn L McGrathab821542003-12-26 02:19:34 +000045static const char ERR_PRELOAD_FILE[] =
Denis Vlasenko89f0b342006-11-18 22:04:09 +000046 "error: cannot open preload file '%s'\n";
Glenn L McGrathab821542003-12-26 02:19:34 +000047static const char WARN_BAD_LINE[] =
Eric Andersenb9050282003-12-24 06:02:11 +000048 "warning: %s(%d): invalid syntax, continuing...\n";
49
50
Glenn L McGrathab821542003-12-26 02:19:34 +000051static void dwrite_str(int fd, const char *buf)
52{
53 write(fd, buf, strlen(buf));
54}
55
Eric Andersenb9050282003-12-24 06:02:11 +000056/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 * sysctl_main()...
Eric Andersenb9050282003-12-24 06:02:11 +000058 */
59int sysctl_main(int argc, char **argv)
60{
61 int retval = 0;
62 int output = 1;
63 int write_mode = 0;
64 int switches_allowed = 1;
65
66 if (argc < 2)
Glenn L McGrathab821542003-12-26 02:19:34 +000067 bb_show_usage();
Eric Andersenb9050282003-12-24 06:02:11 +000068
69 argv++;
70
71 for (; argv && *argv && **argv; argv++) {
72 if (switches_allowed && **argv == '-') { /* we have a switch */
73 switch ((*argv)[1]) {
74 case 'n':
75 output = 0;
76 break;
77 case 'w':
78 write_mode = 1;
79 switches_allowed = 0;
80 break;
81 case 'p':
82 argv++;
83 return
84 sysctl_preload_file(((argv && *argv
85 && **argv) ? *argv :
86 DEFAULT_PRELOAD), output);
87 case 'a':
88 case 'A':
89 switches_allowed = 0;
90 return sysctl_display_all(PROC_PATH, output,
91 ((*argv)[1] == 'a') ? 0 : 1);
92 case 'h':
93 case '?':
Glenn L McGrathab821542003-12-26 02:19:34 +000094 bb_show_usage();
Eric Andersenb9050282003-12-24 06:02:11 +000095 default:
96 bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv);
Glenn L McGrathab821542003-12-26 02:19:34 +000097 bb_show_usage();
Eric Andersenb9050282003-12-24 06:02:11 +000098 }
99 } else {
100 switches_allowed = 0;
101 if (write_mode)
102 retval = sysctl_write_setting(*argv, output);
103 else
104 sysctl_read_setting(*argv, output);
105 }
106 }
107 return retval;
108} /* end sysctl_main() */
109
Eric Andersenb9050282003-12-24 06:02:11 +0000110
111
112/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113 * sysctl_preload_file
Eric Andersenb9050282003-12-24 06:02:11 +0000114 * preload the sysctl's from a conf file
115 * - we parse the file and then reform it (strip out whitespace)
116 */
Glenn L McGrathab821542003-12-26 02:19:34 +0000117#define PRELOAD_BUF 256
118
Eric Andersenb9050282003-12-24 06:02:11 +0000119int sysctl_preload_file(const char *filename, int output)
120{
121 int lineno = 0;
Glenn L McGrathab821542003-12-26 02:19:34 +0000122 char oneline[PRELOAD_BUF];
123 char buffer[PRELOAD_BUF];
Eric Andersenb9050282003-12-24 06:02:11 +0000124 char *name, *value, *ptr;
125 FILE *fp = NULL;
126
127 if (!filename || ((fp = fopen(filename, "r")) == NULL)) {
Glenn L McGrathab821542003-12-26 02:19:34 +0000128 bb_error_msg_and_die(ERR_PRELOAD_FILE, filename);
Eric Andersenb9050282003-12-24 06:02:11 +0000129 }
130
131 while (fgets(oneline, sizeof(oneline) - 1, fp)) {
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000132 oneline[sizeof(oneline) - 1] = '\0';
Eric Andersenb9050282003-12-24 06:02:11 +0000133 lineno++;
134 trim(oneline);
135 ptr = (char *) oneline;
136
137 if (*ptr == '#' || *ptr == ';')
138 continue;
139
Rob Landleya3896512006-05-07 20:20:34 +0000140 if (strlen(ptr) < 2)
Eric Andersenb9050282003-12-24 06:02:11 +0000141 continue;
142
143 name = strtok(ptr, "=");
144 if (!name || !*name) {
145 bb_error_msg(WARN_BAD_LINE, filename, lineno);
146 continue;
147 }
148
149 trim(name);
150
151 value = strtok(NULL, "\n\r");
152 if (!value || !*value) {
153 bb_error_msg(WARN_BAD_LINE, filename, lineno);
154 continue;
155 }
156
157 while ((*value == ' ' || *value == '\t') && *value != 0)
158 value++;
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000159 /* safe because sizeof(oneline) == sizeof(buffer) */
160 sprintf(buffer, "%s=%s", name, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000161 sysctl_write_setting(buffer, output);
162 }
163 fclose(fp);
164 return 0;
165} /* end sysctl_preload_file() */
166
167
168/*
169 * Write a single sysctl setting
170 */
171int sysctl_write_setting(const char *setting, int output)
172{
173 int retval = 0;
174 const char *name = setting;
175 const char *value;
176 const char *equals;
177 char *tmpname, *outname, *cptr;
178 int fd = -1;
179
180 if (!name) /* probably dont' want to display this err */
181 return 0;
182
Glenn L McGrathab821542003-12-26 02:19:34 +0000183 if (!(equals = strchr(setting, '='))) {
Eric Andersenb9050282003-12-24 06:02:11 +0000184 bb_error_msg(ERR_NO_EQUALS, setting);
185 return -1;
186 }
187
188 value = equals + sizeof(char); /* point to the value in name=value */
189
190 if (!*name || !*value || name == equals) {
191 bb_error_msg(ERR_MALFORMED_SETTING, setting);
192 return -2;
193 }
194
Rob Landleyd921b2e2006-08-03 15:41:12 +0000195 tmpname = xasprintf("%s%.*s", PROC_PATH, (int)(equals - name), name);
196 outname = xstrdup(tmpname + strlen(PROC_PATH));
Eric Andersenb9050282003-12-24 06:02:11 +0000197
198 while ((cptr = strchr(tmpname, '.')) != NULL)
199 *cptr = '/';
200
201 while ((cptr = strchr(outname, '/')) != NULL)
202 *cptr = '.';
203
204 if ((fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
205 switch (errno) {
206 case ENOENT:
207 bb_error_msg(ERR_INVALID_KEY, outname);
208 break;
209 case EACCES:
210 bb_perror_msg(ERR_PERMISSION_DENIED, outname);
211 break;
212 default:
213 bb_error_msg(ERR_UNKNOWN_WRITING, errno, outname);
214 break;
215 }
216 retval = -1;
217 } else {
Glenn L McGrathab821542003-12-26 02:19:34 +0000218 dwrite_str(fd, value);
Eric Andersenb9050282003-12-24 06:02:11 +0000219 close(fd);
220 if (output) {
Glenn L McGrathab821542003-12-26 02:19:34 +0000221 dwrite_str(STDOUT_FILENO, outname);
222 dwrite_str(STDOUT_FILENO, " = ");
Eric Andersenb9050282003-12-24 06:02:11 +0000223 }
Glenn L McGrathab821542003-12-26 02:19:34 +0000224 dwrite_str(STDOUT_FILENO, value);
225 dwrite_str(STDOUT_FILENO, "\n");
Eric Andersenb9050282003-12-24 06:02:11 +0000226 }
227
228 /* cleanup */
229 free(tmpname);
230 free(outname);
231 return retval;
232} /* end sysctl_write_setting() */
233
234
235/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000236 * Read a sysctl setting
Eric Andersenb9050282003-12-24 06:02:11 +0000237 *
238 */
239int sysctl_read_setting(const char *setting, int output)
240{
241 int retval = 0;
242 char *tmpname, *outname, *cptr;
243 char inbuf[1025];
244 const char *name = setting;
245 FILE *fp;
246
247 if (!setting || !*setting)
248 bb_error_msg(ERR_INVALID_KEY, setting);
249
Glenn L McGrathab821542003-12-26 02:19:34 +0000250 tmpname = concat_path_file(PROC_PATH, name);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000251 outname = xstrdup(tmpname + strlen(PROC_PATH));
Eric Andersenb9050282003-12-24 06:02:11 +0000252
253 while ((cptr = strchr(tmpname, '.')) != NULL)
254 *cptr = '/';
255 while ((cptr = strchr(outname, '/')) != NULL)
256 *cptr = '.';
257
258 if ((fp = fopen(tmpname, "r")) == NULL) {
259 switch (errno) {
260 case ENOENT:
261 bb_error_msg(ERR_INVALID_KEY, outname);
262 break;
263 case EACCES:
264 bb_error_msg(ERR_PERMISSION_DENIED, outname);
265 break;
266 default:
267 bb_error_msg(ERR_UNKNOWN_READING, errno, outname);
268 break;
269 }
270 retval = -1;
271 } else {
272 while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
273 if (output) {
Glenn L McGrathab821542003-12-26 02:19:34 +0000274 dwrite_str(STDOUT_FILENO, outname);
275 dwrite_str(STDOUT_FILENO, " = ");
Eric Andersenb9050282003-12-24 06:02:11 +0000276 }
Glenn L McGrathab821542003-12-26 02:19:34 +0000277 dwrite_str(STDOUT_FILENO, inbuf);
Eric Andersenb9050282003-12-24 06:02:11 +0000278 }
279 fclose(fp);
280 }
281
282 free(tmpname);
283 free(outname);
284 return retval;
285} /* end sysctl_read_setting() */
286
287
288
289/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000290 * Display all the sysctl settings
Eric Andersenb9050282003-12-24 06:02:11 +0000291 *
292 */
293int sysctl_display_all(const char *path, int output, int show_table)
294{
295 int retval = 0;
296 int retval2;
297 DIR *dp;
298 struct dirent *de;
299 char *tmpdir;
300 struct stat ts;
301
Rob Landleyd921b2e2006-08-03 15:41:12 +0000302 if (!(dp = opendir(path))) {
Eric Andersenb9050282003-12-24 06:02:11 +0000303 retval = -1;
304 } else {
Eric Andersenb9050282003-12-24 06:02:11 +0000305 while ((de = readdir(dp)) != NULL) {
Glenn L McGrathab821542003-12-26 02:19:34 +0000306 tmpdir = concat_subpath_file(path, de->d_name);
307 if(tmpdir == NULL)
308 continue;
Eric Andersenb9050282003-12-24 06:02:11 +0000309 if ((retval2 = stat(tmpdir, &ts)) != 0)
310 bb_perror_msg(tmpdir);
311 else {
312 if (S_ISDIR(ts.st_mode)) {
Eric Andersenb9050282003-12-24 06:02:11 +0000313 sysctl_display_all(tmpdir, output, show_table);
314 } else
315 retval |=
Rob Landleya3896512006-05-07 20:20:34 +0000316 sysctl_read_setting(tmpdir + strlen(PROC_PATH),
Eric Andersenb9050282003-12-24 06:02:11 +0000317 output);
318
319 }
320 free(tmpdir);
321 } /* end while */
322 closedir(dp);
323 }
324
325 return retval;
326} /* end sysctl_display_all() */