diff options
10 files changed, 187 insertions, 15 deletions
diff --git a/security-utils/pom.xml b/security-utils/pom.xml index ac856dff6c3..5c4deecc437 100644 --- a/security-utils/pom.xml +++ b/security-utils/pom.xml @@ -96,7 +96,7 @@ <configuration> <instructions> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> - <Bundle-Version>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}</Bundle-Version> + <Bundle-Version>7.0.0</Bundle-Version> <Export-Package>com.yahoo.security.*;version=1.0.0;-noimport:=true</Export-Package> <_nouses>true</_nouses> <!-- Don't include 'uses' directives for package exports --> <_fixupmessages>"Classes found in the wrong directory"</_fixupmessages> <!-- Hide warnings for bouncycastle multi-release jars --> diff --git a/vespajlib/src/main/java/com/yahoo/io/NativeIO.java b/vespajlib/src/main/java/com/yahoo/io/NativeIO.java index 28c3f21b24c..af101211b4a 100644 --- a/vespajlib/src/main/java/com/yahoo/io/NativeIO.java +++ b/vespajlib/src/main/java/com/yahoo/io/NativeIO.java @@ -1,6 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.io; +import com.yahoo.nativec.PosixFAdvise; + import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; @@ -10,17 +12,12 @@ import java.io.SyncFailedException; import java.lang.reflect.Field; import java.util.logging.Logger; -import com.sun.jna.LastErrorException; -import com.sun.jna.Native; -import com.sun.jna.Platform; - /** * Provides functionality only possible through native C library. */ public class NativeIO { private static final Logger logger = Logger.getLogger(NativeIO.class.getName()); private static final String DISABLE_NATIVE_IO = "DISABLE_NATIVE_IO"; - private static final int POSIX_FADV_DONTNEED = 4; // See /usr/include/linux/fadvise.h private static final InitResult fdField = new InitResult(); private static class InitResult { private final boolean initialized; @@ -31,17 +28,14 @@ public class NativeIO { boolean initComplete = false; boolean disabled = true; Field field = null; - Throwable exception = null; + Throwable exception = PosixFAdvise.init(); try { - if (Platform.isLinux()) { + if (exception == null) { disabled = System.getenv().containsKey(DISABLE_NATIVE_IO); if (!disabled) { - Native.register(Platform.C_LIBRARY_NAME); field = getField(FileDescriptor.class, "fd"); initComplete = true; } - } else { - exception = new RuntimeException("Platform is unsúpported. Only supported on linux."); } } catch (Throwable throwable) { exception = throwable; @@ -68,14 +62,12 @@ public class NativeIO { } } - private static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException; - public NativeIO() { if ( ! fdField.isInitialized()) { if (fdField.isEnabled()) { logger.warning("Native IO not possible due to " + getError().getMessage()); } else { - logger.info("Native IO has been disable explicit via system property " + DISABLE_NATIVE_IO); + logger.info("Native IO has been disabled explicit via system property " + DISABLE_NATIVE_IO); } } } @@ -96,7 +88,7 @@ public class NativeIO { } } if (valid()) { - posix_fadvise(fdField.getNativeFD(fd), offset, len, POSIX_FADV_DONTNEED); + PosixFAdvise.posix_fadvise(fdField.getNativeFD(fd), offset, len, PosixFAdvise.POSIX_FADV_DONTNEED); } } /** diff --git a/vespajlib/src/main/java/com/yahoo/nativec/GLibcVersion.java b/vespajlib/src/main/java/com/yahoo/nativec/GLibcVersion.java new file mode 100644 index 00000000000..67ae30c84f5 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/nativec/GLibcVersion.java @@ -0,0 +1,21 @@ +package com.yahoo.nativec; + +public class GLibcVersion { + private final static Throwable initException = NativeC.loadLibrary(GLibcVersion.class); + public static Throwable init() { + return initException; + } + private final String version; + private final int major; + private final int minor; + public GLibcVersion() { + version = gnu_get_libc_version(); + String [] parts = version.split("\\."); + major = parts.length > 0 ? Integer.valueOf(parts[0]) : -1; + minor = parts.length > 1 ? Integer.valueOf(parts[1]) : -1; + } + private native static String gnu_get_libc_version(); + public String version() { return version; } + public int major() { return major; } + public int minor() { return minor; } +} diff --git a/vespajlib/src/main/java/com/yahoo/nativec/MallInfo.java b/vespajlib/src/main/java/com/yahoo/nativec/MallInfo.java new file mode 100644 index 00000000000..a4f5486ccf1 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/nativec/MallInfo.java @@ -0,0 +1,30 @@ +package com.yahoo.nativec; + +import com.sun.jna.Structure; + +public class MallInfo { + private final static Throwable initException = NativeC.loadLibrary(MallInfo.class); + public static Throwable init() { + return initException; + } + + @Structure.FieldOrder({"arena", "ordblks", "smblks", "hblks", "hblkhd", "usmblks", "fsmblks", "uordblks", "fordblks", "keepcost"}) + public static class MallInfoStruct extends Structure { + public static class ByValue extends MallInfoStruct implements Structure.ByValue { } + public int arena; /* Non-mmapped space allocated (bytes) */ + public int ordblks; /* Number of free chunks */ + public int smblks; /* Number of free fastbin blocks */ + public int hblks; /* Number of mmapped regions */ + public int hblkhd; /* Space allocated in mmapped regions (bytes) */ + public int usmblks; /* See below */ + public int fsmblks; /* Space in freed fastbin blocks (bytes) */ + public int uordblks; /* Total allocated space (bytes) */ + public int fordblks; /* Total free space (bytes) */ + public int keepcost; /* Top-most, releasable space (bytes) */ + } + private static native MallInfoStruct.ByValue mallinfo(); + public MallInfo() { + mallinfo = mallinfo(); + } + private final MallInfoStruct mallinfo; +} diff --git a/vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java b/vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java new file mode 100644 index 00000000000..1ae3bc590e2 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java @@ -0,0 +1,30 @@ +package com.yahoo.nativec; + +import com.sun.jna.Structure; + +public class MallInfo2 { + private final static Throwable initException = NativeC.loadLibrary(MallInfo2.class); + public static Throwable init() { + return initException; + } + + @Structure.FieldOrder({"arena", "ordblks", "smblks", "hblks", "hblkhd", "usmblks", "fsmblks", "uordblks", "fordblks", "keepcost"}) + public static class MallInfo2Struct extends Structure { + public static class ByValue extends MallInfo2Struct implements Structure.ByValue { } + public long arena; /* Non-mmapped space allocated (bytes) */ + public long ordblks; /* Number of free chunks */ + public long smblks; /* Number of free fastbin blocks */ + public long hblks; /* Number of mmapped regions */ + public long hblkhd; /* Space allocated in mmapped regions (bytes) */ + public long usmblks; /* See below */ + public long fsmblks; /* Space in freed fastbin blocks (bytes) */ + public long uordblks; /* Total allocated space (bytes) */ + public long fordblks; /* Total free space (bytes) */ + public long keepcost; /* Top-most, releasable space (bytes) */ + } + private static native MallInfo2Struct.ByValue mallinfo2(); + public MallInfo2() { + mallinfo = mallinfo2(); + } + private final MallInfo2Struct mallinfo; +} diff --git a/vespajlib/src/main/java/com/yahoo/nativec/NativeC.java b/vespajlib/src/main/java/com/yahoo/nativec/NativeC.java new file mode 100644 index 00000000000..4d808b4b155 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/nativec/NativeC.java @@ -0,0 +1,20 @@ +package com.yahoo.nativec; + +import com.sun.jna.Native; +import com.sun.jna.Platform; + +class NativeC { + static Throwable loadLibrary(Class<?> cls) { + if (Platform.isLinux()) { + try { + Native.register(cls, Platform.C_LIBRARY_NAME); + } catch (Throwable e) { + return e; + } + } else { + return new RuntimeException("Platform is unsúpported. Only supported on linux."); + } + return null; + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java b/vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java new file mode 100644 index 00000000000..3e2c26d2ef2 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java @@ -0,0 +1,12 @@ +package com.yahoo.nativec; + +import com.sun.jna.LastErrorException; + +public class PosixFAdvise { + public static final int POSIX_FADV_DONTNEED = 4; // See /usr/include/linux/fadvise.h + private final static Throwable initException = NativeC.loadLibrary(PosixFAdvise.class); + public static Throwable init() { + return initException; + } + public static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException; +} diff --git a/vespajlib/src/main/java/com/yahoo/nativec/package-info.java b/vespajlib/src/main/java/com/yahoo/nativec/package-info.java new file mode 100644 index 00000000000..cdf497ea39b --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/nativec/package-info.java @@ -0,0 +1,5 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +package com.yahoo.nativec; + +import com.yahoo.osgi.annotation.ExportPackage; diff --git a/vespajlib/src/test/java/com/yahoo/nativec/GlibCTestCase.java b/vespajlib/src/test/java/com/yahoo/nativec/GlibCTestCase.java new file mode 100644 index 00000000000..3a37116f606 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/nativec/GlibCTestCase.java @@ -0,0 +1,33 @@ +package com.yahoo.nativec; + +import com.sun.jna.Platform; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class GlibCTestCase { + @Test + public void requireThatPosixFAdviseIsDetected() { + if (Platform.isLinux()) { + assertNull(PosixFAdvise.init()); + } else { + assertEquals("Platform is unsúpported. Only supported on linux.", PosixFAdvise.init().getMessage()); + } + } + + @Test + public void requireGlibcVersionIsDetected() { + if (Platform.isLinux()) { + assertNull(GLibcVersion.init()); + GLibcVersion version = new GLibcVersion(); + assertNotEquals("", version.version()); + assertTrue(version.major() >= 2); + assertTrue((version.major() >= 3) || ((version.major() == 2) && (version.minor() >= 17))); + } else { + assertEquals("Platform is unsúpported. Only supported on linux.", PosixFAdvise.init().getMessage()); + } + } +} diff --git a/vespajlib/src/test/java/com/yahoo/nativec/MallInfoTestCase.java b/vespajlib/src/test/java/com/yahoo/nativec/MallInfoTestCase.java new file mode 100644 index 00000000000..e6b3e1cbf85 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/nativec/MallInfoTestCase.java @@ -0,0 +1,29 @@ +package com.yahoo.nativec; + +import com.sun.jna.Platform; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class MallInfoTestCase { + @Test + public void requireThatMallInfo2IsDetected() { + if (Platform.isLinux()) { + GLibcVersion version = new GLibcVersion(); + if ((version.major() >= 3) || ((version.major() == 2) && (version.minor() >= 33))) { + assertNull(MallInfo2.init()); + } + } else { + assertEquals("Platform is unsúpported. Only supported on linux.", MallInfo2.init().getMessage()); + } + } + @Test + public void requireThatMallInfoIsDetected() { + if (Platform.isLinux()) { + assertNull(MallInfo.init()); + } else { + assertEquals("Platform is unsúpported. Only supported on linux.", MallInfo.init().getMessage()); + } + } +} |