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