- first attempt for connection loading and starting via "stroke"
[strongswan.git] / Source / charon / daemon.c
1 /**
2 * @file daemon.c
3 *
4 * @brief Implementation of daemon_t and main of IKEv2-Daemon.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdio.h>
24 #include <signal.h>
25 #include <pthread.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include "daemon.h"
31
32 #include <types.h>
33 #include <utils/allocator.h>
34 #include <queues/jobs/initiate_ike_sa_job.h>
35 #include <config/static_configuration.h>
36 #include <config/stroke_configuration.h>
37
38
39 typedef struct private_daemon_t private_daemon_t;
40
41 /**
42 * Private additions to daemon_t, contains threads and internal functions.
43 */
44 struct private_daemon_t {
45 /**
46 * Public members of daemon_t.
47 */
48 daemon_t public;
49
50 /**
51 * A logger_t object assigned for daemon things.
52 */
53 logger_t *logger;
54
55 /**
56 * Signal set used for signal handling.
57 */
58 sigset_t signal_set;
59
60 /**
61 * The thread_id of main-thread.
62 */
63 pthread_t main_thread_id;
64
65 /**
66 * Main loop function.
67 *
68 * @param this calling object
69 */
70 void (*run) (private_daemon_t *this);
71
72 /**
73 * A routine to add job for testing.
74 *
75 * @param this calling object
76 * @param configuration_name name of configuration to use for initialization
77 */
78 void (*build_test_job) (private_daemon_t *this,char *configuration_name);
79
80 /**
81 * Initialize the daemon.
82 *
83 * @param this calling object
84 */
85 void (*initialize) (private_daemon_t *this);
86
87 /**
88 * Destroy the daemon.
89 *
90 * @param this calling object
91 */
92 void (*destroy) (private_daemon_t *this);
93 };
94
95 /**
96 * One and only instance of the daemon.
97 */
98 daemon_t *charon;
99
100 /**
101 * Implementation of private_daemon_t.run.
102 */
103 static void run(private_daemon_t *this)
104 {
105 while(TRUE)
106 {
107 int signal_number;
108 int error;
109
110 error = sigwait(&(this->signal_set), &signal_number);
111 if(error)
112 {
113 this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
114 return;
115 }
116 switch (signal_number)
117 {
118 case SIGHUP:
119 {
120 this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
121 break;
122 }
123 case SIGINT:
124 {
125 this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
126 return;
127 }
128 case SIGTERM:
129 this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
130 return;
131 default:
132 {
133 this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
134 break;
135 }
136 }
137 }
138 }
139
140 /**
141 * Implementation of daemon_t.kill.
142 */
143 static void kill_daemon(private_daemon_t *this, char *reason)
144 {
145 /* we send SIGTERM, so the daemon can cleanly shut down */
146 this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
147 if (this->main_thread_id == pthread_self())
148 {
149 /* initialization failed, terminate daemon */
150 this->destroy(this);
151 unlink(PID_FILE);
152 exit(-1);
153 }
154 else
155 {
156 this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
157 kill(0, SIGTERM);
158 /* thread must die, since he produced a ciritcal failure and can't continue */
159 pthread_exit(NULL);
160 }
161 }
162
163 /**
164 * Implementation of private_daemon_t.build_test_job.
165 */
166 static void build_test_job(private_daemon_t *this, char *configuration_name)
167 {
168 initiate_ike_sa_job_t *initiate_job;
169
170 /* configuration_name = "localhost-rsa"; */
171 /* configuration_name = "localhost-shared"; */
172 /* configuration_name = "localhost-bad_dh_group"; */
173
174
175 initiate_job = initiate_ike_sa_job_create(configuration_name);
176
177 this->public.event_queue->add_relative(this->public.event_queue, (job_t*)initiate_job, 2000);
178
179 }
180
181 /**
182 * Implementation of private_daemon_t.initialize.
183 */
184 static void initialize(private_daemon_t *this)
185 {
186 this->public.socket = socket_create(IKEV2_UDP_PORT);
187 this->public.ike_sa_manager = ike_sa_manager_create();
188 this->public.job_queue = job_queue_create();
189 this->public.event_queue = event_queue_create();
190 this->public.send_queue = send_queue_create();
191 this->public.configuration = (configuration_t*)stroke_configuration_create();
192
193 this->public.sender = sender_create();
194 this->public.receiver = receiver_create();
195 this->public.scheduler = scheduler_create();
196 this->public.kernel_interface = kernel_interface_create();
197 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
198 }
199
200 /**
201 * Destory all initiated objects
202 */
203 static void destroy(private_daemon_t *this)
204 {
205 if (this->public.ike_sa_manager != NULL)
206 {
207 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
208 }
209 if (this->public.kernel_interface != NULL)
210 {
211 this->public.kernel_interface->destroy(this->public.kernel_interface);
212 }
213 if (this->public.receiver != NULL)
214 {
215 this->public.receiver->destroy(this->public.receiver);
216 }
217 if (this->public.scheduler != NULL)
218 {
219 this->public.scheduler->destroy(this->public.scheduler);
220 }
221 if (this->public.sender != NULL)
222 {
223 this->public.sender->destroy(this->public.sender);
224 }
225 if (this->public.thread_pool != NULL)
226 {
227 this->public.thread_pool->destroy(this->public.thread_pool);
228 }
229 if (this->public.job_queue != NULL)
230 {
231 this->public.job_queue->destroy(this->public.job_queue);
232 }
233 if (this->public.event_queue != NULL)
234 {
235 this->public.event_queue->destroy(this->public.event_queue);
236 }
237 if (this->public.send_queue != NULL)
238 {
239 this->public.send_queue->destroy(this->public.send_queue);
240 }
241 if (this->public.socket != NULL)
242 {
243 this->public.socket->destroy(this->public.socket);
244 }
245 if (this->public.configuration != NULL)
246 {
247 this->public.configuration->destroy(this->public.configuration);
248 }
249
250 this->public.logger_manager->destroy(this->public.logger_manager);
251 allocator_free(this);
252 }
253
254
255
256 /**
257 * @brief Create the daemon.
258 *
259 * @return created daemon_t
260 */
261 private_daemon_t *daemon_create()
262 {
263 private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
264
265 /* assign methods */
266 this->run = run;
267 this->destroy = destroy;
268 this->build_test_job = build_test_job;
269 this->initialize = initialize;
270 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
271
272 /* first build a logger */
273 this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
274 this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
275
276 /* NULL members for clean destruction */
277 this->public.socket = NULL;
278 this->public.ike_sa_manager = NULL;
279 this->public.job_queue = NULL;
280 this->public.event_queue = NULL;
281 this->public.send_queue = NULL;
282 this->public.configuration = NULL;
283 this->public.sender= NULL;
284 this->public.receiver = NULL;
285 this->public.scheduler = NULL;
286 this->public.kernel_interface = NULL;
287 this->public.thread_pool = NULL;
288
289 this->main_thread_id = pthread_self();
290
291 /* setup signal handling */
292 sigemptyset(&(this->signal_set));
293 sigaddset(&(this->signal_set), SIGINT);
294 sigaddset(&(this->signal_set), SIGHUP);
295 sigaddset(&(this->signal_set), SIGTERM);
296 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
297
298 return this;
299 }
300
301 /**
302 * Main function, manages the daemon.
303 */
304 int main(int argc, char *argv[])
305 {
306 private_daemon_t *private_charon;
307 FILE *pid_file;
308 struct stat stb;
309
310 /* allocation needs initialization, before any allocs are done */
311 allocator_init();
312 private_charon = daemon_create();
313 charon = (daemon_t*)private_charon;
314
315 /* check/setup PID file */
316 if (stat(PID_FILE, &stb) == 0)
317 {
318 private_charon->logger->log(private_charon->logger, ERROR,
319 "charon already running (\""PID_FILE"\" exists)");
320 private_charon->destroy(private_charon);
321 exit(-1);
322 }
323 pid_file = fopen(PID_FILE, "w");
324 if (pid_file)
325 {
326 fprintf(pid_file, "%d\n", getpid());
327 fclose(pid_file);
328 }
329
330 /* initialize and run daemon*/
331 private_charon->initialize(private_charon);
332 if (argc == 2)
333 {
334 private_charon->build_test_job(private_charon,argv[1]);
335 }
336 private_charon->run(private_charon);
337
338 /* normal termination, cleanup and exit */
339 private_charon->destroy(private_charon);
340 unlink(PID_FILE);
341
342 #ifdef LEAK_DETECTIVE
343 report_memory_leaks(void);
344 #endif
345
346 exit(0);
347 }
348