procfile.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_PROCFILE_H
  3. #define NETDATA_PROCFILE_H 1
  4. #include "../libnetdata.h"
  5. // ----------------------------------------------------------------------------
  6. // An array of words
  7. typedef struct {
  8. size_t len; // used entries
  9. size_t size; // capacity
  10. char *words[]; // array of pointers
  11. } pfwords;
  12. // ----------------------------------------------------------------------------
  13. // An array of lines
  14. typedef struct {
  15. size_t words; // how many words this line has
  16. size_t first; // the id of the first word of this line
  17. // in the words array
  18. } ffline;
  19. typedef struct {
  20. size_t len; // used entries
  21. size_t size; // capacity
  22. ffline lines[]; // array of lines
  23. } pflines;
  24. // ----------------------------------------------------------------------------
  25. // The procfile
  26. #define PROCFILE_FLAG_DEFAULT 0x00000000 // To store inside `collector.log`
  27. #define PROCFILE_FLAG_NO_ERROR_ON_FILE_IO 0x00000001 // Do not store nothing
  28. #define PROCFILE_FLAG_ERROR_ON_ERROR_LOG 0x00000002 // Store inside `error.log`
  29. typedef enum __attribute__ ((__packed__)) procfile_separator {
  30. PF_CHAR_IS_SEPARATOR,
  31. PF_CHAR_IS_NEWLINE,
  32. PF_CHAR_IS_WORD,
  33. PF_CHAR_IS_QUOTE,
  34. PF_CHAR_IS_OPEN,
  35. PF_CHAR_IS_CLOSE
  36. } PF_CHAR_TYPE;
  37. typedef struct procfile {
  38. char *filename; // not populated until procfile_filename() is called
  39. uint32_t flags;
  40. int fd; // the file descriptor
  41. size_t len; // the bytes we have placed into data
  42. size_t size; // the bytes we have allocated for data
  43. pflines *lines;
  44. pfwords *words;
  45. PF_CHAR_TYPE separators[256];
  46. char data[]; // allocated buffer to keep file contents
  47. } procfile;
  48. // close the proc file and free all related memory
  49. void procfile_close(procfile *ff);
  50. // (re)read and parse the proc file
  51. procfile *procfile_readall(procfile *ff);
  52. // open a /proc or /sys file
  53. procfile *procfile_open(const char *filename, const char *separators, uint32_t flags);
  54. // re-open a file
  55. // if separators == NULL, the last separators are used
  56. procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags);
  57. // example walk-through a procfile parsed file
  58. void procfile_print(procfile *ff);
  59. void procfile_set_quotes(procfile *ff, const char *quotes);
  60. void procfile_set_open_close(procfile *ff, const char *open, const char *close);
  61. char *procfile_filename(procfile *ff);
  62. // ----------------------------------------------------------------------------
  63. // set to the O_XXXX flags, to have procfile_open and procfile_reopen use them when opening proc files
  64. extern int procfile_open_flags;
  65. // set this to 1, to have procfile adapt its initial buffer allocation to the max allocation used so far
  66. extern int procfile_adaptive_initial_allocation;
  67. // return the number of lines present
  68. #define procfile_lines(ff) ((ff)->lines->len)
  69. // return the number of words of the Nth line
  70. #define procfile_linewords(ff, line) (((line) < procfile_lines(ff)) ? (ff)->lines->lines[(line)].words : 0)
  71. // return the Nth word of the file, or empty string
  72. #define procfile_word(ff, word) (((word) < (ff)->words->len) ? (ff)->words->words[(word)] : "")
  73. // return the first word of the Nth line, or empty string
  74. #define procfile_line(ff, line) (((line) < procfile_lines(ff)) ? procfile_word((ff), (ff)->lines->lines[(line)].first) : "")
  75. // return the Nth word of the current line
  76. #define procfile_lineword(ff, line, word) (((line) < procfile_lines(ff) && (word) < procfile_linewords((ff), (line))) ? procfile_word((ff), (ff)->lines->lines[(line)].first + (word)) : "")
  77. // Open file without logging file IO error if any
  78. #define procfile_open_no_log(filename, separators, flags) procfile_open(filename, separators, flags | PROCFILE_FLAG_NO_ERROR_ON_FILE_IO)
  79. #endif /* NETDATA_PROCFILE_H */