| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "pluginsd_internals.h" |
| 4 | |
| 5 | ssize_t send_to_plugin(const char *txt, PARSER *parser, STREAM_TRAFFIC_TYPE type) { |
| 6 | if(!txt || !*txt || !parser) |
| 7 | return 0; |
| 8 | |
| 9 | if(parser->send_to_plugin_cb) |
| 10 | return parser->send_to_plugin_cb(txt, parser->send_to_plugin_data, type); |
| 11 | |
| 12 | spinlock_lock(&parser->writer.spinlock); |
| 13 | |
| 14 | ND_SOCK tmp = { .fd = parser->fd_output, }; |
| 15 | const char *destination = "child"; |
| 16 | ND_SOCK *s = parser->sock; // try the socket |
| 17 | if(!s) { |
| 18 | destination = "plugin"; |
| 19 | s = &tmp; // socket is not there, use the pipe |
| 20 | } |
| 21 | |
| 22 | if(s->fd != -1) { |
| 23 | // plugins pipe or socket (with or without SSL) |
| 24 | |
| 25 | size_t total = strlen(txt); |
| 26 | ssize_t bytes = nd_sock_write_persist(s, txt, total, 100); |
| 27 | if(bytes < (ssize_t)total) { |
| 28 | nd_log(NDLS_DAEMON, NDLP_WARNING, |
| 29 | "PLUGINSD: cannot send command to %s (fd = %d, sent bytes = %zd out of %zu)", |
| 30 | destination, s->fd, bytes, total); |
| 31 | spinlock_unlock(&parser->writer.spinlock); |
| 32 | return -3; |
| 33 | } |
| 34 | |
| 35 | spinlock_unlock(&parser->writer.spinlock); |
| 36 | return (int)bytes; |
| 37 | } |
| 38 | |
| 39 | spinlock_unlock(&parser->writer.spinlock); |
| 40 | nd_log(NDLS_DAEMON, NDLP_WARNING, |
| 41 | "PLUGINSD: cannot send command to %s (probably the receiver got disconnected, since no output descriptor is available)", |
| 42 | destination); |
| 43 | return -4; |
| 44 | } |
| 45 | |
| 46 | PARSER_RC PLUGINSD_DISABLE_PLUGIN(PARSER *parser, const char *keyword, const char *msg) { |
| 47 | parser->user.enabled = 0; |
| 48 | |
| 49 | if(keyword && msg) { |
| 50 | nd_log_limit_static_global_var(erl, 1, 0); |
| 51 | nd_log_limit(&erl, NDLS_COLLECTORS, NDLP_INFO, |
| 52 | "PLUGINSD: keyword %s: %s", keyword, msg); |
| 53 | } |
| 54 | |
| 55 | return PARSER_RC_ERROR; |
| 56 | } |
| 57 | |
| 58 | void pluginsd_keywords_init(PARSER *parser, PARSER_REPERTOIRE repertoire) { |
| 59 | parser_init_repertoire(parser, repertoire); |
| 60 | |
| 61 | if (repertoire & (PARSER_INIT_PLUGINSD | PARSER_INIT_STREAMING)) |
| 62 | pluginsd_inflight_functions_init(parser); |
| 63 | } |
| 64 | |
| 65 | void parser_destroy(PARSER *parser) { |
| 66 | if (unlikely(!parser)) |
| 67 | return; |
| 68 | |
| 69 | pluginsd_inflight_functions_cleanup(parser); |
| 70 | |
| 71 | freez(parser); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | PARSER *parser_init(struct parser_user_object *user, int fd_input, int fd_output, |
| 76 | PARSER_INPUT_TYPE flags, ND_SOCK *sock) { |
| 77 | PARSER *parser; |
| 78 | |
| 79 | parser = callocz(1, sizeof(*parser)); |
| 80 | |
| 81 | if(user) |
| 82 | parser->user = *user; |
| 83 | |
| 84 | if(sock) { |
| 85 | parser->fd_input = sock->fd; |
| 86 | parser->fd_output = sock->fd; |
| 87 | parser->sock = sock; |
| 88 | } |
| 89 | else { |
| 90 | parser->fd_input = fd_input; |
| 91 | parser->fd_output = fd_output; |
| 92 | } |
| 93 | |
| 94 | parser->flags = flags; |
| 95 | |
| 96 | spinlock_init(&parser->writer.spinlock); |
| 97 | return parser; |
| 98 | } |