reintegrated bus-refactoring branch
[strongswan.git] / src / charon / bus / bus.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "bus.h"
19
20 #include <pthread.h>
21
22 #include <daemon.h>
23 #include <utils/mutex.h>
24
25 ENUM(debug_names, DBG_DMN, DBG_LIB,
26 "DMN",
27 "MGR",
28 "IKE",
29 "CHD",
30 "JOB",
31 "CFG",
32 "KNL",
33 "NET",
34 "ENC",
35 "LIB",
36 );
37
38 typedef struct private_bus_t private_bus_t;
39
40 /**
41 * Private data of a bus_t object.
42 */
43 struct private_bus_t {
44 /**
45 * Public part of a bus_t object.
46 */
47 bus_t public;
48
49 /**
50 * List of registered listeners as entry_t's
51 */
52 linked_list_t *listeners;
53
54 /**
55 * mutex to synchronize active listeners, recursively
56 */
57 mutex_t *mutex;
58
59 /**
60 * Thread local storage for a unique, simple thread ID
61 */
62 pthread_key_t thread_id;
63
64 /**
65 * Thread local storage the threads IKE_SA
66 */
67 pthread_key_t thread_sa;
68 };
69
70 typedef struct entry_t entry_t;
71
72 /**
73 * a listener entry, either active or passive
74 */
75 struct entry_t {
76
77 /**
78 * registered listener interface
79 */
80 listener_t *listener;
81
82 /**
83 * is this a active listen() call with a blocking thread
84 */
85 bool blocker;
86
87 /**
88 * are we currently calling this listener
89 */
90 bool calling;
91
92 /**
93 * condvar where active listeners wait
94 */
95 condvar_t *condvar;
96 };
97
98 /**
99 * create a listener entry
100 */
101 static entry_t *entry_create(listener_t *listener, bool blocker)
102 {
103 entry_t *this = malloc_thing(entry_t);
104
105 this->listener = listener;
106 this->blocker = blocker;
107 this->calling = FALSE;
108 this->condvar = condvar_create(CONDVAR_DEFAULT);
109
110 return this;
111 }
112
113 /**
114 * destroy an entry_t
115 */
116 static void entry_destroy(entry_t *entry)
117 {
118 entry->condvar->destroy(entry->condvar);
119 free(entry);
120 }
121
122 /**
123 * Get a unique thread number for a calling thread. Since
124 * pthread_self returns large and ugly numbers, use this function
125 * for logging; these numbers are incremental starting at 1
126 */
127 static int get_thread_number(private_bus_t *this)
128 {
129 static long current_num = 0;
130 long stored_num;
131
132 stored_num = (long)pthread_getspecific(this->thread_id);
133 if (stored_num == 0)
134 { /* first call of current thread */
135 pthread_setspecific(this->thread_id, (void*)++current_num);
136 return current_num;
137 }
138 else
139 {
140 return stored_num;
141 }
142 }
143
144 /**
145 * Implementation of bus_t.add_listener.
146 */
147 static void add_listener(private_bus_t *this, listener_t *listener)
148 {
149 this->mutex->lock(this->mutex);
150 this->listeners->insert_last(this->listeners, entry_create(listener, FALSE));
151 this->mutex->unlock(this->mutex);
152 }
153
154 /**
155 * Implementation of bus_t.remove_listener.
156 */
157 static void remove_listener(private_bus_t *this, listener_t *listener)
158 {
159 enumerator_t *enumerator;
160 entry_t *entry;
161
162 this->mutex->lock(this->mutex);
163 enumerator = this->listeners->create_enumerator(this->listeners);
164 while (enumerator->enumerate(enumerator, &entry))
165 {
166 if (entry->listener == listener)
167 {
168 this->listeners->remove_at(this->listeners, enumerator);
169 entry_destroy(entry);
170 break;
171 }
172 }
173 enumerator->destroy(enumerator);
174 this->mutex->unlock(this->mutex);
175 }
176
177 typedef struct cleanup_data_t cleanup_data_t;
178
179 /**
180 * data to remove a listener using pthread_cleanup handler
181 */
182 struct cleanup_data_t {
183 /** bus instance */
184 private_bus_t *this;
185 /** listener entry */
186 entry_t *entry;
187 };
188
189 /**
190 * pthread_cleanup handler to remove a listener
191 */
192 static void listener_cleanup(cleanup_data_t *data)
193 {
194 data->this->listeners->remove(data->this->listeners, data->entry, NULL);
195 entry_destroy(data->entry);
196 }
197
198 /**
199 * Implementation of bus_t.listen.
200 */
201 static void listen_(private_bus_t *this, listener_t *listener, job_t *job)
202 {
203 int old;
204 cleanup_data_t data;
205
206 data.this = this;
207 data.entry = entry_create(listener, TRUE);
208
209 this->mutex->lock(this->mutex);
210 this->listeners->insert_last(this->listeners, data.entry);
211 charon->processor->queue_job(charon->processor, job);
212 pthread_cleanup_push((void*)this->mutex->unlock, this->mutex);
213 pthread_cleanup_push((void*)listener_cleanup, &data);
214 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
215 while (data.entry->blocker)
216 {
217 data.entry->condvar->wait(data.entry->condvar, this->mutex);
218 }
219 pthread_setcancelstate(old, NULL);
220 pthread_cleanup_pop(FALSE);
221 /* unlock mutex */
222 pthread_cleanup_pop(TRUE);
223 entry_destroy(data.entry);
224 }
225
226 /**
227 * Implementation of bus_t.set_sa.
228 */
229 static void set_sa(private_bus_t *this, ike_sa_t *ike_sa)
230 {
231 pthread_setspecific(this->thread_sa, ike_sa);
232 }
233
234 /**
235 * data associated to a signal, passed to callback
236 */
237 typedef struct {
238 /** associated IKE_SA */
239 ike_sa_t *ike_sa;
240 /** invoking thread */
241 long thread;
242 /** debug group */
243 debug_t group;
244 /** debug level */
245 level_t level;
246 /** format string */
247 char *format;
248 /** argument list */
249 va_list args;
250 } log_data_t;
251
252 /**
253 * listener->log() invocation as a list remove callback
254 */
255 static bool log_cb(entry_t *entry, log_data_t *data)
256 {
257 va_list args;
258
259 if (entry->calling || !entry->listener->log)
260 { /* avoid recursive calls */
261 return FALSE;
262 }
263 entry->calling = TRUE;
264 va_copy(args, data->args);
265 if (!entry->listener->log(entry->listener, data->group, data->level,
266 data->thread, data->ike_sa, data->format, args))
267 {
268 if (entry->blocker)
269 {
270 entry->blocker = FALSE;
271 entry->condvar->signal(entry->condvar);
272 }
273 else
274 {
275 entry_destroy(entry);
276 }
277 va_end(args);
278 entry->calling = FALSE;
279 return TRUE;
280 }
281 va_end(args);
282 entry->calling = FALSE;
283 return FALSE;
284 }
285
286 /**
287 * Implementation of bus_t.vlog.
288 */
289 static void vlog(private_bus_t *this, debug_t group, level_t level,
290 char* format, va_list args)
291 {
292 log_data_t data;
293
294 data.ike_sa = pthread_getspecific(this->thread_sa);
295 data.thread = get_thread_number(this);
296 data.group = group;
297 data.level = level;
298 data.format = format;
299 va_copy(data.args, args);
300
301 this->mutex->lock(this->mutex);
302 /* We use the remove() method to invoke all listeners. This is cheap and
303 * does not require an allocation for this performance critical function. */
304 this->listeners->remove(this->listeners, &data, (void*)log_cb);
305 this->mutex->unlock(this->mutex);
306
307 va_end(data.args);
308 }
309
310 /**
311 * Implementation of bus_t.log.
312 */
313 static void log_(private_bus_t *this, debug_t group, level_t level,
314 char* format, ...)
315 {
316 va_list args;
317
318 va_start(args, format);
319 vlog(this, group, level, format, args);
320 va_end(args);
321 }
322
323 /**
324 * Implementation of bus_t.ike_state_change
325 */
326 static void ike_state_change(private_bus_t *this, ike_sa_t *ike_sa,
327 ike_sa_state_t state)
328 {
329 enumerator_t *enumerator;
330 entry_t *entry;
331
332 this->mutex->lock(this->mutex);
333 enumerator = this->listeners->create_enumerator(this->listeners);
334 while (enumerator->enumerate(enumerator, &entry))
335 {
336 if (entry->listener->ike_state_change &&
337 !entry->listener->ike_state_change(entry->listener, ike_sa, state))
338 {
339 if (entry->blocker)
340 {
341 entry->blocker = FALSE;
342 entry->condvar->signal(entry->condvar);
343 }
344 else
345 {
346 entry_destroy(entry);
347 }
348 this->listeners->remove_at(this->listeners, enumerator);
349 break;
350 }
351 }
352 enumerator->destroy(enumerator);
353 this->mutex->unlock(this->mutex);
354 }
355
356 /**
357 * Implementation of bus_t.child_state_change
358 */
359 static void child_state_change(private_bus_t *this, child_sa_t *child_sa,
360 child_sa_state_t state)
361 {
362 enumerator_t *enumerator;
363 ike_sa_t *ike_sa;
364 entry_t *entry;
365
366 ike_sa = pthread_getspecific(this->thread_sa);
367
368 this->mutex->lock(this->mutex);
369 enumerator = this->listeners->create_enumerator(this->listeners);
370 while (enumerator->enumerate(enumerator, &entry))
371 {
372 if (entry->listener->child_state_change &&
373 !entry->listener->child_state_change(entry->listener, ike_sa,
374 child_sa, state))
375 {
376 if (entry->blocker)
377 {
378 entry->blocker = FALSE;
379 entry->condvar->signal(entry->condvar);
380 }
381 else
382 {
383 entry_destroy(entry);
384 }
385 this->listeners->remove_at(this->listeners, enumerator);
386 break;
387 }
388 }
389 enumerator->destroy(enumerator);
390 this->mutex->unlock(this->mutex);
391 }
392
393 /**
394 * Implementation of bus_t.message
395 */
396 static void message(private_bus_t *this, message_t *message, bool incoming)
397 {
398 enumerator_t *enumerator;
399 ike_sa_t *ike_sa;
400 entry_t *entry;
401
402 ike_sa = pthread_getspecific(this->thread_sa);
403
404 this->mutex->lock(this->mutex);
405 enumerator = this->listeners->create_enumerator(this->listeners);
406 while (enumerator->enumerate(enumerator, &entry))
407 {
408 if (entry->listener->message &&
409 !entry->listener->message(entry->listener, ike_sa, message, incoming))
410 {
411 if (entry->blocker)
412 {
413 entry->blocker = FALSE;
414 entry->condvar->signal(entry->condvar);
415 }
416 else
417 {
418 entry_destroy(entry);
419 }
420 this->listeners->remove_at(this->listeners, enumerator);
421 break;
422 }
423 }
424 enumerator->destroy(enumerator);
425 this->mutex->unlock(this->mutex);
426 }
427
428 /**
429 * Implementation of bus_t.destroy.
430 */
431 static void destroy(private_bus_t *this)
432 {
433 this->mutex->destroy(this->mutex);
434 this->listeners->destroy_function(this->listeners, (void*)entry_destroy);
435 free(this);
436 }
437
438 /*
439 * Described in header.
440 */
441 bus_t *bus_create()
442 {
443 private_bus_t *this = malloc_thing(private_bus_t);
444
445 this->public.add_listener = (void(*)(bus_t*,listener_t*))add_listener;
446 this->public.remove_listener = (void(*)(bus_t*,listener_t*))remove_listener;
447 this->public.listen = (void(*)(bus_t*, listener_t *listener, job_t *job))listen_;
448 this->public.set_sa = (void(*)(bus_t*,ike_sa_t*))set_sa;
449 this->public.log = (void(*)(bus_t*,debug_t,level_t,char*,...))log_;
450 this->public.vlog = (void(*)(bus_t*,debug_t,level_t,char*,va_list))vlog;
451 this->public.ike_state_change = (void(*)(bus_t*,ike_sa_t*,ike_sa_state_t))ike_state_change;
452 this->public.child_state_change = (void(*)(bus_t*,child_sa_t*,child_sa_state_t))child_state_change;
453 this->public.message = (void(*)(bus_t*, message_t *message, bool incoming))message;
454 this->public.destroy = (void(*)(bus_t*)) destroy;
455
456 this->listeners = linked_list_create();
457 this->mutex = mutex_create(MUTEX_DEFAULT);
458 pthread_key_create(&this->thread_id, NULL);
459 pthread_key_create(&this->thread_sa, NULL);
460
461 return &this->public;
462 }
463