Preserve thread name after attaching it to JVM (#838)

When native thread is attached to JVM, then it's name is taken from
JavaVMAttachArgs. When no JavaVMAttachArgs, or no JavaVMAttachArgs::name
passed, then JVM on its own decides on taming thread. Those names are
not descriptive. To preserve thread name, pass the currently set thread
name in JavaVMAttachArgs::name. pthread_getname_np was introduced in API
26, hence use more generic approach.

Fixes #837
Authored By: Yauheni Khnykin (@Hsilgos)
pull/846/head
Yauheni Khnykin 4 months ago committed by GitHub
parent 4f2f3427e8
commit 9377126687
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 26
      src/lib/ares_android.c

@ -26,6 +26,7 @@
#if defined(ANDROID) || defined(__ANDROID__)
# include "ares_private.h"
# include <jni.h>
# include <sys/prctl.h>
static JavaVM *android_jvm = NULL;
static jobject android_connectivity_manager = NULL;
@ -80,6 +81,23 @@ static jmethodID jni_get_method_id(JNIEnv *env, jclass cls,
return mid;
}
static int jvm_attach(JNIEnv **env)
{
char name[17] = {0};
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
if (prctl(PR_GET_NAME, name) == 0) {
args.name = name;
} else {
args.name = NULL;
}
args.group = NULL;
return (*android_jvm)->AttachCurrentThread(android_jvm, env, &args);
}
void ares_library_init_jvm(JavaVM *jvm)
{
android_jvm = jvm;
@ -100,7 +118,7 @@ int ares_library_init_android(jobject connectivity_manager)
res = (*android_jvm)->GetEnv(android_jvm, (void **)&env, JNI_VERSION_1_6);
if (res == JNI_EDETACHED) {
env = NULL;
res = (*android_jvm)->AttachCurrentThread(android_jvm, &env, NULL);
res = jvm_attach(&env);
need_detatch = 1;
}
if (res != JNI_OK || env == NULL) {
@ -233,7 +251,7 @@ void ares_library_cleanup_android(void)
res = (*android_jvm)->GetEnv(android_jvm, (void **)&env, JNI_VERSION_1_6);
if (res == JNI_EDETACHED) {
env = NULL;
res = (*android_jvm)->AttachCurrentThread(android_jvm, &env, NULL);
res = jvm_attach(&env);
need_detatch = 1;
}
if (res != JNI_OK || env == NULL) {
@ -285,7 +303,7 @@ char **ares_get_android_server_list(size_t max_servers, size_t *num_servers)
res = (*android_jvm)->GetEnv(android_jvm, (void **)&env, JNI_VERSION_1_6);
if (res == JNI_EDETACHED) {
env = NULL;
res = (*android_jvm)->AttachCurrentThread(android_jvm, &env, NULL);
res = jvm_attach(&env);
need_detatch = 1;
}
if (res != JNI_OK || env == NULL) {
@ -403,7 +421,7 @@ char *ares_get_android_search_domains_list(void)
res = (*android_jvm)->GetEnv(android_jvm, (void **)&env, JNI_VERSION_1_6);
if (res == JNI_EDETACHED) {
env = NULL;
res = (*android_jvm)->AttachCurrentThread(android_jvm, &env, NULL);
res = jvm_attach(&env);
need_detatch = 1;
}
if (res != JNI_OK || env == NULL) {

Loading…
Cancel
Save