qdevice-heuristics-log.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "qdevice-heuristics-io.h"
  35. #include "qdevice-heuristics-log.h"
  36. #include "qdevice-log.h"
  37. /*
  38. * 1 - Line logged
  39. * 0 - No line to log - everything processed
  40. * -1 - Error
  41. */
  42. static int
  43. qdevice_heuristics_log_process_one_line(struct dynar *data)
  44. {
  45. char *str;
  46. char *log_str_start;
  47. size_t str_len;
  48. size_t nl_pos;
  49. size_t zi;
  50. int status;
  51. unsigned int log_priority;
  52. str = dynar_data(data);
  53. str_len = dynar_size(data);
  54. log_str_start = str;
  55. status = 0;
  56. log_priority = 0;
  57. /*
  58. * Find start of log message and end of line
  59. */
  60. for (zi = 0; zi < str_len && status != -1; zi++) {
  61. switch (status) {
  62. case 0:
  63. if (str[zi] >= '0' && str[zi] <= '9') {
  64. log_priority = log_priority * 10 + (str[zi] - '0');
  65. } else if (str[zi] == ' ') {
  66. status = 1;
  67. } else {
  68. log(LOG_ERR, "Parsing of heuristics log line failed. "
  69. "Unexpected char '%c'", str[zi]);
  70. return (-1);
  71. }
  72. break;
  73. case 1:
  74. if (str[zi] != ' ') {
  75. status = 2;
  76. log_str_start = str + zi;
  77. }
  78. break;
  79. case 2:
  80. if (str[zi] == '\n' || str[zi] == '\r') {
  81. str[zi] = '\0';
  82. nl_pos = zi;
  83. status = -1;
  84. }
  85. break;
  86. }
  87. }
  88. if (status != -1) {
  89. return (0);
  90. }
  91. /*
  92. * Do actual logging
  93. */
  94. log(log_priority, "worker: %s", log_str_start);
  95. /*
  96. * Find place where is begining of new "valid" line
  97. */
  98. for (zi = nl_pos + 1; zi < str_len && (str[zi] == '\0' || str[zi] == '\n' || str[zi] == '\r'); zi++) ;
  99. memmove(str, str + zi, str_len - zi);
  100. if (dynar_set_size(data, str_len - zi) == -1) {
  101. log(LOG_ERR, "qdevice_heuristics_log_process_one_line: Can't set dynar size");
  102. return (-1);
  103. }
  104. return (1);
  105. }
  106. /*
  107. * 0 - No error
  108. * -1 - Error
  109. */
  110. static int
  111. qdevice_heuristics_log_process(struct qdevice_heuristics_instance *instance)
  112. {
  113. int res;
  114. while ((res = qdevice_heuristics_log_process_one_line(&instance->log_in_buffer)) == 1) ;
  115. return (res);
  116. }
  117. /*
  118. * 0 - No error
  119. * 1 - Error
  120. */
  121. int
  122. qdevice_heuristics_log_read_from_pipe(struct qdevice_heuristics_instance *instance)
  123. {
  124. int res;
  125. int ret;
  126. res = qdevice_heuristics_io_read(instance->pipe_log_recv, &instance->log_in_buffer);
  127. ret = 0;
  128. switch (res) {
  129. case 0:
  130. /*
  131. * Partial read
  132. */
  133. break;
  134. case -1:
  135. log(LOG_ERR, "Lost connection with heuristics worker");
  136. ret = -1;
  137. break;
  138. case -2:
  139. log(LOG_ERR, "Heuristics worker sent too long log. Ignoring line");
  140. dynar_clean(&instance->log_in_buffer);
  141. break;
  142. case -3:
  143. log(LOG_ERR, "Unhandled error when reading from heuristics worker log fd");
  144. ret = -1;
  145. break;
  146. case 1:
  147. /*
  148. * At least one log line received
  149. */
  150. ret = qdevice_heuristics_log_process(instance);
  151. break;
  152. }
  153. return (ret);
  154. }