charon-cmd: move command line options to separate file, obsolete short options
[strongswan.git] / src / charon-cmd / charon-cmd.c
1 /*
2 * Copyright (C) 2006-2012 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 #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
21 #include <signal.h>
22 #undef _POSIX_PTHREAD_SEMANTICS
23 #include <pthread.h>
24 #include <sys/types.h>
25 #include <sys/utsname.h>
26 #include <unistd.h>
27 #include <getopt.h>
28
29 #include <library.h>
30 #include <hydra.h>
31 #include <daemon.h>
32 #include <utils/backtrace.h>
33 #include <threading/thread.h>
34
35 #include "cmd/cmd_options.h"
36
37 /**
38 * Loglevel configuration
39 */
40 static level_t levels[DBG_MAX];
41
42 /**
43 * hook in library for debugging messages
44 */
45 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
46
47 /**
48 * Logging hook for library logs, using stderr output
49 */
50 static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
51 {
52 va_list args;
53
54 if (level <= 1)
55 {
56 va_start(args, fmt);
57 fprintf(stderr, "00[%N] ", debug_names, group);
58 vfprintf(stderr, fmt, args);
59 fprintf(stderr, "\n");
60 va_end(args);
61 }
62 }
63
64 /**
65 * Run the daemon and handle unix signals
66 */
67 static void run()
68 {
69 sigset_t set;
70
71 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
72 sigemptyset(&set);
73 sigaddset(&set, SIGINT);
74 sigaddset(&set, SIGHUP);
75 sigaddset(&set, SIGTERM);
76 sigprocmask(SIG_BLOCK, &set, NULL);
77
78 while (TRUE)
79 {
80 int sig;
81 int error;
82
83 error = sigwait(&set, &sig);
84 if (error)
85 {
86 DBG1(DBG_DMN, "error %d while waiting for a signal", error);
87 return;
88 }
89 switch (sig)
90 {
91 case SIGHUP:
92 {
93 DBG1(DBG_DMN, "signal of type SIGHUP received. Reloading "
94 "configuration");
95 if (lib->settings->load_files(lib->settings, NULL, FALSE))
96 {
97 charon->load_loggers(charon, levels, TRUE);
98 lib->plugins->reload(lib->plugins, NULL);
99 }
100 else
101 {
102 DBG1(DBG_DMN, "reloading config failed, keeping old");
103 }
104 break;
105 }
106 case SIGINT:
107 {
108 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
109 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
110 return;
111 }
112 case SIGTERM:
113 {
114 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
115 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
116 return;
117 }
118 default:
119 {
120 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
121 break;
122 }
123 }
124 }
125 }
126
127 /**
128 * lookup UID and GID
129 */
130 static bool lookup_uid_gid()
131 {
132 #ifdef IPSEC_USER
133 if (!charon->caps->resolve_uid(charon->caps, IPSEC_USER))
134 {
135 return FALSE;
136 }
137 #endif
138 #ifdef IPSEC_GROUP
139 if (!charon->caps->resolve_gid(charon->caps, IPSEC_GROUP))
140 {
141 return FALSE;
142 }
143 #endif
144 return TRUE;
145 }
146
147 /**
148 * Handle SIGSEGV/SIGILL signals raised by threads
149 */
150 static void segv_handler(int signal)
151 {
152 backtrace_t *backtrace;
153
154 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
155 backtrace = backtrace_create(2);
156 backtrace->log(backtrace, stderr, TRUE);
157 backtrace->destroy(backtrace);
158
159 DBG1(DBG_DMN, "killing ourself, received critical signal");
160 abort();
161 }
162
163 /**
164 * Print command line usage and exit
165 */
166 static void usage(FILE *out, char *msg, char *binary)
167 {
168 int i, pre, post, padto = 0, spacing = 2;
169
170 for (i = 0; i < CMD_OPT_COUNT; i++)
171 {
172 padto = max(padto, strlen(cmd_options[i].name) +
173 strlen(cmd_options[i].arg));
174 }
175 padto += spacing;
176
177 if (msg)
178 {
179 fprintf(out, "%s\n", msg);
180 }
181 fprintf(out, "Usage: %s\n", binary);
182 for (i = 0; i < CMD_OPT_COUNT; i++)
183 {
184 switch (cmd_options[i].has_arg)
185 {
186 case required_argument:
187 pre = '<';
188 post = '>';
189 break;
190 case optional_argument:
191 pre = '[';
192 post = ']';
193 break;
194 case no_argument:
195 default:
196 pre = post = ' ';
197 break;
198 }
199 fprintf(out, " --%s %c%s%c %-*s%s\n",
200 cmd_options[i].name,
201 pre, cmd_options[i].arg, post,
202 padto - strlen(cmd_options[i].name) - strlen(cmd_options[i].arg), "",
203 cmd_options[i].desc);
204 }
205 }
206
207 /**
208 * Handle command line options
209 */
210 static void handle_arguments(int argc, char *argv[])
211 {
212 while (TRUE)
213 {
214 struct option long_opts[CMD_OPT_COUNT + 1] = {};
215 int i;
216
217 for (i = 0; i < CMD_OPT_COUNT; i++)
218 {
219 long_opts[i].name = cmd_options[i].name;
220 long_opts[i].val = cmd_options[i].id;
221 long_opts[i].has_arg = cmd_options[i].has_arg;
222 }
223
224 switch (getopt_long(argc, argv, "", long_opts, NULL))
225 {
226 case EOF:
227 break;
228 case CMD_OPT_HELP:
229 usage(stdout, NULL, argv[0]);
230 exit(0);
231 case CMD_OPT_VERSION:
232 printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
233 exit(0);
234 default:
235 usage(stderr, NULL, argv[0]);
236 exit(1);
237 }
238 break;
239 }
240 }
241
242 /**
243 * Main function, starts the daemon.
244 */
245 int main(int argc, char *argv[])
246 {
247 struct sigaction action;
248 struct utsname utsname;
249 int group;
250
251 dbg = dbg_stderr;
252 atexit(library_deinit);
253 if (!library_init(NULL))
254 {
255 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
256 }
257 if (lib->integrity)
258 {
259 if (!lib->integrity->check_file(lib->integrity, "charon-cmd", argv[0]))
260 {
261 exit(SS_RC_DAEMON_INTEGRITY);
262 }
263 }
264 atexit(libhydra_deinit);
265 if (!libhydra_init("charon-cmd"))
266 {
267 exit(SS_RC_INITIALIZATION_FAILED);
268 }
269 atexit(libcharon_deinit);
270 if (!libcharon_init("charon-cmd"))
271 {
272 exit(SS_RC_INITIALIZATION_FAILED);
273 }
274 for (group = 0; group < DBG_MAX; group++)
275 {
276 levels[group] = LEVEL_CTRL;
277 }
278
279 handle_arguments(argc, argv);
280
281 if (!lookup_uid_gid())
282 {
283 exit(SS_RC_INITIALIZATION_FAILED);
284 }
285 charon->load_loggers(charon, levels, TRUE);
286
287 if (uname(&utsname) != 0)
288 {
289 memset(&utsname, 0, sizeof(utsname));
290 }
291 DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
292 VERSION, utsname.sysname, utsname.release, utsname.machine);
293
294 if (!charon->initialize(charon,
295 lib->settings->get_str(lib->settings, "charon-cmd.load", PLUGINS)))
296 {
297 exit(SS_RC_INITIALIZATION_FAILED);
298 }
299 if (!charon->caps->drop(charon->caps))
300 {
301 exit(SS_RC_INITIALIZATION_FAILED);
302 }
303
304 /* add handler for SEGV and ILL,
305 * INT, TERM and HUP are handled by sigwait() in run() */
306 action.sa_handler = segv_handler;
307 action.sa_flags = 0;
308 sigemptyset(&action.sa_mask);
309 sigaddset(&action.sa_mask, SIGINT);
310 sigaddset(&action.sa_mask, SIGTERM);
311 sigaddset(&action.sa_mask, SIGHUP);
312 sigaction(SIGSEGV, &action, NULL);
313 sigaction(SIGILL, &action, NULL);
314 sigaction(SIGBUS, &action, NULL);
315 action.sa_handler = SIG_IGN;
316 sigaction(SIGPIPE, &action, NULL);
317
318 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
319
320 /* start daemon with thread-pool */
321 charon->start(charon);
322 /* wait for signal */
323 run();
324
325 return 0;
326 }