2 * Copyright (C) 2012 Giuliano Grassi
3 * Copyright (C) 2012 Ralf Sager
4 * Copyright (C) 2012 Tobias Brunner
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
20 #include <android/log.h>
22 #include "charonservice.h"
23 #include "android_jni.h"
29 #include <threading/thread.h>
31 #define ANDROID_DEBUG_LEVEL 1
33 typedef struct private_charonservice_t private_charonservice_t
;
36 * private data of charonservice
38 struct private_charonservice_t
{
43 charonservice_t
public;
47 * Single instance of charonservice_t.
49 charonservice_t
*charonservice
;
52 * hook in library for debugging messages
54 extern void (*dbg
)(debug_t group
, level_t level
, char *fmt
, ...);
57 * Logging hook for library logs, using android specific logging
59 static void dbg_android(debug_t group
, level_t level
, char *fmt
, ...)
63 if (level
<= ANDROID_DEBUG_LEVEL
)
65 char sgroup
[16], buffer
[8192];
66 char *current
= buffer
, *next
;
68 snprintf(sgroup
, sizeof(sgroup
), "%N", debug_names
, group
);
70 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
73 { /* log each line separately */
74 next
= strchr(current
, '\n');
79 __android_log_print(ANDROID_LOG_INFO
, "charon", "00[%s] %s\n",
87 * Initialize the charonservice object
89 static void charonservice_init()
91 private_charonservice_t
*this;
97 charonservice
= &this->public;
99 lib
->settings
->set_int(lib
->settings
,
100 "charon.plugins.android_log.loglevel", ANDROID_DEBUG_LEVEL
);
104 * Deinitialize the charonservice object
106 static void charonservice_deinit()
108 private_charonservice_t
*this = (private_charonservice_t
*)charonservice
;
111 charonservice
= NULL
;
115 * Handle SIGSEGV/SIGILL signals raised by threads
117 static void segv_handler(int signal
)
119 dbg_android(DBG_DMN
, 1, "thread %u received %d", thread_current_id(),
125 * Initialize charon and the libraries via JNI
127 JNI_METHOD(CharonVpnService
, initializeCharon
, void)
129 struct sigaction action
;
131 /* logging for library during initialization, as we have no bus yet */
134 /* initialize library */
135 if (!library_init(NULL
))
141 if (!libhydra_init("charon"))
148 if (!libipsec_init())
156 charonservice_init();
158 if (!libcharon_init("charon") ||
159 !charon
->initialize(charon
, PLUGINS
))
162 charonservice_deinit();
169 /* add handler for SEGV and ILL etc. */
170 action
.sa_handler
= segv_handler
;
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
);
179 /* start daemon (i.e. the threads in the thread-pool) */
180 charon
->start(charon
);
184 * Deinitialize charon and all libraries
186 JNI_METHOD(CharonVpnService
, deinitializeCharon
, void)
189 charonservice_deinit();