parser.c 8.2 KB

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