parser.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "parser.h"
  3. #include "collectors/plugins.d/pluginsd_parser.h"
  4. static inline int find_first_keyword(const char *src, char *dst, int dst_size, int (*custom_isspace)(char)) {
  5. const char *s = src, *keyword_start;
  6. while (unlikely(custom_isspace(*s))) s++;
  7. keyword_start = s;
  8. while (likely(*s && !custom_isspace(*s)) && dst_size > 1) {
  9. *dst++ = *s++;
  10. dst_size--;
  11. }
  12. *dst = '\0';
  13. return dst_size == 0 ? 0 : (int) (s - keyword_start);
  14. }
  15. /*
  16. * Initialize a parser
  17. * user : as defined by the user, will be shared across calls
  18. * input : main input stream (auto detect stream -- file, socket, pipe)
  19. * buffer : This is the buffer to be used (if null a buffer of size will be allocated)
  20. * size : buffer size either passed or will be allocated
  21. * If the buffer is auto allocated, it will auto freed when the parser is destroyed
  22. *
  23. *
  24. */
  25. PARSER *parser_init(void *user, FILE *fp_input, FILE *fp_output, int fd,
  26. PARSER_INPUT_TYPE flags, void *ssl __maybe_unused)
  27. {
  28. PARSER *parser;
  29. parser = callocz(1, sizeof(*parser));
  30. parser->user = user;
  31. parser->fd = fd;
  32. parser->fp_input = fp_input;
  33. parser->fp_output = fp_output;
  34. #ifdef ENABLE_HTTPS
  35. parser->ssl_output = ssl;
  36. #endif
  37. parser->flags = flags;
  38. parser->worker_job_next_id = WORKER_PARSER_FIRST_JOB;
  39. return parser;
  40. }
  41. static inline PARSER_KEYWORD *parser_find_keyword(PARSER *parser, const char *command) {
  42. uint32_t hash = parser_hash_function(command);
  43. uint32_t slot = hash % PARSER_KEYWORDS_HASHTABLE_SIZE;
  44. PARSER_KEYWORD *t = parser->keywords.hashtable[slot];
  45. if(likely(t && strcmp(t->keyword, command) == 0))
  46. return t;
  47. return NULL;
  48. }
  49. /*
  50. * Add a keyword and the corresponding function that will be called
  51. * Multiple functions may be added
  52. * Input : keyword
  53. * : callback function
  54. * : flags
  55. * Output: > 0 registered function number
  56. * : 0 Error
  57. */
  58. void parser_add_keyword(PARSER *parser, char *keyword, keyword_function func) {
  59. if(unlikely(!parser || !keyword || !*keyword || !func))
  60. fatal("PARSER: invalid parameters");
  61. PARSER_KEYWORD *t = callocz(1, sizeof(*t));
  62. t->worker_job_id = parser->worker_job_next_id++;
  63. t->keyword = strdupz(keyword);
  64. t->func = func;
  65. uint32_t hash = parser_hash_function(keyword);
  66. uint32_t slot = hash % PARSER_KEYWORDS_HASHTABLE_SIZE;
  67. if(unlikely(parser->keywords.hashtable[slot]))
  68. fatal("PARSER: hashtable collision between keyword '%s' and '%s' on slot %u. "
  69. "Change the hashtable size and / or the hashing function. "
  70. "Run the unit test to find the optimal values.",
  71. parser->keywords.hashtable[slot]->keyword,
  72. t->keyword,
  73. slot
  74. );
  75. parser->keywords.hashtable[slot] = t;
  76. worker_register_job_name(t->worker_job_id, t->keyword);
  77. }
  78. /*
  79. * Cleanup a previously allocated parser
  80. */
  81. void parser_destroy(PARSER *parser)
  82. {
  83. if (unlikely(!parser))
  84. return;
  85. dictionary_destroy(parser->inflight.functions);
  86. // Remove keywords
  87. for(size_t i = 0 ; i < PARSER_KEYWORDS_HASHTABLE_SIZE; i++) {
  88. PARSER_KEYWORD *t = parser->keywords.hashtable[i];
  89. if (t) {
  90. freez(t->keyword);
  91. freez(t);
  92. }
  93. }
  94. freez(parser);
  95. }
  96. /*
  97. * Fetch the next line to process
  98. *
  99. */
  100. int parser_next(PARSER *parser, char *buffer, size_t buffer_size)
  101. {
  102. char *tmp = fgets(buffer, (int)buffer_size, (FILE *)parser->fp_input);
  103. if (unlikely(!tmp)) {
  104. if (feof((FILE *)parser->fp_input))
  105. error("PARSER: read failed: end of file");
  106. else if (ferror((FILE *)parser->fp_input))
  107. error("PARSER: read failed: input error");
  108. else
  109. error("PARSER: read failed: unknown error");
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * Takes an initialized parser object that has an unprocessed entry (by calling parser_next)
  116. * and if it contains a valid keyword, it will execute all the callbacks
  117. *
  118. */
  119. inline int parser_action(PARSER *parser, char *input)
  120. {
  121. parser->line++;
  122. if(unlikely(parser->flags & PARSER_DEFER_UNTIL_KEYWORD)) {
  123. char command[PLUGINSD_LINE_MAX + 1];
  124. bool has_keyword = find_first_keyword(input, command, PLUGINSD_LINE_MAX, pluginsd_space);
  125. if(!has_keyword || strcmp(command, parser->defer.end_keyword) != 0) {
  126. if(parser->defer.response) {
  127. buffer_strcat(parser->defer.response, input);
  128. if(buffer_strlen(parser->defer.response) > 10 * 1024 * 1024) {
  129. // more than 10MB of data
  130. // a bad plugin that did not send the end_keyword
  131. internal_error(true, "PLUGINSD: deferred response is too big (%zu bytes). Stopping this plugin.", buffer_strlen(parser->defer.response));
  132. return 1;
  133. }
  134. }
  135. return 0;
  136. }
  137. else {
  138. // call the action
  139. parser->defer.action(parser, parser->defer.action_data);
  140. // empty everything
  141. parser->defer.action = NULL;
  142. parser->defer.action_data = NULL;
  143. parser->defer.end_keyword = NULL;
  144. parser->defer.response = NULL;
  145. parser->flags &= ~PARSER_DEFER_UNTIL_KEYWORD;
  146. }
  147. return 0;
  148. }
  149. char *words[PLUGINSD_MAX_WORDS];
  150. size_t num_words = pluginsd_split_words(input, words, PLUGINSD_MAX_WORDS);
  151. const char *command = get_word(words, num_words, 0);
  152. if(unlikely(!command))
  153. return 0;
  154. PARSER_RC rc;
  155. PARSER_KEYWORD *t = parser_find_keyword(parser, command);
  156. if(likely(t)) {
  157. worker_is_busy(t->worker_job_id);
  158. rc = (*t->func)(words, num_words, parser->user);
  159. worker_is_idle();
  160. }
  161. else
  162. rc = PARSER_RC_ERROR;
  163. #ifdef NETDATA_INTERNAL_CHECKS
  164. if(rc == PARSER_RC_ERROR) {
  165. BUFFER *wb = buffer_create(PLUGINSD_LINE_MAX, NULL);
  166. for(size_t i = 0; i < num_words ;i++) {
  167. if(i) buffer_fast_strcat(wb, " ", 1);
  168. buffer_fast_strcat(wb, "\"", 1);
  169. const char *s = get_word(words, num_words, i);
  170. buffer_strcat(wb, s?s:"");
  171. buffer_fast_strcat(wb, "\"", 1);
  172. }
  173. internal_error(true, "PLUGINSD: parser_action('%s') failed on line %zu: { %s } (quotes added to show parsing)",
  174. command, parser->line, buffer_tostring(wb));
  175. buffer_free(wb);
  176. }
  177. #endif
  178. return (rc == PARSER_RC_ERROR || rc == PARSER_RC_STOP);
  179. }