procfile.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. // 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. // 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. // 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. // debug(D_PROCFILE, PF_PREFIX ": resetting words");
  57. fw->len = 0;
  58. }
  59. static inline void procfile_words_free(pfwords *fw) {
  60. // 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. // 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. // 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. // 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. // debug(D_PROCFILE, PF_PREFIX ": resetting lines");
  93. fl->len = 0;
  94. }
  95. static inline void procfile_lines_free(pflines *fl) {
  96. // 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. 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. // 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. // 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. // 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. 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. 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)) error(PF_PREFIX ": Cannot read from file '%s' on fd %d", procfile_filename(ff), ff->fd);
  236. procfile_close(ff);
  237. return NULL;
  238. }
  239. ff->len += r;
  240. }
  241. // debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
  242. if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
  243. if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) collector_error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
  244. else if(unlikely(ff->flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG)) error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
  245. procfile_close(ff);
  246. return NULL;
  247. }
  248. procfile_lines_reset(ff->lines);
  249. procfile_words_reset(ff->words);
  250. procfile_parser(ff);
  251. if(unlikely(procfile_adaptive_initial_allocation)) {
  252. if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
  253. if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
  254. if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
  255. }
  256. // debug(D_PROCFILE, "File '%s' updated.", ff->filename);
  257. return ff;
  258. }
  259. static PF_CHAR_TYPE procfile_default_separators[256];
  260. __attribute__((constructor)) void procfile_initialize_default_separators(void) {
  261. int i = 256;
  262. while(i--) {
  263. if(unlikely(i == '\n' || i == '\r'))
  264. procfile_default_separators[i] = PF_CHAR_IS_NEWLINE;
  265. else if(unlikely(isspace(i) || !isprint(i)))
  266. procfile_default_separators[i] = PF_CHAR_IS_SEPARATOR;
  267. else
  268. procfile_default_separators[i] = PF_CHAR_IS_WORD;
  269. }
  270. }
  271. NOINLINE
  272. static void procfile_set_separators(procfile *ff, const char *separators) {
  273. // set the separators
  274. if(unlikely(!separators))
  275. separators = " \t=|";
  276. // copy the default
  277. memcpy(ff->separators, procfile_default_separators, 256 * sizeof(PF_CHAR_TYPE));
  278. PF_CHAR_TYPE *ffs = ff->separators;
  279. const char *s = separators;
  280. while(*s)
  281. ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
  282. }
  283. void procfile_set_quotes(procfile *ff, const char *quotes) {
  284. PF_CHAR_TYPE *ffs = ff->separators;
  285. // remove all quotes
  286. int i = 256;
  287. while(i--)
  288. if(unlikely(ffs[i] == PF_CHAR_IS_QUOTE))
  289. ffs[i] = PF_CHAR_IS_WORD;
  290. // if nothing given, return
  291. if(unlikely(!quotes || !*quotes))
  292. return;
  293. // set the quotes
  294. const char *s = quotes;
  295. while(*s)
  296. ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
  297. }
  298. void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
  299. PF_CHAR_TYPE *ffs = ff->separators;
  300. // remove all open/close
  301. int i = 256;
  302. while(i--)
  303. if(unlikely(ffs[i] == PF_CHAR_IS_OPEN || ffs[i] == PF_CHAR_IS_CLOSE))
  304. ffs[i] = PF_CHAR_IS_WORD;
  305. // if nothing given, return
  306. if(unlikely(!open || !*open || !close || !*close))
  307. return;
  308. // set the openings
  309. const char *s = open;
  310. while(*s)
  311. ffs[(int)*s++] = PF_CHAR_IS_OPEN;
  312. // set the closings
  313. s = close;
  314. while(*s)
  315. ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
  316. }
  317. procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
  318. debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
  319. int fd = open(filename, procfile_open_flags, 0666);
  320. if(unlikely(fd == -1)) {
  321. if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) collector_error(PF_PREFIX ": Cannot open file '%s'", filename);
  322. else if(unlikely(flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG)) error(PF_PREFIX ": Cannot open file '%s'", filename);
  323. return NULL;
  324. }
  325. // info("PROCFILE: opened '%s' on fd %d", filename, fd);
  326. size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
  327. procfile *ff = mallocz(sizeof(procfile) + size);
  328. //strncpyz(ff->filename, filename, FILENAME_MAX);
  329. ff->filename = NULL;
  330. ff->fd = fd;
  331. ff->size = size;
  332. ff->len = 0;
  333. ff->flags = flags;
  334. ff->lines = procfile_lines_create();
  335. ff->words = procfile_words_create();
  336. procfile_set_separators(ff, separators);
  337. debug(D_PROCFILE, "File '%s' opened.", filename);
  338. return ff;
  339. }
  340. procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
  341. if(unlikely(!ff)) return procfile_open(filename, separators, flags);
  342. if(likely(ff->fd != -1)) {
  343. // info("PROCFILE: closing fd %d", ff->fd);
  344. close(ff->fd);
  345. }
  346. ff->fd = open(filename, procfile_open_flags, 0666);
  347. if(unlikely(ff->fd == -1)) {
  348. procfile_close(ff);
  349. return NULL;
  350. }
  351. // info("PROCFILE: opened '%s' on fd %d", filename, ff->fd);
  352. //strncpyz(ff->filename, filename, FILENAME_MAX);
  353. freez(ff->filename);
  354. ff->filename = NULL;
  355. ff->flags = flags;
  356. // do not do the separators again if NULL is given
  357. if(likely(separators)) procfile_set_separators(ff, separators);
  358. return ff;
  359. }
  360. // ----------------------------------------------------------------------------
  361. // example parsing of procfile data
  362. void procfile_print(procfile *ff) {
  363. size_t lines = procfile_lines(ff), l;
  364. char *s;
  365. (void)s;
  366. debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", procfile_filename(ff), ff->lines->len, ff->words->len);
  367. for(l = 0; likely(l < lines) ;l++) {
  368. size_t words = procfile_linewords(ff, l);
  369. debug(D_PROCFILE, " line %zu starts at word %zu and has %zu words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
  370. size_t w;
  371. for(w = 0; likely(w < words) ;w++) {
  372. s = procfile_lineword(ff, l, w);
  373. debug(D_PROCFILE, " [%zu.%zu] '%s'", l, w, s);
  374. }
  375. }
  376. }