sigwaitinfo() may fail with EINTR if interrupted by an unblocked signal not in the set
[strongswan.git] / src / frontends / osx / charon-xpc / charon-xpc.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <sys/utsname.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <pthread.h>
22 #include <errno.h>
23
24 #include <library.h>
25 #include <hydra.h>
26 #include <daemon.h>
27 #include <threading/thread.h>
28 #include <utils/backtrace.h>
29
30 #include "xpc_dispatch.h"
31
32 /**
33 * XPC dispatcher class
34 */
35 static xpc_dispatch_t *dispatcher;
36
37 /**
38 * atexit() cleanup for dispatcher
39 */
40 void dispatcher_cleanup()
41 {
42 DESTROY_IF(dispatcher);
43 }
44
45 /**
46 * Loglevel configuration
47 */
48 static level_t levels[DBG_MAX];
49
50 /**
51 * hook in library for debugging messages
52 */
53 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
54
55 /**
56 * Logging hook for library logs, using stderr output
57 */
58 static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
59 {
60 va_list args;
61
62 if (level <= 1)
63 {
64 va_start(args, fmt);
65 fprintf(stderr, "00[%N] ", debug_names, group);
66 vfprintf(stderr, fmt, args);
67 fprintf(stderr, "\n");
68 va_end(args);
69 }
70 }
71
72 /**
73 * Run the daemon and handle unix signals
74 */
75 static int run()
76 {
77 sigset_t set;
78
79 sigemptyset(&set);
80 sigaddset(&set, SIGINT);
81 sigaddset(&set, SIGTERM);
82 sigprocmask(SIG_BLOCK, &set, NULL);
83
84 while (TRUE)
85 {
86 int sig;
87
88 sig = sigwaitinfo(&set, NULL);
89 if (sig == -1)
90 {
91 if (errno == EINTR)
92 { /* ignore signals we didn't wait for */
93 continue;
94 }
95 DBG1(DBG_DMN, "waiting for signal failed: %s", strerror(errno));
96 return 1;
97 }
98 switch (sig)
99 {
100 case SIGINT:
101 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
102 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
103 return 0;
104 case SIGTERM:
105 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
106 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
107 return 0;
108 }
109 }
110 }
111
112 /**
113 * Handle SIGSEGV/SIGILL signals raised by threads
114 */
115 static void segv_handler(int signal)
116 {
117 backtrace_t *backtrace;
118
119 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
120 backtrace = backtrace_create(2);
121 backtrace->log(backtrace, NULL, TRUE);
122 backtrace->destroy(backtrace);
123
124 DBG1(DBG_DMN, "killing ourself, received critical signal");
125 abort();
126 }
127
128 /**
129 * PF_ROUTE for some reason does not send an event for the first address
130 * installed after a fresh boot. Fix this by installing a fake tun address
131 * just to remove it afterwards.
132 */
133 static void fixup_pf_route()
134 {
135 tun_device_t *tun;
136 host_t *host;
137
138 tun = tun_device_create(NULL);
139 if (tun)
140 {
141 if (tun->up(tun))
142 {
143 host = host_create_from_string("127.0.0.99", 0);
144 tun->set_address(tun, host, 32);
145 host->destroy(host);
146 }
147 tun->destroy(tun);
148 }
149 }
150
151 /**
152 * Main function, starts the daemon.
153 */
154 int main(int argc, char *argv[])
155 {
156 struct sigaction action;
157 struct utsname utsname;
158 int group;
159
160 dbg = dbg_stderr;
161 atexit(library_deinit);
162 if (!library_init(NULL, "charon-xpc"))
163 {
164 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
165 }
166 if (lib->integrity)
167 {
168 if (!lib->integrity->check_file(lib->integrity, "charon-xpc", argv[0]))
169 {
170 exit(SS_RC_DAEMON_INTEGRITY);
171 }
172 }
173 atexit(libhydra_deinit);
174 if (!libhydra_init())
175 {
176 exit(SS_RC_INITIALIZATION_FAILED);
177 }
178 atexit(libcharon_deinit);
179 if (!libcharon_init())
180 {
181 exit(SS_RC_INITIALIZATION_FAILED);
182 }
183 for (group = 0; group < DBG_MAX; group++)
184 {
185 levels[group] = LEVEL_CTRL;
186 }
187 charon->load_loggers(charon, levels, TRUE);
188
189 lib->settings->set_default_str(lib->settings, "charon-xpc.port", "0");
190 lib->settings->set_default_str(lib->settings, "charon-xpc.port_nat_t", "0");
191 lib->settings->set_default_str(lib->settings,
192 "charon-xpc.close_ike_on_child_failure", "yes");
193 lib->settings->set_default_str(lib->settings,
194 "charon-xpc.plugins.osx-attr.append", "no");
195 if (!charon->initialize(charon,
196 lib->settings->get_str(lib->settings, "charon-xpc.load",
197 "nonce pkcs1 openssl keychain ctr ccm gcm kernel-libipsec "
198 "kernel-pfroute socket-default eap-identity eap-mschapv2 "
199 "eap-md5 xauth-generic osx-attr")))
200 {
201 exit(SS_RC_INITIALIZATION_FAILED);
202 }
203
204 if (uname(&utsname) != 0)
205 {
206 memset(&utsname, 0, sizeof(utsname));
207 }
208 DBG1(DBG_DMN, "Starting charon-xpc IKE daemon (strongSwan %s, %s %s, %s)",
209 VERSION, utsname.sysname, utsname.release, utsname.machine);
210
211 /* add handler for SEGV and ILL,
212 * INT, TERM and HUP are handled by sigwaitinfo() in run() */
213 action.sa_handler = segv_handler;
214 action.sa_flags = 0;
215 sigemptyset(&action.sa_mask);
216 sigaddset(&action.sa_mask, SIGINT);
217 sigaddset(&action.sa_mask, SIGTERM);
218 sigaddset(&action.sa_mask, SIGHUP);
219 sigaction(SIGSEGV, &action, NULL);
220 sigaction(SIGILL, &action, NULL);
221 sigaction(SIGBUS, &action, NULL);
222 action.sa_handler = SIG_IGN;
223 sigaction(SIGPIPE, &action, NULL);
224
225 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
226
227 dispatcher = xpc_dispatch_create();
228 if (!dispatcher)
229 {
230 exit(SS_RC_INITIALIZATION_FAILED);
231 }
232 atexit(dispatcher_cleanup);
233
234 charon->start(charon);
235 fixup_pf_route();
236 return run();
237 }