sysctl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. *cptr = '/';
  55. //bb_error_msg("replaced:'%s'", name);
  56. last_good = cptr;
  57. goto again;
  58. }
  59. *cptr = '.';
  60. }
  61. cptr--;
  62. }
  63. *end = '\0';
  64. }
  65. static int sysctl_act_on_setting(char *setting)
  66. {
  67. int fd, retval = EXIT_SUCCESS;
  68. char *cptr, *outname;
  69. char *value = value; /* for compiler */
  70. outname = xstrdup(setting);
  71. cptr = outname;
  72. while (*cptr) {
  73. if (*cptr == '/')
  74. *cptr = '.';
  75. cptr++;
  76. }
  77. if (option_mask32 & FLAG_WRITE) {
  78. cptr = strchr(setting, '=');
  79. if (cptr == NULL) {
  80. bb_error_msg("error: '%s' must be of the form name=value",
  81. outname);
  82. retval = EXIT_FAILURE;
  83. goto end;
  84. }
  85. value = cptr + 1; /* point to the value in name=value */
  86. if (setting == cptr || !*value) {
  87. bb_error_msg("error: malformed setting '%s'", outname);
  88. retval = EXIT_FAILURE;
  89. goto end;
  90. }
  91. *cptr = '\0';
  92. outname[cptr - setting] = '\0';
  93. /* procps 3.2.7 actually uses these flags */
  94. fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  95. } else {
  96. fd = open(setting, O_RDONLY);
  97. }
  98. if (fd < 0) {
  99. switch (errno) {
  100. case ENOENT:
  101. if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
  102. bb_error_msg("error: '%s' is an unknown key", outname);
  103. break;
  104. default:
  105. bb_perror_msg("error %sing key '%s'",
  106. option_mask32 & FLAG_WRITE ?
  107. "sett" : "read",
  108. outname);
  109. break;
  110. }
  111. retval = EXIT_FAILURE;
  112. goto end;
  113. }
  114. if (option_mask32 & FLAG_WRITE) {
  115. //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
  116. xwrite_str(fd, value);
  117. close(fd);
  118. if (option_mask32 & FLAG_SHOW_KEYS)
  119. printf("%s = ", outname);
  120. puts(value);
  121. } else {
  122. char c;
  123. value = cptr = xmalloc_read(fd, NULL);
  124. close(fd);
  125. if (value == NULL) {
  126. bb_perror_msg("error reading key '%s'", outname);
  127. goto end;
  128. }
  129. /* dev.cdrom.info and sunrpc.transports, for example,
  130. * are multi-line. Try "sysctl sunrpc.transports"
  131. */
  132. while ((c = *cptr) != '\0') {
  133. if (option_mask32 & FLAG_SHOW_KEYS)
  134. printf("%s = ", outname);
  135. while (1) {
  136. fputc(c, stdout);
  137. cptr++;
  138. if (c == '\n')
  139. break;
  140. c = *cptr;
  141. if (c == '\0')
  142. break;
  143. }
  144. }
  145. free(value);
  146. }
  147. end:
  148. free(outname);
  149. return retval;
  150. }
  151. static int sysctl_act_recursive(const char *path)
  152. {
  153. DIR *dirp;
  154. struct stat buf;
  155. struct dirent *entry;
  156. char *next;
  157. int retval = 0;
  158. stat(path, &buf);
  159. if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
  160. dirp = opendir(path);
  161. if (dirp == NULL)
  162. return -1;
  163. while ((entry = readdir(dirp)) != NULL) {
  164. next = concat_subpath_file(path, entry->d_name);
  165. if (next == NULL)
  166. continue; /* d_name is "." or ".." */
  167. /* if path was ".", drop "./" prefix: */
  168. retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
  169. next + 2 : next);
  170. free(next);
  171. }
  172. closedir(dirp);
  173. } else {
  174. char *name = xstrdup(path);
  175. retval |= sysctl_act_on_setting(name);
  176. free(name);
  177. }
  178. return retval;
  179. }
  180. /* Set sysctl's from a conf file. Format example:
  181. * # Controls IP packet forwarding
  182. * net.ipv4.ip_forward = 0
  183. */
  184. static int sysctl_handle_preload_file(const char *filename)
  185. {
  186. char *token[2];
  187. parser_t *parser;
  188. parser = config_open(filename);
  189. /* Must do it _after_ config_open(): */
  190. xchdir("/proc/sys");
  191. /* xchroot(".") - if you are paranoid */
  192. //TODO: ';' is comment char too
  193. //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
  194. // (but _whitespace_ from ends should be trimmed first (and we do it right))
  195. //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
  196. while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
  197. char *tp;
  198. sysctl_dots_to_slashes(token[0]);
  199. tp = xasprintf("%s=%s", token[0], token[1]);
  200. sysctl_act_recursive(tp);
  201. free(tp);
  202. }
  203. if (ENABLE_FEATURE_CLEAN_UP)
  204. config_close(parser);
  205. return 0;
  206. }
  207. int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  208. int sysctl_main(int argc UNUSED_PARAM, char **argv)
  209. {
  210. int retval;
  211. int opt;
  212. opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
  213. argv += optind;
  214. opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
  215. option_mask32 = opt;
  216. if (opt & FLAG_PRELOAD_FILE) {
  217. option_mask32 |= FLAG_WRITE;
  218. /* xchdir("/proc/sys") is inside */
  219. return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
  220. }
  221. xchdir("/proc/sys");
  222. /* xchroot(".") - if you are paranoid */
  223. if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
  224. return sysctl_act_recursive(".");
  225. }
  226. retval = 0;
  227. while (*argv) {
  228. sysctl_dots_to_slashes(*argv);
  229. retval |= sysctl_act_recursive(*argv);
  230. argv++;
  231. }
  232. return retval;
  233. }