procfile.c 14 KB

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