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