2 * Copyright (C) 2006-2010 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
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>.
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
21 #include <sys/prctl.h>
23 #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
25 #undef _POSIX_PTHREAD_SEMANTICS
28 #include <sys/types.h>
38 #include <utils/backtrace.h>
39 #include <threading/thread.h>
42 * PID file, in which charon stores its process id
44 #define PID_FILE IPSEC_PIDDIR "/charon.pid"
47 * hook in library for debugging messages
49 extern void (*dbg
) (int level
, char *fmt
, ...);
52 * Logging hook for library logs, using stderr output
54 static void dbg_stderr(int level
, char *fmt
, ...)
61 fprintf(stderr
, "00[LIB] ");
62 vfprintf(stderr
, fmt
, args
);
63 fprintf(stderr
, "\n");
69 * Run the daemon and handle unix signals
75 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
77 sigaddset(&set
, SIGINT
);
78 sigaddset(&set
, SIGHUP
);
79 sigaddset(&set
, SIGTERM
);
80 sigprocmask(SIG_BLOCK
, &set
, NULL
);
87 error
= sigwait(&set
, &sig
);
90 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
97 DBG1(DBG_DMN
, "signal of type SIGHUP received. Ignored");
102 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
103 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
108 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
109 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
114 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
122 * drop daemon capabilities
124 static bool drop_capabilities()
127 prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0);
130 if (setgid(charon
->gid
) != 0)
132 DBG1(DBG_DMN
, "change to unprivileged group failed");
135 if (setuid(charon
->uid
) != 0)
137 DBG1(DBG_DMN
, "change to unprivileged user failed");
140 if (!charon
->drop_capabilities(charon
))
142 DBG1(DBG_DMN
, "unable to drop daemon capabilities");
151 static bool lookup_uid_gid()
156 struct passwd passwd
, *pwp
;
158 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
161 DBG1(DBG_DMN
, "resolving user '"IPSEC_USER
"' failed");
164 charon
->uid
= pwp
->pw_uid
;
170 struct group group
, *grp
;
172 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
175 DBG1(DBG_DMN
, "resolving group '"IPSEC_GROUP
"' failed");
178 charon
->gid
= grp
->gr_gid
;
185 * Handle SIGSEGV/SIGILL signals raised by threads
187 static void segv_handler(int signal
)
189 backtrace_t
*backtrace
;
191 DBG1(DBG_DMN
, "thread %u received %d", thread_current_id(), signal
);
192 backtrace
= backtrace_create(2);
193 backtrace
->log(backtrace
, stderr
);
194 backtrace
->destroy(backtrace
);
196 DBG1(DBG_DMN
, "killing ourself, received critical signal");
201 * Check/create PID file, return TRUE if already running
203 static bool check_pidfile()
208 if (stat(PID_FILE
, &stb
) == 0)
210 file
= fopen(PID_FILE
, "r");
216 memset(buf
, 0, sizeof(buf
));
217 if (fread(buf
, 1, sizeof(buf
), file
))
222 if (pid
&& kill(pid
, 0) == 0)
223 { /* such a process is running */
227 DBG1(DBG_DMN
, "removing pidfile '"PID_FILE
"', process not running");
231 /* create new pidfile */
232 file
= fopen(PID_FILE
, "w");
235 fprintf(file
, "%d\n", getpid());
236 ignore_result(fchown(fileno(file
), charon
->uid
, charon
->gid
));
243 * print command line usage and exit
245 static void usage(const char *msg
)
247 if (msg
!= NULL
&& *msg
!= '\0')
249 fprintf(stderr
, "%s\n", msg
);
251 fprintf(stderr
, "Usage: charon\n"
255 " [--debug-<type> <level>]\n"
256 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
257 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
258 " 2 = controlmore, 3 = raw, 4 = private)\n"
261 exit(msg
== NULL?
0 : 1);
265 * Main function, starts the daemon.
267 int main(int argc
, char *argv
[])
269 struct sigaction action
;
270 bool use_syslog
= FALSE
;
271 level_t levels
[DBG_MAX
];
272 int group
, status
= SS_RC_INITIALIZATION_FAILED
;
274 /* logging for library during initialization, as we have no bus yet */
277 /* initialize library */
278 if (!library_init(NULL
))
281 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
284 if (lib
->integrity
&&
285 !lib
->integrity
->check_file(lib
->integrity
, "charon", argv
[0]))
287 dbg_stderr(1, "integrity check of charon failed");
289 exit(SS_RC_DAEMON_INTEGRITY
);
292 if (!libhydra_init())
294 dbg_stderr(1, "initialization failed - aborting charon");
297 exit(SS_RC_INITIALIZATION_FAILED
);
300 if (!libcharon_init())
302 dbg_stderr(1, "initialization failed - aborting charon");
306 /* use CTRL loglevel for default */
307 for (group
= 0; group
< DBG_MAX
; group
++)
309 levels
[group
] = LEVEL_CTRL
;
312 /* handle arguments */
315 struct option long_opts
[] = {
316 { "help", no_argument
, NULL
, 'h' },
317 { "version", no_argument
, NULL
, 'v' },
318 { "use-syslog", no_argument
, NULL
, 'l' },
319 /* TODO: handle "debug-all" */
320 { "debug-dmn", required_argument
, &group
, DBG_DMN
},
321 { "debug-mgr", required_argument
, &group
, DBG_MGR
},
322 { "debug-ike", required_argument
, &group
, DBG_IKE
},
323 { "debug-chd", required_argument
, &group
, DBG_CHD
},
324 { "debug-job", required_argument
, &group
, DBG_JOB
},
325 { "debug-cfg", required_argument
, &group
, DBG_CFG
},
326 { "debug-knl", required_argument
, &group
, DBG_KNL
},
327 { "debug-net", required_argument
, &group
, DBG_NET
},
328 { "debug-enc", required_argument
, &group
, DBG_ENC
},
329 { "debug-lib", required_argument
, &group
, DBG_LIB
},
333 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
342 printf("Linux strongSwan %s\n", VERSION
);
348 /* option is in group */
349 levels
[group
] = atoi(optarg
);
358 if (!lookup_uid_gid())
360 dbg_stderr(1, "invalid uid/gid - aborting charon");
364 /* initialize daemon */
365 if (!charon
->initialize(charon
, use_syslog
, levels
))
367 DBG1(DBG_DMN
, "initialization failed - aborting charon");
373 DBG1(DBG_DMN
, "charon already running (\""PID_FILE
"\" exists)");
378 if (!drop_capabilities())
380 DBG1(DBG_DMN
, "capability dropping failed - aborting charon");
384 /* add handler for SEGV and ILL,
385 * INT, TERM and HUP are handled by sigwait() in run() */
386 action
.sa_handler
= segv_handler
;
388 sigemptyset(&action
.sa_mask
);
389 sigaddset(&action
.sa_mask
, SIGINT
);
390 sigaddset(&action
.sa_mask
, SIGTERM
);
391 sigaddset(&action
.sa_mask
, SIGHUP
);
392 sigaction(SIGSEGV
, &action
, NULL
);
393 sigaction(SIGILL
, &action
, NULL
);
394 sigaction(SIGBUS
, &action
, NULL
);
395 action
.sa_handler
= SIG_IGN
;
396 sigaction(SIGPIPE
, &action
, NULL
);
398 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
400 /* start daemon (i.e. the threads in the thread-pool) */
401 charon
->start(charon
);
403 /* main thread goes to run loop */
406 /* normal termination, cleanup and exit */