Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <pthread.h>
28
29 #include "generic/bitmap.h"
30 #include "seap-descriptor.h"
31 #include "_sexp-atomic.h"
32
33 SEAP_desctable_t *SEAP_desctable_new (void)
34 {
35 SEAP_desctable_t *t = malloc(sizeof(SEAP_desctable_t));
36 t->tree = NULL;
37 t->bmap = NULL;
38
39 return(t);
40 }
41
42 int SEAP_desc_add(SEAP_desctable_t *sd_table, SEAP_scheme_t scheme,
43 void *scheme_data)
44 {
45 bitmap_bitn_t sd;
46 pthread_mutexattr_t mutex_attr;
47 SEAP_desc_t *sd_dsc;
48
49 if (sd_table->bmap == NULL)
50 sd_table->bmap = bitmap_new(128); /* XXX: hardcoded size */
51 if (sd_table->bmap == NULL)
52 return (-1);
53
54 sd = bitmap_setfree (sd_table->bmap);
55
56 if (sd >= 0) {
57 if (sd_table->tree == NULL)
58 sd_table->tree = rbt_i32_new();
59
60 sd_dsc = malloc(sizeof(SEAP_desc_t));
61
62 sd_dsc->next_id = 0;
63 /* sd_dsc->sexpcnt = 0; */
64 sd_dsc->scheme = scheme;
65 sd_dsc->scheme_data = scheme_data;
66 sd_dsc->next_cid = 0;
67 sd_dsc->cmd_c_table = SEAP_cmdtbl_new ();
68 sd_dsc->cmd_w_table = SEAP_cmdtbl_new ();
69 sd_dsc->msg_queue = NULL;
70 sd_dsc->err_queue = rbt_i32_new();
71 sd_dsc->cmd_queue = NULL;
72
73 SEAP_packetq_init(&sd_dsc->pck_queue);
74
75 pthread_mutexattr_init (&mutex_attr);
76 pthread_mutexattr_settype (&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
77
78 pthread_mutex_init (&(sd_dsc->r_lock), &mutex_attr);
79 pthread_mutex_init (&(sd_dsc->w_lock), &mutex_attr);
80
81 pthread_mutexattr_destroy (&mutex_attr);
82
83 if (rbt_i32_add(sd_table->tree, (int32_t)sd, (void *)sd_dsc, NULL) != 0) {
84 SEAP_desc_free(sd_dsc);
85 return (-1);
86 }
87
88 return ((int)sd);
89 }
90
91 return (-1);
92 }
93
94 int SEAP_desc_del (SEAP_desctable_t *sd_table, int sd)
95 {
96 SEAP_desc_t *dsc = NULL;
97
98 if (sd < 0) {
99 errno = EINVAL;
100 return (-1);
101 }
102
103 if (sd_table->tree == NULL)
104 return (-1);
105
106 if (rbt_i32_del(sd_table->tree, sd, (void *)&dsc) != 0)
107 return (-1);
108
109 bitmap_unset(sd_table->bmap, sd);
110
111 if (dsc != NULL)
112 SEAP_desc_free(dsc);
113
114 return (0);
115 }
116
117 static void __SEAP_desc_errqueue_free_cb(struct rbt_i32_node *n)
118 {
119 SEAP_error_free(n->data);
120 }
121
122 void SEAP_desc_free(SEAP_desc_t *dsc)
123 {
124 SEAP_cmdtbl_free(dsc->cmd_c_table);
125 SEAP_cmdtbl_free(dsc->cmd_w_table);
126 SEAP_packetq_free(&dsc->pck_queue);
127 pthread_mutex_destroy(&(dsc->r_lock));
128 pthread_mutex_destroy(&(dsc->w_lock));
129 rbt_i32_free_cb(dsc->err_queue, __SEAP_desc_errqueue_free_cb);
130 free(dsc);
131 }
132
133 static void SEAP_desc_free_node(struct rbt_i32_node *n)
134 {
135 SEAP_desc_free(n->data);
136 }
137
138 void SEAP_desctable_free(SEAP_desctable_t *sd_table)
139 {
140 if (sd_table->tree != NULL)
141 rbt_i32_free_cb(sd_table->tree, &SEAP_desc_free_node);
142 if (sd_table->bmap != NULL)
143 bitmap_free(sd_table->bmap);
144 free(sd_table);
145 }
146
147 SEAP_desc_t *SEAP_desc_get (SEAP_desctable_t *sd_table, int sd)
148 {
149 SEAP_desc_t *dsc = NULL;
150
151 if (sd < 0 || sd_table->tree == NULL) {
152 errno = EBADF;
153 return (NULL);
154 }
155
156 if (rbt_i32_get(sd_table->tree, sd, (void *)&dsc) != 0) {
157 errno = EBADF;
158 return (NULL);
159 }
160
161 return(dsc);
162 }
163
164 SEAP_msgid_t SEAP_desc_genmsgid (SEAP_desctable_t *sd_table, int sd)
165 {
166 SEAP_desc_t *dsc;
167 SEAP_msgid_t id;
168 dsc = SEAP_desc_get (sd_table, sd);
169
170 if (dsc == NULL) {
171 errno = EINVAL;
172 return (-1);
173 }
174
175 #if SEAP_MSGID_BITS == 64
176 id = SEXP_atomic_inc_u64 (&(dsc->next_id));
177 #else
178 id = SEXP_atomic_inc_u32 (&(dsc->next_id));
179 #endif
180 return (id);
181 }
182
183 SEAP_cmdid_t SEAP_desc_gencmdid (SEAP_desctable_t *sd_table, int sd)
184 {
185 SEAP_desc_t *dsc;
186 SEAP_cmdid_t id;
187
188 dsc = SEAP_desc_get (sd_table, sd);
189
190 if (dsc == NULL) {
191 errno = EINVAL;
192 return (-1);
193 }
194
195 id = SEXP_atomic_inc_u16 (&(dsc->next_cid));
196
197 return (id);
198 }