blob: 79f0074c99370b4e3113d56ed9ae8a29d846e3ab [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 Vlasenkoa60f84e2008-07-05 09:18:54 +000060int sysctl_main(int argc UNUSED_PARAM, 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
Denis Vlasenko64309f82007-11-29 06:40:28 +000092static int sysctl_preload_file_and_exit(const char *filename)
Eric Andersenb9050282003-12-24 06:02:11 +000093{
Denis Vlasenko4a717e02008-07-20 13:01:56 +000094 char *token[2];
95 parser_t *parser;
Eric Andersenb9050282003-12-24 06:02:11 +000096
Denis Vlasenko4a717e02008-07-20 13:01:56 +000097 parser = config_open(filename);
Denis Vlasenko084266e2008-07-26 23:08:31 +000098 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) { // TODO: ';' is comment char too
Denis Vlasenko5415c852008-07-21 23:05:26 +000099// if (!token[1]) {
100// bb_error_msg(WARN_BAD_LINE, filename, parser->lineno);
101// } else {
102 {
Denis Vlasenko4a717e02008-07-20 13:01:56 +0000103#if 0
104 char *s = xasprintf("%s=%s", token[0], token[1]);
105 sysctl_write_setting(s);
106 free(s);
107#else // PLAY_WITH_FIRE for -4 bytes?
108 sprintf(parser->line, "%s=%s", token[0], token[1]); // must have room by definition
109 sysctl_write_setting(parser->line);
110#endif
Eric Andersenb9050282003-12-24 06:02:11 +0000111 }
Eric Andersenb9050282003-12-24 06:02:11 +0000112 }
Denis Vlasenko64309f82007-11-29 06:40:28 +0000113 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko4a717e02008-07-20 13:01:56 +0000114 config_close(parser);
Eric Andersenb9050282003-12-24 06:02:11 +0000115 return 0;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000116} /* end sysctl_preload_file_and_exit() */
Eric Andersenb9050282003-12-24 06:02:11 +0000117
118/*
119 * Write a single sysctl setting
120 */
Denis Vlasenko64309f82007-11-29 06:40:28 +0000121static int sysctl_write_setting(const char *setting)
Eric Andersenb9050282003-12-24 06:02:11 +0000122{
Denis Vlasenko64309f82007-11-29 06:40:28 +0000123 int retval;
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000124 const char *name;
Eric Andersenb9050282003-12-24 06:02:11 +0000125 const char *value;
126 const char *equals;
127 char *tmpname, *outname, *cptr;
Denis Vlasenko64309f82007-11-29 06:40:28 +0000128 int fd;
Eric Andersenb9050282003-12-24 06:02:11 +0000129
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000130 name = setting;
131 equals = strchr(setting, '=');
132 if (!equals) {
Eric Andersenb9050282003-12-24 06:02:11 +0000133 bb_error_msg(ERR_NO_EQUALS, setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000134 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000135 }
136
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000137 value = equals + 1; /* point to the value in name=value */
138 if (name == equals || !*value) {
Eric Andersenb9050282003-12-24 06:02:11 +0000139 bb_error_msg(ERR_MALFORMED_SETTING, setting);
Denis Vlasenko64309f82007-11-29 06:40:28 +0000140 return EXIT_FAILURE;
Eric Andersenb9050282003-12-24 06:02:11 +0000141 }
142
Denis Vlasenko5a28a252007-10-29 19:22:13 +0000143 tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
144 outname = xstrdup(tmpname + strlen_PROC_SYS);
Eric Andersenb9050282003-12-24 06:02:11 +0000145
146 while ((cptr = strchr(tmpname, '.')) != NULL)
147 *cptr = '/';
148
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
201 while ((cptr = strchr(tmpname, '.')) != NULL)
202 *cptr = '/';
203 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() */