sysctl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 EACCES:
  158. /* Happens for write-only settings, e.g. net.ipv6.route.flush */
  159. goto end;
  160. case ENOENT:
  161. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  162. bb_error_msg("error: '%s' is an unknown key", outname);
  163. break;
  164. default:
  165. bb_perror_msg("error %sing key '%s'",
  166. writing ?
  167. "sett" : "read",
  168. outname);
  169. break;
  170. }
  171. retval = EXIT_FAILURE;
  172. goto end;
  173. }
  174. if (writing) {
  175. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  176. xwrite_str(fd, value);
  177. close(fd);
  178. if (!(option_mask32 & FLAG_QUIET)) {
  179. if (option_mask32 & FLAG_SHOW_KEYS)
  180. printf("%s = ", outname);
  181. puts(value);
  182. }
  183. } else {
  184. char c;
  185. value = cptr = xmalloc_read(fd, NULL);
  186. close(fd);
  187. if (value == NULL) {
  188. bb_perror_msg("error reading key '%s'", outname);
  189. retval = EXIT_FAILURE;
  190. goto end;
  191. }
  192. /* dev.cdrom.info and sunrpc.transports, for example,
  193. * are multi-line. Try "sysctl sunrpc.transports"
  194. */
  195. while ((c = *cptr) != '\0') {
  196. if (option_mask32 & FLAG_SHOW_KEYS)
  197. printf("%s = ", outname);
  198. while (1) {
  199. fputc(c, stdout);
  200. cptr++;
  201. if (c == '\n')
  202. break;
  203. c = *cptr;
  204. if (c == '\0')
  205. break;
  206. }
  207. }
  208. free(value);
  209. }
  210. end:
  211. free(outname);
  212. return retval;
  213. }
  214. static int sysctl_act_recursive(const char *path)
  215. {
  216. struct stat buf;
  217. int retval = 0;
  218. if (!(option_mask32 & FLAG_WRITE)
  219. && stat(path, &buf) == 0
  220. && S_ISDIR(buf.st_mode)
  221. ) {
  222. struct dirent *entry;
  223. DIR *dirp;
  224. dirp = opendir(path);
  225. if (dirp == NULL)
  226. return -1;
  227. while ((entry = readdir(dirp)) != NULL) {
  228. char *next = concat_subpath_file(path, entry->d_name);
  229. if (next == NULL)
  230. continue; /* d_name is "." or ".." */
  231. /* if path was ".", drop "./" prefix: */
  232. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  233. next + 2 : next);
  234. free(next);
  235. }
  236. closedir(dirp);
  237. } else {
  238. char *name = xstrdup(path);
  239. retval |= sysctl_act_on_setting(name);
  240. free(name);
  241. }
  242. return retval;
  243. }
  244. /* Set sysctl's from a conf file. Format example:
  245. * # Controls IP packet forwarding
  246. * net.ipv4.ip_forward = 0
  247. */
  248. static int sysctl_handle_preload_file(const char *filename)
  249. {
  250. char *token[2];
  251. parser_t *parser;
  252. int parse_flags;
  253. parser = config_open(filename);
  254. /* Must do it _after_ config_open(): */
  255. xchdir("/proc/sys");
  256. parse_flags = 0;
  257. parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one
  258. parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters
  259. parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line
  260. parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found
  261. parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
  262. parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#'
  263. /* <space><tab><space>#comment is also comment, not strictly 1st char only */
  264. parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before
  265. while (config_read(parser, token, 2, 2, ";#=", parse_flags)) {
  266. char *tp;
  267. trim(token[1]);
  268. tp = trim(token[0]);
  269. sysctl_dots_to_slashes(token[0]);
  270. /* ^^^converted in-place. tp still points to NUL */
  271. /* now, add "=TOKEN1" */
  272. *tp++ = '=';
  273. overlapping_strcpy(tp, token[1]);
  274. sysctl_act_on_setting(token[0]);
  275. }
  276. if (ENABLE_FEATURE_CLEAN_UP)
  277. config_close(parser);
  278. return 0;
  279. }
  280. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  281. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  282. {
  283. int retval;
  284. int opt;
  285. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  286. argv += optind;
  287. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  288. option_mask32 = opt;
  289. if (opt & FLAG_PRELOAD_FILE) {
  290. int cur_dir_fd;
  291. option_mask32 |= FLAG_WRITE;
  292. if (!*argv)
  293. *--argv = (char*)"/etc/sysctl.conf";
  294. cur_dir_fd = xopen(".", O_RDONLY | O_DIRECTORY);
  295. do {
  296. /* xchdir("/proc/sys") is inside */
  297. sysctl_handle_preload_file(*argv);
  298. xfchdir(cur_dir_fd); /* files can be relative, must restore cwd */
  299. } while (*++argv);
  300. return 0; /* procps-ng 3.3.10 does not flag parse errors */
  301. }
  302. xchdir("/proc/sys");
  303. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  304. return sysctl_act_recursive(".");
  305. }
  306. //TODO: if(!argv[0]) bb_show_usage() ?
  307. retval = 0;
  308. while (*argv) {
  309. sysctl_dots_to_slashes(*argv);
  310. retval |= sysctl_act_recursive(*argv);
  311. argv++;
  312. }
  313. return retval;
  314. }