sysctl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 source tree.
  8. *
  9. * Changelog:
  10. * v1.01 - added -p <preload> to preload values from a file
  11. * v1.01.1 - busybox applet aware by <solar@gentoo.org>
  12. */
  13. //usage:#define sysctl_trivial_usage
  14. //usage: "[OPTIONS] [KEY[=VALUE]]..."
  15. //usage:#define sysctl_full_usage "\n\n"
  16. //usage: "Show/set kernel parameters\n"
  17. //usage: "\n -e Don't warn about unknown keys"
  18. //usage: "\n -n Don't show key names"
  19. //usage: "\n -a Show all values"
  20. /* Same as -a, no need to show it */
  21. /* //usage: "\n -A Show all values in table form" */
  22. //usage: "\n -w Set values"
  23. //usage: "\n -p FILE Set values from FILE (default /etc/sysctl.conf)"
  24. //usage: "\n -q Set values silently"
  25. //usage:
  26. //usage:#define sysctl_example_usage
  27. //usage: "sysctl [-n] [-e] variable...\n"
  28. //usage: "sysctl [-n] [-e] [-q] -w variable=value...\n"
  29. //usage: "sysctl [-n] [-e] -a\n"
  30. //usage: "sysctl [-n] [-e] [-q] -p file (default /etc/sysctl.conf)\n"
  31. //usage: "sysctl [-n] [-e] -A\n"
  32. #include "libbb.h"
  33. enum {
  34. FLAG_SHOW_KEYS = 1 << 0,
  35. FLAG_SHOW_KEY_ERRORS = 1 << 1,
  36. FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
  37. FLAG_SHOW_ALL = 1 << 3,
  38. FLAG_PRELOAD_FILE = 1 << 4,
  39. /* TODO: procps 3.2.8 seems to not require -w for KEY=VAL to work: */
  40. FLAG_WRITE = 1 << 5,
  41. FLAG_QUIET = 1 << 6,
  42. };
  43. #define OPTION_STR "neAapwq"
  44. static void sysctl_dots_to_slashes(char *name)
  45. {
  46. char *cptr, *last_good, *end;
  47. /* Convert minimum number of '.' to '/' so that
  48. * we end up with existing file's name.
  49. *
  50. * Example from bug 3894:
  51. * net.ipv4.conf.eth0.100.mc_forwarding ->
  52. * net/ipv4/conf/eth0.100/mc_forwarding
  53. * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
  54. * therefore we must start from the end, and if
  55. * we replaced even one . -> /, start over again,
  56. * but never replace dots before the position
  57. * where last replacement occurred.
  58. *
  59. * Another bug we later had is that
  60. * net.ipv4.conf.eth0.100
  61. * (without .mc_forwarding) was mishandled.
  62. *
  63. * To set up testing: modprobe 8021q; vconfig add eth0 100
  64. */
  65. end = name + strlen(name);
  66. last_good = name - 1;
  67. *end = '.'; /* trick the loop into trying full name too */
  68. again:
  69. cptr = end;
  70. while (cptr > last_good) {
  71. if (*cptr == '.') {
  72. *cptr = '\0';
  73. //bb_error_msg("trying:'%s'", name);
  74. if (access(name, F_OK) == 0) {
  75. *cptr = '/';
  76. //bb_error_msg("replaced:'%s'", name);
  77. last_good = cptr;
  78. goto again;
  79. }
  80. *cptr = '.';
  81. }
  82. cptr--;
  83. }
  84. *end = '\0';
  85. }
  86. static int sysctl_act_on_setting(char *setting)
  87. {
  88. int fd, retval = EXIT_SUCCESS;
  89. char *cptr, *outname;
  90. char *value = value; /* for compiler */
  91. outname = xstrdup(setting);
  92. cptr = outname;
  93. while (*cptr) {
  94. if (*cptr == '/')
  95. *cptr = '.';
  96. cptr++;
  97. }
  98. if (option_mask32 & FLAG_WRITE) {
  99. cptr = strchr(setting, '=');
  100. if (cptr == NULL) {
  101. bb_error_msg("error: '%s' must be of the form name=value",
  102. outname);
  103. retval = EXIT_FAILURE;
  104. goto end;
  105. }
  106. value = cptr + 1; /* point to the value in name=value */
  107. if (setting == cptr || !*value) {
  108. bb_error_msg("error: malformed setting '%s'", outname);
  109. retval = EXIT_FAILURE;
  110. goto end;
  111. }
  112. *cptr = '\0';
  113. outname[cptr - setting] = '\0';
  114. /* procps 3.2.7 actually uses these flags */
  115. fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  116. } else {
  117. fd = open(setting, O_RDONLY);
  118. }
  119. if (fd < 0) {
  120. switch (errno) {
  121. case ENOENT:
  122. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  123. bb_error_msg("error: '%s' is an unknown key", outname);
  124. break;
  125. default:
  126. bb_perror_msg("error %sing key '%s'",
  127. option_mask32 & FLAG_WRITE ?
  128. "sett" : "read",
  129. outname);
  130. break;
  131. }
  132. retval = EXIT_FAILURE;
  133. goto end;
  134. }
  135. if (option_mask32 & FLAG_WRITE) {
  136. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  137. xwrite_str(fd, value);
  138. close(fd);
  139. if (!(option_mask32 & FLAG_QUIET)) {
  140. if (option_mask32 & FLAG_SHOW_KEYS)
  141. printf("%s = ", outname);
  142. puts(value);
  143. }
  144. } else {
  145. char c;
  146. value = cptr = xmalloc_read(fd, NULL);
  147. close(fd);
  148. if (value == NULL) {
  149. bb_perror_msg("error reading key '%s'", outname);
  150. goto end;
  151. }
  152. /* dev.cdrom.info and sunrpc.transports, for example,
  153. * are multi-line. Try "sysctl sunrpc.transports"
  154. */
  155. while ((c = *cptr) != '\0') {
  156. if (option_mask32 & FLAG_SHOW_KEYS)
  157. printf("%s = ", outname);
  158. while (1) {
  159. fputc(c, stdout);
  160. cptr++;
  161. if (c == '\n')
  162. break;
  163. c = *cptr;
  164. if (c == '\0')
  165. break;
  166. }
  167. }
  168. free(value);
  169. }
  170. end:
  171. free(outname);
  172. return retval;
  173. }
  174. static int sysctl_act_recursive(const char *path)
  175. {
  176. DIR *dirp;
  177. struct stat buf;
  178. struct dirent *entry;
  179. char *next;
  180. int retval = 0;
  181. stat(path, &buf);
  182. if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
  183. dirp = opendir(path);
  184. if (dirp == NULL)
  185. return -1;
  186. while ((entry = readdir(dirp)) != NULL) {
  187. next = concat_subpath_file(path, entry->d_name);
  188. if (next == NULL)
  189. continue; /* d_name is "." or ".." */
  190. /* if path was ".", drop "./" prefix: */
  191. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  192. next + 2 : next);
  193. free(next);
  194. }
  195. closedir(dirp);
  196. } else {
  197. char *name = xstrdup(path);
  198. retval |= sysctl_act_on_setting(name);
  199. free(name);
  200. }
  201. return retval;
  202. }
  203. /* Set sysctl's from a conf file. Format example:
  204. * # Controls IP packet forwarding
  205. * net.ipv4.ip_forward = 0
  206. */
  207. static int sysctl_handle_preload_file(const char *filename)
  208. {
  209. char *token[2];
  210. parser_t *parser;
  211. parser = config_open(filename);
  212. /* Must do it _after_ config_open(): */
  213. xchdir("/proc/sys");
  214. /* xchroot("/proc/sys") - if you are paranoid */
  215. //TODO: ';' is comment char too
  216. //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
  217. // (but _whitespace_ from ends should be trimmed first (and we do it right))
  218. //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
  219. // can it be fixed by removing PARSE_COLLAPSE bit?
  220. while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
  221. char *tp;
  222. sysctl_dots_to_slashes(token[0]);
  223. tp = xasprintf("%s=%s", token[0], token[1]);
  224. sysctl_act_recursive(tp);
  225. free(tp);
  226. }
  227. if (ENABLE_FEATURE_CLEAN_UP)
  228. config_close(parser);
  229. return 0;
  230. }
  231. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  232. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  233. {
  234. int retval;
  235. int opt;
  236. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  237. argv += optind;
  238. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  239. option_mask32 = opt;
  240. if (opt & FLAG_PRELOAD_FILE) {
  241. option_mask32 |= FLAG_WRITE;
  242. /* xchdir("/proc/sys") is inside */
  243. return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
  244. }
  245. xchdir("/proc/sys");
  246. /* xchroot("/proc/sys") - if you are paranoid */
  247. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  248. return sysctl_act_recursive(".");
  249. }
  250. retval = 0;
  251. while (*argv) {
  252. sysctl_dots_to_slashes(*argv);
  253. retval |= sysctl_act_recursive(*argv);
  254. argv++;
  255. }
  256. return retval;
  257. }