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