Use the vararg list constructor in quick mode task
[strongswan.git] / src / libcharon / daemon.c
1 /*
2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <time.h>
23
24 #include "daemon.h"
25
26 #include <library.h>
27 #include <plugins/plugin_feature.h>
28 #include <config/proposal.h>
29 #include <kernel/kernel_handler.h>
30 #include <processing/jobs/start_action_job.h>
31
32 #ifndef CAP_NET_ADMIN
33 #define CAP_NET_ADMIN 12
34 #endif
35
36 typedef struct private_daemon_t private_daemon_t;
37
38 /**
39 * Private additions to daemon_t, contains threads and internal functions.
40 */
41 struct private_daemon_t {
42 /**
43 * Public members of daemon_t.
44 */
45 daemon_t public;
46
47 /**
48 * Handler for kernel events
49 */
50 kernel_handler_t *kernel_handler;
51 };
52
53 /**
54 * One and only instance of the daemon.
55 */
56 daemon_t *charon;
57
58 /**
59 * hook in library for debugging messages
60 */
61 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
62
63 /**
64 * we store the previous debug function so we can reset it
65 */
66 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
67
68 /**
69 * Logging hook for library logs, spreads debug message over bus
70 */
71 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
72 {
73 va_list args;
74
75 va_start(args, fmt);
76 charon->bus->vlog(charon->bus, group, level, fmt, args);
77 va_end(args);
78 }
79
80 /**
81 * Clean up all daemon resources
82 */
83 static void destroy(private_daemon_t *this)
84 {
85 /* terminate all idle threads */
86 lib->processor->set_threads(lib->processor, 0);
87
88 /* close all IKE_SAs */
89 if (this->public.ike_sa_manager)
90 {
91 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
92 }
93 if (this->public.traps)
94 {
95 this->public.traps->flush(this->public.traps);
96 }
97 if (this->public.sender)
98 {
99 this->public.sender->flush(this->public.sender);
100 }
101
102 /* cancel all threads and wait for their termination */
103 lib->processor->cancel(lib->processor);
104
105 #ifdef ME
106 DESTROY_IF(this->public.connect_manager);
107 DESTROY_IF(this->public.mediation_manager);
108 #endif /* ME */
109 /* make sure the cache is clear before unloading plugins */
110 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
111 lib->plugins->unload(lib->plugins);
112 DESTROY_IF(this->kernel_handler);
113 DESTROY_IF(this->public.traps);
114 DESTROY_IF(this->public.shunts);
115 DESTROY_IF(this->public.ike_sa_manager);
116 DESTROY_IF(this->public.controller);
117 DESTROY_IF(this->public.eap);
118 DESTROY_IF(this->public.xauth);
119 DESTROY_IF(this->public.backends);
120 DESTROY_IF(this->public.socket);
121 DESTROY_IF(this->public.caps);
122
123 /* rehook library logging, shutdown logging */
124 dbg = dbg_old;
125 DESTROY_IF(this->public.bus);
126 this->public.file_loggers->destroy_offset(this->public.file_loggers,
127 offsetof(file_logger_t, destroy));
128 this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
129 offsetof(sys_logger_t, destroy));
130 free((void*)this->public.name);
131 free(this);
132 }
133
134 METHOD(daemon_t, start, void,
135 private_daemon_t *this)
136 {
137 /* start the engine, go multithreaded */
138 lib->processor->set_threads(lib->processor,
139 lib->settings->get_int(lib->settings, "%s.threads",
140 DEFAULT_THREADS, charon->name));
141 }
142
143
144 /**
145 * Initialize/deinitialize sender and receiver
146 */
147 static bool sender_receiver_cb(void *plugin, plugin_feature_t *feature,
148 bool reg, private_daemon_t *this)
149 {
150 if (reg)
151 {
152 this->public.receiver = receiver_create();
153 if (!this->public.receiver)
154 {
155 return FALSE;
156 }
157 this->public.sender = sender_create();
158 }
159 else
160 {
161 DESTROY_IF(this->public.receiver);
162 DESTROY_IF(this->public.sender);
163 }
164 return TRUE;
165 }
166
167 METHOD(daemon_t, initialize, bool,
168 private_daemon_t *this, char *plugins)
169 {
170 plugin_feature_t features[] = {
171 PLUGIN_PROVIDE(CUSTOM, "libcharon"),
172 PLUGIN_DEPENDS(NONCE_GEN),
173 PLUGIN_DEPENDS(CUSTOM, "libcharon-receiver"),
174 PLUGIN_DEPENDS(CUSTOM, "kernel-ipsec"),
175 PLUGIN_DEPENDS(CUSTOM, "kernel-net"),
176 PLUGIN_CALLBACK((plugin_feature_callback_t)sender_receiver_cb, this),
177 PLUGIN_PROVIDE(CUSTOM, "libcharon-receiver"),
178 PLUGIN_DEPENDS(HASHER, HASH_SHA1),
179 PLUGIN_DEPENDS(RNG, RNG_STRONG),
180 PLUGIN_DEPENDS(CUSTOM, "socket"),
181 };
182 lib->plugins->add_static_features(lib->plugins, charon->name, features,
183 countof(features), TRUE);
184
185 /* load plugins, further infrastructure may need it */
186 if (!lib->plugins->load(lib->plugins, NULL, plugins))
187 {
188 return FALSE;
189 }
190 DBG1(DBG_DMN, "loaded plugins: %s",
191 lib->plugins->loaded_plugins(lib->plugins));
192
193 this->public.ike_sa_manager = ike_sa_manager_create();
194 if (this->public.ike_sa_manager == NULL)
195 {
196 return FALSE;
197 }
198
199 /* Queue start_action job */
200 lib->processor->queue_job(lib->processor, (job_t*)start_action_job_create());
201
202 #ifdef ME
203 this->public.connect_manager = connect_manager_create();
204 if (this->public.connect_manager == NULL)
205 {
206 return FALSE;
207 }
208 this->public.mediation_manager = mediation_manager_create();
209 #endif /* ME */
210
211 return TRUE;
212 }
213
214 /**
215 * Create the daemon.
216 */
217 private_daemon_t *daemon_create(const char *name)
218 {
219 private_daemon_t *this;
220
221 INIT(this,
222 .public = {
223 .initialize = _initialize,
224 .start = _start,
225 .bus = bus_create(),
226 .file_loggers = linked_list_create(),
227 .sys_loggers = linked_list_create(),
228 .name = strdup(name ?: "libcharon"),
229 },
230 );
231 charon = &this->public;
232 this->public.caps = capabilities_create();
233 this->public.controller = controller_create();
234 this->public.eap = eap_manager_create();
235 this->public.xauth = xauth_manager_create();
236 this->public.backends = backend_manager_create();
237 this->public.socket = socket_manager_create();
238 this->public.traps = trap_manager_create();
239 this->public.shunts = shunt_manager_create();
240 this->kernel_handler = kernel_handler_create();
241
242 this->public.caps->keep(this->public.caps, CAP_NET_ADMIN);
243
244 return this;
245 }
246
247 /**
248 * Described in header.
249 */
250 void libcharon_deinit()
251 {
252 destroy((private_daemon_t*)charon);
253 charon = NULL;
254 }
255
256 /**
257 * Described in header.
258 */
259 bool libcharon_init(const char *name)
260 {
261 daemon_create(name);
262
263 /* for uncritical pseudo random numbers */
264 srandom(time(NULL) + getpid());
265
266 /* set up hook to log dbg message in library via charons message bus */
267 dbg_old = dbg;
268 dbg = dbg_bus;
269
270 lib->printf_hook->add_handler(lib->printf_hook, 'P',
271 proposal_printf_hook,
272 PRINTF_HOOK_ARGTYPE_POINTER,
273 PRINTF_HOOK_ARGTYPE_END);
274
275 if (lib->integrity &&
276 !lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
277 {
278 dbg(DBG_DMN, 1, "integrity check of libcharon failed");
279 return FALSE;
280 }
281
282 return TRUE;
283 }