sigwaitinfo() may fail with EINTR if interrupted by an unblocked signal not in the set
[strongswan.git] / src / charon-cmd / charon-cmd.c
1 /*
2 * Copyright (C) 2006-2013 Tobias Brunner
3 * Copyright (C) 2005-2013 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 #include <signal.h>
21 #include <pthread.h>
22 #include <sys/types.h>
23 #include <sys/utsname.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <errno.h>
27
28 #include <library.h>
29 #include <hydra.h>
30 #include <daemon.h>
31 #include <utils/backtrace.h>
32 #include <threading/thread.h>
33
34 #include "cmd/cmd_options.h"
35 #include "cmd/cmd_connection.h"
36 #include "cmd/cmd_creds.h"
37
38 /**
39 * Default loglevel
40 */
41 static level_t default_loglevel = LEVEL_CTRL;
42
43 /**
44 * Loglevel configuration
45 */
46 static level_t levels[DBG_MAX];
47
48 /**
49 * Connection to initiate
50 */
51 static cmd_connection_t *conn;
52
53 /**
54 * Credential backend
55 */
56 static cmd_creds_t *creds;
57
58 /**
59 * hook in library for debugging messages
60 */
61 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
62
63 /**
64 * Logging hook for library logs, using stderr output
65 */
66 static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
67 {
68 va_list args;
69
70 if (level <= default_loglevel)
71 {
72 va_start(args, fmt);
73 fprintf(stderr, "00[%N] ", debug_names, group);
74 vfprintf(stderr, fmt, args);
75 fprintf(stderr, "\n");
76 va_end(args);
77 }
78 }
79
80 /**
81 * Clean up connection definition atexit()
82 */
83 static void cleanup_conn()
84 {
85 DESTROY_IF(conn);
86 }
87
88 /**
89 * Clean up credentials atexit()
90 */
91 static void cleanup_creds()
92 {
93 DESTROY_IF(creds);
94 }
95
96 /**
97 * Run the daemon and handle unix signals
98 */
99 static int run()
100 {
101 sigset_t set;
102
103 /* handle SIGINT, SIGHUP and SIGTERM in this handler */
104 sigemptyset(&set);
105 sigaddset(&set, SIGINT);
106 sigaddset(&set, SIGHUP);
107 sigaddset(&set, SIGTERM);
108 sigaddset(&set, SIGUSR1);
109 sigprocmask(SIG_BLOCK, &set, NULL);
110
111 while (TRUE)
112 {
113 int sig;
114
115 sig = sigwaitinfo(&set, NULL);
116 if (sig == -1)
117 {
118 if (errno == EINTR)
119 { /* ignore signals we didn't wait for */
120 continue;
121 }
122 DBG1(DBG_DMN, "waiting for signal failed: %s", strerror(errno));
123 return 1;
124 }
125 switch (sig)
126 {
127 case SIGHUP:
128 {
129 DBG1(DBG_DMN, "signal of type SIGHUP received. Reloading "
130 "configuration");
131 if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
132 {
133 charon->load_loggers(charon, levels, TRUE);
134 lib->plugins->reload(lib->plugins, NULL);
135 }
136 else
137 {
138 DBG1(DBG_DMN, "reloading config failed, keeping old");
139 }
140 break;
141 }
142 case SIGINT:
143 {
144 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
145 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
146 return 0;
147 }
148 case SIGTERM:
149 {
150 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
151 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
152 return 0;
153 }
154 case SIGUSR1:
155 { /* an error occurred */
156 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
157 return 1;
158 }
159 }
160 }
161 }
162
163 /**
164 * lookup UID and GID
165 */
166 static bool lookup_uid_gid()
167 {
168 #ifdef IPSEC_USER
169 if (!lib->caps->resolve_uid(lib->caps, IPSEC_USER))
170 {
171 return FALSE;
172 }
173 #endif
174 #ifdef IPSEC_GROUP
175 if (!lib->caps->resolve_gid(lib->caps, IPSEC_GROUP))
176 {
177 return FALSE;
178 }
179 #endif
180 return TRUE;
181 }
182
183 /**
184 * Handle SIGSEGV/SIGILL signals raised by threads
185 */
186 static void segv_handler(int signal)
187 {
188 backtrace_t *backtrace;
189
190 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
191 backtrace = backtrace_create(2);
192 backtrace->log(backtrace, stderr, TRUE);
193 backtrace->destroy(backtrace);
194
195 DBG1(DBG_DMN, "killing ourself, received critical signal");
196 abort();
197 }
198
199 /**
200 * Print command line usage and exit
201 */
202 static void usage(FILE *out, char *msg, char *binary)
203 {
204 static const int padto = 18;
205 char cmd[64], *pre, *post;
206 int i, line, pad;
207
208 if (msg)
209 {
210 fprintf(out, "%s\n", msg);
211 }
212 fprintf(out, "Usage: %s\n", binary);
213 for (i = 0; i < CMD_OPT_COUNT; i++)
214 {
215 switch (cmd_options[i].has_arg)
216 {
217 case required_argument:
218 pre = " <";
219 post = ">";
220 break;
221 case optional_argument:
222 pre = "[=";
223 post = "]";
224 break;
225 case no_argument:
226 default:
227 pre = " ";
228 post = " ";
229 break;
230 }
231 snprintf(cmd, sizeof(cmd), " --%s%s%s%s", cmd_options[i].name,
232 pre, cmd_options[i].arg, post);
233 pad = padto - strlen(cmd);
234 if (pad >= 1)
235 {
236 fprintf(out, "%s%-*s%s\n", cmd, pad, "", cmd_options[i].desc);
237 }
238 else
239 { /* write description to a separate line */
240 fprintf(out, "%s\n%-*s%s\n", cmd, padto, "", cmd_options[i].desc);
241 }
242 for (line = 0; line < countof(cmd_options[i].lines); line++)
243 {
244 if (cmd_options[i].lines[line])
245 {
246 fprintf(out, "%-*s%s\n", padto, "", cmd_options[i].lines[line]);
247 }
248 }
249 }
250 }
251
252 /**
253 * Handle command line options, if simple is TRUE only arguments like --help
254 * and --version are handled.
255 */
256 static void handle_arguments(int argc, char *argv[], bool simple)
257 {
258 struct option long_opts[CMD_OPT_COUNT + 1] = {};
259 int i, opt;
260
261 for (i = 0; i < CMD_OPT_COUNT; i++)
262 {
263 long_opts[i].name = cmd_options[i].name;
264 long_opts[i].val = cmd_options[i].id;
265 long_opts[i].has_arg = cmd_options[i].has_arg;
266 }
267 /* reset option parser */
268 optind = 1;
269 while (TRUE)
270 {
271 bool handled = FALSE;
272
273 opt = getopt_long(argc, argv, "", long_opts, NULL);
274 switch (opt)
275 {
276 case EOF:
277 break;
278 case CMD_OPT_HELP:
279 usage(stdout, NULL, argv[0]);
280 exit(0);
281 case CMD_OPT_VERSION:
282 printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
283 exit(0);
284 case CMD_OPT_DEBUG:
285 default_loglevel = atoi(optarg);
286 continue;
287 default:
288 if (simple)
289 {
290 continue;
291 }
292 handled |= conn->handle(conn, opt, optarg);
293 handled |= creds->handle(creds, opt, optarg);
294 if (handled)
295 {
296 continue;
297 }
298 /* fall-through */
299 case '?':
300 /* missing argument, unrecognized option */
301 usage(stderr, NULL, argv[0]);
302 exit(1);
303 }
304 break;
305 }
306 }
307
308 /**
309 * Main function, starts the daemon.
310 */
311 int main(int argc, char *argv[])
312 {
313 struct sigaction action;
314 struct utsname utsname;
315 int group;
316
317 /* handle simple arguments */
318 handle_arguments(argc, argv, TRUE);
319
320 dbg = dbg_stderr;
321 atexit(library_deinit);
322 if (!library_init(NULL, "charon-cmd"))
323 {
324 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
325 }
326 if (lib->integrity)
327 {
328 if (!lib->integrity->check_file(lib->integrity, "charon-cmd", argv[0]))
329 {
330 exit(SS_RC_DAEMON_INTEGRITY);
331 }
332 }
333 atexit(libhydra_deinit);
334 if (!libhydra_init())
335 {
336 exit(SS_RC_INITIALIZATION_FAILED);
337 }
338 atexit(libcharon_deinit);
339 if (!libcharon_init())
340 {
341 exit(SS_RC_INITIALIZATION_FAILED);
342 }
343 for (group = 0; group < DBG_MAX; group++)
344 {
345 levels[group] = default_loglevel;
346 }
347 charon->load_loggers(charon, levels, TRUE);
348
349 if (!lookup_uid_gid())
350 {
351 exit(SS_RC_INITIALIZATION_FAILED);
352 }
353 lib->settings->set_default_str(lib->settings, "charon-cmd.port", "0");
354 lib->settings->set_default_str(lib->settings, "charon-cmd.port_nat_t", "0");
355 if (!charon->initialize(charon,
356 lib->settings->get_str(lib->settings, "charon-cmd.load", PLUGINS)))
357 {
358 exit(SS_RC_INITIALIZATION_FAILED);
359 }
360 if (!lib->caps->drop(lib->caps))
361 {
362 exit(SS_RC_INITIALIZATION_FAILED);
363 }
364
365 conn = cmd_connection_create();
366 atexit(cleanup_conn);
367 creds = cmd_creds_create();
368 atexit(cleanup_creds);
369
370 /* handle all arguments */
371 handle_arguments(argc, argv, FALSE);
372
373 if (uname(&utsname) != 0)
374 {
375 memset(&utsname, 0, sizeof(utsname));
376 }
377 DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
378 VERSION, utsname.sysname, utsname.release, utsname.machine);
379 lib->plugins->status(lib->plugins, LEVEL_CTRL);
380
381 /* add handler for SEGV and ILL,
382 * INT, TERM and HUP are handled by sigwaitinfo() in run() */
383 action.sa_handler = segv_handler;
384 action.sa_flags = 0;
385 sigemptyset(&action.sa_mask);
386 sigaddset(&action.sa_mask, SIGINT);
387 sigaddset(&action.sa_mask, SIGTERM);
388 sigaddset(&action.sa_mask, SIGHUP);
389 sigaddset(&action.sa_mask, SIGUSR1);
390 sigaction(SIGSEGV, &action, NULL);
391 sigaction(SIGILL, &action, NULL);
392 sigaction(SIGBUS, &action, NULL);
393 action.sa_handler = SIG_IGN;
394 sigaction(SIGPIPE, &action, NULL);
395
396 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
397
398 /* start daemon with thread-pool */
399 charon->start(charon);
400 /* wait for signal */
401 return run();
402 }