Debian Codegraph @

src/common/error.c

raw symbols SCIP
1
2 /*
3 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Lukas Kuklinek <[email protected]>
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #ifdef OSCAP_THREAD_SAFE
29 #include <pthread.h>
30 #endif
31 #include <string.h>
32
33 #include "util.h"
34 #include "_error.h"
35 #include "err_queue.h"
36 #include "debug_priv.h"
37 #include "oscap_helpers.h"
38
39 #ifdef OSCAP_THREAD_SAFE
40 static pthread_key_t __key;
41 static pthread_once_t __once = PTHREAD_ONCE_INIT;
42 #else
43 struct err_queue *q = NULL;
44 #endif
45
46 #ifdef OSCAP_THREAD_SAFE
47 static void oscap_errkey_init(void)
48 {
49 (void)pthread_key_create(&__key, NULL);
50 }
51 #endif
52
53 static struct oscap_err_t *oscap_err_new(oscap_errfamily_t family, const char *desc,
54 const char *func, uint32_t line, const char *file)
55 {
56 struct oscap_err_t *err = (struct oscap_err_t*)malloc(sizeof(struct oscap_err_t));
57 err->family = family;
58 err->desc = oscap_sprintf("%s [%s:%d]", desc, file, line);
59 err->func = func;
60 err->line = line;
61 err->file = file;
62
63 return (err);
64 }
65
66 static void oscap_err_free(struct oscap_err_t *err)
67 {
68 if (err->desc != NULL)
69 free(err->desc);
70 free(err);
71 }
72
73 static inline void _push_err(struct oscap_err_t *err)
74 {
75 #ifdef OSCAP_THREAD_SAFE
76 struct err_queue *q = pthread_getspecific(__key);
77 #endif
78 if (q == NULL) {
79 q = err_queue_new();
80 assert(q != NULL);
81 #ifdef OSCAP_THREAD_SAFE
82 (void)pthread_setspecific(__key, q);
83 #endif
84 }
85 (void)err_queue_push(q, err);
86 }
87
88 void __oscap_setxmlerr(const char *file, uint32_t line, const char *func, const xmlError *error)
89 {
90
91 if (error == NULL)
92 return;
93
94 struct oscap_err_t *err;
95 #ifdef OSCAP_THREAD_SAFE
96 (void)pthread_once(&__once, oscap_errkey_init);
97 #endif
98
99 /* get rid of a newline that was pass by xmlGetLastError() */
100 int len = strlen(error->message);
101 if (len != 0 && error->message[len - 1] == '\n')
102 error->message[len-1] = 0;
103 if (error->file != NULL) {
104 char *msg = oscap_sprintf("%s [%s:%d]", error->message, error->file, error->line);
105 if (msg != NULL) {
106 err = oscap_err_new(OSCAP_EFAMILY_XML, msg, func, line, file);
107 free(msg);
108 _push_err(err);
109 return;
110 }
111 }
112
113 err = oscap_err_new(OSCAP_EFAMILY_XML, error->message, func, line, file);
114 _push_err(err);
115 }
116
117 void __oscap_seterr(const char *file, uint32_t line, const char *func, oscap_errfamily_t family, const char *fmt, ...)
118 {
119 struct oscap_err_t *err;
120 char *msg;
121 va_list ap;
122
123 #ifdef OSCAP_THREAD_SAFE
124 (void)pthread_once(&__once, oscap_errkey_init);
125 #endif
126
127 va_start(ap, fmt);
128 msg = oscap_vsprintf(fmt, ap);
129 va_end(ap);
130
131 err = oscap_err_new(family, msg, func, line, file);
132
133 free(msg);
134 _push_err(err);
135 }
136
137 void oscap_clearerr(void)
138 {
139 #ifdef OSCAP_THREAD_SAFE
140 struct err_queue *q;
141
142 (void)pthread_once(&__once, oscap_errkey_init);
143
144 q = pthread_getspecific(__key);
145 (void)pthread_setspecific(__key, NULL);
146 #endif
147 err_queue_free(q, (oscap_destruct_func) oscap_err_free);
148 }
149
150 bool oscap_err(void)
151 {
152 #ifdef OSCAP_THREAD_SAFE
153 (void)pthread_once(&__once, oscap_errkey_init);
154 struct err_queue *q = pthread_getspecific(__key);
155 #endif
156 return (q != NULL && !err_queue_is_empty(q));
157 }
158
159 oscap_errfamily_t oscap_err_family(void)
160 {
161 #ifdef OSCAP_THREAD_SAFE
162 struct err_queue *q;
163
164 (void)pthread_once(&__once, oscap_errkey_init);
165 q = pthread_getspecific(__key);
166 #endif
167
168 if (q == NULL || err_queue_is_empty(q))
169 return 0;
170 return err_queue_get_last(q)->family;
171 }
172
173 const char *oscap_err_desc(void)
174 {
175 #ifdef OSCAP_THREAD_SAFE
176 struct err_queue *q;
177
178 (void)pthread_once(&__once, oscap_errkey_init);
179 q = pthread_getspecific(__key);
180 #endif
181
182 if (q == NULL || err_queue_is_empty(q))
183 return 0;
184 return (const char *) err_queue_get_last(q)->desc;
185 }
186
187 char *oscap_err_get_full_error(void)
188 {
189 #ifdef OSCAP_THREAD_SAFE
190 struct err_queue *q;
191 (void)pthread_once(&__once, oscap_errkey_init);
192 q = pthread_getspecific(__key);
193 #endif
194
195 char *res = NULL;
196 if (q == NULL || err_queue_is_empty(q))
197 return NULL;
198 if (err_queue_to_string(q, &res) != 0)
199 return NULL;
200
201 #ifdef OSCAP_THREAD_SAFE
202 (void)pthread_setspecific(__key, NULL);
203 #endif
204 err_queue_free(q, (oscap_destruct_func) oscap_err_free);
205 return res;
206 }