4
0

qdevice-heuristics-exec-list.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <inttypes.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "qdevice-heuristics-exec-list.h"
  38. void
  39. qdevice_heuristics_exec_list_init(struct qdevice_heuristics_exec_list *list)
  40. {
  41. TAILQ_INIT(list);
  42. }
  43. struct qdevice_heuristics_exec_list_entry *
  44. qdevice_heuristics_exec_list_add(struct qdevice_heuristics_exec_list *list,
  45. char *name, char *command)
  46. {
  47. struct qdevice_heuristics_exec_list_entry *entry;
  48. entry = (struct qdevice_heuristics_exec_list_entry *)malloc(sizeof(*entry));
  49. if (entry == NULL) {
  50. return (NULL);
  51. }
  52. memset(entry, 0, sizeof(*entry));
  53. entry->name = strdup(name);
  54. if (entry->name == NULL) {
  55. free(entry);
  56. return (NULL);
  57. }
  58. entry->command = strdup(command);
  59. if (entry->command == NULL) {
  60. free(entry->name);
  61. free(entry);
  62. return (NULL);
  63. }
  64. TAILQ_INSERT_TAIL(list, entry, entries);
  65. return (entry);
  66. }
  67. void
  68. qdevice_heuristics_exec_list_free(struct qdevice_heuristics_exec_list *list)
  69. {
  70. struct qdevice_heuristics_exec_list_entry *entry;
  71. struct qdevice_heuristics_exec_list_entry *entry_next;
  72. entry = TAILQ_FIRST(list);
  73. while (entry != NULL) {
  74. entry_next = TAILQ_NEXT(entry, entries);
  75. free(entry->name);
  76. free(entry->command);
  77. free(entry);
  78. entry = entry_next;
  79. }
  80. TAILQ_INIT(list);
  81. }
  82. size_t
  83. qdevice_heuristics_exec_list_size(const struct qdevice_heuristics_exec_list *list)
  84. {
  85. struct qdevice_heuristics_exec_list_entry *entry;
  86. size_t res;
  87. res = 0;
  88. TAILQ_FOREACH(entry, list, entries) {
  89. res++;
  90. }
  91. return (res);
  92. }
  93. int
  94. qdevice_heuristics_exec_list_clone(struct qdevice_heuristics_exec_list *dst_list,
  95. const struct qdevice_heuristics_exec_list *src_list)
  96. {
  97. struct qdevice_heuristics_exec_list_entry *entry;
  98. qdevice_heuristics_exec_list_init(dst_list);
  99. TAILQ_FOREACH(entry, src_list, entries) {
  100. if (qdevice_heuristics_exec_list_add(dst_list, entry->name, entry->command) == NULL) {
  101. qdevice_heuristics_exec_list_free(dst_list);
  102. return (-1);
  103. }
  104. }
  105. return (0);
  106. }
  107. void
  108. qdevice_heuristics_exec_list_del(struct qdevice_heuristics_exec_list *list,
  109. struct qdevice_heuristics_exec_list_entry *entry)
  110. {
  111. TAILQ_REMOVE(list, entry, entries);
  112. free(entry->name);
  113. free(entry->command);
  114. free(entry);
  115. }
  116. int
  117. qdevice_heuristics_exec_list_is_empty(const struct qdevice_heuristics_exec_list *list)
  118. {
  119. return (TAILQ_EMPTY(list));
  120. }
  121. struct qdevice_heuristics_exec_list_entry *
  122. qdevice_heuristics_exec_list_find_name(const struct qdevice_heuristics_exec_list *list,
  123. const char *name)
  124. {
  125. struct qdevice_heuristics_exec_list_entry *entry;
  126. TAILQ_FOREACH(entry, list, entries) {
  127. if (strcmp(entry->name, name) == 0) {
  128. return (entry);
  129. }
  130. }
  131. return (NULL);
  132. }
  133. int
  134. qdevice_heuristics_exec_list_eq(const struct qdevice_heuristics_exec_list *list1,
  135. const struct qdevice_heuristics_exec_list *list2)
  136. {
  137. struct qdevice_heuristics_exec_list_entry *entry1;
  138. struct qdevice_heuristics_exec_list_entry *entry2;
  139. struct qdevice_heuristics_exec_list tmp_list;
  140. int res;
  141. res = 1;
  142. if (qdevice_heuristics_exec_list_clone(&tmp_list, list2) != 0) {
  143. return (-1);
  144. }
  145. TAILQ_FOREACH(entry1, list1, entries) {
  146. entry2 = qdevice_heuristics_exec_list_find_name(&tmp_list, entry1->name);
  147. if (entry2 == NULL) {
  148. res = 0;
  149. goto return_res;
  150. }
  151. if (strcmp(entry1->command, entry2->command) != 0) {
  152. res = 0;
  153. goto return_res;
  154. }
  155. qdevice_heuristics_exec_list_del(&tmp_list, entry2);
  156. }
  157. if (!qdevice_heuristics_exec_list_is_empty(&tmp_list)) {
  158. res = 0;
  159. goto return_res;
  160. }
  161. return_res:
  162. qdevice_heuristics_exec_list_free(&tmp_list);
  163. return (res);
  164. }