qdevice-heuristics-log.c 4.3 KB

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