sysctl.c 7.4 KB

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