Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Sysctl 1.01 - A utility to read and manipulate the sysctl parameters |
| 4 | * |
Rob Landley | 8b1f11d | 2006-04-17 21:49:34 +0000 | [diff] [blame] | 5 | * Copyright 1999 George Staikos |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 6 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 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> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 14 | * |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Function Prototypes |
| 21 | */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 22 | static int sysctl_read_setting(const char *setting, int output); |
| 23 | static int sysctl_write_setting(const char *setting, int output); |
| 24 | static int sysctl_preload_file(const char *filename, int output); |
| 25 | static int sysctl_display_all(const char *path, int output, int show_table); |
| 26 | |
| 27 | /* |
| 28 | * Globals... |
| 29 | */ |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 30 | static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf"; |
| 31 | static const char PROC_SYS[] ALIGN1 = "/proc/sys/"; |
| 32 | enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 }; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 33 | |
| 34 | /* error messages */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 35 | static const char ERR_UNKNOWN_PARAMETER[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 36 | "error: unknown parameter '%s'"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 37 | static const char ERR_MALFORMED_SETTING[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 38 | "error: malformed setting '%s'"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 39 | static const char ERR_NO_EQUALS[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 40 | "error: '%s' must be of the form name=value"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 41 | static const char ERR_INVALID_KEY[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 42 | "error: '%s' is an unknown key"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 43 | static const char ERR_UNKNOWN_WRITING[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 44 | "error setting key '%s'"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 45 | static const char ERR_UNKNOWN_READING[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 46 | "error reading key '%s'"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 47 | static const char ERR_PERMISSION_DENIED[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 48 | "error: permission denied on key '%s'"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 49 | static const char ERR_PRELOAD_FILE[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 50 | "error: cannot open preload file '%s'"; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 51 | static const char WARN_BAD_LINE[] ALIGN1 = |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 52 | "warning: %s(%d): invalid syntax, continuing"; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 53 | |
| 54 | |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 55 | static void dwrite_str(int fd, const char *buf) |
| 56 | { |
| 57 | write(fd, buf, strlen(buf)); |
| 58 | } |
| 59 | |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 60 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | * sysctl_main()... |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 62 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 63 | int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 64 | int sysctl_main(int argc, char **argv) |
| 65 | { |
| 66 | int retval = 0; |
| 67 | int output = 1; |
| 68 | int write_mode = 0; |
| 69 | int switches_allowed = 1; |
| 70 | |
| 71 | if (argc < 2) |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 72 | bb_show_usage(); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 73 | |
| 74 | argv++; |
| 75 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 76 | for (; *argv /*&& **argv*/; argv++) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 77 | if (switches_allowed && **argv == '-') { /* we have a switch */ |
| 78 | switch ((*argv)[1]) { |
| 79 | case 'n': |
| 80 | output = 0; |
| 81 | break; |
| 82 | case 'w': |
| 83 | write_mode = 1; |
| 84 | switches_allowed = 0; |
| 85 | break; |
| 86 | case 'p': |
| 87 | argv++; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 88 | return sysctl_preload_file(((*argv /*&& **argv*/) ? *argv : ETC_SYSCTL_CONF), |
| 89 | output); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 90 | case 'a': |
| 91 | case 'A': |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 92 | return sysctl_display_all(PROC_SYS, output, |
| 93 | ((*argv)[1] == 'A')); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 94 | default: |
| 95 | bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv); |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 96 | /* fall through */ |
| 97 | //case 'h': |
| 98 | //case '?': |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 99 | bb_show_usage(); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 100 | } |
| 101 | } else { |
| 102 | switches_allowed = 0; |
| 103 | if (write_mode) |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 104 | retval |= sysctl_write_setting(*argv, output); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 105 | else |
| 106 | sysctl_read_setting(*argv, output); |
| 107 | } |
| 108 | } |
| 109 | return retval; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 110 | } /* end sysctl_main() */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 113 | * sysctl_preload_file |
| 114 | * preload the sysctl's from a conf file |
| 115 | * - we parse the file and then reform it (strip out whitespace) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 116 | */ |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 117 | #define PRELOAD_BUF 256 |
| 118 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 119 | static int sysctl_preload_file(const char *filename, int output) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 120 | { |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 121 | int lineno; |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 122 | char oneline[PRELOAD_BUF]; |
| 123 | char buffer[PRELOAD_BUF]; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 124 | char *name, *value; |
| 125 | FILE *fp; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 127 | fp = fopen(filename, "r"); |
| 128 | if (fp == NULL) { |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 129 | bb_error_msg_and_die(ERR_PRELOAD_FILE, filename); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 132 | lineno = 0; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 133 | while (fgets(oneline, sizeof(oneline) - 1, fp)) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 134 | lineno++; |
| 135 | trim(oneline); |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 136 | if (oneline[0] == '#' || oneline[0] == ';') |
| 137 | continue; |
| 138 | if (!oneline[0] || !oneline[1]) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 139 | continue; |
| 140 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 141 | name = strtok(oneline, "="); |
| 142 | if (!name) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 143 | bb_error_msg(WARN_BAD_LINE, filename, lineno); |
| 144 | continue; |
| 145 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 146 | trim(name); |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 147 | if (!*name) { |
| 148 | bb_error_msg(WARN_BAD_LINE, filename, lineno); |
| 149 | continue; |
| 150 | } |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 151 | |
| 152 | value = strtok(NULL, "\n\r"); |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 153 | if (!value) { |
| 154 | bb_error_msg(WARN_BAD_LINE, filename, lineno); |
| 155 | continue; |
| 156 | } |
| 157 | while (*value == ' ' || *value == '\t') |
| 158 | value++; |
| 159 | if (!*value) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 160 | bb_error_msg(WARN_BAD_LINE, filename, lineno); |
| 161 | continue; |
| 162 | } |
| 163 | |
Denis Vlasenko | 9cac521 | 2006-09-09 12:24:19 +0000 | [diff] [blame] | 164 | /* safe because sizeof(oneline) == sizeof(buffer) */ |
| 165 | sprintf(buffer, "%s=%s", name, value); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 166 | sysctl_write_setting(buffer, output); |
| 167 | } |
| 168 | fclose(fp); |
| 169 | return 0; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 170 | } /* end sysctl_preload_file() */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Write a single sysctl setting |
| 174 | */ |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 175 | static int sysctl_write_setting(const char *setting, int output) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 176 | { |
| 177 | int retval = 0; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 178 | const char *name; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 179 | const char *value; |
| 180 | const char *equals; |
| 181 | char *tmpname, *outname, *cptr; |
| 182 | int fd = -1; |
| 183 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 184 | name = setting; |
| 185 | equals = strchr(setting, '='); |
| 186 | if (!equals) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 187 | bb_error_msg(ERR_NO_EQUALS, setting); |
| 188 | return -1; |
| 189 | } |
| 190 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 191 | value = equals + 1; /* point to the value in name=value */ |
| 192 | if (name == equals || !*value) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 193 | bb_error_msg(ERR_MALFORMED_SETTING, setting); |
| 194 | return -2; |
| 195 | } |
| 196 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 197 | tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name); |
| 198 | outname = xstrdup(tmpname + strlen_PROC_SYS); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 199 | |
| 200 | while ((cptr = strchr(tmpname, '.')) != NULL) |
| 201 | *cptr = '/'; |
| 202 | |
| 203 | while ((cptr = strchr(outname, '/')) != NULL) |
| 204 | *cptr = '.'; |
| 205 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 206 | fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 207 | if (fd < 0) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 208 | switch (errno) { |
| 209 | case ENOENT: |
| 210 | bb_error_msg(ERR_INVALID_KEY, outname); |
| 211 | break; |
| 212 | case EACCES: |
| 213 | bb_perror_msg(ERR_PERMISSION_DENIED, outname); |
| 214 | break; |
| 215 | default: |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 216 | bb_perror_msg(ERR_UNKNOWN_WRITING, outname); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | retval = -1; |
| 220 | } else { |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 221 | dwrite_str(fd, value); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 222 | close(fd); |
| 223 | if (output) { |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 224 | dwrite_str(STDOUT_FILENO, outname); |
| 225 | dwrite_str(STDOUT_FILENO, " = "); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 226 | } |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 227 | dwrite_str(STDOUT_FILENO, value); |
| 228 | dwrite_str(STDOUT_FILENO, "\n"); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 231 | free(tmpname); |
| 232 | free(outname); |
| 233 | return retval; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 234 | } /* end sysctl_write_setting() */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 235 | |
| 236 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 237 | * Read a sysctl setting |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 238 | */ |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 239 | static int sysctl_read_setting(const char *setting, int output) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 240 | { |
| 241 | int retval = 0; |
| 242 | char *tmpname, *outname, *cptr; |
| 243 | char inbuf[1025]; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 244 | const char *name; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 245 | FILE *fp; |
| 246 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 247 | if (!*setting) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 248 | bb_error_msg(ERR_INVALID_KEY, setting); |
| 249 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 250 | name = setting; |
| 251 | tmpname = concat_path_file(PROC_SYS, name); |
| 252 | outname = xstrdup(tmpname + strlen_PROC_SYS); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 253 | |
| 254 | while ((cptr = strchr(tmpname, '.')) != NULL) |
| 255 | *cptr = '/'; |
| 256 | while ((cptr = strchr(outname, '/')) != NULL) |
| 257 | *cptr = '.'; |
| 258 | |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 259 | fp = fopen(tmpname, "r"); |
| 260 | if (fp == NULL) { |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 261 | switch (errno) { |
| 262 | case ENOENT: |
| 263 | bb_error_msg(ERR_INVALID_KEY, outname); |
| 264 | break; |
| 265 | case EACCES: |
| 266 | bb_error_msg(ERR_PERMISSION_DENIED, outname); |
| 267 | break; |
| 268 | default: |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 269 | bb_perror_msg(ERR_UNKNOWN_READING, outname); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | retval = -1; |
| 273 | } else { |
| 274 | while (fgets(inbuf, sizeof(inbuf) - 1, fp)) { |
| 275 | if (output) { |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 276 | dwrite_str(STDOUT_FILENO, outname); |
| 277 | dwrite_str(STDOUT_FILENO, " = "); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 278 | } |
Glenn L McGrath | ab82154 | 2003-12-26 02:19:34 +0000 | [diff] [blame] | 279 | dwrite_str(STDOUT_FILENO, inbuf); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 280 | } |
| 281 | fclose(fp); |
| 282 | } |
| 283 | |
| 284 | free(tmpname); |
| 285 | free(outname); |
| 286 | return retval; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 287 | } /* end sysctl_read_setting() */ |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 288 | |
| 289 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 290 | * Display all the sysctl settings |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 291 | */ |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 292 | static int sysctl_display_all(const char *path, int output, int show_table) |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 293 | { |
| 294 | int retval = 0; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 295 | DIR *dp; |
| 296 | struct dirent *de; |
| 297 | char *tmpdir; |
| 298 | struct stat ts; |
| 299 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 300 | dp = opendir(path); |
| 301 | if (!dp) { |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 302 | return -1; |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 303 | } |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 304 | while ((de = readdir(dp)) != NULL) { |
| 305 | tmpdir = concat_subpath_file(path, de->d_name); |
| 306 | if (tmpdir == NULL) |
| 307 | continue; |
| 308 | if (stat(tmpdir, &ts) != 0) { |
| 309 | bb_perror_msg(tmpdir); |
| 310 | } else if (S_ISDIR(ts.st_mode)) { |
| 311 | sysctl_display_all(tmpdir, output, show_table); |
| 312 | } else { |
| 313 | retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS, output); |
| 314 | } |
| 315 | free(tmpdir); |
| 316 | } /* end while */ |
| 317 | closedir(dp); |
Eric Andersen | b905028 | 2003-12-24 06:02:11 +0000 | [diff] [blame] | 318 | |
| 319 | return retval; |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame^] | 320 | } /* end sysctl_display_all() */ |