2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Reto Buerki
4 * Copyright (C) 2012 Adrian-Ken Rueegsegger
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include <sys/types.h>
32 #include <utils/backtrace.h>
33 #include <threading/thread.h>
36 #include "tkm_nonceg.h"
39 * PID file, in which charon-tkm stores its process id
41 static char *pidfile_name
= NULL
;
44 * Global reference to PID file (required to truncate, if undeletable)
46 static FILE *pidfile
= NULL
;
49 * Hook in library for debugging messages
51 extern void (*dbg
) (debug_t group
, level_t level
, char *fmt
, ...);
54 * Simple logging hook for library logs, using syslog output
56 static void dbg_syslog(debug_t group
, level_t level
, char *fmt
, ...)
64 /* write in memory buffer first */
65 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
66 syslog(LOG_DAEMON
|LOG_INFO
, "00[%s] %s", debug_names
->names
[group
],
73 * Run the daemon and handle unix signals
79 /* handle SIGINT and SIGTERM in this handler */
81 sigaddset(&set
, SIGINT
);
82 sigaddset(&set
, SIGTERM
);
83 sigprocmask(SIG_BLOCK
, &set
, NULL
);
90 error
= sigwait(&set
, &sig
);
93 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
100 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
101 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
106 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
107 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
112 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
120 * Handle SIGSEGV/SIGILL signals raised by threads
122 static void segv_handler(int signal
)
124 backtrace_t
*backtrace
;
126 DBG1(DBG_DMN
, "thread %u received %d", thread_current_id(), signal
);
127 backtrace
= backtrace_create(2);
128 backtrace
->log(backtrace
, stderr
, TRUE
);
129 backtrace
->destroy(backtrace
);
131 DBG1(DBG_DMN
, "killing ourself, received critical signal");
138 static bool lookup_uid_gid()
141 if (!charon
->caps
->resolve_uid(charon
->caps
, IPSEC_USER
))
147 if (!charon
->caps
->resolve_gid(charon
->caps
, IPSEC_GROUP
))
156 * Check/create PID file, return TRUE if already running
158 static bool check_pidfile()
162 if (stat(pidfile_name
, &stb
) == 0)
164 pidfile
= fopen(pidfile_name
, "r");
170 memset(buf
, 0, sizeof(buf
));
171 if (fread(buf
, 1, sizeof(buf
), pidfile
))
173 buf
[sizeof(buf
) - 1] = '\0';
177 if (pid
&& kill(pid
, 0) == 0)
178 { /* such a process is running */
182 DBG1(DBG_DMN
, "removing pidfile '%s', process not running", pidfile_name
);
183 unlink(pidfile_name
);
186 /* create new pidfile */
187 pidfile
= fopen(pidfile_name
, "w");
190 ignore_result(fchown(fileno(pidfile
),
191 charon
->caps
->get_uid(charon
->caps
),
192 charon
->caps
->get_gid(charon
->caps
)));
193 fprintf(pidfile
, "%d\n", getpid());
200 * Delete/truncate the PID file
202 static void unlink_pidfile()
204 /* because unlinking the PID file may fail, we truncate it to ensure the
205 * daemon can be properly restarted. one probable cause for this is the
206 * combination of not running as root and the effective user lacking
207 * permissions on the parent dir(s) of the PID file */
210 ignore_result(ftruncate(fileno(pidfile
), 0));
213 unlink(pidfile_name
);
216 * Main function, starts TKM backend.
218 int main(int argc
, char *argv
[])
221 if (argc
> 0 && strlen(argv
[0]) > 0)
223 dmn_name
= basename(argv
[0]);
227 dmn_name
= "charon-tkm";
230 struct sigaction action
;
231 int status
= SS_RC_INITIALIZATION_FAILED
;
233 /* logging for library during initialization, as we have no bus yet */
236 /* initialize library */
237 if (!library_init(NULL
))
243 if (!libhydra_init(dmn_name
))
245 dbg_syslog(DBG_DMN
, 1, "initialization failed - aborting %s", dmn_name
);
251 if (!libcharon_init(dmn_name
))
253 dbg_syslog(DBG_DMN
, 1, "initialization failed - aborting %s", dmn_name
);
257 if (!lookup_uid_gid())
259 dbg_syslog(DBG_DMN
, 1, "invalid uid/gid - aborting %s", dmn_name
);
263 /* make sure we log to the DAEMON facility by default */
264 lib
->settings
->set_int(lib
->settings
, "%s.syslog.daemon.default",
265 lib
->settings
->get_int(lib
->settings
, "%s.syslog.daemon.default", 1,
266 dmn_name
), dmn_name
);
267 charon
->load_loggers(charon
, NULL
, FALSE
);
269 DBG1(DBG_DMN
, "Starting charon with TKM backend (strongSwan "VERSION
")");
271 /* register TKM specific plugins */
272 static plugin_feature_t features
[] = {
273 PLUGIN_REGISTER(NONCE_GEN
, tkm_nonceg_create
),
274 PLUGIN_PROVIDE(NONCE_GEN
)
276 lib
->plugins
->add_static_features(lib
->plugins
, "tkm-backend", features
,
277 countof(features
), TRUE
);
279 /* initialize daemon */
280 if (!charon
->initialize(charon
, PLUGINS
))
282 DBG1(DBG_DMN
, "initialization failed - aborting %s", dmn_name
);
286 /* set global pidfile name depending on daemon name */
287 if (asprintf(&pidfile_name
, IPSEC_PIDDIR
"/%s.pid", dmn_name
) < 0)
289 DBG1(DBG_DMN
, "unable to set pidfile name - aborting %s", dmn_name
);
295 DBG1(DBG_DMN
, "%s already running (\"%s\" exists)", dmn_name
,
300 if (!charon
->caps
->drop(charon
->caps
))
302 DBG1(DBG_DMN
, "capability dropping failed - aborting %s", dmn_name
);
306 /* initialize TKM client */
309 DBG1(DBG_DMN
, "init of TKM client failed - aborting %s", dmn_name
);
313 /* add handler for SEGV and ILL,
314 * INT and TERM are handled by sigwait() in run() */
315 action
.sa_handler
= segv_handler
;
317 sigemptyset(&action
.sa_mask
);
318 sigaddset(&action
.sa_mask
, SIGINT
);
319 sigaddset(&action
.sa_mask
, SIGTERM
);
320 sigaction(SIGSEGV
, &action
, NULL
);
321 sigaction(SIGILL
, &action
, NULL
);
322 sigaction(SIGBUS
, &action
, NULL
);
323 action
.sa_handler
= SIG_IGN
;
324 sigaction(SIGPIPE
, &action
, NULL
);
326 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
328 /* start daemon (i.e. the threads in the thread-pool) */
329 charon
->start(charon
);
331 /* main thread goes to run loop */