1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include "libbb.h"
35
36enum {
37 FLAG_SHOW_KEYS = 1 << 0,
38 FLAG_SHOW_KEY_ERRORS = 1 << 1,
39 FLAG_TABLE_FORMAT = 1 << 2,
40 FLAG_SHOW_ALL = 1 << 3,
41 FLAG_PRELOAD_FILE = 1 << 4,
42
43 FLAG_WRITE = 1 << 5,
44 FLAG_QUIET = 1 << 6,
45};
46#define OPTION_STR "neAapwq"
47
48static void sysctl_dots_to_slashes(char *name)
49{
50 char *cptr, *last_good, *end;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 end = name + strlen(name);
71 last_good = name - 1;
72 *end = '.';
73
74 again:
75 cptr = end;
76 while (cptr > last_good) {
77 if (*cptr == '.') {
78 *cptr = '\0';
79
80 if (access(name, F_OK) == 0) {
81 *cptr = '/';
82
83 last_good = cptr;
84 goto again;
85 }
86 *cptr = '.';
87 }
88 cptr--;
89 }
90 *end = '\0';
91}
92
93static int sysctl_act_on_setting(char *setting)
94{
95 int fd, retval = EXIT_SUCCESS;
96 char *cptr, *outname;
97 char *value = value;
98
99 outname = xstrdup(setting);
100
101 cptr = outname;
102 while (*cptr) {
103 if (*cptr == '/')
104 *cptr = '.';
105 cptr++;
106 }
107
108 if (option_mask32 & FLAG_WRITE) {
109 cptr = strchr(setting, '=');
110 if (cptr == NULL) {
111 bb_error_msg("error: '%s' must be of the form name=value",
112 outname);
113 retval = EXIT_FAILURE;
114 goto end;
115 }
116 value = cptr + 1;
117 if (setting == cptr || !*value) {
118 bb_error_msg("error: malformed setting '%s'", outname);
119 retval = EXIT_FAILURE;
120 goto end;
121 }
122 *cptr = '\0';
123 outname[cptr - setting] = '\0';
124
125 fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
126 } else {
127 fd = open(setting, O_RDONLY);
128 }
129
130 if (fd < 0) {
131 switch (errno) {
132 case EACCES:
133
134 goto end;
135 case ENOENT:
136 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
137 bb_error_msg("error: '%s' is an unknown key", outname);
138 break;
139 default:
140 bb_perror_msg("error %sing key '%s'",
141 option_mask32 & FLAG_WRITE ?
142 "sett" : "read",
143 outname);
144 break;
145 }
146 retval = EXIT_FAILURE;
147 goto end;
148 }
149
150 if (option_mask32 & FLAG_WRITE) {
151
152 xwrite_str(fd, value);
153 close(fd);
154 if (!(option_mask32 & FLAG_QUIET)) {
155 if (option_mask32 & FLAG_SHOW_KEYS)
156 printf("%s = ", outname);
157 puts(value);
158 }
159 } else {
160 char c;
161
162 value = cptr = xmalloc_read(fd, NULL);
163 close(fd);
164 if (value == NULL) {
165 bb_perror_msg("error reading key '%s'", outname);
166 goto end;
167 }
168
169
170
171
172 while ((c = *cptr) != '\0') {
173 if (option_mask32 & FLAG_SHOW_KEYS)
174 printf("%s = ", outname);
175 while (1) {
176 fputc(c, stdout);
177 cptr++;
178 if (c == '\n')
179 break;
180 c = *cptr;
181 if (c == '\0')
182 break;
183 }
184 }
185 free(value);
186 }
187 end:
188 free(outname);
189 return retval;
190}
191
192static int sysctl_act_recursive(const char *path)
193{
194 DIR *dirp;
195 struct stat buf;
196 struct dirent *entry;
197 char *next;
198 int retval = 0;
199
200 stat(path, &buf);
201 if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
202 dirp = opendir(path);
203 if (dirp == NULL)
204 return -1;
205 while ((entry = readdir(dirp)) != NULL) {
206 next = concat_subpath_file(path, entry->d_name);
207 if (next == NULL)
208 continue;
209
210 retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
211 next + 2 : next);
212 free(next);
213 }
214 closedir(dirp);
215 } else {
216 char *name = xstrdup(path);
217 retval |= sysctl_act_on_setting(name);
218 free(name);
219 }
220
221 return retval;
222}
223
224
225
226
227
228static int sysctl_handle_preload_file(const char *filename)
229{
230 char *token[2];
231 parser_t *parser;
232
233 parser = config_open(filename);
234
235 xchdir("/proc/sys");
236
237
238
239
240
241
242
243 while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
244 char *tp;
245 sysctl_dots_to_slashes(token[0]);
246 tp = xasprintf("%s=%s", token[0], token[1]);
247 sysctl_act_recursive(tp);
248 free(tp);
249 }
250 if (ENABLE_FEATURE_CLEAN_UP)
251 config_close(parser);
252 return 0;
253}
254
255int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
256int sysctl_main(int argc UNUSED_PARAM, char **argv)
257{
258 int retval;
259 int opt;
260
261 opt = getopt32(argv, "+" OPTION_STR);
262 argv += optind;
263 opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
264 option_mask32 = opt;
265
266 if (opt & FLAG_PRELOAD_FILE) {
267 option_mask32 |= FLAG_WRITE;
268
269 return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
270 }
271 xchdir("/proc/sys");
272
273 if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
274 return sysctl_act_recursive(".");
275 }
276
277 retval = 0;
278 while (*argv) {
279 sysctl_dots_to_slashes(*argv);
280 retval |= sysctl_act_recursive(*argv);
281 argv++;
282 }
283
284 return retval;
285}
286