mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-24 07:16:43 +00:00
c8bf8a7733
The CLI and server main loop now have no conditional JNI code. All JNI code has been moved into separate source files, which #include the new "jni_common.h" instead of <jni.h>. The "cli.h" header no longer includes <jni.h>, so the rest of the Serval source code is now unaffected by JNI definitions. The 'cf_limbo' global variable is now thread-local, so that each thread has its own independent copy of the loaded configuration. The JNI server entry point now calls cf_init() once. The new 'cf_initialised' flag prevents clobbering the config state by redundant calls to cf_init(). The CLI "stop" command now sends SIGHUP to the specific thread in which the server is running. This is achieved by writing the PID and TID (Linux Thread ID) into the pidfile, separated by a space, on systems that support the Linux gettid() and tgkill() system calls. The server's signal handler has been overhauled, and its logging improved.
158 lines
5.1 KiB
C
158 lines
5.1 KiB
C
/*
|
|
Serval DNA server main loop - JNI entry point
|
|
Copyright (C) 2015 Serval Project Inc.
|
|
Copyright (C) 2016 Flinders University
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License
|
|
as published by the Free Software Foundation; either version 2
|
|
of the License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include "jni_common.h"
|
|
#include "server.h"
|
|
#include "serval.h"
|
|
#include "conf.h"
|
|
#include "instance.h"
|
|
|
|
JNIEnv *server_env=NULL;
|
|
jclass IJniServer= NULL;
|
|
jmethodID aboutToWait, wokeUp, started;
|
|
jobject JniCallback;
|
|
|
|
static time_ms_t waiting(time_ms_t now, time_ms_t next_run, time_ms_t next_wakeup)
|
|
{
|
|
if (server_env && JniCallback){
|
|
jlong r = (*server_env)->CallLongMethod(server_env, JniCallback, aboutToWait, (jlong)now, (jlong)next_run, (jlong)next_wakeup);
|
|
// stop the server if there are any issues
|
|
if ((*server_env)->ExceptionCheck(server_env)){
|
|
serverMode=SERVER_CLOSING;
|
|
INFO("Stopping server due to exception");
|
|
return now;
|
|
}
|
|
return r;
|
|
}
|
|
return next_wakeup;
|
|
}
|
|
|
|
static void wokeup()
|
|
{
|
|
if (server_env && JniCallback){
|
|
(*server_env)->CallVoidMethod(server_env, JniCallback, wokeUp);
|
|
// stop the server if there are any issues
|
|
if ((*server_env)->ExceptionCheck(server_env)){
|
|
INFO("Stopping server due to exception");
|
|
serverMode=SERVER_CLOSING;
|
|
}
|
|
}
|
|
}
|
|
|
|
JNIEXPORT jint JNICALL Java_org_servalproject_servaldna_ServalDCommand_server(
|
|
JNIEnv *env, jobject UNUSED(this), jobject callback, jobject keyring_pin, jobjectArray entry_pins)
|
|
{
|
|
if (!IJniServer){
|
|
cf_init();
|
|
|
|
IJniServer = (*env)->FindClass(env, "org/servalproject/servaldna/IJniServer");
|
|
if (IJniServer==NULL)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate class org.servalproject.servaldna.IJniServer");
|
|
// make sure the interface class cannot be garbage collected between invocations
|
|
IJniServer = (jclass)(*env)->NewGlobalRef(env, IJniServer);
|
|
if (IJniServer==NULL)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to create global ref to class org.servalproject.servaldna.IJniServer");
|
|
aboutToWait = (*env)->GetMethodID(env, IJniServer, "aboutToWait", "(JJJ)J");
|
|
if (aboutToWait==NULL)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate method aboutToWait");
|
|
wokeUp = (*env)->GetMethodID(env, IJniServer, "wokeUp", "()V");
|
|
if (wokeUp==NULL)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate method wokeUp");
|
|
started = (*env)->GetMethodID(env, IJniServer, "started", "(Ljava/lang/String;III)V");
|
|
if (started==NULL)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate method started");
|
|
}
|
|
|
|
int pid = server_pid();
|
|
if (pid < 0)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Failed to read server pid");
|
|
if (pid>0)
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Server already running");
|
|
|
|
cf_reload_strict();
|
|
|
|
int ret = -1;
|
|
|
|
{
|
|
const char *cpin = keyring_pin?(*env)->GetStringUTFChars(env, keyring_pin, NULL):NULL;
|
|
if (cpin != NULL){
|
|
keyring = keyring_open_instance(cpin);
|
|
(*env)->ReleaseStringUTFChars(env, keyring_pin, cpin);
|
|
}else{
|
|
keyring = keyring_open_instance("");
|
|
}
|
|
}
|
|
|
|
// Always open all PIN-less entries.
|
|
keyring_enter_pin(keyring, "");
|
|
if (entry_pins){
|
|
jsize len = (*env)->GetArrayLength(env, entry_pins);
|
|
jsize i;
|
|
for (i = 0; i < len; ++i) {
|
|
const jstring pin = (jstring)(*env)->GetObjectArrayElement(env, entry_pins, i);
|
|
if ((*env)->ExceptionCheck(env))
|
|
goto end;
|
|
const char *cpin = (*env)->GetStringUTFChars(env, pin, NULL);
|
|
if (cpin != NULL){
|
|
keyring_enter_pin(keyring, cpin);
|
|
(*env)->ReleaseStringUTFChars(env, pin, cpin);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (server_env){
|
|
jni_throw(env, "java/lang/IllegalStateException", "Server java env variable already set");
|
|
goto end;
|
|
}
|
|
|
|
server_env = env;
|
|
JniCallback = (*env)->NewGlobalRef(env, callback);
|
|
|
|
ret = server_bind();
|
|
|
|
if (ret==-1){
|
|
jni_throw(env, "java/lang/IllegalStateException", "Failed to bind sockets");
|
|
goto end;
|
|
}
|
|
|
|
{
|
|
jstring str = (jstring)(*env)->NewStringUTF(env, instance_path());
|
|
(*env)->CallVoidMethod(env, callback, started, str, getpid(), mdp_loopback_port, httpd_server_port);
|
|
(*env)->DeleteLocalRef(env, str);
|
|
}
|
|
|
|
server_loop(waiting, wokeup);
|
|
|
|
end:
|
|
|
|
server_env=NULL;
|
|
if (JniCallback){
|
|
(*env)->DeleteGlobalRef(env, JniCallback);
|
|
JniCallback = NULL;
|
|
}
|
|
|
|
if (keyring)
|
|
keyring_free(keyring);
|
|
keyring = NULL;
|
|
|
|
return ret;
|
|
}
|