unit-tests: Add a simple thread_cancel() test
[strongswan.git] / src / libstrongswan / library.c
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "library.h"
18
19 #include <stdlib.h>
20
21 #include <utils/debug.h>
22 #include <threading/thread.h>
23 #include <utils/identification.h>
24 #include <networking/host.h>
25 #include <collections/hashtable.h>
26 #include <utils/backtrace.h>
27 #include <selectors/traffic_selector.h>
28
29 #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
30
31 typedef struct private_library_t private_library_t;
32
33 /**
34 * private data of library
35 */
36 struct private_library_t {
37
38 /**
39 * public functions
40 */
41 library_t public;
42
43 /**
44 * Hashtable with registered objects (name => object)
45 */
46 hashtable_t *objects;
47
48 /**
49 * Integrity check failed?
50 */
51 bool integrity_failed;
52
53 /**
54 * Number of times we have been initialized
55 */
56 refcount_t ref;
57 };
58
59 /**
60 * library instance
61 */
62 library_t *lib = NULL;
63
64 #ifdef LEAK_DETECTIVE
65 /**
66 * Default leak report callback
67 */
68 static void report_leaks(void *user, int count, size_t bytes,
69 backtrace_t *bt, bool detailed)
70 {
71 fprintf(stderr, "%zu bytes total, %d allocations, %zu bytes average:\n",
72 bytes, count, bytes / count);
73 bt->log(bt, stderr, detailed);
74 }
75
76 /**
77 * Default leak report summary callback
78 */
79 static void sum_leaks(void* user, int count, size_t bytes, int whitelisted)
80 {
81 switch (count)
82 {
83 case 0:
84 fprintf(stderr, "No leaks detected");
85 break;
86 case 1:
87 fprintf(stderr, "One leak detected");
88 break;
89 default:
90 fprintf(stderr, "%d leaks detected, %zu bytes", count, bytes);
91 break;
92 }
93 fprintf(stderr, ", %d suppressed by whitelist\n", whitelisted);
94 }
95 #endif /* LEAK_DETECTIVE */
96
97 /**
98 * Deinitialize library
99 */
100 void library_deinit()
101 {
102 private_library_t *this = (private_library_t*)lib;
103 bool detailed;
104
105 if (!this || !ref_put(&this->ref))
106 { /* have more users */
107 return;
108 }
109
110 detailed = lib->settings->get_bool(lib->settings,
111 "libstrongswan.leak_detective.detailed", TRUE);
112
113 /* make sure the cache is clear before unloading plugins */
114 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
115
116 this->public.streams->destroy(this->public.streams);
117 this->public.watcher->destroy(this->public.watcher);
118 this->public.scheduler->destroy(this->public.scheduler);
119 this->public.processor->destroy(this->public.processor);
120 this->public.plugins->destroy(this->public.plugins);
121 this->public.hosts->destroy(this->public.hosts);
122 this->public.settings->destroy(this->public.settings);
123 this->public.credmgr->destroy(this->public.credmgr);
124 this->public.creds->destroy(this->public.creds);
125 this->public.encoding->destroy(this->public.encoding);
126 this->public.crypto->destroy(this->public.crypto);
127 this->public.caps->destroy(this->public.caps);
128 this->public.proposal->destroy(this->public.proposal);
129 this->public.fetcher->destroy(this->public.fetcher);
130 this->public.resolver->destroy(this->public.resolver);
131 this->public.db->destroy(this->public.db);
132 this->public.printf_hook->destroy(this->public.printf_hook);
133 this->objects->destroy(this->objects);
134 if (this->public.integrity)
135 {
136 this->public.integrity->destroy(this->public.integrity);
137 }
138
139 if (lib->leak_detective)
140 {
141 lib->leak_detective->report(lib->leak_detective, detailed);
142 lib->leak_detective->destroy(lib->leak_detective);
143 }
144
145 threads_deinit();
146 backtrace_deinit();
147
148 free(this);
149 lib = NULL;
150 }
151
152 METHOD(library_t, get, void*,
153 private_library_t *this, char *name)
154 {
155 return this->objects->get(this->objects, name);
156 }
157
158 METHOD(library_t, set, bool,
159 private_library_t *this, char *name, void *object)
160 {
161 if (object)
162 {
163 if (this->objects->get(this->objects, name))
164 {
165 return FALSE;
166 }
167 this->objects->put(this->objects, name, object);
168 return TRUE;
169 }
170 return this->objects->remove(this->objects, name) != NULL;
171 }
172
173 /**
174 * Hashtable hash function
175 */
176 static u_int hash(char *key)
177 {
178 return chunk_hash(chunk_create(key, strlen(key)));
179 }
180
181 /**
182 * Hashtable equals function
183 */
184 static bool equals(char *a, char *b)
185 {
186 return streq(a, b);
187 }
188
189 /**
190 * Number of words we write and memwipe() in memwipe check
191 */
192 #define MEMWIPE_WIPE_WORDS 16
193
194 /**
195 * Write magic to memory, and try to clear it with memwipe()
196 */
197 __attribute__((noinline))
198 static void do_magic(int *magic, int **out)
199 {
200 int buf[MEMWIPE_WIPE_WORDS], i;
201
202 *out = buf;
203 for (i = 0; i < countof(buf); i++)
204 {
205 buf[i] = *magic;
206 }
207 /* passing buf to dbg should make sure the compiler can't optimize out buf.
208 * we use directly dbg(3), as DBG3() might be stripped with DEBUG_LEVEL. */
209 dbg(DBG_LIB, 3, "memwipe() pre: %b", buf, sizeof(buf));
210 memwipe(buf, sizeof(buf));
211 }
212
213 /**
214 * Check if memwipe works as expected
215 */
216 static bool check_memwipe()
217 {
218 int magic = 0xCAFEBABE, *buf, i;
219
220 do_magic(&magic, &buf);
221
222 for (i = 0; i < MEMWIPE_WIPE_WORDS; i++)
223 {
224 if (buf[i] == magic)
225 {
226 DBG1(DBG_LIB, "memwipe() check failed: stackdir: %b",
227 buf, MEMWIPE_WIPE_WORDS * sizeof(int));
228 return FALSE;
229 }
230 }
231 return TRUE;
232 }
233
234 /*
235 * see header file
236 */
237 bool library_init(char *settings)
238 {
239 private_library_t *this;
240 printf_hook_t *pfh;
241
242 if (lib)
243 { /* already initialized, increase refcount */
244 this = (private_library_t*)lib;
245 ref_get(&this->ref);
246 return !this->integrity_failed;
247 }
248
249 INIT(this,
250 .public = {
251 .get = _get,
252 .set = _set,
253 },
254 .ref = 1,
255 );
256 lib = &this->public;
257
258 backtrace_init();
259 threads_init();
260
261 #ifdef LEAK_DETECTIVE
262 lib->leak_detective = leak_detective_create();
263 lib->leak_detective->set_report_cb(lib->leak_detective,
264 report_leaks, sum_leaks, NULL);
265 #endif /* LEAK_DETECTIVE */
266
267 pfh = printf_hook_create();
268 this->public.printf_hook = pfh;
269
270 pfh->add_handler(pfh, 'b', mem_printf_hook,
271 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
272 PRINTF_HOOK_ARGTYPE_END);
273 pfh->add_handler(pfh, 'B', chunk_printf_hook,
274 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
275 pfh->add_handler(pfh, 'H', host_printf_hook,
276 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
277 pfh->add_handler(pfh, 'N', enum_printf_hook,
278 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
279 PRINTF_HOOK_ARGTYPE_END);
280 pfh->add_handler(pfh, 'T', time_printf_hook,
281 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
282 PRINTF_HOOK_ARGTYPE_END);
283 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
284 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
285 PRINTF_HOOK_ARGTYPE_END);
286 pfh->add_handler(pfh, 'Y', identification_printf_hook,
287 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
288 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
289 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
290
291 this->objects = hashtable_create((hashtable_hash_t)hash,
292 (hashtable_equals_t)equals, 4);
293 this->public.settings = settings_create(settings);
294 this->public.hosts = host_resolver_create();
295 this->public.proposal = proposal_keywords_create();
296 this->public.caps = capabilities_create();
297 this->public.crypto = crypto_factory_create();
298 this->public.creds = credential_factory_create();
299 this->public.credmgr = credential_manager_create();
300 this->public.encoding = cred_encoding_create();
301 this->public.fetcher = fetcher_manager_create();
302 this->public.resolver = resolver_manager_create();
303 this->public.db = database_factory_create();
304 this->public.processor = processor_create();
305 this->public.scheduler = scheduler_create();
306 this->public.watcher = watcher_create();
307 this->public.streams = stream_manager_create();
308 this->public.plugins = plugin_loader_create();
309
310 if (!check_memwipe())
311 {
312 return FALSE;
313 }
314
315 if (lib->settings->get_bool(lib->settings,
316 "libstrongswan.integrity_test", FALSE))
317 {
318 #ifdef INTEGRITY_TEST
319 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
320 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
321 {
322 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
323 this->integrity_failed = TRUE;
324 }
325 #else /* !INTEGRITY_TEST */
326 DBG1(DBG_LIB, "integrity test enabled, but not supported");
327 this->integrity_failed = TRUE;
328 #endif /* INTEGRITY_TEST */
329 }
330
331 return !this->integrity_failed;
332 }