qdevice-heuristics-cmd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2015-2019 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include "dynar.h"
  37. #include "dynar-str.h"
  38. #include "qdevice-heuristics-exec-result.h"
  39. #include "qdevice-heuristics-cmd.h"
  40. #include "qdevice-heuristics-cmd-str.h"
  41. #include "qdevice-heuristics-io.h"
  42. #include "qdevice-log.h"
  43. static int
  44. qdevice_heuristics_cmd_process_exec_result(struct qdevice_heuristics_instance *instance,
  45. struct dynar *data)
  46. {
  47. uint32_t seq_number;
  48. char *str;
  49. enum qdevice_heuristics_exec_result exec_result;
  50. str = dynar_data(data);
  51. if (sscanf(str, QDEVICE_HEURISTICS_CMD_STR_EXEC_RESULT_ADD_SPACE "%"PRIu32" %u", &seq_number,
  52. &exec_result) != 2) {
  53. log(LOG_CRIT, "Can't parse exec result command (sscanf)");
  54. return (-1);
  55. }
  56. log(LOG_DEBUG,
  57. "Received heuristics exec result command with seq_no \"%"PRIu32"\" and result \"%s\"", seq_number,
  58. qdevice_heuristics_exec_result_to_str(exec_result));
  59. if (!instance->waiting_for_result) {
  60. log(LOG_DEBUG, "Received exec result is not expected. Ignoring.");
  61. return (0);
  62. }
  63. if (seq_number != instance->expected_reply_seq_number) {
  64. log(LOG_DEBUG, "Received heuristics exec result seq number %"PRIu32
  65. " is not expected one (expected %"PRIu32"). Ignoring.", seq_number,
  66. instance->expected_reply_seq_number);
  67. return (0);
  68. }
  69. instance->waiting_for_result = 0;
  70. if (qdevice_heuristics_result_notifier_notify(&instance->exec_result_notifier_list,
  71. (void *)instance, seq_number, exec_result) != 0) {
  72. log(LOG_DEBUG, "qdevice_heuristics_result_notifier_notify returned non-zero result");
  73. return (-1);
  74. }
  75. return (0);
  76. }
  77. /*
  78. * 1 - Line processed
  79. * 0 - No line to process - everything processed
  80. * -1 - Error
  81. */
  82. static int
  83. qdevice_heuristics_cmd_process_one_line(struct qdevice_heuristics_instance *instance,
  84. struct dynar *data)
  85. {
  86. char *str;
  87. size_t str_len;
  88. size_t nl_pos;
  89. size_t zi;
  90. str = dynar_data(data);
  91. str_len = dynar_size(data);
  92. /*
  93. * Find valid line
  94. */
  95. for (zi = 0; zi < str_len && str[zi] != '\r' && str[zi] != '\n'; zi++) ;
  96. if (zi >= str_len) {
  97. /*
  98. * Command is not yet fully readed
  99. */
  100. return (0);
  101. }
  102. nl_pos = zi;
  103. str[nl_pos] = '\0';
  104. if (strncmp(str, QDEVICE_HEURISTICS_CMD_STR_EXEC_RESULT_ADD_SPACE,
  105. strlen(QDEVICE_HEURISTICS_CMD_STR_EXEC_RESULT_ADD_SPACE)) == 0) {
  106. if (qdevice_heuristics_cmd_process_exec_result(instance, data) != 0) {
  107. return (-1);
  108. }
  109. } else {
  110. log(LOG_CRIT,
  111. "Heuristics worker sent unknown command \"%s\"", str);
  112. return (-1);
  113. }
  114. /*
  115. * Find place where is begining of new "valid" line
  116. */
  117. for (zi = nl_pos + 1; zi < str_len && (str[zi] == '\0' || str[zi] == '\n' || str[zi] == '\r'); zi++) ;
  118. memmove(str, str + zi, str_len - zi);
  119. if (dynar_set_size(data, str_len - zi) == -1) {
  120. log(LOG_CRIT,
  121. "qdevice_heuristics_cmd_process_one_line: Can't set dynar size");
  122. return (-1);
  123. }
  124. return (1);
  125. }
  126. /*
  127. * 0 - No error
  128. * -1 - Error
  129. */
  130. static int
  131. qdevice_heuristics_cmd_process(struct qdevice_heuristics_instance *instance)
  132. {
  133. int res;
  134. while ((res =
  135. qdevice_heuristics_cmd_process_one_line(instance, &instance->cmd_in_buffer)) == 1) ;
  136. return (res);
  137. }
  138. /*
  139. * 0 - No error
  140. * 1 - Error
  141. */
  142. int
  143. qdevice_heuristics_cmd_read_from_pipe(struct qdevice_heuristics_instance *instance)
  144. {
  145. int res;
  146. int ret;
  147. res = qdevice_heuristics_io_read(instance->pipe_cmd_recv, &instance->cmd_in_buffer);
  148. ret = 0;
  149. switch (res) {
  150. case 0:
  151. /*
  152. * Partial read
  153. */
  154. break;
  155. case -1:
  156. log(LOG_ERR, "Lost connection with heuristics worker");
  157. ret = -1;
  158. break;
  159. case -2:
  160. log(LOG_ERR, "Heuristics worker sent too long cmd.");
  161. ret = -1;
  162. break;
  163. case -3:
  164. log(LOG_ERR, "Unhandled error when reading from heuristics worker cmd fd");
  165. ret = -1;
  166. break;
  167. case 1:
  168. /*
  169. * At least one cmd line received
  170. */
  171. ret = qdevice_heuristics_cmd_process(instance);
  172. break;
  173. }
  174. return (ret);
  175. }
  176. /*
  177. * 0 - No error
  178. * 1 - Error
  179. */
  180. int
  181. qdevice_heuristics_cmd_write(struct qdevice_heuristics_instance *instance)
  182. {
  183. struct send_buffer_list_entry *send_buffer;
  184. int res;
  185. send_buffer = send_buffer_list_get_active(&instance->cmd_out_buffer_list);
  186. if (send_buffer == NULL) {
  187. log(LOG_CRIT, "send_buffer_list_get_active in qdevice_heuristics_cmd_write returned NULL");
  188. return (-1);
  189. }
  190. res = qdevice_heuristics_io_write(instance->pipe_cmd_send, &send_buffer->buffer,
  191. &send_buffer->msg_already_sent_bytes);
  192. if (res == 1) {
  193. send_buffer_list_delete(&instance->cmd_out_buffer_list, send_buffer);
  194. }
  195. if (res == -1) {
  196. log(LOG_CRIT, "qdevice_heuristics_io_write returned -1 (write returned 0)");
  197. return (-1);
  198. }
  199. if (res == -2) {
  200. log(LOG_CRIT, "Unhandled error in during sending message to heuristics "
  201. "worker (qdevice_heuristics_io_write returned -2)");
  202. return (-1);
  203. }
  204. return (0);
  205. }
  206. static int
  207. qdevice_heuristics_cmd_remove_newlines(struct dynar *str)
  208. {
  209. size_t len;
  210. size_t zi;
  211. char *buf;
  212. len = dynar_size(str);
  213. buf = dynar_data(str);
  214. for (zi = 0; zi < len ; zi++) {
  215. if (buf[zi] == '\n' || buf[zi] == '\r') {
  216. buf[zi] = ' ';
  217. }
  218. }
  219. return (0);
  220. }
  221. int
  222. qdevice_heuristics_cmd_write_exec_list(struct qdevice_heuristics_instance *instance,
  223. const struct qdevice_heuristics_exec_list *new_exec_list)
  224. {
  225. struct send_buffer_list_entry *send_buffer;
  226. struct qdevice_heuristics_exec_list_entry *entry;
  227. send_buffer = send_buffer_list_get_new(&instance->cmd_out_buffer_list);
  228. if (send_buffer == NULL) {
  229. log(LOG_ERR, "Can't alloc send list for cmd change exec list");
  230. return (-1);
  231. }
  232. if (dynar_str_cpy(&send_buffer->buffer, QDEVICE_HEURISTICS_CMD_STR_EXEC_LIST_CLEAR) == -1 ||
  233. dynar_str_cat(&send_buffer->buffer, "\n") == -1) {
  234. log(LOG_ERR, "Can't alloc list clear message");
  235. send_buffer_list_discard_new(&instance->cmd_out_buffer_list, send_buffer);
  236. return (-1);
  237. }
  238. send_buffer_list_put(&instance->cmd_out_buffer_list, send_buffer);
  239. if (new_exec_list == NULL) {
  240. return (0);
  241. }
  242. /*
  243. * new_exec_list is not NULL, send it
  244. */
  245. TAILQ_FOREACH(entry, new_exec_list, entries) {
  246. send_buffer = send_buffer_list_get_new(&instance->cmd_out_buffer_list);
  247. if (send_buffer == NULL) {
  248. log(LOG_ERR, "Can't alloc send list for cmd change exec list");
  249. return (-1);
  250. }
  251. if (dynar_str_cpy(&send_buffer->buffer,
  252. QDEVICE_HEURISTICS_CMD_STR_EXEC_LIST_ADD_SPACE) == -1 ||
  253. dynar_str_cat(&send_buffer->buffer, entry->name) == -1 ||
  254. dynar_str_cat(&send_buffer->buffer, " ") == -1 ||
  255. dynar_str_cat(&send_buffer->buffer, entry->command) == -1 ||
  256. qdevice_heuristics_cmd_remove_newlines(&send_buffer->buffer) == -1 ||
  257. dynar_str_cat(&send_buffer->buffer, "\n") == -1) {
  258. log(LOG_ERR, "Can't alloc list add message");
  259. send_buffer_list_discard_new(&instance->cmd_out_buffer_list, send_buffer);
  260. return (-1);
  261. }
  262. send_buffer_list_put(&instance->cmd_out_buffer_list, send_buffer);
  263. }
  264. return (0);
  265. }
  266. int
  267. qdevice_heuristics_cmd_write_exec(struct qdevice_heuristics_instance *instance,
  268. uint32_t timeout, uint32_t seq_number)
  269. {
  270. struct send_buffer_list_entry *send_buffer;
  271. send_buffer = send_buffer_list_get_new(&instance->cmd_out_buffer_list);
  272. if (send_buffer == NULL) {
  273. log(LOG_ERR, "Can't alloc send list for cmd change exec list");
  274. return (-1);
  275. }
  276. if (dynar_str_cpy(&send_buffer->buffer, QDEVICE_HEURISTICS_CMD_STR_EXEC_ADD_SPACE) == -1 ||
  277. dynar_str_catf(&send_buffer->buffer, "%"PRIu32" %"PRIu32"\n", timeout, seq_number) == -1) {
  278. log(LOG_ERR, "Can't alloc exec message");
  279. send_buffer_list_discard_new(&instance->cmd_out_buffer_list, send_buffer);
  280. return (-1);
  281. }
  282. send_buffer_list_put(&instance->cmd_out_buffer_list, send_buffer);
  283. return (0);
  284. }