sysctl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 (6.9 kb)"
  15. //config: default y
  16. //config: help
  17. //config: Configure kernel parameters at runtime.
  18. //applet:IF_BB_SYSCTL(APPLET_NOEXEC(sysctl, sysctl, BB_DIR_SBIN, BB_SUID_DROP, sysctl))
  19. //kbuild:lib-$(CONFIG_BB_SYSCTL) += sysctl.o
  20. //usage:#define sysctl_trivial_usage
  21. //usage: "-p [-enq] [FILE...] / [-enqaw] [KEY[=VALUE]]..."
  22. //usage:#define sysctl_full_usage "\n\n"
  23. //usage: "Show/set kernel parameters\n"
  24. //usage: "\n -p Set values from FILEs (default /etc/sysctl.conf)"
  25. //usage: "\n -e Don't warn about unknown keys"
  26. //usage: "\n -n Don't show key names"
  27. //usage: "\n -q Quiet"
  28. //usage: "\n -a Show all values"
  29. /* Same as -a, no need to show it */
  30. /* //usage: "\n -A Show all values in table form" */
  31. //usage: "\n -w Set values"
  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. /* NB: procps 3.2.8 does not require -w for KEY=VAL to work, it only rejects non-KEY=VAL form */
  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. bool writing = (option_mask32 & FLAG_WRITE);
  99. outname = xstrdup(setting);
  100. cptr = outname;
  101. while (*cptr) {
  102. if (*cptr == '/')
  103. *cptr = '.';
  104. cptr++;
  105. }
  106. cptr = strchr(setting, '=');
  107. if (cptr)
  108. writing = 1;
  109. if (writing) {
  110. if (cptr == NULL) {
  111. bb_error_msg("error: '%s' must be of the form name=value",
  112. outname);
  113. retval = EXIT_FAILURE;
  114. goto end;
  115. }
  116. value = cptr + 1; /* point to the value in name=value */
  117. if (setting == cptr || !*value) {
  118. bb_error_msg("error: malformed setting '%s'", outname);
  119. retval = EXIT_FAILURE;
  120. goto end;
  121. }
  122. *cptr = '\0';
  123. outname[cptr - setting] = '\0';
  124. /* procps 3.2.7 actually uses these flags */
  125. fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  126. } else {
  127. fd = open(setting, O_RDONLY);
  128. }
  129. if (fd < 0) {
  130. switch (errno) {
  131. case EACCES:
  132. /* Happens for write-only settings, e.g. net.ipv6.route.flush */
  133. goto end;
  134. case ENOENT:
  135. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  136. bb_error_msg("error: '%s' is an unknown key", outname);
  137. break;
  138. default:
  139. bb_perror_msg("error %sing key '%s'",
  140. writing ?
  141. "sett" : "read",
  142. outname);
  143. break;
  144. }
  145. retval = EXIT_FAILURE;
  146. goto end;
  147. }
  148. if (writing) {
  149. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  150. xwrite_str(fd, value);
  151. close(fd);
  152. if (!(option_mask32 & FLAG_QUIET)) {
  153. if (option_mask32 & FLAG_SHOW_KEYS)
  154. printf("%s = ", outname);
  155. puts(value);
  156. }
  157. } else {
  158. char c;
  159. value = cptr = xmalloc_read(fd, NULL);
  160. close(fd);
  161. if (value == NULL) {
  162. bb_perror_msg("error reading key '%s'", outname);
  163. goto end;
  164. }
  165. /* dev.cdrom.info and sunrpc.transports, for example,
  166. * are multi-line. Try "sysctl sunrpc.transports"
  167. */
  168. while ((c = *cptr) != '\0') {
  169. if (option_mask32 & FLAG_SHOW_KEYS)
  170. printf("%s = ", outname);
  171. while (1) {
  172. fputc(c, stdout);
  173. cptr++;
  174. if (c == '\n')
  175. break;
  176. c = *cptr;
  177. if (c == '\0')
  178. break;
  179. }
  180. }
  181. free(value);
  182. }
  183. end:
  184. free(outname);
  185. return retval;
  186. }
  187. static int sysctl_act_recursive(const char *path)
  188. {
  189. DIR *dirp;
  190. struct stat buf;
  191. struct dirent *entry;
  192. char *next;
  193. int retval = 0;
  194. stat(path, &buf);
  195. if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
  196. dirp = opendir(path);
  197. if (dirp == NULL)
  198. return -1;
  199. while ((entry = readdir(dirp)) != NULL) {
  200. next = concat_subpath_file(path, entry->d_name);
  201. if (next == NULL)
  202. continue; /* d_name is "." or ".." */
  203. /* if path was ".", drop "./" prefix: */
  204. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  205. next + 2 : next);
  206. free(next);
  207. }
  208. closedir(dirp);
  209. } else {
  210. char *name = xstrdup(path);
  211. retval |= sysctl_act_on_setting(name);
  212. free(name);
  213. }
  214. return retval;
  215. }
  216. /* Set sysctl's from a conf file. Format example:
  217. * # Controls IP packet forwarding
  218. * net.ipv4.ip_forward = 0
  219. */
  220. static int sysctl_handle_preload_file(const char *filename)
  221. {
  222. char *token[2];
  223. parser_t *parser;
  224. int parse_flags;
  225. parser = config_open(filename);
  226. /* Must do it _after_ config_open(): */
  227. xchdir("/proc/sys");
  228. parse_flags = 0;
  229. parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
  230. parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
  231. parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
  232. parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
  233. parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
  234. parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#'
  235. /* <space><tab><space>#comment is also comment, not strictly 1st char only */
  236. parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before
  237. while (config_read(parser, token, 2, 2, ";#=", parse_flags)) {
  238. char *tp;
  239. trim(token[1]);
  240. tp = trim(token[0]);
  241. sysctl_dots_to_slashes(token[0]);
  242. /* ^^^converted in-place. tp still points to NUL */
  243. /* now, add "=TOKEN1" */
  244. *tp++ = '=';
  245. overlapping_strcpy(tp, token[1]);
  246. sysctl_act_on_setting(token[0]);
  247. }
  248. if (ENABLE_FEATURE_CLEAN_UP)
  249. config_close(parser);
  250. return 0;
  251. }
  252. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  253. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  254. {
  255. int retval;
  256. int opt;
  257. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  258. argv += optind;
  259. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  260. option_mask32 = opt;
  261. if (opt & FLAG_PRELOAD_FILE) {
  262. int cur_dir_fd;
  263. option_mask32 |= FLAG_WRITE;
  264. if (!*argv)
  265. *--argv = (char*)"/etc/sysctl.conf";
  266. cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
  267. do {
  268. /* xchdir("/proc/sys") is inside */
  269. sysctl_handle_preload_file(*argv);
  270. xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
  271. } while (*++argv);
  272. return 0; /* procps-ng 3.3.10 does not flag parse errors */
  273. }
  274. xchdir("/proc/sys");
  275. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  276. return sysctl_act_recursive(".");
  277. }
  278. retval = 0;
  279. while (*argv) {
  280. sysctl_dots_to_slashes(*argv);
  281. retval |= sysctl_act_recursive(*argv);
  282. argv++;
  283. }
  284. return retval;
  285. }