qdevice-heuristics-cmd.c 9.2 KB

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