Add signal handler for fatal signals to libandroidbridge
[strongswan.git] / src / frontends / android / jni / libandroidbridge / charonservice.c
1 /*
2 * Copyright (C) 2012 Giuliano Grassi
3 * Copyright (C) 2012 Ralf Sager
4 * Copyright (C) 2012 Tobias Brunner
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 #include <signal.h>
19 #include <string.h>
20 #include <android/log.h>
21
22 #include "charonservice.h"
23 #include "android_jni.h"
24
25 #include <daemon.h>
26 #include <hydra.h>
27 #include <ipsec.h>
28 #include <library.h>
29 #include <threading/thread.h>
30
31 #define ANDROID_DEBUG_LEVEL 1
32
33 typedef struct private_charonservice_t private_charonservice_t;
34
35 /**
36 * private data of charonservice
37 */
38 struct private_charonservice_t {
39
40 /**
41 * public interface
42 */
43 charonservice_t public;
44 };
45
46 /**
47 * Single instance of charonservice_t.
48 */
49 charonservice_t *charonservice;
50
51 /**
52 * hook in library for debugging messages
53 */
54 extern void (*dbg)(debug_t group, level_t level, char *fmt, ...);
55
56 /**
57 * Logging hook for library logs, using android specific logging
58 */
59 static void dbg_android(debug_t group, level_t level, char *fmt, ...)
60 {
61 va_list args;
62
63 if (level <= ANDROID_DEBUG_LEVEL)
64 {
65 char sgroup[16], buffer[8192];
66 char *current = buffer, *next;
67
68 snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
69 va_start(args, fmt);
70 vsnprintf(buffer, sizeof(buffer), fmt, args);
71 va_end(args);
72 while (current)
73 { /* log each line separately */
74 next = strchr(current, '\n');
75 if (next)
76 {
77 *(next++) = '\0';
78 }
79 __android_log_print(ANDROID_LOG_INFO, "charon", "00[%s] %s\n",
80 sgroup, current);
81 current = next;
82 }
83 }
84 }
85
86 /**
87 * Initialize the charonservice object
88 */
89 static void charonservice_init()
90 {
91 private_charonservice_t *this;
92
93 INIT(this,
94 .public = {
95 },
96 );
97 charonservice = &this->public;
98
99 lib->settings->set_int(lib->settings,
100 "charon.plugins.android_log.loglevel", ANDROID_DEBUG_LEVEL);
101 }
102
103 /**
104 * Deinitialize the charonservice object
105 */
106 static void charonservice_deinit()
107 {
108 private_charonservice_t *this = (private_charonservice_t*)charonservice;
109
110 free(this);
111 charonservice = NULL;
112 }
113
114 /**
115 * Handle SIGSEGV/SIGILL signals raised by threads
116 */
117 static void segv_handler(int signal)
118 {
119 dbg_android(DBG_DMN, 1, "thread %u received %d", thread_current_id(),
120 signal);
121 exit(1);
122 }
123
124 /**
125 * Initialize charon and the libraries via JNI
126 */
127 JNI_METHOD(CharonVpnService, initializeCharon, void)
128 {
129 struct sigaction action;
130
131 /* logging for library during initialization, as we have no bus yet */
132 dbg = dbg_android;
133
134 /* initialize library */
135 if (!library_init(NULL))
136 {
137 library_deinit();
138 return;
139 }
140
141 if (!libhydra_init("charon"))
142 {
143 libhydra_deinit();
144 library_deinit();
145 return;
146 }
147
148 if (!libipsec_init())
149 {
150 libipsec_deinit();
151 libhydra_deinit();
152 library_deinit();
153 return;
154 }
155
156 charonservice_init();
157
158 if (!libcharon_init("charon") ||
159 !charon->initialize(charon, PLUGINS))
160 {
161 libcharon_deinit();
162 charonservice_deinit();
163 libipsec_deinit();
164 libhydra_deinit();
165 library_deinit();
166 return;
167 }
168
169 /* add handler for SEGV and ILL etc. */
170 action.sa_handler = segv_handler;
171 action.sa_flags = 0;
172 sigemptyset(&action.sa_mask);
173 sigaction(SIGSEGV, &action, NULL);
174 sigaction(SIGILL, &action, NULL);
175 sigaction(SIGBUS, &action, NULL);
176 action.sa_handler = SIG_IGN;
177 sigaction(SIGPIPE, &action, NULL);
178
179 /* start daemon (i.e. the threads in the thread-pool) */
180 charon->start(charon);
181 }
182
183 /**
184 * Deinitialize charon and all libraries
185 */
186 JNI_METHOD(CharonVpnService, deinitializeCharon, void)
187 {
188 libcharon_deinit();
189 charonservice_deinit();
190 libipsec_deinit();
191 libhydra_deinit();
192 library_deinit();
193 }
194