Skip to content

Commit

Permalink
Ensure we only register native methods once
Browse files Browse the repository at this point in the history
Motivation:

We need to ensure we only register native methods once as otherwise we may end up in an "invalid" state. The problem here was that before it was basically the responsibility the user of transport-native-unix-common to register the methods. This is error prone as there may be multiple users of these on the classpath at the same time.

Modifications:

- Provide a way to init native lib without register the native methods of the provided classes. This is needed to be able to re-use functionality which is exposed to our internal native code
- Use flatten plugin to correctly resolve classifier and so have the correct dependency
- Call Unix.* method to ensure we register the methods correctly once
- Include native lib as well in the native jars of unix-common

Result:

Be able to have multiple artifacts of the classpath that depends on the unix-common. Related to netty/netty-incubator-transport-io_uring#15
  • Loading branch information
normanmaurer committed Dec 17, 2020
1 parent a9ec3d8 commit 5372413
Show file tree
Hide file tree
Showing 24 changed files with 483 additions and 366 deletions.
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,33 @@

<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.2</version>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
<configuration>
<flattenMode>oss</flattenMode>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!-- keep surefire and failsafe in sync -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
11 changes: 6 additions & 5 deletions resolver-dns-native-macos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@
<artifactId>netty-transport-native-unix-common</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<!--
The unix-common with classifier dependency is optional because it is not a runtime dependency, but a build time
dependency to get the static library which is built directly into the shared library generated by this project.
-->
<optional>true</optional>
</dependency>
</dependencies>
</profile>
Expand Down Expand Up @@ -165,6 +160,12 @@

<build>
<plugins>
<!-- We need to use the flatten plugin so the classifier portion of the dependency is resolved-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
</plugin>

<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.netty.resolver.dns.macos;

import io.netty.channel.unix.Unix;
import io.netty.resolver.dns.DnsServerAddressStream;
import io.netty.resolver.dns.DnsServerAddressStreamProvider;
import io.netty.resolver.dns.DnsServerAddressStreamProviders;
Expand Down Expand Up @@ -53,6 +54,7 @@ public final class MacOSDnsServerAddressStreamProvider implements DnsServerAddre
static {
Throwable cause = null;
try {
Unix.ensureAvailability();
loadNativeLibrary();
} catch (Throwable error) {
cause = error;
Expand Down
15 changes: 5 additions & 10 deletions transport-native-epoll/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@
<artifactId>netty-transport-native-unix-common</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<!--
The unix-common with classifier dependency is optional because it is not a runtime dependency, but a build time
dependency to get the static library which is built directly into the shared library generated by this project.
-->
<optional>true</optional>
</dependency>
</dependencies>
</profile>
Expand Down Expand Up @@ -351,11 +346,6 @@
<artifactId>netty-transport-native-unix-common</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<!--
The unix-common with classifier dependency is optional because it is not a runtime dependency, but a build time
dependency to get the static library which is built directly into the shared library generated by this project.
-->
<optional>true</optional>
</dependency>
</dependencies>
</profile>
Expand Down Expand Up @@ -405,6 +395,11 @@

<build>
<plugins>
<!-- We need to use the flatten plugin so the classifier portion of the dependency is resolved-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
</plugin>
<!-- Also include c files in source jar -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
30 changes: 15 additions & 15 deletions transport-native-epoll/src/main/c/netty_epoll_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,27 +637,27 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
nativeRegistered = 1;

// Load all c modules that we depend upon
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto done;
}
limitsOnLoadCalled = 1;

if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_errors_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto done;
}
errorsOnLoadCalled = 1;

if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto done;
}
filedescriptorOnLoadCalled = 1;

if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_socket_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto done;
}
socketOnLoadCalled = 1;

if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto done;
}
bufferOnLoadCalled = 1;
Expand Down Expand Up @@ -693,19 +693,19 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
}
if (limitsOnLoadCalled == 1) {
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (errorsOnLoadCalled == 1) {
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (filedescriptorOnLoadCalled == 1) {
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (socketOnLoadCalled == 1) {
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (bufferOnLoadCalled == 1) {
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (linuxsocketOnLoadCalled == 1) {
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);
Expand All @@ -721,11 +721,11 @@ static jint netty_epoll_native_JNI_OnLoad(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_unix_limits_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix, 0);
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);

packetAddrFieldId = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.channel.epoll;

import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.Unix;
import io.netty.util.internal.SystemPropertyUtil;

/**
Expand All @@ -36,6 +37,7 @@ public final class Epoll {
FileDescriptor epollFd = null;
FileDescriptor eventFd = null;
try {
Unix.ensureAvailability();
epollFd = Native.newEpollCreate();
eventFd = Native.newEventFd();
} catch (Throwable t) {
Expand Down
22 changes: 6 additions & 16 deletions transport-native-kqueue/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.fusesource.hawtjni</groupId>
<artifactId>maven-hawtjni-plugin</artifactId>
Expand Down Expand Up @@ -129,11 +128,6 @@
<artifactId>netty-transport-native-unix-common</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<!--
The unix-common with classifier dependency is optional because it is not a runtime dependency, but a build time
dependency to get the static library which is built directly into the shared library generated by this project.
-->
<optional>true</optional>
</dependency>
</dependencies>
</profile>
Expand Down Expand Up @@ -235,11 +229,6 @@
<artifactId>netty-transport-native-unix-common</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<!--
The unix-common with classifier dependency is optional because it is not a runtime dependency, but a build time
dependency to get the static library which is built directly into the shared library generated by this project.
-->
<optional>true</optional>
</dependency>
</dependencies>
</profile>
Expand Down Expand Up @@ -341,11 +330,6 @@
<artifactId>netty-transport-native-unix-common</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<!--
The unix-common with classifier dependency is optional because it is not a runtime dependency, but a build time
dependency to get the static library which is built directly into the shared library generated by this project.
-->
<optional>true</optional>
</dependency>
</dependencies>
</profile>
Expand Down Expand Up @@ -408,6 +392,12 @@

<build>
<plugins>
<!-- We need to use the flatten plugin so the classifier portion of the dependency is resolved-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
</plugin>

<!-- Also include c files in source jar -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
30 changes: 15 additions & 15 deletions transport-native-kqueue/src/main/c/netty_kqueue_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,27 +299,27 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
}
nativeRegistered = 1;
// Load all c modules that we depend upon
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto error;
}
limitsOnLoadCalled = 1;

if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_errors_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto error;
}
errorsOnLoadCalled = 1;

if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto error;
}
filedescriptorOnLoadCalled = 1;

if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_socket_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto error;
}
socketOnLoadCalled = 1;

if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix, 0) == JNI_ERR) {
goto error;
}
bufferOnLoadCalled = 1;
Expand Down Expand Up @@ -351,19 +351,19 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
}
if (limitsOnLoadCalled == 1) {
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (errorsOnLoadCalled == 1) {
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (filedescriptorOnLoadCalled == 1) {
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (socketOnLoadCalled == 1) {
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (bufferOnLoadCalled == 1) {
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix, 0);
}
if (bsdsocketOnLoadCalled == 1) {
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
Expand All @@ -375,11 +375,11 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
}

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_unix_limits_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix, 0);
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix, 0);
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
netty_kqueue_eventarray_JNI_OnUnLoad(env, packagePrefix);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.channel.kqueue;

import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.Unix;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.UnstableApi;

Expand All @@ -34,6 +35,7 @@ public final class KQueue {
} else {
FileDescriptor kqueueFd = null;
try {
Unix.ensureAvailability();
kqueueFd = Native.newKQueue();
} catch (Throwable t) {
cause = t;
Expand Down

0 comments on commit 5372413

Please sign in to comment.