Ensure native methods for unix-native-common are only registered once. (#10932)
Motiviation: We need to ensure we only register the methods for unix-native-common once as otherwise it may have strange side-effects. Modifications: - Add extra method that should be called to signal that we need to register the methods. The registration will only happen once. - Adjust code to make use of it. Result: No more problems due incorrect registration of these methods.
This commit is contained in:
parent
78646b3d7e
commit
a7e8a33833
@ -48,6 +48,7 @@
|
|||||||
#include "netty_unix_limits.h"
|
#include "netty_unix_limits.h"
|
||||||
#include "netty_unix_socket.h"
|
#include "netty_unix_socket.h"
|
||||||
#include "netty_unix_util.h"
|
#include "netty_unix_util.h"
|
||||||
|
#include "netty_unix.h"
|
||||||
|
|
||||||
// Add define if NETTY_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
|
// Add define if NETTY_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
|
||||||
#ifdef NETTY_BUILD_STATIC
|
#ifdef NETTY_BUILD_STATIC
|
||||||
@ -107,6 +108,9 @@ static jfieldID packetPortFieldId = NULL;
|
|||||||
static jfieldID packetMemoryAddressFieldId = NULL;
|
static jfieldID packetMemoryAddressFieldId = NULL;
|
||||||
static jfieldID packetCountFieldId = NULL;
|
static jfieldID packetCountFieldId = NULL;
|
||||||
|
|
||||||
|
static const char* staticPackagePrefix = NULL;
|
||||||
|
static int register_unix_called = 0;
|
||||||
|
|
||||||
// util methods
|
// util methods
|
||||||
static int getSysctlValue(const char * property, int* returnValue) {
|
static int getSysctlValue(const char * property, int* returnValue) {
|
||||||
int rc = -1;
|
int rc = -1;
|
||||||
@ -498,6 +502,12 @@ static jint netty_epoll_native_tcpMd5SigMaxKeyLen(JNIEnv* env, jclass clazz) {
|
|||||||
|
|
||||||
return TCP_MD5SIG_MAXKEYLEN;
|
return TCP_MD5SIG_MAXKEYLEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static jint netty_epoll_native_registerUnix(JNIEnv* env, jclass clazz) {
|
||||||
|
register_unix_called = 1;
|
||||||
|
return netty_unix_register(env, staticPackagePrefix);
|
||||||
|
}
|
||||||
|
|
||||||
// JNI Registered Methods End
|
// JNI Registered Methods End
|
||||||
|
|
||||||
// JNI Method Registration Table Begin
|
// JNI Method Registration Table Begin
|
||||||
@ -529,7 +539,10 @@ static const JNINativeMethod fixed_method_table[] = {
|
|||||||
{ "epollCtlDel0", "(II)I", (void *) netty_epoll_native_epollCtlDel0 },
|
{ "epollCtlDel0", "(II)I", (void *) netty_epoll_native_epollCtlDel0 },
|
||||||
// "sendmmsg0" has a dynamic signature
|
// "sendmmsg0" has a dynamic signature
|
||||||
{ "sizeofEpollEvent", "()I", (void *) netty_epoll_native_sizeofEpollEvent },
|
{ "sizeofEpollEvent", "()I", (void *) netty_epoll_native_sizeofEpollEvent },
|
||||||
{ "offsetofEpollData", "()I", (void *) netty_epoll_native_offsetofEpollData }
|
{ "offsetofEpollData", "()I", (void *) netty_epoll_native_offsetofEpollData },
|
||||||
|
{ "registerUnix", "()I", (void *) netty_epoll_native_registerUnix },
|
||||||
|
|
||||||
|
>>>>>>> 5b41f3d25b... Ensure native methods for unix-native-common are only registered once. (#10932)
|
||||||
};
|
};
|
||||||
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]);
|
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]);
|
||||||
|
|
||||||
@ -574,11 +587,6 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
|
|||||||
int ret = JNI_ERR;
|
int ret = JNI_ERR;
|
||||||
int staticallyRegistered = 0;
|
int staticallyRegistered = 0;
|
||||||
int nativeRegistered = 0;
|
int nativeRegistered = 0;
|
||||||
int limitsOnLoadCalled = 0;
|
|
||||||
int errorsOnLoadCalled = 0;
|
|
||||||
int filedescriptorOnLoadCalled = 0;
|
|
||||||
int socketOnLoadCalled = 0;
|
|
||||||
int bufferOnLoadCalled = 0;
|
|
||||||
int linuxsocketOnLoadCalled = 0;
|
int linuxsocketOnLoadCalled = 0;
|
||||||
char* nettyClassName = NULL;
|
char* nettyClassName = NULL;
|
||||||
jclass nativeDatagramPacketCls = NULL;
|
jclass nativeDatagramPacketCls = NULL;
|
||||||
@ -609,32 +617,6 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
|
|||||||
}
|
}
|
||||||
nativeRegistered = 1;
|
nativeRegistered = 1;
|
||||||
|
|
||||||
// Load all c modules that we depend upon
|
|
||||||
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
limitsOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
errorsOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
filedescriptorOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
socketOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
bufferOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_epoll_linuxsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
if (netty_epoll_linuxsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
@ -653,6 +635,10 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
|
|||||||
NETTY_JNI_UTIL_GET_FIELD(env, nativeDatagramPacketCls, packetCountFieldId, "count", "I", done);
|
NETTY_JNI_UTIL_GET_FIELD(env, nativeDatagramPacketCls, packetCountFieldId, "count", "I", done);
|
||||||
|
|
||||||
ret = NETTY_JNI_UTIL_JNI_VERSION;
|
ret = NETTY_JNI_UTIL_JNI_VERSION;
|
||||||
|
|
||||||
|
if (packagePrefix != NULL) {
|
||||||
|
staticPackagePrefix = strdup(packagePrefix);
|
||||||
|
}
|
||||||
done:
|
done:
|
||||||
|
|
||||||
netty_jni_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize());
|
netty_jni_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize());
|
||||||
@ -665,21 +651,6 @@ done:
|
|||||||
if (nativeRegistered == 1) {
|
if (nativeRegistered == 1) {
|
||||||
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
|
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
|
||||||
}
|
}
|
||||||
if (limitsOnLoadCalled == 1) {
|
|
||||||
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (errorsOnLoadCalled == 1) {
|
|
||||||
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (filedescriptorOnLoadCalled == 1) {
|
|
||||||
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (socketOnLoadCalled == 1) {
|
|
||||||
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (bufferOnLoadCalled == 1) {
|
|
||||||
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (linuxsocketOnLoadCalled == 1) {
|
if (linuxsocketOnLoadCalled == 1) {
|
||||||
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);
|
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);
|
||||||
}
|
}
|
||||||
@ -694,13 +665,17 @@ done:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void netty_epoll_native_JNI_OnUnload(JNIEnv* env, const char* packagePrefix) {
|
static void netty_epoll_native_JNI_OnUnload(JNIEnv* env, const char* packagePrefix) {
|
||||||
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);
|
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
|
||||||
|
if (register_unix_called == 1) {
|
||||||
|
register_unix_called = 0;
|
||||||
|
netty_unix_unregister(env, staticPackagePrefix);
|
||||||
|
}
|
||||||
|
if (staticPackagePrefix != NULL) {
|
||||||
|
free((void *) staticPackagePrefix);
|
||||||
|
staticPackagePrefix = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
packetAddrFieldId = NULL;
|
packetAddrFieldId = NULL;
|
||||||
packetAddrLenFieldId = NULL;
|
packetAddrLenFieldId = NULL;
|
||||||
packetScopeIdFieldId = NULL;
|
packetScopeIdFieldId = NULL;
|
||||||
|
@ -17,6 +17,7 @@ package io.netty.channel.epoll;
|
|||||||
|
|
||||||
import io.netty.channel.unix.FileDescriptor;
|
import io.netty.channel.unix.FileDescriptor;
|
||||||
import io.netty.channel.unix.Socket;
|
import io.netty.channel.unix.Socket;
|
||||||
|
import io.netty.channel.unix.Unix;
|
||||||
import io.netty.util.internal.NativeLibraryLoader;
|
import io.netty.util.internal.NativeLibraryLoader;
|
||||||
import io.netty.util.internal.PlatformDependent;
|
import io.netty.util.internal.PlatformDependent;
|
||||||
import io.netty.util.internal.SystemPropertyUtil;
|
import io.netty.util.internal.SystemPropertyUtil;
|
||||||
@ -76,9 +77,16 @@ public final class Native {
|
|||||||
// Just ignore
|
// Just ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Socket.initialize();
|
Unix.registerInternal(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
registerUnix();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static native int registerUnix();
|
||||||
|
|
||||||
// EventLoop operations and constants
|
// EventLoop operations and constants
|
||||||
public static final int EPOLLIN = epollin();
|
public static final int EPOLLIN = epollin();
|
||||||
public static final int EPOLLOUT = epollout();
|
public static final int EPOLLOUT = epollout();
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
#include "netty_unix_limits.h"
|
#include "netty_unix_limits.h"
|
||||||
#include "netty_unix_socket.h"
|
#include "netty_unix_socket.h"
|
||||||
#include "netty_unix_util.h"
|
#include "netty_unix_util.h"
|
||||||
|
#include "netty_unix.h"
|
||||||
|
|
||||||
// Add define if NETTY_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
|
// Add define if NETTY_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
|
||||||
#ifdef NETTY_BUILD_STATIC
|
#ifdef NETTY_BUILD_STATIC
|
||||||
@ -75,6 +76,8 @@
|
|||||||
#endif /* __APPLE__ */
|
#endif /* __APPLE__ */
|
||||||
|
|
||||||
static clockid_t waitClockId = 0; // initialized by netty_unix_util_initialize_wait_clock
|
static clockid_t waitClockId = 0; // initialized by netty_unix_util_initialize_wait_clock
|
||||||
|
static const char* staticPackagePrefix = NULL;
|
||||||
|
static int register_unix_called = 0;
|
||||||
|
|
||||||
static jint netty_kqueue_native_kqueueCreate(JNIEnv* env, jclass clazz) {
|
static jint netty_kqueue_native_kqueueCreate(JNIEnv* env, jclass clazz) {
|
||||||
jint kq = kqueue();
|
jint kq = kqueue();
|
||||||
@ -159,6 +162,11 @@ static jint netty_kqueue_native_keventWait(JNIEnv* env, jclass clazz, jint kqueu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static jint netty_kqueue_native_registerUnix(JNIEnv* env, jclass clazz) {
|
||||||
|
register_unix_called = 1;
|
||||||
|
return netty_unix_register(env, staticPackagePrefix);
|
||||||
|
}
|
||||||
|
|
||||||
static jint netty_kqueue_native_sizeofKEvent(JNIEnv* env, jclass clazz) {
|
static jint netty_kqueue_native_sizeofKEvent(JNIEnv* env, jclass clazz) {
|
||||||
return sizeof(struct kevent);
|
return sizeof(struct kevent);
|
||||||
}
|
}
|
||||||
@ -267,7 +275,8 @@ static const JNINativeMethod fixed_method_table[] = {
|
|||||||
{ "offsetofKEventFlags", "()I", (void *) netty_kqueue_native_offsetofKEventFlags },
|
{ "offsetofKEventFlags", "()I", (void *) netty_kqueue_native_offsetofKEventFlags },
|
||||||
{ "offsetofKEventFFlags", "()I", (void *) netty_kqueue_native_offsetofKEventFFlags },
|
{ "offsetofKEventFFlags", "()I", (void *) netty_kqueue_native_offsetofKEventFFlags },
|
||||||
{ "offsetofKEventFilter", "()I", (void *) netty_kqueue_native_offsetofKEventFilter },
|
{ "offsetofKEventFilter", "()I", (void *) netty_kqueue_native_offsetofKEventFilter },
|
||||||
{ "offsetofKeventData", "()I", (void *) netty_kqueue_native_offsetofKeventData }
|
{ "offsetofKeventData", "()I", (void *) netty_kqueue_native_offsetofKeventData },
|
||||||
|
{ "registerUnix", "()I", (void *) netty_kqueue_native_registerUnix }
|
||||||
};
|
};
|
||||||
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]);
|
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]);
|
||||||
// JNI Method Registration Table End
|
// JNI Method Registration Table End
|
||||||
@ -275,11 +284,6 @@ static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(
|
|||||||
static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) {
|
static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) {
|
||||||
int staticallyRegistered = 0;
|
int staticallyRegistered = 0;
|
||||||
int nativeRegistered = 0;
|
int nativeRegistered = 0;
|
||||||
int limitsOnLoadCalled = 0;
|
|
||||||
int errorsOnLoadCalled = 0;
|
|
||||||
int filedescriptorOnLoadCalled = 0;
|
|
||||||
int socketOnLoadCalled = 0;
|
|
||||||
int bufferOnLoadCalled = 0;
|
|
||||||
int bsdsocketOnLoadCalled = 0;
|
int bsdsocketOnLoadCalled = 0;
|
||||||
int eventarrayOnLoadCalled = 0;
|
int eventarrayOnLoadCalled = 0;
|
||||||
|
|
||||||
@ -298,31 +302,6 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
nativeRegistered = 1;
|
nativeRegistered = 1;
|
||||||
// Load all c modules that we depend upon
|
|
||||||
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
limitsOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
errorsOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
filedescriptorOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
socketOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
bufferOnLoadCalled = 1;
|
|
||||||
|
|
||||||
if (netty_kqueue_bsdsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
if (netty_kqueue_bsdsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
goto error;
|
goto error;
|
||||||
@ -342,6 +321,9 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (packagePrefix != NULL) {
|
||||||
|
staticPackagePrefix = strdup(packagePrefix);
|
||||||
|
}
|
||||||
return NETTY_JNI_UTIL_JNI_VERSION;
|
return NETTY_JNI_UTIL_JNI_VERSION;
|
||||||
error:
|
error:
|
||||||
if (staticallyRegistered == 1) {
|
if (staticallyRegistered == 1) {
|
||||||
@ -350,21 +332,6 @@ error:
|
|||||||
if (nativeRegistered == 1) {
|
if (nativeRegistered == 1) {
|
||||||
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
|
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
|
||||||
}
|
}
|
||||||
if (limitsOnLoadCalled == 1) {
|
|
||||||
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (errorsOnLoadCalled == 1) {
|
|
||||||
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (filedescriptorOnLoadCalled == 1) {
|
|
||||||
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (socketOnLoadCalled == 1) {
|
|
||||||
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (bufferOnLoadCalled == 1) {
|
|
||||||
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
}
|
|
||||||
if (bsdsocketOnLoadCalled == 1) {
|
if (bsdsocketOnLoadCalled == 1) {
|
||||||
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
|
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
|
||||||
}
|
}
|
||||||
@ -375,14 +342,18 @@ error:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void netty_kqueue_native_JNI_OnUnload(JNIEnv* env, const char* packagePrefix) {
|
static void netty_kqueue_native_JNI_OnUnload(JNIEnv* env, const char* packagePrefix) {
|
||||||
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
|
|
||||||
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
|
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
|
||||||
netty_kqueue_eventarray_JNI_OnUnLoad(env, packagePrefix);
|
netty_kqueue_eventarray_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
|
||||||
|
if (register_unix_called == 1) {
|
||||||
|
register_unix_called = 0;
|
||||||
|
netty_unix_unregister(env, staticPackagePrefix);
|
||||||
|
}
|
||||||
|
if (staticPackagePrefix != NULL) {
|
||||||
|
free((void *) staticPackagePrefix);
|
||||||
|
staticPackagePrefix = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
netty_jni_util_unregister_natives(env, packagePrefix, STATICALLY_CLASSNAME);
|
netty_jni_util_unregister_natives(env, packagePrefix, STATICALLY_CLASSNAME);
|
||||||
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
|
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
package io.netty.channel.kqueue;
|
package io.netty.channel.kqueue;
|
||||||
|
|
||||||
import io.netty.channel.unix.FileDescriptor;
|
import io.netty.channel.unix.FileDescriptor;
|
||||||
import io.netty.channel.unix.Socket;
|
import io.netty.channel.unix.Unix;
|
||||||
import io.netty.util.internal.NativeLibraryLoader;
|
import io.netty.util.internal.NativeLibraryLoader;
|
||||||
import io.netty.util.internal.PlatformDependent;
|
import io.netty.util.internal.PlatformDependent;
|
||||||
import io.netty.util.internal.SystemPropertyUtil;
|
import io.netty.util.internal.SystemPropertyUtil;
|
||||||
@ -59,9 +59,16 @@ final class Native {
|
|||||||
// The library was not previously loaded, load it now.
|
// The library was not previously loaded, load it now.
|
||||||
loadNativeLibrary();
|
loadNativeLibrary();
|
||||||
}
|
}
|
||||||
Socket.initialize();
|
Unix.registerInternal(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
registerUnix();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static native int registerUnix();
|
||||||
|
|
||||||
static final short EV_ADD = evAdd();
|
static final short EV_ADD = evAdd();
|
||||||
static final short EV_ENABLE = evEnable();
|
static final short EV_ENABLE = evEnable();
|
||||||
static final short EV_DISABLE = evDisable();
|
static final short EV_DISABLE = evDisable();
|
||||||
|
@ -30,7 +30,7 @@ public final class UnixTestUtils {
|
|||||||
if (!file.delete()) {
|
if (!file.delete()) {
|
||||||
throw new IOException("failed to delete: " + file);
|
throw new IOException("failed to delete: " + file);
|
||||||
}
|
}
|
||||||
} while (file.getAbsolutePath().length() > Socket.UDS_SUN_PATH_SIZE);
|
} while (file.getAbsolutePath().length() > 128);
|
||||||
return new DomainSocketAddress(file);
|
return new DomainSocketAddress(file);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
|
85
transport-native-unix-common/src/main/c/netty_unix.c
Normal file
85
transport-native-unix-common/src/main/c/netty_unix.c
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
#include "netty_unix_jni.h"
|
||||||
|
#include "netty_unix.h"
|
||||||
|
#include "netty_unix_buffer.h"
|
||||||
|
#include "netty_unix_errors.h"
|
||||||
|
#include "netty_unix_filedescriptor.h"
|
||||||
|
#include "netty_unix_limits.h"
|
||||||
|
#include "netty_unix_socket.h"
|
||||||
|
#include "netty_unix_util.h"
|
||||||
|
|
||||||
|
jint netty_unix_register(JNIEnv* env, const char* packagePrefix) {
|
||||||
|
int limitsOnLoadCalled = 0;
|
||||||
|
int errorsOnLoadCalled = 0;
|
||||||
|
int filedescriptorOnLoadCalled = 0;
|
||||||
|
int socketOnLoadCalled = 0;
|
||||||
|
int bufferOnLoadCalled = 0;
|
||||||
|
|
||||||
|
// Load all c modules that we depend upon
|
||||||
|
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
limitsOnLoadCalled = 1;
|
||||||
|
|
||||||
|
if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
errorsOnLoadCalled = 1;
|
||||||
|
|
||||||
|
if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
filedescriptorOnLoadCalled = 1;
|
||||||
|
|
||||||
|
if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
socketOnLoadCalled = 1;
|
||||||
|
|
||||||
|
if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
bufferOnLoadCalled = 1;
|
||||||
|
|
||||||
|
return NETTY_JNI_UTIL_JNI_VERSION;
|
||||||
|
error:
|
||||||
|
if (limitsOnLoadCalled == 1) {
|
||||||
|
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
}
|
||||||
|
if (errorsOnLoadCalled == 1) {
|
||||||
|
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
}
|
||||||
|
if (filedescriptorOnLoadCalled == 1) {
|
||||||
|
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
}
|
||||||
|
if (socketOnLoadCalled == 1) {
|
||||||
|
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
}
|
||||||
|
if (bufferOnLoadCalled == 1) {
|
||||||
|
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
}
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
void netty_unix_unregister(JNIEnv* env, const char* packagePrefix) {
|
||||||
|
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
|
||||||
|
}
|
||||||
|
|
25
transport-native-unix-common/src/main/c/netty_unix.h
Normal file
25
transport-native-unix-common/src/main/c/netty_unix.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
#ifndef NETTY_UNIX_H_
|
||||||
|
#define NETTY_UNIX_H_
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
|
||||||
|
// JNI initialization hooks.
|
||||||
|
jint netty_unix_register(JNIEnv* env, const char* packagePrefix);
|
||||||
|
void netty_unix_unregister(JNIEnv* env, const char* packagePrefix);
|
||||||
|
|
||||||
|
#endif /* NETTY_UNIX_H_ */
|
@ -46,7 +46,8 @@ import static io.netty.channel.unix.NativeInetAddress.ipv4MappedIpv6Address;
|
|||||||
*/
|
*/
|
||||||
public class Socket extends FileDescriptor {
|
public class Socket extends FileDescriptor {
|
||||||
|
|
||||||
public static final int UDS_SUN_PATH_SIZE = udsSunPathSize();
|
@Deprecated
|
||||||
|
public static final int UDS_SUN_PATH_SIZE = 100;
|
||||||
|
|
||||||
protected final boolean ipv6;
|
protected final boolean ipv6;
|
||||||
|
|
||||||
|
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.channel.unix;
|
||||||
|
|
||||||
|
import io.netty.util.internal.UnstableApi;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells if <a href="https://netty.io/wiki/native-transports.html">{@code netty-transport-native-unix}</a> is
|
||||||
|
* supported.
|
||||||
|
*/
|
||||||
|
public final class Unix {
|
||||||
|
private static final AtomicBoolean registered = new AtomicBoolean();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal method... Should never be called from the user.
|
||||||
|
*
|
||||||
|
* @param registerTask
|
||||||
|
*/
|
||||||
|
@UnstableApi
|
||||||
|
public static void registerInternal(Runnable registerTask) {
|
||||||
|
if (registered.compareAndSet(false, true)) {
|
||||||
|
registerTask.run();
|
||||||
|
Socket.initialize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if and only if the <a href="https://netty.io/wiki/native-transports.html">{@code
|
||||||
|
* netty_transport_native_unix}</a> is available.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static boolean isAvailable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that <a href="https://netty.io/wiki/native-transports.html">{@code netty_transport_native_unix}</a> is
|
||||||
|
* available.
|
||||||
|
*
|
||||||
|
* @throws UnsatisfiedLinkError if unavailable
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static void ensureAvailability() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the cause of unavailability of <a href="https://netty.io/wiki/native-transports.html">
|
||||||
|
* {@code netty_transport_native_unix}</a>.
|
||||||
|
*
|
||||||
|
* @return the cause if unavailable. {@code null} if available.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static Throwable unavailabilityCause() {
|
||||||
|
return new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Unix() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user