procfile.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. #define PF_PREFIX "PROCFILE"
  4. #define PFWORDS_INCREASE_STEP 2000
  5. #define PFLINES_INCREASE_STEP 200
  6. #define PROCFILE_INCREMENT_BUFFER 4096
  7. int procfile_open_flags = O_RDONLY;
  8. int procfile_adaptive_initial_allocation = 0;
  9. // if adaptive allocation is set, these store the
  10. // max values we have seen so far
  11. size_t procfile_max_lines = PFLINES_INCREASE_STEP;
  12. size_t procfile_max_words = PFWORDS_INCREASE_STEP;
  13. size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
  14. // ----------------------------------------------------------------------------
  15. char *procfile_filename(procfile *ff) {
  16. if(ff->filename)
  17. return ff->filename;
  18. char filename[FILENAME_MAX + 1];
  19. char buffer[FILENAME_MAX + 1];
  20. snprintfz(buffer, FILENAME_MAX, "/proc/self/fd/%d", ff->fd);
  21. ssize_t l = readlink(buffer, filename, FILENAME_MAX);
  22. if(unlikely(l == -1))
  23. snprintfz(filename, FILENAME_MAX, "unknown filename for fd %d", ff->fd);
  24. else
  25. filename[l] = '\0';
  26. ff->filename = strdupz(filename);
  27. // on non-linux systems, something like this will be needed
  28. // fcntl(ff->fd, F_GETPATH, ff->filename)
  29. return ff->filename;
  30. }
  31. // ----------------------------------------------------------------------------
  32. // An array of words
  33. static inline void procfile_words_add(procfile *ff, char *str) {
  34. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
  35. pfwords *fw = ff->words;
  36. if(unlikely(fw->len == fw->size)) {
  37. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": expanding words");
  38. size_t minimum = PFWORDS_INCREASE_STEP;
  39. size_t optimal = fw->size / 2;
  40. size_t wanted = (optimal > minimum)?optimal:minimum;
  41. ff->words = fw = reallocz(fw, sizeof(pfwords) + (fw->size + wanted) * sizeof(char *));
  42. fw->size += wanted;
  43. }
  44. fw->words[fw->len++] = str;
  45. }
  46. NEVERNULL
  47. static inline pfwords *procfile_words_create(void) {
  48. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": initializing words");
  49. size_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
  50. pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
  51. new->len = 0;
  52. new->size = size;
  53. return new;
  54. }
  55. static inline void procfile_words_reset(pfwords *fw) {
  56. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": resetting words");
  57. fw->len = 0;
  58. }
  59. static inline void procfile_words_free(pfwords *fw) {
  60. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": freeing words");
  61. freez(fw);
  62. }
  63. // ----------------------------------------------------------------------------
  64. // An array of lines
  65. NEVERNULL
  66. static inline size_t *procfile_lines_add(procfile *ff) {
  67. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
  68. pflines *fl = ff->lines;
  69. if(unlikely(fl->len == fl->size)) {
  70. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": expanding lines");
  71. size_t minimum = PFLINES_INCREASE_STEP;
  72. size_t optimal = fl->size / 2;
  73. size_t wanted = (optimal > minimum)?optimal:minimum;
  74. ff->lines = fl = reallocz(fl, sizeof(pflines) + (fl->size + wanted) * sizeof(ffline));
  75. fl->size += wanted;
  76. }
  77. ffline *ffl = &fl->lines[fl->len++];
  78. ffl->words = 0;
  79. ffl->first = ff->words->len;
  80. return &ffl->words;
  81. }
  82. NEVERNULL
  83. static inline pflines *procfile_lines_create(void) {
  84. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": initializing lines");
  85. size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
  86. pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
  87. new->len = 0;
  88. new->size = size;
  89. return new;
  90. }
  91. static inline void procfile_lines_reset(pflines *fl) {
  92. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": resetting lines");
  93. fl->len = 0;
  94. }
  95. static inline void procfile_lines_free(pflines *fl) {
  96. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": freeing lines");
  97. freez(fl);
  98. }
  99. // ----------------------------------------------------------------------------
  100. // The procfile
  101. void procfile_close(procfile *ff) {
  102. if(unlikely(!ff)) return;
  103. netdata_log_debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", procfile_filename(ff));
  104. freez(ff->filename);
  105. procfile_lines_free(ff->lines);
  106. procfile_words_free(ff->words);
  107. if(likely(ff->fd != -1)) close(ff->fd);
  108. freez(ff);
  109. }
  110. NOINLINE
  111. static void procfile_parser(procfile *ff) {
  112. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
  113. char *s = ff->data // our current position
  114. , *e = &ff->data[ff->len] // the terminating null
  115. , *t = ff->data; // the first character of a word (or quoted / parenthesized string)
  116. // the look up array to find our type of character
  117. PF_CHAR_TYPE *separators = ff->separators;
  118. char quote = 0; // the quote character - only when in quoted string
  119. size_t opened = 0; // counts the number of open parenthesis
  120. size_t *line_words = procfile_lines_add(ff);
  121. while(s < e) {
  122. PF_CHAR_TYPE ct = separators[(unsigned char)(*s)];
  123. // this is faster than a switch()
  124. // read more here: http://lazarenko.me/switch/
  125. if(likely(ct == PF_CHAR_IS_WORD)) {
  126. s++;
  127. }
  128. else if(likely(ct == PF_CHAR_IS_SEPARATOR)) {
  129. if(!quote && !opened) {
  130. if (s != t) {
  131. // separator, but we have word before it
  132. *s = '\0';
  133. procfile_words_add(ff, t);
  134. (*line_words)++;
  135. t = ++s;
  136. }
  137. else {
  138. // separator at the beginning
  139. // skip it
  140. t = ++s;
  141. }
  142. }
  143. else {
  144. // we are inside a quote or parenthesized string
  145. s++;
  146. }
  147. }
  148. else if(likely(ct == PF_CHAR_IS_NEWLINE)) {
  149. // end of line
  150. *s = '\0';
  151. procfile_words_add(ff, t);
  152. (*line_words)++;
  153. t = ++s;
  154. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
  155. line_words = procfile_lines_add(ff);
  156. }
  157. else if(likely(ct == PF_CHAR_IS_QUOTE)) {
  158. if(unlikely(!quote && s == t)) {
  159. // quote opened at the beginning
  160. quote = *s;
  161. t = ++s;
  162. }
  163. else if(unlikely(quote && quote == *s)) {
  164. // quote closed
  165. quote = 0;
  166. *s = '\0';
  167. procfile_words_add(ff, t);
  168. (*line_words)++;
  169. t = ++s;
  170. }
  171. else
  172. s++;
  173. }
  174. else if(likely(ct == PF_CHAR_IS_OPEN)) {
  175. if(s == t) {
  176. opened++;
  177. t = ++s;
  178. }
  179. else if(opened) {
  180. opened++;
  181. s++;
  182. }
  183. else
  184. s++;
  185. }
  186. else if(likely(ct == PF_CHAR_IS_CLOSE)) {
  187. if(opened) {
  188. opened--;
  189. if(!opened) {
  190. *s = '\0';
  191. procfile_words_add(ff, t);
  192. (*line_words)++;
  193. t = ++s;
  194. }
  195. else
  196. s++;
  197. }
  198. else
  199. s++;
  200. }
  201. else
  202. fatal("Internal Error: procfile_readall() does not handle all the cases.");
  203. }
  204. if(likely(s > t && t < e)) {
  205. // the last word
  206. if(unlikely(ff->len >= ff->size)) {
  207. // we are going to loose the last byte
  208. s = &ff->data[ff->size - 1];
  209. }
  210. *s = '\0';
  211. procfile_words_add(ff, t);
  212. (*line_words)++;
  213. // t = ++s;
  214. }
  215. }
  216. procfile *procfile_readall(procfile *ff) {
  217. // netdata_log_debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
  218. ff->len = 0; // zero the used size
  219. ssize_t r = 1; // read at least once
  220. while(r > 0) {
  221. ssize_t s = ff->len;
  222. ssize_t x = ff->size - s;
  223. if(unlikely(!x)) {
  224. size_t minimum = PROCFILE_INCREMENT_BUFFER;
  225. size_t optimal = ff->size / 2;
  226. size_t wanted = (optimal > minimum)?optimal:minimum;
  227. netdata_log_debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s' by %zu bytes.", procfile_filename(ff), wanted);
  228. ff = reallocz(ff, sizeof(procfile) + ff->size + wanted);
  229. ff->size += wanted;
  230. }
  231. netdata_log_debug(D_PROCFILE, "Reading file '%s', from position %zd with length %zd", procfile_filename(ff), s, (ssize_t)(ff->size - s));
  232. r = read(ff->fd, &ff->data[s], ff->size - s);
  233. if(unlikely(r == -1)) {
  234. if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) collector_error(PF_PREFIX ": Cannot read from file '%s' on fd %d", procfile_filename(ff), ff->fd);
  235. else if(unlikely(ff->flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG))
  236. netdata_log_error(PF_PREFIX ": Cannot read from file '%s' on fd %d", procfile_filename(ff), ff->fd);
  237. procfile_close(ff);
  238. return NULL;
  239. }
  240. ff->len += r;
  241. }
  242. // netdata_log_debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
  243. if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
  244. if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) collector_error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
  245. else if(unlikely(ff->flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG))
  246. netdata_log_error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
  247. procfile_close(ff);
  248. return NULL;
  249. }
  250. procfile_lines_reset(ff->lines);
  251. procfile_words_reset(ff->words);
  252. procfile_parser(ff);
  253. if(unlikely(procfile_adaptive_initial_allocation)) {
  254. if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
  255. if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
  256. if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
  257. }
  258. // netdata_log_debug(D_PROCFILE, "File '%s' updated.", ff->filename);
  259. return ff;
  260. }
  261. static PF_CHAR_TYPE procfile_default_separators[256];
  262. __attribute__((constructor)) void procfile_initialize_default_separators(void) {
  263. int i = 256;
  264. while(i--) {
  265. if(unlikely(i == '\n' || i == '\r'))
  266. procfile_default_separators[i] = PF_CHAR_IS_NEWLINE;
  267. else if(unlikely(isspace(i) || !isprint(i)))
  268. procfile_default_separators[i] = PF_CHAR_IS_SEPARATOR;
  269. else
  270. procfile_default_separators[i] = PF_CHAR_IS_WORD;
  271. }
  272. }
  273. NOINLINE
  274. static void procfile_set_separators(procfile *ff, const char *separators) {
  275. // set the separators
  276. if(unlikely(!separators))
  277. separators = " \t=|";
  278. // copy the default
  279. memcpy(ff->separators, procfile_default_separators, 256 * sizeof(PF_CHAR_TYPE));
  280. PF_CHAR_TYPE *ffs = ff->separators;
  281. const char *s = separators;
  282. while(*s)
  283. ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
  284. }
  285. void procfile_set_quotes(procfile *ff, const char *quotes) {
  286. PF_CHAR_TYPE *ffs = ff->separators;
  287. // remove all quotes
  288. int i = 256;
  289. while(i--)
  290. if(unlikely(ffs[i] == PF_CHAR_IS_QUOTE))
  291. ffs[i] = PF_CHAR_IS_WORD;
  292. // if nothing given, return
  293. if(unlikely(!quotes || !*quotes))
  294. return;
  295. // set the quotes
  296. const char *s = quotes;
  297. while(*s)
  298. ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
  299. }
  300. void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
  301. PF_CHAR_TYPE *ffs = ff->separators;
  302. // remove all open/close
  303. int i = 256;
  304. while(i--)
  305. if(unlikely(ffs[i] == PF_CHAR_IS_OPEN || ffs[i] == PF_CHAR_IS_CLOSE))
  306. ffs[i] = PF_CHAR_IS_WORD;
  307. // if nothing given, return
  308. if(unlikely(!open || !*open || !close || !*close))
  309. return;
  310. // set the openings
  311. const char *s = open;
  312. while(*s)
  313. ffs[(int)*s++] = PF_CHAR_IS_OPEN;
  314. // set the closings
  315. s = close;
  316. while(*s)
  317. ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
  318. }
  319. procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
  320. netdata_log_debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
  321. int fd = open(filename, procfile_open_flags, 0666);
  322. if(unlikely(fd == -1)) {
  323. if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) collector_error(PF_PREFIX ": Cannot open file '%s'", filename);
  324. else if(unlikely(flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG))
  325. netdata_log_error(PF_PREFIX ": Cannot open file '%s'", filename);
  326. return NULL;
  327. }
  328. // netdata_log_info("PROCFILE: opened '%s' on fd %d", filename, fd);
  329. size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
  330. procfile *ff = mallocz(sizeof(procfile) + size);
  331. //strncpyz(ff->filename, filename, FILENAME_MAX);
  332. ff->filename = NULL;
  333. ff->fd = fd;
  334. ff->size = size;
  335. ff->len = 0;
  336. ff->flags = flags;
  337. ff->lines = procfile_lines_create();
  338. ff->words = procfile_words_create();
  339. procfile_set_separators(ff, separators);
  340. netdata_log_debug(D_PROCFILE, "File '%s' opened.", filename);
  341. return ff;
  342. }
  343. procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
  344. if(unlikely(!ff)) return procfile_open(filename, separators, flags);
  345. if(likely(ff->fd != -1)) {
  346. // netdata_log_info("PROCFILE: closing fd %d", ff->fd);
  347. close(ff->fd);
  348. }
  349. ff->fd = open(filename, procfile_open_flags, 0666);
  350. if(unlikely(ff->fd == -1)) {
  351. procfile_close(ff);
  352. return NULL;
  353. }
  354. // netdata_log_info("PROCFILE: opened '%s' on fd %d", filename, ff->fd);
  355. //strncpyz(ff->filename, filename, FILENAME_MAX);
  356. freez(ff->filename);
  357. ff->filename = NULL;
  358. ff->flags = flags;
  359. // do not do the separators again if NULL is given
  360. if(likely(separators)) procfile_set_separators(ff, separators);
  361. return ff;
  362. }
  363. // ----------------------------------------------------------------------------
  364. // example parsing of procfile data
  365. void procfile_print(procfile *ff) {
  366. size_t lines = procfile_lines(ff), l;
  367. char *s;
  368. (void)s;
  369. netdata_log_debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", procfile_filename(ff), ff->lines->len, ff->words->len);
  370. for(l = 0; likely(l < lines) ;l++) {
  371. size_t words = procfile_linewords(ff, l);
  372. netdata_log_debug(D_PROCFILE, " line %zu starts at word %zu and has %zu words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
  373. size_t w;
  374. for(w = 0; likely(w < words) ;w++) {
  375. s = procfile_lineword(ff, l, w);
  376. netdata_log_debug(D_PROCFILE, " [%zu.%zu] '%s'", l, w, s);
  377. }
  378. }
  379. }