Debian Codegraph @

src/OVAL/probes/SEAP/seap-packetq.c

raw symbols SCIP
1 #include <config.h>
2 #include <stdbool.h>
3 #include <pthread.h>
4 #include <errno.h>
5 #include "_seap-packet.h"
6 #include "_seap-packetq.h"
7
8 int SEAP_packetq_init(SEAP_packetq_t *queue)
9 {
10 if (queue == NULL) {
11 errno = EFAULT;
12 return (-1);
13 }
14
15 queue->flags = SEAP_PACKETQ_DONTFREE;
16 queue->count = 0;
17 queue->first = NULL;
18 queue->last = NULL;
19
20 if (pthread_mutex_init(&queue->lock, NULL) != 0)
21 return (-1);
22
23 return (0);
24 }
25
26 void SEAP_packetq_free(SEAP_packetq_t *queue)
27 {
28 if (queue == NULL)
29 return;
30
31 pthread_mutex_destroy(&queue->lock);
32 }
33
34 struct SEAP_packetq_item *SEAP_packetq_item_new(void)
35 {
36 struct SEAP_packetq_item *i = malloc(sizeof(struct SEAP_packetq_item));
37
38 i->next = NULL;
39 i->prev = NULL;
40 i->packet = NULL;
41
42 return (i);
43 }
44
45 void SEAP_packetq_item_free(struct SEAP_packetq_item *i, bool freepacket)
46 {
47 if (freepacket)
48 SEAP_packet_free(i->packet);
49
50 i->prev = NULL;
51 i->next = NULL;
52 i->packet = NULL;
53
54 free(i);
55 }
56
57 int SEAP_packetq_get(SEAP_packetq_t *queue, SEAP_packet_t **packet_dst)
58 {
59 int count;
60 struct SEAP_packetq_item *save;
61
62 if (queue == NULL || packet_dst == NULL)
63 return (-1);
64 if (pthread_mutex_lock(&queue->lock) != 0)
65 return (-1);
66
67 if (queue->first == NULL) {
68 if (queue->last != NULL) {
69 return -1;
70 }
71 if (queue->count != 0) {
72 return -1;
73 }
74
75 count = -1;
76 goto __unlock_and_return;
77 }
78
79 if (queue->last == NULL) {
80 return -1;
81 }
82 if (queue->count <= 0) {
83 return -1;
84 }
85
86 save = queue->first->next;
87 (*packet_dst) = queue->first->packet;
88 SEAP_packetq_item_free(queue->first, false);
89 queue->first = save;
90
91 if (queue->first != NULL)
92 queue->first->prev = NULL;
93 else
94 queue->last = NULL;
95
96 count = --queue->count;
97
98 __unlock_and_return:
99 if (pthread_mutex_unlock(&queue->lock) != 0)
100 return (-1); /* abort()? */
101
102 return (count);
103 }
104
105 int SEAP_packetq_put(SEAP_packetq_t *queue, SEAP_packet_t *packet)
106 {
107 int count;
108
109 if (queue == NULL)
110 return (-1);
111 if (pthread_mutex_lock(&queue->lock) != 0)
112 return (-1);
113
114 if (queue->first == NULL) {
115 queue->first = SEAP_packetq_item_new();
116 queue->first->packet = packet;
117 queue->last = queue->first;
118 } else {
119 if (queue->last == NULL) {
120 pthread_mutex_unlock(&queue->lock);
121 return -1;
122 }
123 if (queue->last->next != NULL) {
124 pthread_mutex_unlock(&queue->lock);
125 return -1;
126 }
127
128 queue->last->next = SEAP_packetq_item_new();
129 queue->last->next->packet = packet;
130 queue->last->next->prev = queue->last;
131 }
132
133 count = ++queue->count;
134
135 if (pthread_mutex_unlock(&queue->lock) != 0)
136 return (-1); /* abort()? */
137
138 return (count);
139 }
140
141 int SEAP_packetq_count(SEAP_packetq_t *queue)
142 {
143 int count;
144
145 if (queue == NULL)
146 return (-1);
147 if (pthread_mutex_lock(&queue->lock) != 0)
148 return (-1);
149
150 count = queue->count;
151
152 if (pthread_mutex_unlock(&queue->lock) != 0)
153 return (-1); /* abort()? */
154
155 return (count);
156 }