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