sysctl.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 EACCES:
  122. /* Happens for write-only settings, e.g. net.ipv6.route.flush */
  123. goto end;
  124. case ENOENT:
  125. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  126. bb_error_msg("error: '%s' is an unknown key", outname);
  127. break;
  128. default:
  129. bb_perror_msg("error %sing key '%s'",
  130. option_mask32 & FLAG_WRITE ?
  131. "sett" : "read",
  132. outname);
  133. break;
  134. }
  135. retval = EXIT_FAILURE;
  136. goto end;
  137. }
  138. if (option_mask32 & FLAG_WRITE) {
  139. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  140. xwrite_str(fd, value);
  141. close(fd);
  142. if (!(option_mask32 & FLAG_QUIET)) {
  143. if (option_mask32 & FLAG_SHOW_KEYS)
  144. printf("%s = ", outname);
  145. puts(value);
  146. }
  147. } else {
  148. char c;
  149. value = cptr = xmalloc_read(fd, NULL);
  150. close(fd);
  151. if (value == NULL) {
  152. bb_perror_msg("error reading key '%s'", outname);
  153. goto end;
  154. }
  155. /* dev.cdrom.info and sunrpc.transports, for example,
  156. * are multi-line. Try "sysctl sunrpc.transports"
  157. */
  158. while ((c = *cptr) != '\0') {
  159. if (option_mask32 & FLAG_SHOW_KEYS)
  160. printf("%s = ", outname);
  161. while (1) {
  162. fputc(c, stdout);
  163. cptr++;
  164. if (c == '\n')
  165. break;
  166. c = *cptr;
  167. if (c == '\0')
  168. break;
  169. }
  170. }
  171. free(value);
  172. }
  173. end:
  174. free(outname);
  175. return retval;
  176. }
  177. static int sysctl_act_recursive(const char *path)
  178. {
  179. DIR *dirp;
  180. struct stat buf;
  181. struct dirent *entry;
  182. char *next;
  183. int retval = 0;
  184. stat(path, &buf);
  185. if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
  186. dirp = opendir(path);
  187. if (dirp == NULL)
  188. return -1;
  189. while ((entry = readdir(dirp)) != NULL) {
  190. next = concat_subpath_file(path, entry->d_name);
  191. if (next == NULL)
  192. continue; /* d_name is "." or ".." */
  193. /* if path was ".", drop "./" prefix: */
  194. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  195. next + 2 : next);
  196. free(next);
  197. }
  198. closedir(dirp);
  199. } else {
  200. char *name = xstrdup(path);
  201. retval |= sysctl_act_on_setting(name);
  202. free(name);
  203. }
  204. return retval;
  205. }
  206. /* Set sysctl's from a conf file. Format example:
  207. * # Controls IP packet forwarding
  208. * net.ipv4.ip_forward = 0
  209. */
  210. static int sysctl_handle_preload_file(const char *filename)
  211. {
  212. char *token[2];
  213. parser_t *parser;
  214. parser = config_open(filename);
  215. /* Must do it _after_ config_open(): */
  216. xchdir("/proc/sys");
  217. /* xchroot("/proc/sys") - if you are paranoid */
  218. //TODO: ';' is comment char too
  219. //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
  220. // (but _whitespace_ from ends should be trimmed first (and we do it right))
  221. //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
  222. // can it be fixed by removing PARSE_COLLAPSE bit?
  223. while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
  224. char *tp;
  225. sysctl_dots_to_slashes(token[0]);
  226. tp = xasprintf("%s=%s", token[0], token[1]);
  227. sysctl_act_recursive(tp);
  228. free(tp);
  229. }
  230. if (ENABLE_FEATURE_CLEAN_UP)
  231. config_close(parser);
  232. return 0;
  233. }
  234. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  235. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  236. {
  237. int retval;
  238. int opt;
  239. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  240. argv += optind;
  241. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  242. option_mask32 = opt;
  243. if (opt & FLAG_PRELOAD_FILE) {
  244. option_mask32 |= FLAG_WRITE;
  245. /* xchdir("/proc/sys") is inside */
  246. return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
  247. }
  248. xchdir("/proc/sys");
  249. /* xchroot("/proc/sys") - if you are paranoid */
  250. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  251. return sysctl_act_recursive(".");
  252. }
  253. retval = 0;
  254. while (*argv) {
  255. sysctl_dots_to_slashes(*argv);
  256. retval |= sysctl_act_recursive(*argv);
  257. argv++;
  258. }
  259. return retval;
  260. }