debugfs_plugin.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "debugfs_plugin.h"
  3. #include "libnetdata/required_dummies.h"
  4. static char *user_config_dir = CONFIG_DIR;
  5. static char *stock_config_dir = LIBCONFIG_DIR;
  6. static int update_every = 1;
  7. static struct debugfs_module {
  8. const char *name;
  9. int enabled;
  10. int (*func)(int update_every, const char *name);
  11. } debugfs_modules[] = {
  12. // Memory Fragmentation
  13. { .name = "/sys/kernel/debug/extfrag", .enabled = CONFIG_BOOLEAN_YES,
  14. .func = do_debugfs_extfrag},
  15. { .name = "/sys/kernel/debug/zswap", .enabled = CONFIG_BOOLEAN_YES,
  16. .func = do_debugfs_zswap},
  17. // The terminator
  18. { .name = NULL, .enabled = CONFIG_BOOLEAN_NO, .func = NULL}
  19. };
  20. #ifdef HAVE_CAPABILITY
  21. static int debugfs_check_capabilities()
  22. {
  23. cap_t caps = cap_get_proc();
  24. if (!caps) {
  25. error("Cannot get current capabilities.");
  26. return 0;
  27. }
  28. int ret = 1;
  29. cap_flag_value_t cfv = CAP_CLEAR;
  30. if (cap_get_flag(caps, CAP_DAC_READ_SEARCH, CAP_EFFECTIVE, &cfv) == -1) {
  31. error("Cannot find if CAP_DAC_READ_SEARCH is effective.");
  32. ret = 0;
  33. } else {
  34. if (cfv != CAP_SET) {
  35. error("debugfs.plugin should run with CAP_DAC_READ_SEARCH.");
  36. ret = 0;
  37. }
  38. }
  39. cap_free(caps);
  40. return ret;
  41. }
  42. #else
  43. static int debugfs_check_capabilities()
  44. {
  45. return 0;
  46. }
  47. #endif
  48. // TODO: This is a function used by 3 different collector, we should do it global (next PR)
  49. static int debugfs_am_i_running_as_root()
  50. {
  51. uid_t uid = getuid(), euid = geteuid();
  52. if (uid == 0 || euid == 0) {
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. void debugfs2lower(char *name)
  58. {
  59. while (*name) {
  60. *name = tolower(*name);
  61. name++;
  62. }
  63. }
  64. // Consiidering our goal to redce binaries, I preferred to copy function, instead to force link with unecessary libs
  65. const char *debugfs_rrdset_type_name(RRDSET_TYPE chart_type) {
  66. switch(chart_type) {
  67. case RRDSET_TYPE_LINE:
  68. default:
  69. return RRDSET_TYPE_LINE_NAME;
  70. case RRDSET_TYPE_AREA:
  71. return RRDSET_TYPE_AREA_NAME;
  72. case RRDSET_TYPE_STACKED:
  73. return RRDSET_TYPE_STACKED_NAME;
  74. }
  75. }
  76. const char *debugfs_rrd_algorithm_name(RRD_ALGORITHM algorithm) {
  77. switch(algorithm) {
  78. case RRD_ALGORITHM_ABSOLUTE:
  79. default:
  80. return RRD_ALGORITHM_ABSOLUTE_NAME;
  81. case RRD_ALGORITHM_INCREMENTAL:
  82. return RRD_ALGORITHM_INCREMENTAL_NAME;
  83. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  84. return RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME;
  85. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  86. return RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME;
  87. }
  88. }
  89. int debugfs_check_sys_permission() {
  90. int ret = 0;
  91. char filename[FILENAME_MAX + 1];
  92. snprintfz(filename, FILENAME_MAX, "%s/sys/kernel/debug/extfrag/extfrag_index", netdata_configured_host_prefix);
  93. procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  94. if(!ff) goto dcsp_cleanup;
  95. ff = procfile_readall(ff);
  96. if(!ff) goto dcsp_cleanup;
  97. ret = 1;
  98. dcsp_cleanup:
  99. if (!ret)
  100. perror("Cannot open /sys/kernel/debug/extfrag/extfrag_index file");
  101. procfile_close(ff);
  102. return ret;
  103. }
  104. static void debugfs_parse_args(int argc, char **argv)
  105. {
  106. int i, freq = 0;
  107. for(i = 1; i < argc; i++) {
  108. if(!freq) {
  109. int n = (int)str2l(argv[i]);
  110. if(n > 0) {
  111. freq = n;
  112. continue;
  113. }
  114. }
  115. if(strcmp("test-permissions", argv[i]) == 0 || strcmp("-t", argv[i]) == 0) {
  116. if(!debugfs_check_sys_permission()) {
  117. exit(2);
  118. }
  119. printf("OK\n");
  120. exit(0);
  121. }
  122. }
  123. if(freq > 0) update_every = freq;
  124. }
  125. int main(int argc, char **argv)
  126. {
  127. // debug_flags = D_PROCFILE;
  128. stderror = stderr;
  129. // set the name for logging
  130. program_name = "debugfs.plugin";
  131. // disable syslog for debugfs.plugin
  132. error_log_syslog = 0;
  133. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  134. if (verify_netdata_host_prefix() == -1)
  135. exit(1);
  136. user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  137. if (user_config_dir == NULL) {
  138. user_config_dir = CONFIG_DIR;
  139. }
  140. stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  141. if (stock_config_dir == NULL) {
  142. // info("NETDATA_CONFIG_DIR is not passed from netdata");
  143. stock_config_dir = LIBCONFIG_DIR;
  144. }
  145. // FIXME: should first check if /sys/kernel/debug is mounted
  146. // FIXME: remove debugfs_check_sys_permission() after https://github.com/netdata/netdata/issues/15048 is fixed
  147. if (!debugfs_check_capabilities() && !debugfs_am_i_running_as_root() && !debugfs_check_sys_permission()) {
  148. uid_t uid = getuid(), euid = geteuid();
  149. #ifdef HAVE_CAPABILITY
  150. error(
  151. "debugfs.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
  152. "Without these, debugfs.plugin cannot access /sys/kernel/debug. "
  153. "To enable capabilities run: sudo setcap cap_dac_read_search,cap_sys_ptrace+ep %s; "
  154. "To enable setuid to root run: sudo chown root:netdata %s; sudo chmod 4750 %s; ",
  155. uid,
  156. euid,
  157. argv[0],
  158. argv[0],
  159. argv[0]);
  160. #else
  161. error(
  162. "debugfs.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
  163. "Without these, debugfs.plugin cannot access /sys/kernel/debug."
  164. "Your system does not support capabilities. "
  165. "To enable setuid to root run: sudo chown root:netdata %s; sudo chmod 4750 %s; ",
  166. uid,
  167. euid,
  168. argv[0],
  169. argv[0]);
  170. #endif
  171. exit(1);
  172. }
  173. // if (!debugfs_check_sys_permission()) {
  174. // exit(2);
  175. // }
  176. debugfs_parse_args(argc, argv);
  177. size_t iteration;
  178. usec_t step = update_every * USEC_PER_SEC;
  179. heartbeat_t hb;
  180. heartbeat_init(&hb);
  181. for (iteration = 0; iteration < 86400; iteration++) {
  182. heartbeat_next(&hb, step);
  183. int enabled = 0;
  184. for (int i = 0; debugfs_modules[i].name; i++) {
  185. struct debugfs_module *pm = &debugfs_modules[i];
  186. if (unlikely(!pm->enabled))
  187. continue;
  188. pm->enabled = !pm->func(update_every, pm->name);
  189. if (likely(pm->enabled))
  190. enabled++;
  191. }
  192. if (!enabled) {
  193. info("all modules are disabled, exiting...");
  194. return 1;
  195. }
  196. }
  197. return 0;
  198. }