4
0

qdevice-heuristics-cmd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <string.h>
  37. #include "dynar.h"
  38. #include "dynar-str.h"
  39. #include "log.h"
  40. #include "qdevice-heuristics-exec-result.h"
  41. #include "qdevice-heuristics-cmd.h"
  42. #include "qdevice-heuristics-cmd-str.h"
  43. #include "qdevice-heuristics-io.h"
  44. static int
  45. qdevice_heuristics_cmd_process_exec_result(struct qdevice_heuristics_instance *instance,
  46. struct dynar *data)
  47. {
  48. uint32_t seq_number;
  49. char *str;
  50. enum qdevice_heuristics_exec_result exec_result;
  51. str = dynar_data(data);
  52. if (sscanf(str, QDEVICE_HEURISTICS_CMD_STR_EXEC_RESULT_ADD_SPACE "%"PRIu32" %u", &seq_number,
  53. &exec_result) != 2) {
  54. log(LOG_CRIT, "Can't parse exec result command (sscanf)");
  55. return (-1);
  56. }
  57. log(LOG_DEBUG,
  58. "Received heuristics exec result command with seq_no \"%"PRIu32"\" and result \"%s\"", seq_number,
  59. qdevice_heuristics_exec_result_to_str(exec_result));
  60. if (!instance->waiting_for_result) {
  61. log(LOG_DEBUG, "Received exec result is not expected. Ignoring.");
  62. return (0);
  63. }
  64. if (seq_number != instance->expected_reply_seq_number) {
  65. log(LOG_DEBUG, "Received heuristics exec result seq number %"PRIu32
  66. " is not expected one (expected %"PRIu32"). Ignoring.", seq_number,
  67. instance->expected_reply_seq_number);
  68. return (0);
  69. }
  70. instance->waiting_for_result = 0;
  71. if (qdevice_heuristics_result_notifier_notify(&instance->exec_result_notifier_list,
  72. (void *)instance, seq_number, exec_result) != 0) {
  73. log(LOG_DEBUG, "qdevice_heuristics_result_notifier_notify returned non-zero result");
  74. return (-1);
  75. }
  76. return (0);
  77. }
  78. /*
  79. * 1 - Line processed
  80. * 0 - No line to process - everything processed
  81. * -1 - Error
  82. */
  83. static int
  84. qdevice_heuristics_cmd_process_one_line(struct qdevice_heuristics_instance *instance,
  85. struct dynar *data)
  86. {
  87. char *str;
  88. size_t str_len;
  89. size_t nl_pos;
  90. size_t zi;
  91. str = dynar_data(data);
  92. str_len = dynar_size(data);
  93. /*
  94. * Find valid line
  95. */
  96. for (zi = 0; zi < str_len && str[zi] != '\r' && str[zi] != '\n'; zi++) ;
  97. if (zi >= str_len) {
  98. /*
  99. * Command is not yet fully readed
  100. */
  101. return (0);
  102. }
  103. nl_pos = zi;
  104. str[nl_pos] = '\0';
  105. if (strncmp(str, QDEVICE_HEURISTICS_CMD_STR_EXEC_RESULT_ADD_SPACE,
  106. strlen(QDEVICE_HEURISTICS_CMD_STR_EXEC_RESULT_ADD_SPACE)) == 0) {
  107. if (qdevice_heuristics_cmd_process_exec_result(instance, data) != 0) {
  108. return (-1);
  109. }
  110. } else {
  111. log(LOG_CRIT,
  112. "Heuristics worker sent unknown command \"%s\"", str);
  113. return (-1);
  114. }
  115. /*
  116. * Find place where is begining of new "valid" line
  117. */
  118. for (zi = nl_pos + 1; zi < str_len && (str[zi] == '\0' || str[zi] == '\n' || str[zi] == '\r'); zi++) ;
  119. memmove(str, str + zi, str_len - zi);
  120. if (dynar_set_size(data, str_len - zi) == -1) {
  121. log(LOG_CRIT,
  122. "qdevice_heuristics_cmd_process_one_line: Can't set dynar size");
  123. return (-1);
  124. }
  125. return (1);
  126. }
  127. /*
  128. * 0 - No error
  129. * -1 - Error
  130. */
  131. static int
  132. qdevice_heuristics_cmd_process(struct qdevice_heuristics_instance *instance)
  133. {
  134. int res;
  135. while ((res =
  136. qdevice_heuristics_cmd_process_one_line(instance, &instance->cmd_in_buffer)) == 1) ;
  137. return (res);
  138. }
  139. /*
  140. * 0 - No error
  141. * 1 - Error
  142. */
  143. int
  144. qdevice_heuristics_cmd_read_from_pipe(struct qdevice_heuristics_instance *instance)
  145. {
  146. int res;
  147. int ret;
  148. res = qdevice_heuristics_io_read(instance->pipe_cmd_recv, &instance->cmd_in_buffer);
  149. ret = 0;
  150. switch (res) {
  151. case 0:
  152. /*
  153. * Partial read
  154. */
  155. break;
  156. case -1:
  157. log(LOG_ERR, "Lost connection with heuristics worker");
  158. ret = -1;
  159. break;
  160. case -2:
  161. log(LOG_ERR, "Heuristics worker sent too long cmd.");
  162. ret = -1;
  163. break;
  164. case -3:
  165. log(LOG_ERR, "Unhandled error when reading from heuristics worker cmd fd");
  166. ret = -1;
  167. break;
  168. case 1:
  169. /*
  170. * At least one cmd line received
  171. */
  172. ret = qdevice_heuristics_cmd_process(instance);
  173. break;
  174. }
  175. return (ret);
  176. }
  177. /*
  178. * 0 - No error
  179. * 1 - Error
  180. */
  181. int
  182. qdevice_heuristics_cmd_write(struct qdevice_heuristics_instance *instance)
  183. {
  184. struct send_buffer_list_entry *send_buffer;
  185. int res;
  186. send_buffer = send_buffer_list_get_active(&instance->cmd_out_buffer_list);
  187. if (send_buffer == NULL) {
  188. log(LOG_CRIT, "send_buffer_list_get_active in qdevice_heuristics_cmd_write returned NULL");
  189. return (-1);
  190. }
  191. res = qdevice_heuristics_io_write(instance->pipe_cmd_send, &send_buffer->buffer,
  192. &send_buffer->msg_already_sent_bytes);
  193. if (res == 1) {
  194. send_buffer_list_delete(&instance->cmd_out_buffer_list, send_buffer);
  195. }
  196. if (res == -1) {
  197. log(LOG_CRIT, "qdevice_heuristics_io_write returned -1 (write returned 0)");
  198. return (-1);
  199. }
  200. if (res == -2) {
  201. log(LOG_CRIT, "Unhandled error in during sending message to heuristics "
  202. "worker (qdevice_heuristics_io_write returned -2)");
  203. return (-1);
  204. }
  205. return (0);
  206. }
  207. static int
  208. qdevice_heuristics_cmd_remove_newlines(struct dynar *str)
  209. {
  210. size_t len;
  211. size_t zi;
  212. char *buf;
  213. len = dynar_size(str);
  214. buf = dynar_data(str);
  215. for (zi = 0; zi < len ; zi++) {
  216. if (buf[zi] == '\n' || buf[zi] == '\r') {
  217. buf[zi] = ' ';
  218. }
  219. }
  220. return (0);
  221. }
  222. int
  223. qdevice_heuristics_cmd_write_exec_list(struct qdevice_heuristics_instance *instance,
  224. const struct qdevice_heuristics_exec_list *new_exec_list)
  225. {
  226. struct send_buffer_list_entry *send_buffer;
  227. struct qdevice_heuristics_exec_list_entry *entry;
  228. send_buffer = send_buffer_list_get_new(&instance->cmd_out_buffer_list);
  229. if (send_buffer == NULL) {
  230. log(LOG_ERR, "Can't alloc send list for cmd change exec list");
  231. return (-1);
  232. }
  233. if (dynar_str_cpy(&send_buffer->buffer, QDEVICE_HEURISTICS_CMD_STR_EXEC_LIST_CLEAR) == -1 ||
  234. dynar_str_cat(&send_buffer->buffer, "\n") == -1) {
  235. log(LOG_ERR, "Can't alloc list clear message");
  236. send_buffer_list_discard_new(&instance->cmd_out_buffer_list, send_buffer);
  237. return (-1);
  238. }
  239. send_buffer_list_put(&instance->cmd_out_buffer_list, send_buffer);
  240. if (new_exec_list == NULL) {
  241. return (0);
  242. }
  243. /*
  244. * new_exec_list is not NULL, send it
  245. */
  246. TAILQ_FOREACH(entry, new_exec_list, entries) {
  247. send_buffer = send_buffer_list_get_new(&instance->cmd_out_buffer_list);
  248. if (send_buffer == NULL) {
  249. log(LOG_ERR, "Can't alloc send list for cmd change exec list");
  250. return (-1);
  251. }
  252. if (dynar_str_cpy(&send_buffer->buffer,
  253. QDEVICE_HEURISTICS_CMD_STR_EXEC_LIST_ADD_SPACE) == -1 ||
  254. dynar_str_cat(&send_buffer->buffer, entry->name) == -1 ||
  255. dynar_str_cat(&send_buffer->buffer, " ") == -1 ||
  256. dynar_str_cat(&send_buffer->buffer, entry->command) == -1 ||
  257. qdevice_heuristics_cmd_remove_newlines(&send_buffer->buffer) == -1 ||
  258. dynar_str_cat(&send_buffer->buffer, "\n") == -1) {
  259. log(LOG_ERR, "Can't alloc list add message");
  260. send_buffer_list_discard_new(&instance->cmd_out_buffer_list, send_buffer);
  261. return (-1);
  262. }
  263. send_buffer_list_put(&instance->cmd_out_buffer_list, send_buffer);
  264. }
  265. return (0);
  266. }
  267. int
  268. qdevice_heuristics_cmd_write_exec(struct qdevice_heuristics_instance *instance,
  269. uint32_t timeout, uint32_t seq_number)
  270. {
  271. struct send_buffer_list_entry *send_buffer;
  272. send_buffer = send_buffer_list_get_new(&instance->cmd_out_buffer_list);
  273. if (send_buffer == NULL) {
  274. log(LOG_ERR, "Can't alloc send list for cmd change exec list");
  275. return (-1);
  276. }
  277. if (dynar_str_cpy(&send_buffer->buffer, QDEVICE_HEURISTICS_CMD_STR_EXEC_ADD_SPACE) == -1 ||
  278. dynar_str_catf(&send_buffer->buffer, "%"PRIu32" %"PRIu32"\n", timeout, seq_number) == -1) {
  279. log(LOG_ERR, "Can't alloc exec message");
  280. send_buffer_list_discard_new(&instance->cmd_out_buffer_list, send_buffer);
  281. return (-1);
  282. }
  283. send_buffer_list_put(&instance->cmd_out_buffer_list, send_buffer);
  284. return (0);
  285. }