sysctl.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 (7.4 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, *slash;
  54. char end_ch;
  55. end = strchrnul(name, '=');
  56. slash = strchrnul(name, '/');
  57. if (slash < end
  58. && strchrnul(name, '.') < slash
  59. ) {
  60. /* There are both dots and slashes, and 1st dot is
  61. * before 1st slash.
  62. * (IOW: not raw, unmangled a/b/c.d format)
  63. *
  64. * procps supports this syntax for names with dots:
  65. * net.ipv4.conf.eth0/100.mc_forwarding
  66. * (dots and slashes are simply swapped)
  67. */
  68. while (end != name) {
  69. end--;
  70. if (*end == '.') *end = '/';
  71. else if (*end == '/') *end = '.';
  72. }
  73. return;
  74. }
  75. /* else: use our old behavior: */
  76. /* Convert minimum number of '.' to '/' so that
  77. * we end up with existing file's name.
  78. *
  79. * Example from bug 3894:
  80. * net.ipv4.conf.eth0.100.mc_forwarding ->
  81. * net/ipv4/conf/eth0.100/mc_forwarding
  82. * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
  83. * therefore we must start from the end, and if
  84. * we replaced even one . -> /, start over again,
  85. * but never replace dots before the position
  86. * where last replacement occurred.
  87. *
  88. * Another bug we later had is that
  89. * net.ipv4.conf.eth0.100
  90. * (without .mc_forwarding) was mishandled.
  91. *
  92. * To set up testing: modprobe 8021q; vconfig add eth0 100
  93. */
  94. end_ch = *end;
  95. *end = '.'; /* trick the loop into trying full name too */
  96. last_good = name - 1;
  97. again:
  98. cptr = end;
  99. while (cptr > last_good) {
  100. if (*cptr == '.') {
  101. *cptr = '\0';
  102. //bb_error_msg("trying:'%s'", name);
  103. if (access(name, F_OK) == 0) {
  104. *cptr = '/';
  105. //bb_error_msg("replaced:'%s'", name);
  106. last_good = cptr;
  107. goto again;
  108. }
  109. *cptr = '.';
  110. }
  111. cptr--;
  112. }
  113. *end = end_ch;
  114. }
  115. static int sysctl_act_on_setting(char *setting)
  116. {
  117. int fd, retval = EXIT_SUCCESS;
  118. char *cptr, *outname;
  119. char *value = value; /* for compiler */
  120. bool writing = (option_mask32 & FLAG_WRITE);
  121. outname = xstrdup(setting);
  122. cptr = outname;
  123. while (*cptr) {
  124. if (*cptr == '/')
  125. *cptr = '.';
  126. else if (*cptr == '.')
  127. *cptr = '/';
  128. cptr++;
  129. }
  130. cptr = strchr(setting, '=');
  131. if (cptr)
  132. writing = 1;
  133. if (writing) {
  134. if (!cptr) {
  135. bb_error_msg("error: '%s' must be of the form name=value",
  136. outname);
  137. retval = EXIT_FAILURE;
  138. goto end;
  139. }
  140. value = cptr + 1; /* point to the value in name=value */
  141. if (setting == cptr /* "name" can't be empty */
  142. /* || !*value - WRONG: "sysctl net.ipv4.ip_local_reserved_ports=" is a valid syntax (clears the value) */
  143. ) {
  144. bb_error_msg("error: malformed setting '%s'", outname);
  145. retval = EXIT_FAILURE;
  146. goto end;
  147. }
  148. *cptr = '\0';
  149. outname[cptr - setting] = '\0';
  150. /* procps 3.2.7 actually uses these flags */
  151. fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  152. } else {
  153. fd = open(setting, O_RDONLY);
  154. }
  155. if (fd < 0) {
  156. switch (errno) {
  157. case ENOENT:
  158. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  159. bb_error_msg("error: '%s' is an unknown key", outname);
  160. break;
  161. case EACCES:
  162. /* Happens for write-only settings, e.g. net.ipv6.route.flush */
  163. if (!writing)
  164. goto end;
  165. /* fall through */
  166. default:
  167. bb_perror_msg("error %sing key '%s'",
  168. writing ?
  169. "sett" : "read",
  170. outname);
  171. break;
  172. }
  173. retval = EXIT_FAILURE;
  174. goto end;
  175. }
  176. if (writing) {
  177. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  178. xwrite_str(fd, value);
  179. close(fd);
  180. if (!(option_mask32 & FLAG_QUIET)) {
  181. if (option_mask32 & FLAG_SHOW_KEYS)
  182. printf("%s = ", outname);
  183. puts(value);
  184. }
  185. } else {
  186. char c;
  187. value = cptr = xmalloc_read(fd, NULL);
  188. close(fd);
  189. if (value == NULL) {
  190. bb_perror_msg("error reading key '%s'", outname);
  191. retval = EXIT_FAILURE;
  192. goto end;
  193. }
  194. /* dev.cdrom.info and sunrpc.transports, for example,
  195. * are multi-line. Try "sysctl sunrpc.transports"
  196. */
  197. while ((c = *cptr) != '\0') {
  198. if (option_mask32 & FLAG_SHOW_KEYS)
  199. printf("%s = ", outname);
  200. while (1) {
  201. fputc(c, stdout);
  202. cptr++;
  203. if (c == '\n')
  204. break;
  205. c = *cptr;
  206. if (c == '\0')
  207. break;
  208. }
  209. }
  210. free(value);
  211. }
  212. end:
  213. free(outname);
  214. return retval;
  215. }
  216. static int sysctl_act_recursive(const char *path)
  217. {
  218. struct stat buf;
  219. int retval = 0;
  220. if (!(option_mask32 & FLAG_WRITE)
  221. && !strchr(path, '=') /* do not try to resurse on "var=val" */
  222. && stat(path, &buf) == 0
  223. && S_ISDIR(buf.st_mode)
  224. ) {
  225. struct dirent *entry;
  226. DIR *dirp;
  227. dirp = opendir(path);
  228. if (dirp == NULL)
  229. return -1;
  230. while ((entry = readdir(dirp)) != NULL) {
  231. char *next = concat_subpath_file(path, entry->d_name);
  232. if (next == NULL)
  233. continue; /* d_name is "." or ".." */
  234. /* if path was ".", drop "./" prefix: */
  235. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  236. next + 2 : next);
  237. free(next);
  238. }
  239. closedir(dirp);
  240. } else {
  241. char *name = xstrdup(path);
  242. retval |= sysctl_act_on_setting(name);
  243. free(name);
  244. }
  245. return retval;
  246. }
  247. /* Set sysctl's from a conf file. Format example:
  248. * # Controls IP packet forwarding
  249. * net.ipv4.ip_forward = 0
  250. */
  251. static int sysctl_handle_preload_file(const char *filename)
  252. {
  253. char *token[2];
  254. parser_t *parser;
  255. int parse_flags;
  256. parser = config_open(filename);
  257. /* Must do it _after_ config_open(): */
  258. xchdir("/proc/sys");
  259. parse_flags = 0;
  260. parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
  261. parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
  262. parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
  263. parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
  264. parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
  265. parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#'
  266. /* <space><tab><space>#comment is also comment, not strictly 1st char only */
  267. parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before
  268. while (config_read(parser, token, 2, 2, ";#=", parse_flags)) {
  269. char *tp;
  270. trim(token[1]);
  271. tp = trim(token[0]);
  272. sysctl_dots_to_slashes(token[0]);
  273. /* ^^^converted in-place. tp still points to NUL */
  274. /* now, add "=TOKEN1" */
  275. *tp++ = '=';
  276. overlapping_strcpy(tp, token[1]);
  277. sysctl_act_on_setting(token[0]);
  278. }
  279. if (ENABLE_FEATURE_CLEAN_UP)
  280. config_close(parser);
  281. return 0;
  282. }
  283. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  284. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  285. {
  286. int retval;
  287. int opt;
  288. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  289. argv += optind;
  290. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  291. option_mask32 = opt;
  292. if (opt & FLAG_PRELOAD_FILE) {
  293. int cur_dir_fd;
  294. option_mask32 |= FLAG_WRITE;
  295. if (!*argv)
  296. *--argv = (char*)"/etc/sysctl.conf";
  297. cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
  298. do {
  299. /* xchdir("/proc/sys") is inside */
  300. sysctl_handle_preload_file(*argv);
  301. xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
  302. } while (*++argv);
  303. return 0; /* procps-ng 3.3.10 does not flag parse errors */
  304. }
  305. xchdir("/proc/sys");
  306. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  307. return sysctl_act_recursive(".");
  308. }
  309. //TODO: if(!argv[0]) bb_show_usage() ?
  310. retval = 0;
  311. while (*argv) {
  312. sysctl_dots_to_slashes(*argv);
  313. retval |= sysctl_act_recursive(*argv);
  314. argv++;
  315. }
  316. return retval;
  317. }