sysctl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 tarball for details.
  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. #include "libbb.h"
  14. enum {
  15. FLAG_SHOW_KEYS = 1 << 0,
  16. FLAG_SHOW_KEY_ERRORS = 1 << 1,
  17. FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
  18. FLAG_SHOW_ALL = 1 << 3,
  19. FLAG_PRELOAD_FILE = 1 << 4,
  20. FLAG_WRITE = 1 << 5,
  21. };
  22. #define OPTION_STR "neAapw"
  23. static void sysctl_dots_to_slashes(char *name)
  24. {
  25. char *cptr, *last_good, *end;
  26. /* Convert minimum number of '.' to '/' so that
  27. * we end up with existing file's name.
  28. *
  29. * Example from bug 3894:
  30. * net.ipv4.conf.eth0.100.mc_forwarding ->
  31. * net/ipv4/conf/eth0.100/mc_forwarding
  32. * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
  33. * therefore we must start from the end, and if
  34. * we replaced even one . -> /, start over again,
  35. * but never replace dots before the position
  36. * where last replacement occurred.
  37. *
  38. * Another bug we later had is that
  39. * net.ipv4.conf.eth0.100
  40. * (without .mc_forwarding) was mishandled.
  41. *
  42. * To set up testing: modprobe 8021q; vconfig add eth0 100
  43. */
  44. end = name + strlen(name);
  45. last_good = name - 1;
  46. *end = '.'; /* trick the loop into trying full name too */
  47. again:
  48. cptr = end;
  49. while (cptr > last_good) {
  50. if (*cptr == '.') {
  51. *cptr = '\0';
  52. //bb_error_msg("trying:'%s'", name);
  53. if (access(name, F_OK) == 0) {
  54. if (cptr != end) /* prevent trailing '/' */
  55. *cptr = '/';
  56. //bb_error_msg("replaced:'%s'", name);
  57. last_good = cptr;
  58. goto again;
  59. }
  60. *cptr = '.';
  61. }
  62. cptr--;
  63. }
  64. *end = '\0';
  65. }
  66. static int sysctl_act_on_setting(char *setting)
  67. {
  68. int fd, retval = EXIT_SUCCESS;
  69. char *cptr, *outname;
  70. char *value = value; /* for compiler */
  71. outname = xstrdup(setting);
  72. cptr = outname;
  73. while (*cptr) {
  74. if (*cptr == '/')
  75. *cptr = '.';
  76. cptr++;
  77. }
  78. if (option_mask32 & FLAG_WRITE) {
  79. cptr = strchr(setting, '=');
  80. if (cptr == NULL) {
  81. bb_error_msg("error: '%s' must be of the form name=value",
  82. outname);
  83. retval = EXIT_FAILURE;
  84. goto end;
  85. }
  86. value = cptr + 1; /* point to the value in name=value */
  87. if (setting == cptr || !*value) {
  88. bb_error_msg("error: malformed setting '%s'", outname);
  89. retval = EXIT_FAILURE;
  90. goto end;
  91. }
  92. *cptr = '\0';
  93. outname[cptr - setting] = '\0';
  94. /* procps 3.2.7 actually uses these flags */
  95. fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  96. } else {
  97. fd = open(setting, O_RDONLY);
  98. }
  99. if (fd < 0) {
  100. switch (errno) {
  101. case ENOENT:
  102. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  103. bb_error_msg("error: '%s' is an unknown key", outname);
  104. break;
  105. default:
  106. bb_perror_msg("error %sing key '%s'",
  107. option_mask32 & FLAG_WRITE ?
  108. "sett" : "read",
  109. outname);
  110. break;
  111. }
  112. retval = EXIT_FAILURE;
  113. goto end;
  114. }
  115. if (option_mask32 & FLAG_WRITE) {
  116. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  117. xwrite_str(fd, value);
  118. close(fd);
  119. if (option_mask32 & FLAG_SHOW_KEYS)
  120. printf("%s = ", outname);
  121. puts(value);
  122. } else {
  123. char c;
  124. value = cptr = xmalloc_read(fd, NULL);
  125. close(fd);
  126. if (value == NULL) {
  127. bb_perror_msg("error reading key '%s'", outname);
  128. goto end;
  129. }
  130. /* dev.cdrom.info and sunrpc.transports, for example,
  131. * are multi-line. Try "sysctl sunrpc.transports"
  132. */
  133. while ((c = *cptr) != '\0') {
  134. if (option_mask32 & FLAG_SHOW_KEYS)
  135. printf("%s = ", outname);
  136. while (1) {
  137. fputc(c, stdout);
  138. cptr++;
  139. if (c == '\n')
  140. break;
  141. c = *cptr;
  142. if (c == '\0')
  143. break;
  144. }
  145. }
  146. free(value);
  147. }
  148. end:
  149. free(outname);
  150. return retval;
  151. }
  152. static int sysctl_act_recursive(const char *path)
  153. {
  154. DIR *dirp;
  155. struct stat buf;
  156. struct dirent *entry;
  157. char *next;
  158. int retval = 0;
  159. stat(path, &buf);
  160. if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
  161. dirp = opendir(path);
  162. if (dirp == NULL)
  163. return -1;
  164. while ((entry = readdir(dirp)) != NULL) {
  165. next = concat_subpath_file(path, entry->d_name);
  166. if (next == NULL)
  167. continue; /* d_name is "." or ".." */
  168. /* if path was ".", drop "./" prefix: */
  169. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  170. next + 2 : next);
  171. free(next);
  172. }
  173. closedir(dirp);
  174. } else {
  175. char *name = xstrdup(path);
  176. retval |= sysctl_act_on_setting(name);
  177. free(name);
  178. }
  179. return retval;
  180. }
  181. /* Set sysctl's from a conf file. Format example:
  182. * # Controls IP packet forwarding
  183. * net.ipv4.ip_forward = 0
  184. */
  185. static int sysctl_handle_preload_file(const char *filename)
  186. {
  187. char *token[2];
  188. parser_t *parser;
  189. parser = config_open(filename);
  190. /* Must do it _after_ config_open(): */
  191. xchdir("/proc/sys");
  192. /* xchroot(".") - if you are paranoid */
  193. //TODO: ';' is comment char too
  194. //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
  195. // (but _whitespace_ from ends should be trimmed first (and we do it right))
  196. //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
  197. while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
  198. char *tp;
  199. sysctl_dots_to_slashes(token[0]);
  200. tp = xasprintf("%s=%s", token[0], token[1]);
  201. sysctl_act_recursive(tp);
  202. free(tp);
  203. }
  204. if (ENABLE_FEATURE_CLEAN_UP)
  205. config_close(parser);
  206. return 0;
  207. }
  208. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  209. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  210. {
  211. int retval;
  212. int opt;
  213. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  214. argv += optind;
  215. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  216. option_mask32 = opt;
  217. if (opt & FLAG_PRELOAD_FILE) {
  218. option_mask32 |= FLAG_WRITE;
  219. /* xchdir("/proc/sys") is inside */
  220. return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
  221. }
  222. xchdir("/proc/sys");
  223. /* xchroot(".") - if you are paranoid */
  224. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  225. return sysctl_act_recursive(".");
  226. }
  227. retval = 0;
  228. while (*argv) {
  229. sysctl_dots_to_slashes(*argv);
  230. retval |= sysctl_act_recursive(*argv);
  231. argv++;
  232. }
  233. return retval;
  234. }