sysctl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* sysctl.c - A utility to read and manipulate the sysctl parameters.
  2. *
  3. * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
  4. * Copyright 2014 Kyungwan Han <asura321@gmail.com>
  5. *
  6. * No Standard
  7. USE_SYSCTL(NEWTOY(sysctl, "^neNqwpaA[!ap][!aq][!aw][+aA]", TOYFLAG_SBIN))
  8. config SYSCTL
  9. bool "sysctl"
  10. default y
  11. help
  12. usage: sysctl [-aAeNnqw] [-p [FILE] | KEY[=VALUE]...]
  13. Read/write system control data (under /proc/sys).
  14. -a,A Show all values
  15. -e Don't warn about unknown keys
  16. -N Don't print key values
  17. -n Don't print key names
  18. -p Read values from FILE (default /etc/sysctl.conf)
  19. -q Don't show value after write
  20. -w Only write values (object to reading)
  21. */
  22. #define FOR_sysctl
  23. #include "toys.h"
  24. // Null terminate at =, return value
  25. static char *split_key(char *key)
  26. {
  27. char *value = strchr(key, '=');
  28. if (value) *(value++)=0;
  29. return value;
  30. }
  31. static void replace_char(char *str, char old, char new)
  32. {
  33. for (; *str; str++) if (*str == old) *str = new;
  34. }
  35. static void key_error(char *key)
  36. {
  37. if (errno == ENOENT) {
  38. if (!(toys.optflags & FLAG_e)) error_msg("unknown key '%s'", key);
  39. } else perror_msg("key '%s'", key);
  40. }
  41. static int write_key(char *path, char *key, char *value)
  42. {
  43. int fd = open(path, O_WRONLY);
  44. if (fd < 0) {
  45. key_error(key);
  46. return 0;
  47. }
  48. xwrite(fd, value, strlen(value));
  49. xclose(fd);
  50. return 1;
  51. }
  52. // Display all keys under a path
  53. static int do_show_keys(struct dirtree *dt)
  54. {
  55. char *path, *data, *key;
  56. if (!dirtree_notdotdot(dt)) return 0; // Skip . and ..
  57. if (S_ISDIR(dt->st.st_mode)) return DIRTREE_RECURSE;
  58. path = dirtree_path(dt, 0);
  59. data = readfile(path, 0, 0);
  60. replace_char(key = path + 10, '/', '.'); // skip "/proc/sys/"
  61. if (!data) key_error(key);
  62. else {
  63. // Print the parts that aren't switched off by flags.
  64. if (!(toys.optflags & FLAG_n)) xprintf("%s", key);
  65. if (!(toys.optflags & (FLAG_N|FLAG_n))) xprintf(" = ");
  66. for (key = data+strlen(data); key > data && isspace(*--key); *key = 0);
  67. if (!(toys.optflags & FLAG_N)) xprintf("%s", data);
  68. if ((toys.optflags & (FLAG_N|FLAG_n)) != (FLAG_N|FLAG_n)) xputc('\n');
  69. }
  70. free(data);
  71. free(path);
  72. return 0;
  73. }
  74. // Read/write entries under a key. Accepts "key=value" in key if !value
  75. static void process_key(char *key, char *value)
  76. {
  77. char *path;
  78. if (!value) value = split_key(key);
  79. if ((toys.optflags & FLAG_w) && !value) {
  80. error_msg("'%s' not key=value", key);
  81. return;
  82. }
  83. path = xmprintf("/proc/sys/%s", key);
  84. replace_char(path, '.', '/');
  85. // Note: failure to assign to a non-leaf node suppresses the display.
  86. if (!(value && (!write_key(path, key, value) || (toys.optflags & FLAG_q)))) {
  87. if (!access(path, R_OK)) dirtree_read(path, do_show_keys);
  88. else key_error(key);
  89. }
  90. free(path);
  91. }
  92. void sysctl_main()
  93. {
  94. char **args = 0;
  95. // Display all keys
  96. if (toys.optflags & FLAG_a) dirtree_read("/proc/sys", do_show_keys);
  97. // read file
  98. else if (toys.optflags & FLAG_p) {
  99. FILE *fp = xfopen(*toys.optargs ? *toys.optargs : "/etc/sysctl.conf", "r");
  100. size_t len;
  101. for (;;) {
  102. char *line = 0, *key, *val;
  103. if (-1 == (len = getline(&line, &len, fp))) break;
  104. key = line;
  105. while (isspace(*key)) key++;
  106. if (*key == '#' || *key == ';' || !*key) continue;
  107. while (len && isspace(line[len-1])) line[--len] = 0;
  108. if (!(val = split_key(line))) {
  109. error_msg("'%s' not key=value", line);
  110. continue;
  111. }
  112. // Trim whitespace around =
  113. len = (val-line)-1;
  114. while (len && isspace(line[len-1])) line[--len] = 0;
  115. while (isspace(*val)) val++;;
  116. process_key(key, val);
  117. free(line);
  118. }
  119. fclose(fp);
  120. // Loop through arguments, displaying or assigning as appropriate
  121. } else {
  122. if (!*toys.optargs) help_exit("Needs 1 arg");
  123. for (args = toys.optargs; *args; args++) process_key(*args, 0);
  124. }
  125. }