Initial public busybox upstream commit
[busybox4maemo] / procps / sysctl.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
4  *
5  * Copyright 1999 George Staikos
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  *
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>
14  *
15  */
16
17 #include "libbb.h"
18
19 static int sysctl_read_setting(const char *setting);
20 static int sysctl_write_setting(const char *setting);
21 static int sysctl_display_all(const char *path);
22 static int sysctl_preload_file_and_exit(const char *filename);
23
24 static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";
25 static const char PROC_SYS[] ALIGN1 = "/proc/sys/";
26 enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };
27
28 /* error messages */
29 static const char ERR_MALFORMED_SETTING[] ALIGN1 =
30         "error: malformed setting '%s'";
31 static const char ERR_NO_EQUALS[] ALIGN1 =
32         "error: '%s' must be of the form name=value";
33 static const char ERR_INVALID_KEY[] ALIGN1 =
34         "error: '%s' is an unknown key";
35 static const char ERR_UNKNOWN_WRITING[] ALIGN1 =
36         "error setting key '%s'";
37 static const char ERR_UNKNOWN_READING[] ALIGN1 =
38         "error reading key '%s'";
39 static const char ERR_PERMISSION_DENIED[] ALIGN1 =
40         "error: permission denied on key '%s'";
41 static const char WARN_BAD_LINE[] ALIGN1 =
42         "warning: %s(%d): invalid syntax, continuing";
43
44
45 static void dwrite_str(int fd, const char *buf)
46 {
47         write(fd, buf, strlen(buf));
48 }
49
50 enum {
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
59 int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60 int sysctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
61 {
62         int retval;
63         int opt;
64
65         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);
69
70         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);
74
75         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++;
82         }
83
84         return retval;
85 } /* end sysctl_main() */
86
87 /*
88  * preload the sysctl's from a conf file
89  * - we parse the file and then reform it (strip out whitespace)
90  */
91 #define PRELOAD_BUF 256
92
93 static int sysctl_preload_file_and_exit(const char *filename)
94 {
95         int lineno;
96         char oneline[PRELOAD_BUF];
97         char buffer[PRELOAD_BUF];
98         char *name, *value;
99         FILE *fp;
100
101         fp = xfopen(filename, "r");
102
103         lineno = 0;
104         while (fgets(oneline, sizeof(oneline) - 1, fp)) {
105                 lineno++;
106                 trim(oneline);
107                 if (oneline[0] == '#' || oneline[0] == ';')
108                         continue;
109                 if (!oneline[0] || !oneline[1])
110                         continue;
111
112                 name = strtok(oneline, "=");
113                 if (!name) {
114                         bb_error_msg(WARN_BAD_LINE, filename, lineno);
115                         continue;
116                 }
117                 trim(name);
118                 if (!*name) {
119                         bb_error_msg(WARN_BAD_LINE, filename, lineno);
120                         continue;
121                 }
122
123                 value = strtok(NULL, "\n\r");
124                 if (!value) {
125                         bb_error_msg(WARN_BAD_LINE, filename, lineno);
126                         continue;
127                 }
128                 while (*value == ' ' || *value == '\t')
129                         value++;
130                 if (!*value) {
131                         bb_error_msg(WARN_BAD_LINE, filename, lineno);
132                         continue;
133                 }
134
135                 /* safe because sizeof(oneline) == sizeof(buffer) */
136                 sprintf(buffer, "%s=%s", name, value);
137                 sysctl_write_setting(buffer);
138         }
139         if (ENABLE_FEATURE_CLEAN_UP)
140                 fclose(fp);
141         return 0;
142 } /* end sysctl_preload_file_and_exit() */
143
144 /*
145  *     Write a single sysctl setting
146  */
147 static int sysctl_write_setting(const char *setting)
148 {
149         int retval;
150         const char *name;
151         const char *value;
152         const char *equals;
153         char *tmpname, *outname, *cptr;
154         int fd;
155
156         name = setting;
157         equals = strchr(setting, '=');
158         if (!equals) {
159                 bb_error_msg(ERR_NO_EQUALS, setting);
160                 return EXIT_FAILURE;
161         }
162
163         value = equals + 1;     /* point to the value in name=value */
164         if (name == equals || !*value) {
165                 bb_error_msg(ERR_MALFORMED_SETTING, setting);
166                 return EXIT_FAILURE;
167         }
168
169         tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
170         outname = xstrdup(tmpname + strlen_PROC_SYS);
171
172         while ((cptr = strchr(tmpname, '.')) != NULL)
173                 *cptr = '/';
174
175         while ((cptr = strchr(outname, '/')) != NULL)
176                 *cptr = '.';
177
178         fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
179         if (fd < 0) {
180                 switch (errno) {
181                 case ENOENT:
182                         if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
183                                 bb_error_msg(ERR_INVALID_KEY, outname);
184                         break;
185                 case EACCES:
186                         bb_perror_msg(ERR_PERMISSION_DENIED, outname);
187                         break;
188                 default:
189                         bb_perror_msg(ERR_UNKNOWN_WRITING, outname);
190                         break;
191                 }
192                 retval = EXIT_FAILURE;
193         } else {
194                 dwrite_str(fd, value);
195                 close(fd);
196                 if (option_mask32 & FLAG_SHOW_KEYS) {
197                         printf("%s = ", outname);
198                 }
199                 puts(value);
200                 retval = EXIT_SUCCESS;
201         }
202
203         free(tmpname);
204         free(outname);
205         return retval;
206 } /* end sysctl_write_setting() */
207
208 /*
209  *     Read a sysctl setting
210  */
211 static int sysctl_read_setting(const char *name)
212 {
213         int retval;
214         char *tmpname, *outname, *cptr;
215         char inbuf[1025];
216         FILE *fp;
217
218         if (!*name) {
219                 if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
220                         bb_error_msg(ERR_INVALID_KEY, name);
221                 return -1;
222         }
223
224         tmpname = concat_path_file(PROC_SYS, name);
225         outname = xstrdup(tmpname + strlen_PROC_SYS);
226
227         while ((cptr = strchr(tmpname, '.')) != NULL)
228                 *cptr = '/';
229         while ((cptr = strchr(outname, '/')) != NULL)
230                 *cptr = '.';
231
232         fp = fopen(tmpname, "r");
233         if (fp == NULL) {
234                 switch (errno) {
235                 case ENOENT:
236                         if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
237                                 bb_error_msg(ERR_INVALID_KEY, outname);
238                         break;
239                 case EACCES:
240                         bb_error_msg(ERR_PERMISSION_DENIED, outname);
241                         break;
242                 default:
243                         bb_perror_msg(ERR_UNKNOWN_READING, outname);
244                         break;
245                 }
246                 retval = EXIT_FAILURE;
247         } else {
248                 while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
249                         if (option_mask32 & FLAG_SHOW_KEYS) {
250                                 printf("%s = ", outname);
251                         }
252                         fputs(inbuf, stdout);
253                 }
254                 fclose(fp);
255                 retval = EXIT_SUCCESS;
256         }
257
258         free(tmpname);
259         free(outname);
260         return retval;
261 } /* end sysctl_read_setting() */
262
263 /*
264  *     Display all the sysctl settings
265  */
266 static int sysctl_display_all(const char *path)
267 {
268         int retval = 0;
269         DIR *dp;
270         struct dirent *de;
271         char *tmpdir;
272         struct stat ts;
273
274         dp = opendir(path);
275         if (!dp) {
276                 return EXIT_FAILURE;
277         }
278         while ((de = readdir(dp)) != NULL) {
279                 tmpdir = concat_subpath_file(path, de->d_name);
280                 if (tmpdir == NULL)
281                         continue; /* . or .. */
282                 if (stat(tmpdir, &ts) != 0) {
283                         bb_perror_msg(tmpdir);
284                 } else if (S_ISDIR(ts.st_mode)) {
285                         retval |= sysctl_display_all(tmpdir);
286                 } else {
287                         retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);
288                 }
289                 free(tmpdir);
290         } /* end while */
291         closedir(dp);
292
293         return retval;
294 } /* end sysctl_display_all() */