moved attribute_manager to libstrongswan
[strongswan.git] / src / charon / daemon.c
1 /*
2 * Copyright (C) 2006-2009 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 #ifdef HAVE_PRCTL
21 #include <sys/prctl.h>
22 #endif
23 #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
24 #include <signal.h>
25 #undef _POSIX_PTHREAD_SEMANTICS
26 #include <pthread.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <errno.h>
34 #include <pwd.h>
35 #include <grp.h>
36 #ifdef CAPABILITIES
37 #include <sys/capability.h>
38 #endif /* CAPABILITIES */
39
40 #include "daemon.h"
41
42 #include <library.h>
43 #include <utils/backtrace.h>
44 #include <config/traffic_selector.h>
45 #include <config/proposal.h>
46
47 #ifndef LOG_AUTHPRIV /* not defined on OpenSolaris */
48 #define LOG_AUTHPRIV LOG_AUTH
49 #endif
50
51 typedef struct private_daemon_t private_daemon_t;
52
53 /**
54 * Private additions to daemon_t, contains threads and internal functions.
55 */
56 struct private_daemon_t {
57 /**
58 * Public members of daemon_t.
59 */
60 daemon_t public;
61
62 /**
63 * Signal set used for signal handling.
64 */
65 sigset_t signal_set;
66
67 #ifdef CAPABILITIES
68 /**
69 * capabilities to keep
70 */
71 cap_t caps;
72 #endif /* CAPABILITIES */
73 };
74
75 /**
76 * One and only instance of the daemon.
77 */
78 daemon_t *charon;
79
80 /**
81 * hook in library for debugging messages
82 */
83 extern void (*dbg) (int level, char *fmt, ...);
84
85 /**
86 * Logging hook for library logs, spreads debug message over bus
87 */
88 static void dbg_bus(int level, char *fmt, ...)
89 {
90 va_list args;
91
92 va_start(args, fmt);
93 charon->bus->vlog(charon->bus, DBG_LIB, level, fmt, args);
94 va_end(args);
95 }
96
97 /**
98 * Logging hook for library logs, using stderr output
99 */
100 static void dbg_stderr(int level, char *fmt, ...)
101 {
102 va_list args;
103
104 if (level <= 1)
105 {
106 va_start(args, fmt);
107 fprintf(stderr, "00[LIB] ");
108 vfprintf(stderr, fmt, args);
109 fprintf(stderr, "\n");
110 va_end(args);
111 }
112 }
113
114 /**
115 * Run the daemon and handle unix signals
116 */
117 static void run(private_daemon_t *this)
118 {
119 sigset_t set;
120
121 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
122 sigemptyset(&set);
123 sigaddset(&set, SIGINT);
124 sigaddset(&set, SIGHUP);
125 sigaddset(&set, SIGTERM);
126
127 while (TRUE)
128 {
129 int sig;
130 int error;
131
132 error = sigwait(&set, &sig);
133 if (error)
134 {
135 DBG1(DBG_DMN, "error %d while waiting for a signal", error);
136 return;
137 }
138 switch (sig)
139 {
140 case SIGHUP:
141 {
142 DBG1(DBG_DMN, "signal of type SIGHUP received. Ignored");
143 break;
144 }
145 case SIGINT:
146 {
147 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
148 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
149 return;
150 }
151 case SIGTERM:
152 {
153 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
154 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
155 return;
156 }
157 default:
158 {
159 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
160 break;
161 }
162 }
163 }
164 }
165
166 /**
167 * Clean up all daemon resources
168 */
169 static void destroy(private_daemon_t *this)
170 {
171 /* terminate all idle threads */
172 if (this->public.processor)
173 {
174 this->public.processor->set_threads(this->public.processor, 0);
175 }
176 /* close all IKE_SAs */
177 if (this->public.ike_sa_manager)
178 {
179 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
180 }
181 /* unload plugins to release threads */
182 lib->plugins->unload(lib->plugins);
183 #ifdef CAPABILITIES
184 cap_free(this->caps);
185 #endif /* CAPABILITIES */
186 DESTROY_IF(this->public.traps);
187 DESTROY_IF(this->public.ike_sa_manager);
188 DESTROY_IF(this->public.kernel_interface);
189 DESTROY_IF(this->public.scheduler);
190 DESTROY_IF(this->public.controller);
191 DESTROY_IF(this->public.eap);
192 DESTROY_IF(this->public.sim);
193 #ifdef ME
194 DESTROY_IF(this->public.connect_manager);
195 DESTROY_IF(this->public.mediation_manager);
196 #endif /* ME */
197 DESTROY_IF(this->public.backends);
198 DESTROY_IF(this->public.credentials);
199 DESTROY_IF(this->public.sender);
200 DESTROY_IF(this->public.receiver);
201 DESTROY_IF(this->public.socket);
202 /* wait until all threads are gone */
203 DESTROY_IF(this->public.processor);
204
205 /* rehook library logging, shutdown logging */
206 dbg = dbg_stderr;
207 DESTROY_IF(this->public.bus);
208 this->public.file_loggers->destroy_offset(this->public.file_loggers,
209 offsetof(file_logger_t, destroy));
210 this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
211 offsetof(sys_logger_t, destroy));
212 free(this);
213 }
214
215 /**
216 * Enforce daemon shutdown, with a given reason to do so.
217 */
218 static void kill_daemon(private_daemon_t *this, char *reason)
219 {
220 /* we send SIGTERM, so the daemon can cleanly shut down */
221 if (this->public.bus)
222 {
223 DBG1(DBG_DMN, "killing daemon: %s", reason);
224 }
225 else
226 {
227 fprintf(stderr, "killing daemon: %s\n", reason);
228 }
229 if (this->public.main_thread_id == pthread_self())
230 {
231 /* initialization failed, terminate daemon */
232 unlink(PID_FILE);
233 exit(-1);
234 }
235 else
236 {
237 DBG1(DBG_DMN, "sending SIGTERM to ourself");
238 pthread_kill(this->public.main_thread_id, SIGTERM);
239 /* thread must die, since he produced a ciritcal failure and can't continue */
240 pthread_exit(NULL);
241 }
242 }
243
244 /**
245 * drop daemon capabilities
246 */
247 static void drop_capabilities(private_daemon_t *this)
248 {
249 #ifdef HAVE_PRCTL
250 prctl(PR_SET_KEEPCAPS, 1);
251 #endif
252
253 if (setgid(charon->gid) != 0)
254 {
255 kill_daemon(this, "change to unprivileged group failed");
256 }
257 if (setuid(charon->uid) != 0)
258 {
259 kill_daemon(this, "change to unprivileged user failed");
260 }
261
262 #ifdef CAPABILITIES
263 if (cap_set_proc(this->caps) != 0)
264 {
265 kill_daemon(this, "unable to drop daemon capabilities");
266 }
267 #endif /* CAPABILITIES */
268 }
269
270 /**
271 * Implementation of daemon_t.keep_cap
272 */
273 static void keep_cap(private_daemon_t *this, u_int cap)
274 {
275 #ifdef CAPABILITIES
276 cap_set_flag(this->caps, CAP_EFFECTIVE, 1, &cap, CAP_SET);
277 cap_set_flag(this->caps, CAP_INHERITABLE, 1, &cap, CAP_SET);
278 cap_set_flag(this->caps, CAP_PERMITTED, 1, &cap, CAP_SET);
279 #endif /* CAPABILITIES */
280 }
281
282 /**
283 * lookup UID and GID
284 */
285 static void lookup_uid_gid(private_daemon_t *this)
286 {
287 #ifdef IPSEC_USER
288 {
289 char buf[1024];
290 struct passwd passwd, *pwp;
291
292 if (getpwnam_r(IPSEC_USER, &passwd, buf, sizeof(buf), &pwp) != 0 ||
293 pwp == NULL)
294 {
295 kill_daemon(this, "resolving user '"IPSEC_USER"' failed");
296 }
297 charon->uid = pwp->pw_uid;
298 }
299 #endif
300 #ifdef IPSEC_GROUP
301 {
302 char buf[1024];
303 struct group group, *grp;
304
305 if (getgrnam_r(IPSEC_GROUP, &group, buf, sizeof(buf), &grp) != 0 ||
306 grp == NULL)
307 {
308 kill_daemon(this, "resolving group '"IPSEC_GROUP"' failed");
309 }
310 charon->gid = grp->gr_gid;
311 }
312 #endif
313 }
314
315 /**
316 * Log loaded plugins
317 */
318 static void print_plugins()
319 {
320 char buf[512], *plugin;
321 int len = 0;
322 enumerator_t *enumerator;
323
324 buf[0] = '\0';
325 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
326 while (len < sizeof(buf) && enumerator->enumerate(enumerator, &plugin))
327 {
328 len += snprintf(&buf[len], sizeof(buf)-len, "%s ", plugin);
329 }
330 enumerator->destroy(enumerator);
331 DBG1(DBG_DMN, "loaded plugins: %s", buf);
332 }
333
334 /**
335 * Initialize logging
336 */
337 static void initialize_loggers(private_daemon_t *this, bool use_stderr,
338 level_t levels[])
339 {
340 sys_logger_t *sys_logger;
341 file_logger_t *file_logger;
342 enumerator_t *enumerator;
343 char *facility, *filename;
344 int loggers_defined = 0;
345 debug_t group;
346 level_t def;
347 bool append;
348 FILE *file;
349
350 /* setup sysloggers */
351 enumerator = lib->settings->create_section_enumerator(lib->settings,
352 "charon.syslog");
353 while (enumerator->enumerate(enumerator, &facility))
354 {
355 loggers_defined++;
356 if (streq(facility, "daemon"))
357 {
358 sys_logger = sys_logger_create(LOG_DAEMON);
359 }
360 else if (streq(facility, "auth"))
361 {
362 sys_logger = sys_logger_create(LOG_AUTHPRIV);
363 }
364 else
365 {
366 continue;
367 }
368 def = lib->settings->get_int(lib->settings,
369 "charon.syslog.%s.default", 1, facility);
370 for (group = 0; group < DBG_MAX; group++)
371 {
372 sys_logger->set_level(sys_logger, group,
373 lib->settings->get_int(lib->settings,
374 "charon.syslog.%s.%N", def,
375 facility, debug_lower_names, group));
376 }
377 this->public.sys_loggers->insert_last(this->public.sys_loggers,
378 sys_logger);
379 this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
380 }
381 enumerator->destroy(enumerator);
382
383 /* and file loggers */
384 enumerator = lib->settings->create_section_enumerator(lib->settings,
385 "charon.filelog");
386 while (enumerator->enumerate(enumerator, &filename))
387 {
388 loggers_defined++;
389 if (streq(filename, "stderr"))
390 {
391 file = stderr;
392 }
393 else if (streq(filename, "stdout"))
394 {
395 file = stdout;
396 }
397 else
398 {
399 append = lib->settings->get_bool(lib->settings,
400 "charon.filelog.%s.append", TRUE, filename);
401 file = fopen(filename, append ? "a" : "w");
402 if (file == NULL)
403 {
404 DBG1(DBG_DMN, "opening file %s for logging failed: %s",
405 filename, strerror(errno));
406 continue;
407 }
408 }
409 file_logger = file_logger_create(file);
410 def = lib->settings->get_int(lib->settings,
411 "charon.filelog.%s.default", 1, filename);
412 for (group = 0; group < DBG_MAX; group++)
413 {
414 file_logger->set_level(file_logger, group,
415 lib->settings->get_int(lib->settings,
416 "charon.filelog.%s.%N", def,
417 filename, debug_lower_names, group));
418 }
419 this->public.file_loggers->insert_last(this->public.file_loggers,
420 file_logger);
421 this->public.bus->add_listener(this->public.bus, &file_logger->listener);
422
423 }
424 enumerator->destroy(enumerator);
425
426 /* set up legacy style default loggers provided via command-line */
427 if (!loggers_defined)
428 {
429 /* set up default stdout file_logger */
430 file_logger = file_logger_create(stdout);
431 this->public.bus->add_listener(this->public.bus, &file_logger->listener);
432 this->public.file_loggers->insert_last(this->public.file_loggers,
433 file_logger);
434 /* set up default daemon sys_logger */
435 sys_logger = sys_logger_create(LOG_DAEMON);
436 this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
437 this->public.sys_loggers->insert_last(this->public.sys_loggers,
438 sys_logger);
439 for (group = 0; group < DBG_MAX; group++)
440 {
441 sys_logger->set_level(sys_logger, group, levels[group]);
442 if (use_stderr)
443 {
444 file_logger->set_level(file_logger, group, levels[group]);
445 }
446 }
447
448 /* set up default auth sys_logger */
449 sys_logger = sys_logger_create(LOG_AUTHPRIV);
450 this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
451 this->public.sys_loggers->insert_last(this->public.sys_loggers,
452 sys_logger);
453 sys_logger->set_level(sys_logger, DBG_ANY, LEVEL_AUDIT);
454 }
455 }
456
457 /**
458 * Initialize the daemon
459 */
460 static bool initialize(private_daemon_t *this, bool syslog, level_t levels[])
461 {
462 /* for uncritical pseudo random numbers */
463 srandom(time(NULL) + getpid());
464
465 /* setup bus and it's listeners first to enable log output */
466 this->public.bus = bus_create();
467 /* set up hook to log dbg message in library via charons message bus */
468 dbg = dbg_bus;
469
470 initialize_loggers(this, !syslog, levels);
471
472 DBG1(DBG_DMN, "Starting IKEv2 charon daemon (strongSwan "VERSION")");
473
474 if (lib->integrity)
475 {
476 DBG1(DBG_DMN, "integrity tests enabled:");
477 DBG1(DBG_DMN, "lib 'libstrongswan': passed file and segment integrity tests");
478 DBG1(DBG_DMN, "daemon 'charon': passed file integrity test");
479 }
480
481 /* load secrets, ca certificates and crls */
482 this->public.processor = processor_create();
483 this->public.scheduler = scheduler_create();
484 this->public.credentials = credential_manager_create();
485 this->public.controller = controller_create();
486 this->public.eap = eap_manager_create();
487 this->public.sim = sim_manager_create();
488 this->public.backends = backend_manager_create();
489 this->public.kernel_interface = kernel_interface_create();
490 this->public.socket = socket_create();
491 this->public.traps = trap_manager_create();
492
493 /* load plugins, further infrastructure may need it */
494 if (!lib->plugins->load(lib->plugins, NULL,
495 lib->settings->get_str(lib->settings, "charon.load", PLUGINS)))
496 {
497 return FALSE;
498 }
499
500 print_plugins();
501
502 this->public.ike_sa_manager = ike_sa_manager_create();
503 if (this->public.ike_sa_manager == NULL)
504 {
505 return FALSE;
506 }
507 this->public.sender = sender_create();
508 this->public.receiver = receiver_create();
509 if (this->public.receiver == NULL)
510 {
511 return FALSE;
512 }
513
514 #ifdef ME
515 this->public.connect_manager = connect_manager_create();
516 if (this->public.connect_manager == NULL)
517 {
518 return FALSE;
519 }
520 this->public.mediation_manager = mediation_manager_create();
521 #endif /* ME */
522
523 return TRUE;
524 }
525
526 /**
527 * Handle SIGSEGV/SIGILL signals raised by threads
528 */
529 static void segv_handler(int signal)
530 {
531 backtrace_t *backtrace;
532
533 DBG1(DBG_DMN, "thread %u received %d", pthread_self(), signal);
534 backtrace = backtrace_create(2);
535 backtrace->log(backtrace, stderr);
536 backtrace->destroy(backtrace);
537
538 DBG1(DBG_DMN, "killing ourself, received critical signal");
539 raise(SIGKILL);
540 }
541
542 /**
543 * Create the daemon.
544 */
545 private_daemon_t *daemon_create(void)
546 {
547 struct sigaction action;
548 private_daemon_t *this = malloc_thing(private_daemon_t);
549
550 /* assign methods */
551 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
552 this->public.keep_cap = (void(*)(daemon_t*, u_int cap))keep_cap;
553
554 /* NULL members for clean destruction */
555 this->public.socket = NULL;
556 this->public.ike_sa_manager = NULL;
557 this->public.traps = NULL;
558 this->public.credentials = NULL;
559 this->public.backends = NULL;
560 this->public.sender= NULL;
561 this->public.receiver = NULL;
562 this->public.scheduler = NULL;
563 this->public.kernel_interface = NULL;
564 this->public.processor = NULL;
565 this->public.controller = NULL;
566 this->public.eap = NULL;
567 this->public.sim = NULL;
568 this->public.bus = NULL;
569 this->public.file_loggers = linked_list_create();
570 this->public.sys_loggers = linked_list_create();
571 #ifdef ME
572 this->public.connect_manager = NULL;
573 this->public.mediation_manager = NULL;
574 #endif /* ME */
575 this->public.uid = 0;
576 this->public.gid = 0;
577
578 this->public.main_thread_id = pthread_self();
579 #ifdef CAPABILITIES
580 this->caps = cap_init();
581 keep_cap(this, CAP_NET_ADMIN);
582 if (lib->leak_detective)
583 {
584 keep_cap(this, CAP_SYS_NICE);
585 }
586 #endif /* CAPABILITIES */
587
588 /* add handler for SEGV and ILL,
589 * add handler for USR1 (cancellation).
590 * INT, TERM and HUP are handled by sigwait() in run() */
591 action.sa_handler = segv_handler;
592 action.sa_flags = 0;
593 sigemptyset(&action.sa_mask);
594 sigaddset(&action.sa_mask, SIGINT);
595 sigaddset(&action.sa_mask, SIGTERM);
596 sigaddset(&action.sa_mask, SIGHUP);
597 sigaction(SIGSEGV, &action, NULL);
598 sigaction(SIGILL, &action, NULL);
599 sigaction(SIGBUS, &action, NULL);
600 action.sa_handler = SIG_IGN;
601 sigaction(SIGPIPE, &action, NULL);
602
603 pthread_sigmask(SIG_SETMASK, &action.sa_mask, 0);
604
605 return this;
606 }
607
608 /**
609 * Check/create PID file, return TRUE if already running
610 */
611 static bool check_pidfile()
612 {
613 struct stat stb;
614 FILE *file;
615
616 if (stat(PID_FILE, &stb) == 0)
617 {
618 file = fopen(PID_FILE, "r");
619 if (file)
620 {
621 char buf[64];
622 pid_t pid = 0;
623
624 memset(buf, 0, sizeof(buf));
625 if (fread(buf, 1, sizeof(buf), file))
626 {
627 pid = atoi(buf);
628 }
629 fclose(file);
630 if (pid && kill(pid, 0) == 0)
631 { /* such a process is running */
632 return TRUE;
633 }
634 }
635 DBG1(DBG_DMN, "removing pidfile '"PID_FILE"', process not running");
636 unlink(PID_FILE);
637 }
638
639 /* create new pidfile */
640 file = fopen(PID_FILE, "w");
641 if (file)
642 {
643 fprintf(file, "%d\n", getpid());
644 ignore_result(fchown(fileno(file), charon->uid, charon->gid));
645 fclose(file);
646 }
647 return FALSE;
648 }
649
650 /**
651 * print command line usage and exit
652 */
653 static void usage(const char *msg)
654 {
655 if (msg != NULL && *msg != '\0')
656 {
657 fprintf(stderr, "%s\n", msg);
658 }
659 fprintf(stderr, "Usage: charon\n"
660 " [--help]\n"
661 " [--version]\n"
662 " [--use-syslog]\n"
663 " [--debug-<type> <level>]\n"
664 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
665 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
666 " 2 = controlmore, 3 = raw, 4 = private)\n"
667 "\n"
668 );
669 exit(msg == NULL? 0 : 1);
670 }
671
672 /**
673 * Main function, manages the daemon.
674 */
675 int main(int argc, char *argv[])
676 {
677 bool use_syslog = FALSE;
678 private_daemon_t *private_charon;
679 level_t levels[DBG_MAX];
680 int group;
681
682 /* logging for library during initialization, as we have no bus yet */
683 dbg = dbg_stderr;
684
685 /* initialize library */
686 if (!library_init(NULL))
687 {
688 library_deinit();
689 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
690 }
691
692 if (lib->integrity &&
693 !lib->integrity->check_file(lib->integrity, "charon", argv[0]))
694 {
695 dbg_stderr(1, "integrity check of charon failed");
696 library_deinit();
697 exit(SS_RC_DAEMON_INTEGRITY);
698 }
699
700 lib->printf_hook->add_handler(lib->printf_hook, 'R',
701 traffic_selector_printf_hook,
702 PRINTF_HOOK_ARGTYPE_POINTER,
703 PRINTF_HOOK_ARGTYPE_END);
704 lib->printf_hook->add_handler(lib->printf_hook, 'P',
705 proposal_printf_hook,
706 PRINTF_HOOK_ARGTYPE_POINTER,
707 PRINTF_HOOK_ARGTYPE_END);
708 private_charon = daemon_create();
709 charon = (daemon_t*)private_charon;
710
711 lookup_uid_gid(private_charon);
712
713 /* use CTRL loglevel for default */
714 for (group = 0; group < DBG_MAX; group++)
715 {
716 levels[group] = LEVEL_CTRL;
717 }
718
719 /* handle arguments */
720 for (;;)
721 {
722 struct option long_opts[] = {
723 { "help", no_argument, NULL, 'h' },
724 { "version", no_argument, NULL, 'v' },
725 { "use-syslog", no_argument, NULL, 'l' },
726 /* TODO: handle "debug-all" */
727 { "debug-dmn", required_argument, &group, DBG_DMN },
728 { "debug-mgr", required_argument, &group, DBG_MGR },
729 { "debug-ike", required_argument, &group, DBG_IKE },
730 { "debug-chd", required_argument, &group, DBG_CHD },
731 { "debug-job", required_argument, &group, DBG_JOB },
732 { "debug-cfg", required_argument, &group, DBG_CFG },
733 { "debug-knl", required_argument, &group, DBG_KNL },
734 { "debug-net", required_argument, &group, DBG_NET },
735 { "debug-enc", required_argument, &group, DBG_ENC },
736 { "debug-lib", required_argument, &group, DBG_LIB },
737 { 0,0,0,0 }
738 };
739
740 int c = getopt_long(argc, argv, "", long_opts, NULL);
741 switch (c)
742 {
743 case EOF:
744 break;
745 case 'h':
746 usage(NULL);
747 break;
748 case 'v':
749 printf("Linux strongSwan %s\n", VERSION);
750 exit(0);
751 case 'l':
752 use_syslog = TRUE;
753 continue;
754 case 0:
755 /* option is in group */
756 levels[group] = atoi(optarg);
757 continue;
758 default:
759 usage("");
760 break;
761 }
762 break;
763 }
764
765 /* initialize daemon */
766 if (!initialize(private_charon, use_syslog, levels))
767 {
768 DBG1(DBG_DMN, "initialization failed - aborting charon");
769 destroy(private_charon);
770 exit(SS_RC_INITIALIZATION_FAILED);
771 }
772
773 if (check_pidfile())
774 {
775 DBG1(DBG_DMN, "charon already running (\""PID_FILE"\" exists)");
776 destroy(private_charon);
777 exit(-1);
778 }
779
780 /* drop the capabilities we won't need */
781 drop_capabilities(private_charon);
782
783 /* start the engine, go multithreaded */
784 charon->processor->set_threads(charon->processor,
785 lib->settings->get_int(lib->settings, "charon.threads",
786 DEFAULT_THREADS));
787
788 /* run daemon */
789 run(private_charon);
790
791 /* normal termination, cleanup and exit */
792 destroy(private_charon);
793 unlink(PID_FILE);
794
795 library_deinit();
796
797 return 0;
798 }
799