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