handle plugin loading failures
[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 if (!lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR,
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.attributes = NULL;
561 this->public.sender= NULL;
562 this->public.receiver = NULL;
563 this->public.scheduler = NULL;
564 this->public.kernel_interface = NULL;
565 this->public.processor = NULL;
566 this->public.controller = NULL;
567 this->public.eap = NULL;
568 this->public.sim = NULL;
569 this->public.bus = NULL;
570 this->public.file_loggers = linked_list_create();
571 this->public.sys_loggers = linked_list_create();
572 #ifdef ME
573 this->public.connect_manager = NULL;
574 this->public.mediation_manager = NULL;
575 #endif /* ME */
576 this->public.uid = 0;
577 this->public.gid = 0;
578
579 this->public.main_thread_id = pthread_self();
580 #ifdef CAPABILITIES
581 this->caps = cap_init();
582 keep_cap(this, CAP_NET_ADMIN);
583 if (lib->leak_detective)
584 {
585 keep_cap(this, CAP_SYS_NICE);
586 }
587 #endif /* CAPABILITIES */
588
589 /* add handler for SEGV and ILL,
590 * add handler for USR1 (cancellation).
591 * INT, TERM and HUP are handled by sigwait() in run() */
592 action.sa_handler = segv_handler;
593 action.sa_flags = 0;
594 sigemptyset(&action.sa_mask);
595 sigaddset(&action.sa_mask, SIGINT);
596 sigaddset(&action.sa_mask, SIGTERM);
597 sigaddset(&action.sa_mask, SIGHUP);
598 sigaction(SIGSEGV, &action, NULL);
599 sigaction(SIGILL, &action, NULL);
600 sigaction(SIGBUS, &action, NULL);
601 action.sa_handler = SIG_IGN;
602 sigaction(SIGPIPE, &action, NULL);
603
604 pthread_sigmask(SIG_SETMASK, &action.sa_mask, 0);
605
606 return this;
607 }
608
609 /**
610 * Check/create PID file, return TRUE if already running
611 */
612 static bool check_pidfile()
613 {
614 struct stat stb;
615 FILE *file;
616
617 if (stat(PID_FILE, &stb) == 0)
618 {
619 file = fopen(PID_FILE, "r");
620 if (file)
621 {
622 char buf[64];
623 pid_t pid = 0;
624
625 memset(buf, 0, sizeof(buf));
626 if (fread(buf, 1, sizeof(buf), file))
627 {
628 pid = atoi(buf);
629 }
630 fclose(file);
631 if (pid && kill(pid, 0) == 0)
632 { /* such a process is running */
633 return TRUE;
634 }
635 }
636 DBG1(DBG_DMN, "removing pidfile '"PID_FILE"', process not running");
637 unlink(PID_FILE);
638 }
639
640 /* create new pidfile */
641 file = fopen(PID_FILE, "w");
642 if (file)
643 {
644 fprintf(file, "%d\n", getpid());
645 ignore_result(fchown(fileno(file), charon->uid, charon->gid));
646 fclose(file);
647 }
648 return FALSE;
649 }
650
651 /**
652 * print command line usage and exit
653 */
654 static void usage(const char *msg)
655 {
656 if (msg != NULL && *msg != '\0')
657 {
658 fprintf(stderr, "%s\n", msg);
659 }
660 fprintf(stderr, "Usage: charon\n"
661 " [--help]\n"
662 " [--version]\n"
663 " [--use-syslog]\n"
664 " [--debug-<type> <level>]\n"
665 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
666 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
667 " 2 = controlmore, 3 = raw, 4 = private)\n"
668 "\n"
669 );
670 exit(msg == NULL? 0 : 1);
671 }
672
673 /**
674 * Main function, manages the daemon.
675 */
676 int main(int argc, char *argv[])
677 {
678 bool use_syslog = FALSE;
679 private_daemon_t *private_charon;
680 level_t levels[DBG_MAX];
681 int group;
682
683 /* logging for library during initialization, as we have no bus yet */
684 dbg = dbg_stderr;
685
686 /* initialize library */
687 if (!library_init(STRONGSWAN_CONF))
688 {
689 library_deinit();
690 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
691 }
692
693 if (lib->integrity &&
694 !lib->integrity->check_file(lib->integrity, "charon", argv[0]))
695 {
696 dbg_stderr(1, "integrity check of charon failed");
697 library_deinit();
698 exit(SS_RC_DAEMON_INTEGRITY);
699 }
700
701 lib->printf_hook->add_handler(lib->printf_hook, 'R',
702 traffic_selector_printf_hook,
703 PRINTF_HOOK_ARGTYPE_POINTER,
704 PRINTF_HOOK_ARGTYPE_END);
705 lib->printf_hook->add_handler(lib->printf_hook, 'P',
706 proposal_printf_hook,
707 PRINTF_HOOK_ARGTYPE_POINTER,
708 PRINTF_HOOK_ARGTYPE_END);
709 private_charon = daemon_create();
710 charon = (daemon_t*)private_charon;
711
712 lookup_uid_gid(private_charon);
713
714 /* use CTRL loglevel for default */
715 for (group = 0; group < DBG_MAX; group++)
716 {
717 levels[group] = LEVEL_CTRL;
718 }
719
720 /* handle arguments */
721 for (;;)
722 {
723 struct option long_opts[] = {
724 { "help", no_argument, NULL, 'h' },
725 { "version", no_argument, NULL, 'v' },
726 { "use-syslog", no_argument, NULL, 'l' },
727 /* TODO: handle "debug-all" */
728 { "debug-dmn", required_argument, &group, DBG_DMN },
729 { "debug-mgr", required_argument, &group, DBG_MGR },
730 { "debug-ike", required_argument, &group, DBG_IKE },
731 { "debug-chd", required_argument, &group, DBG_CHD },
732 { "debug-job", required_argument, &group, DBG_JOB },
733 { "debug-cfg", required_argument, &group, DBG_CFG },
734 { "debug-knl", required_argument, &group, DBG_KNL },
735 { "debug-net", required_argument, &group, DBG_NET },
736 { "debug-enc", required_argument, &group, DBG_ENC },
737 { "debug-lib", required_argument, &group, DBG_LIB },
738 { 0,0,0,0 }
739 };
740
741 int c = getopt_long(argc, argv, "", long_opts, NULL);
742 switch (c)
743 {
744 case EOF:
745 break;
746 case 'h':
747 usage(NULL);
748 break;
749 case 'v':
750 printf("Linux strongSwan %s\n", VERSION);
751 exit(0);
752 case 'l':
753 use_syslog = TRUE;
754 continue;
755 case 0:
756 /* option is in group */
757 levels[group] = atoi(optarg);
758 continue;
759 default:
760 usage("");
761 break;
762 }
763 break;
764 }
765
766 /* initialize daemon */
767 if (!initialize(private_charon, use_syslog, levels))
768 {
769 DBG1(DBG_DMN, "initialization failed - aborting charon");
770 destroy(private_charon);
771 exit(SS_RC_INITIALIZATION_FAILED);
772 }
773
774 if (check_pidfile())
775 {
776 DBG1(DBG_DMN, "charon already running (\""PID_FILE"\" exists)");
777 destroy(private_charon);
778 exit(-1);
779 }
780
781 /* drop the capabilities we won't need */
782 drop_capabilities(private_charon);
783
784 /* start the engine, go multithreaded */
785 charon->processor->set_threads(charon->processor,
786 lib->settings->get_int(lib->settings, "charon.threads",
787 DEFAULT_THREADS));
788
789 /* run daemon */
790 run(private_charon);
791
792 /* normal termination, cleanup and exit */
793 destroy(private_charon);
794 unlink(PID_FILE);
795
796 library_deinit();
797
798 return 0;
799 }
800