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>
37 #include <utils/backtrace.h>
38 #include <threading/thread.h>
41 * PID file, in which charon stores its process id
43 #define PID_FILE IPSEC_PIDDIR "/charon.pid"
46 * hook in library for debugging messages
48 extern void (*dbg
) (int level
, char *fmt
, ...);
51 * Logging hook for library logs, using stderr output
53 static void dbg_stderr(int level
, char *fmt
, ...)
60 fprintf(stderr
, "00[LIB] ");
61 vfprintf(stderr
, fmt
, args
);
62 fprintf(stderr
, "\n");
68 * Run the daemon and handle unix signals
74 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
76 sigaddset(&set
, SIGINT
);
77 sigaddset(&set
, SIGHUP
);
78 sigaddset(&set
, SIGTERM
);
79 sigprocmask(SIG_BLOCK
, &set
, NULL
);
86 error
= sigwait(&set
, &sig
);
89 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
96 DBG1(DBG_DMN
, "signal of type SIGHUP received. Ignored");
101 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
102 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
107 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
108 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
113 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
121 * drop daemon capabilities
123 static bool drop_capabilities()
126 prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0);
129 if (setgid(charon
->gid
) != 0)
131 DBG1(DBG_DMN
, "change to unprivileged group failed");
134 if (setuid(charon
->uid
) != 0)
136 DBG1(DBG_DMN
, "change to unprivileged user failed");
139 if (!charon
->drop_capabilities(charon
))
141 DBG1(DBG_DMN
, "unable to drop daemon capabilities");
150 static bool lookup_uid_gid()
155 struct passwd passwd
, *pwp
;
157 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
160 DBG1(DBG_DMN
, "resolving user '"IPSEC_USER
"' failed");
163 charon
->uid
= pwp
->pw_uid
;
169 struct group group
, *grp
;
171 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
174 DBG1(DBG_DMN
, "resolving group '"IPSEC_GROUP
"' failed");
177 charon
->gid
= grp
->gr_gid
;
184 * Handle SIGSEGV/SIGILL signals raised by threads
186 static void segv_handler(int signal
)
188 backtrace_t
*backtrace
;
190 DBG1(DBG_DMN
, "thread %u received %d", thread_current_id(), signal
);
191 backtrace
= backtrace_create(2);
192 backtrace
->log(backtrace
, stderr
);
193 backtrace
->destroy(backtrace
);
195 DBG1(DBG_DMN
, "killing ourself, received critical signal");
200 * Check/create PID file, return TRUE if already running
202 static bool check_pidfile()
207 if (stat(PID_FILE
, &stb
) == 0)
209 file
= fopen(PID_FILE
, "r");
215 memset(buf
, 0, sizeof(buf
));
216 if (fread(buf
, 1, sizeof(buf
), file
))
221 if (pid
&& kill(pid
, 0) == 0)
222 { /* such a process is running */
226 DBG1(DBG_DMN
, "removing pidfile '"PID_FILE
"', process not running");
230 /* create new pidfile */
231 file
= fopen(PID_FILE
, "w");
234 fprintf(file
, "%d\n", getpid());
235 ignore_result(fchown(fileno(file
), charon
->uid
, charon
->gid
));
242 * print command line usage and exit
244 static void usage(const char *msg
)
246 if (msg
!= NULL
&& *msg
!= '\0')
248 fprintf(stderr
, "%s\n", msg
);
250 fprintf(stderr
, "Usage: charon\n"
254 " [--debug-<type> <level>]\n"
255 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
256 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
257 " 2 = controlmore, 3 = raw, 4 = private)\n"
260 exit(msg
== NULL?
0 : 1);
264 * Main function, starts the daemon.
266 int main(int argc
, char *argv
[])
268 struct sigaction action
;
269 bool use_syslog
= FALSE
;
270 level_t levels
[DBG_MAX
];
273 /* logging for library during initialization, as we have no bus yet */
276 /* initialize library */
277 if (!library_init(NULL
))
280 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
283 if (lib
->integrity
&&
284 !lib
->integrity
->check_file(lib
->integrity
, "charon", argv
[0]))
286 dbg_stderr(1, "integrity check of charon failed");
288 exit(SS_RC_DAEMON_INTEGRITY
);
291 if (!libcharon_init())
293 dbg_stderr(1, "initialization failed - aborting charon");
296 exit(SS_RC_INITIALIZATION_FAILED
);
299 /* use CTRL loglevel for default */
300 for (group
= 0; group
< DBG_MAX
; group
++)
302 levels
[group
] = LEVEL_CTRL
;
305 /* handle arguments */
308 struct option long_opts
[] = {
309 { "help", no_argument
, NULL
, 'h' },
310 { "version", no_argument
, NULL
, 'v' },
311 { "use-syslog", no_argument
, NULL
, 'l' },
312 /* TODO: handle "debug-all" */
313 { "debug-dmn", required_argument
, &group
, DBG_DMN
},
314 { "debug-mgr", required_argument
, &group
, DBG_MGR
},
315 { "debug-ike", required_argument
, &group
, DBG_IKE
},
316 { "debug-chd", required_argument
, &group
, DBG_CHD
},
317 { "debug-job", required_argument
, &group
, DBG_JOB
},
318 { "debug-cfg", required_argument
, &group
, DBG_CFG
},
319 { "debug-knl", required_argument
, &group
, DBG_KNL
},
320 { "debug-net", required_argument
, &group
, DBG_NET
},
321 { "debug-enc", required_argument
, &group
, DBG_ENC
},
322 { "debug-lib", required_argument
, &group
, DBG_LIB
},
326 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
335 printf("Linux strongSwan %s\n", VERSION
);
341 /* option is in group */
342 levels
[group
] = atoi(optarg
);
351 if (!lookup_uid_gid())
353 dbg_stderr(1, "invalid uid/gid - aborting charon");
356 exit(SS_RC_INITIALIZATION_FAILED
);
359 /* initialize daemon */
360 if (!charon
->initialize(charon
, use_syslog
, levels
))
362 DBG1(DBG_DMN
, "initialization failed - aborting charon");
365 exit(SS_RC_INITIALIZATION_FAILED
);
370 DBG1(DBG_DMN
, "charon already running (\""PID_FILE
"\" exists)");
376 if (!drop_capabilities())
378 DBG1(DBG_DMN
, "capability dropping failed - aborting charon");
381 exit(SS_RC_INITIALIZATION_FAILED
);
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 */