diff --git a/make/modules/java.base/Lib.gmk b/make/modules/java.base/Lib.gmk index 51d323a0344f2..bd65eb989af79 100644 --- a/make/modules/java.base/Lib.gmk +++ b/make/modules/java.base/Lib.gmk @@ -75,6 +75,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBNIO, \ NAME := nio, \ OPTIMIZATION := HIGH, \ EXTRA_HEADER_DIRS := \ + libjava/nio/ch \ libnio/ch \ libnio/fs \ libjvm, \ @@ -86,7 +87,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBNIO, \ LIBS_macosx := \ -framework CoreFoundation \ -framework CoreServices, \ - LIBS_windows := advapi32.lib mswsock.lib ws2_32.lib, \ + LIBS_windows := advapi32.lib ws2_32.lib, \ )) TARGETS += $(BUILD_LIBNIO) diff --git a/make/modules/java.base/lib/CoreLibraries.gmk b/make/modules/java.base/lib/CoreLibraries.gmk index 316103be4cd95..f5d61d912a853 100644 --- a/make/modules/java.base/lib/CoreLibraries.gmk +++ b/make/modules/java.base/lib/CoreLibraries.gmk @@ -55,6 +55,8 @@ endif $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \ NAME := java, \ OPTIMIZATION := HIGH, \ + EXTRA_HEADER_DIRS := \ + libjava/nio/ch, \ jdk_util.c_CFLAGS := $(VERSION_CFLAGS), \ ProcessImpl_md.c_CFLAGS := $(VERSION_CFLAGS), \ java_props_md.c_CFLAGS := \ @@ -62,13 +64,14 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \ DISABLED_WARNINGS_gcc_ProcessImpl_md.c := unused-result, \ DISABLED_WARNINGS_clang_TimeZone_md.c := unused-variable, \ JDK_LIBS := libjvm, \ - LIBS_linux := $(LIBDL), \ + LIBS_linux := $(LIBDL) -lpthread, \ LIBS_aix := $(LIBDL) $(LIBM), \ LIBS_macosx := \ -framework CoreFoundation \ + -framework CoreServices \ -framework Foundation \ -framework SystemConfiguration, \ - LIBS_windows := advapi32.lib ole32.lib shell32.lib version.lib, \ + LIBS_windows := advapi32.lib mswsock.lib ole32.lib shell32.lib version.lib ws2_32.lib, \ )) TARGETS += $(BUILD_LIBJAVA) diff --git a/make/modules/jdk.sctp/Lib.gmk b/make/modules/jdk.sctp/Lib.gmk index 327f4fa610464..eb462d01f5031 100644 --- a/make/modules/jdk.sctp/Lib.gmk +++ b/make/modules/jdk.sctp/Lib.gmk @@ -38,8 +38,8 @@ ifeq ($(call isTargetOs, linux), true) EXTRA_HEADER_DIRS := \ $(call GetJavaHeaderDir, java.base) \ java.base:libjvm \ - java.base:libnio \ - java.base:libnio/ch, \ + java.base:libjava/nio \ + java.base:libjava/nio/ch, \ JDK_LIBS := java.base:libjava java.base:libnet, \ LIBS_linux := $(LIBDL) $(LIBPTHREAD), \ )) diff --git a/src/java.base/linux/classes/sun/nio/ch/EPoll.java b/src/java.base/linux/classes/sun/nio/ch/EPoll.java index d96582ad90f21..bf17b09ec299e 100644 --- a/src/java.base/linux/classes/sun/nio/ch/EPoll.java +++ b/src/java.base/linux/classes/sun/nio/ch/EPoll.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -51,7 +51,7 @@ private EPoll() { } * } */ static { - IOUtil.load(); + NIOUtil.load(); } private static final int SIZEOF_EPOLLEVENT = eventSize(); private static final int OFFSETOF_EVENTS = eventsOffset(); diff --git a/src/java.base/linux/classes/sun/nio/ch/EPollPort.java b/src/java.base/linux/classes/sun/nio/ch/EPollPort.java index f2a4649d6f8e1..217237e654726 100644 --- a/src/java.base/linux/classes/sun/nio/ch/EPollPort.java +++ b/src/java.base/linux/classes/sun/nio/ch/EPollPort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -95,7 +95,7 @@ static class Event { // create socket pair for wakeup mechanism try { - long fds = IOUtil.makePipe(true); + long fds = NIOUtil.makePipe(true); this.sp = new int[]{(int) (fds >>> 32), (int) fds}; } catch (IOException ioe) { EPoll.freePollArray(address); @@ -136,7 +136,7 @@ private void wakeup() { if (wakeupCount.incrementAndGet() == 1) { // write byte to socketpair to force wakeup try { - IOUtil.write1(sp[1], (byte)0); + NIOUtil.write1(sp[1], (byte)0); } catch (IOException x) { throw new AssertionError(x); } @@ -219,7 +219,7 @@ private Event poll() throws IOException { // a wakeup byte queued to wake each thread int nread; do { - nread = IOUtil.drain1(sp[0]); + nread = NIOUtil.drain1(sp[0]); } while (nread == IOStatus.INTERRUPTED); } diff --git a/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java b/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java index 14f4e87448b31..ff4407c93d802 100644 --- a/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java +++ b/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java @@ -49,7 +49,7 @@ class EPollSelectorImpl extends SelectorImpl { // maximum number of events to poll in one call to epoll_wait - private static final int NUM_EPOLLEVENTS = Math.min(IOUtil.fdLimit(), 1024); + private static final int NUM_EPOLLEVENTS = Math.min(NIOUtil.fdLimit(), 1024); // epoll file descriptor private final int epfd; @@ -79,7 +79,7 @@ class EPollSelectorImpl extends SelectorImpl { try { this.eventfd = new EventFD(); - IOUtil.configureBlocking(IOUtil.newFD(eventfd.efd()), false); + NIOUtil.configureBlocking(NIOUtil.newFD(eventfd.efd()), false); } catch (IOException ioe) { EPoll.freePollArray(pollArrayAddress); FileDispatcherImpl.closeIntFD(epfd); diff --git a/src/java.base/linux/classes/sun/nio/ch/EventFD.java b/src/java.base/linux/classes/sun/nio/ch/EventFD.java index adc41d04ece25..152a375c500d5 100644 --- a/src/java.base/linux/classes/sun/nio/ch/EventFD.java +++ b/src/java.base/linux/classes/sun/nio/ch/EventFD.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -49,7 +49,7 @@ void set() throws IOException { } void reset() throws IOException { - IOUtil.drain(efd); + NIOUtil.drain(efd); } void close() throws IOException { @@ -68,6 +68,6 @@ void close() throws IOException { private static native int set0(int efd) throws IOException; static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/linux/classes/sun/nio/ch/FileDispatcherImpl.java b/src/java.base/linux/classes/sun/nio/ch/FileDispatcherImpl.java index a69f240a608ff..78d440c16645b 100644 --- a/src/java.base/linux/classes/sun/nio/ch/FileDispatcherImpl.java +++ b/src/java.base/linux/classes/sun/nio/ch/FileDispatcherImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -58,7 +58,6 @@ static native long transferFrom0(FileDescriptor src, FileDescriptor dst, static native void init0(); static { - IOUtil.load(); init0(); } } diff --git a/src/java.base/linux/classes/sun/nio/fs/LinuxNativeDispatcher.java b/src/java.base/linux/classes/sun/nio/fs/LinuxNativeDispatcher.java index af42c245c1c7b..08ec6658f3f64 100644 --- a/src/java.base/linux/classes/sun/nio/fs/LinuxNativeDispatcher.java +++ b/src/java.base/linux/classes/sun/nio/fs/LinuxNativeDispatcher.java @@ -90,7 +90,6 @@ static native int directCopy0(int dst, int src, long addressToPollForCancel) private static native void init(); static { - jdk.internal.loader.BootLoader.loadLibrary("nio"); init(); } } diff --git a/src/java.base/linux/native/libnio/ch/FileDispatcherImpl.c b/src/java.base/linux/native/libjava/nio/ch/FileDispatcherImpl.c similarity index 100% rename from src/java.base/linux/native/libnio/ch/FileDispatcherImpl.c rename to src/java.base/linux/native/libjava/nio/ch/FileDispatcherImpl.c diff --git a/src/java.base/linux/native/libnio/fs/LinuxNativeDispatcher.c b/src/java.base/linux/native/libjava/nio/fs/LinuxNativeDispatcher.c similarity index 100% rename from src/java.base/linux/native/libnio/fs/LinuxNativeDispatcher.c rename to src/java.base/linux/native/libjava/nio/fs/LinuxNativeDispatcher.c diff --git a/src/java.base/linux/native/libnio/fs/LinuxWatchService.c b/src/java.base/linux/native/libjava/nio/fs/LinuxWatchService.c similarity index 100% rename from src/java.base/linux/native/libnio/fs/LinuxWatchService.c rename to src/java.base/linux/native/libjava/nio/fs/LinuxWatchService.c diff --git a/src/java.base/macosx/classes/sun/nio/ch/KQueue.java b/src/java.base/macosx/classes/sun/nio/ch/KQueue.java index 30ad9ca92fe1d..64719a3254967 100644 --- a/src/java.base/macosx/classes/sun/nio/ch/KQueue.java +++ b/src/java.base/macosx/classes/sun/nio/ch/KQueue.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -48,7 +48,7 @@ private KQueue() { } * }; */ static { - IOUtil.load(); + NIOUtil.load(); } private static final int SIZEOF_KQUEUEEVENT = keventSize(); private static final int OFFSET_IDENT = identOffset(); diff --git a/src/java.base/macosx/classes/sun/nio/ch/KQueuePort.java b/src/java.base/macosx/classes/sun/nio/ch/KQueuePort.java index e6c0e09ad3465..4b24f1dda72c0 100644 --- a/src/java.base/macosx/classes/sun/nio/ch/KQueuePort.java +++ b/src/java.base/macosx/classes/sun/nio/ch/KQueuePort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -91,7 +91,7 @@ static class Event { // create socket pair for wakeup mechanism try { - long fds = IOUtil.makePipe(true); + long fds = NIOUtil.makePipe(true); this.sp = new int[]{(int) (fds >>> 32), (int) fds}; } catch (IOException ioe) { KQueue.freePollArray(address); @@ -133,7 +133,7 @@ private void wakeup() { if (wakeupCount.incrementAndGet() == 1) { // write byte to socketpair to force wakeup try { - IOUtil.write1(sp[1], (byte)0); + NIOUtil.write1(sp[1], (byte)0); } catch (IOException x) { throw new AssertionError(x); } @@ -220,7 +220,7 @@ private Event poll() throws IOException { // a wakeup byte queued to wake each thread int nread; do { - nread = IOUtil.drain1(sp[0]); + nread = NIOUtil.drain1(sp[0]); } while (nread == IOStatus.INTERRUPTED); } diff --git a/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java b/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java index b1a781d71446b..3d39aba02d5d1 100644 --- a/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java +++ b/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java @@ -82,7 +82,7 @@ class KQueueSelectorImpl extends SelectorImpl { this.pollArrayAddress = KQueue.allocatePollArray(MAX_KEVENTS); try { - long fds = IOUtil.makePipe(false); + long fds = NIOUtil.makePipe(false); this.fd0 = (int) (fds >>> 32); this.fd1 = (int) fds; } catch (IOException ioe) { @@ -323,7 +323,7 @@ public Selector wakeup() { synchronized (interruptLock) { if (!interruptTriggered) { try { - IOUtil.write1(fd1, (byte)0); + NIOUtil.write1(fd1, (byte)0); } catch (IOException ioe) { throw new InternalError(ioe); } @@ -335,7 +335,7 @@ public Selector wakeup() { private void clearInterrupt() throws IOException { synchronized (interruptLock) { - IOUtil.drain(fd0); + NIOUtil.drain(fd0); interruptTriggered = false; } } diff --git a/src/java.base/macosx/native/libnio/ch/FileDispatcherImpl.c b/src/java.base/macosx/native/libjava/nio/ch/FileDispatcherImpl.c similarity index 100% rename from src/java.base/macosx/native/libnio/ch/FileDispatcherImpl.c rename to src/java.base/macosx/native/libjava/nio/ch/FileDispatcherImpl.c diff --git a/src/java.base/macosx/native/libnio/fs/BsdFileSystem.c b/src/java.base/macosx/native/libjava/nio/fs/BsdFileSystem.c similarity index 100% rename from src/java.base/macosx/native/libnio/fs/BsdFileSystem.c rename to src/java.base/macosx/native/libjava/nio/fs/BsdFileSystem.c diff --git a/src/java.base/macosx/native/libnio/fs/BsdNativeDispatcher.c b/src/java.base/macosx/native/libjava/nio/fs/BsdNativeDispatcher.c similarity index 100% rename from src/java.base/macosx/native/libnio/fs/BsdNativeDispatcher.c rename to src/java.base/macosx/native/libjava/nio/fs/BsdNativeDispatcher.c diff --git a/src/java.base/macosx/native/libnio/fs/MacOSXNativeDispatcher.c b/src/java.base/macosx/native/libjava/nio/fs/MacOSXNativeDispatcher.c similarity index 100% rename from src/java.base/macosx/native/libnio/fs/MacOSXNativeDispatcher.c rename to src/java.base/macosx/native/libjava/nio/fs/MacOSXNativeDispatcher.c diff --git a/src/java.base/macosx/native/libnio/fs/UTIFileTypeDetector.c b/src/java.base/macosx/native/libjava/nio/fs/UTIFileTypeDetector.c similarity index 100% rename from src/java.base/macosx/native/libnio/fs/UTIFileTypeDetector.c rename to src/java.base/macosx/native/libjava/nio/fs/UTIFileTypeDetector.c diff --git a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java index afb312ed722c4..fe881f736ea5b 100644 --- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java @@ -1144,7 +1144,7 @@ private void lockedConfigureBlocking(boolean block) throws IOException { ensureOpen(); // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } } } @@ -1157,7 +1157,7 @@ private boolean tryLockedConfigureBlocking(boolean block) throws IOException { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); synchronized (stateLock) { if (!forcedNonBlocking && isOpen()) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); return true; } else { return false; @@ -1174,7 +1174,7 @@ private void configureSocketNonBlocking() throws IOException { if (!forcedNonBlocking) { synchronized (stateLock) { ensureOpen(); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); forcedNonBlocking = true; } } @@ -1412,7 +1412,7 @@ private void repairSocket(InetSocketAddress target) // copy the blocking mode if (!isBlocking() || forcedNonBlocking) { - IOUtil.configureBlocking(newfd, false); + NIOUtil.configureBlocking(newfd, false); } // dup this channel's socket to the new socket. If this succeeds then @@ -1944,6 +1944,6 @@ private static native int send0(FileDescriptor fd, long address, int len, throws IOException; static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/share/classes/sun/nio/ch/IOUtil.java b/src/java.base/share/classes/sun/nio/ch/IOUtil.java index 45f8cb2e588c3..c2f3812bbe240 100644 --- a/src/java.base/share/classes/sun/nio/ch/IOUtil.java +++ b/src/java.base/share/classes/sun/nio/ch/IOUtil.java @@ -559,58 +559,24 @@ static long bufferAddress(ByteBuffer buf) { return NIO_ACCESS.getBufferAddress(buf); } - public static FileDescriptor newFD(int i) { - FileDescriptor fd = new FileDescriptor(); - setfdVal(fd, i); - return fd; - } - - static native boolean randomBytes(byte[] someBytes); - - /** - * Returns two file descriptors for a pipe encoded in a long. - * The read end of the pipe is returned in the high 32 bits, - * while the write end is returned in the low 32 bits. - */ - static native long makePipe(boolean blocking) throws IOException; - - static native int write1(int fd, byte b) throws IOException; - - /** - * Read and discard all bytes. - */ - static native boolean drain(int fd) throws IOException; - - /** - * Read and discard at most one byte - * @return the number of bytes read or IOS_INTERRUPTED - */ - static native int drain1(int fd) throws IOException; - - public static native void configureBlocking(FileDescriptor fd, - boolean blocking) - throws IOException; - public static native int fdVal(FileDescriptor fd); static native void setfdVal(FileDescriptor fd, int value); - static native int fdLimit(); - - static native int iovMax(); + private static native int iovMax(); - static native long writevMax(); + private static native long writevMax(); - static native void initIDs(); + private static native void initIDs(); /** - * Used to trigger loading of native libraries + * Used to trigger initialization of static final fields and of + * FileDescriptor field IDs in the native layer */ public static void load() { } static { - jdk.internal.loader.BootLoader.loadLibrary("net"); - jdk.internal.loader.BootLoader.loadLibrary("nio"); + // Initialize FileDescriptor field IDs in the native layer initIDs(); IOV_MAX = iovMax(); diff --git a/src/java.base/share/classes/sun/nio/ch/NIOUtil.java b/src/java.base/share/classes/sun/nio/ch/NIOUtil.java new file mode 100644 index 0000000000000..9cc9fa37139b9 --- /dev/null +++ b/src/java.base/share/classes/sun/nio/ch/NIOUtil.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package sun.nio.ch; + +import java.io.FileDescriptor; +import java.io.IOException; + +/** + * File-descriptor based I/O utilities that are shared by NIO classes. + */ + +public final class NIOUtil { + + private NIOUtil() { } // No instantiation + + public static FileDescriptor newFD(int i) { + FileDescriptor fd = new FileDescriptor(); + IOUtil.setfdVal(fd, i); + return fd; + } + + /** + * Returns two file descriptors for a pipe encoded in a long. + * The read end of the pipe is returned in the high 32 bits, + * while the write end is returned in the low 32 bits. + */ + static native long makePipe(boolean blocking) throws IOException; + + static native int write1(int fd, byte b) throws IOException; + + /** + * Read and discard all bytes. + */ + static native boolean drain(int fd) throws IOException; + + /** + * Read and discard at most one byte + * @return the number of bytes read or IOS_INTERRUPTED + */ + static native int drain1(int fd) throws IOException; + + public static native void configureBlocking(FileDescriptor fd, + boolean blocking) + throws IOException; + + static native int fdLimit(); + + /** + * Used to trigger loading of native libraries + */ + public static void load() { } + + static { + jdk.internal.loader.BootLoader.loadLibrary("net"); + jdk.internal.loader.BootLoader.loadLibrary("nio"); + } + +} diff --git a/src/java.base/share/classes/sun/nio/ch/NativeSocketAddress.java b/src/java.base/share/classes/sun/nio/ch/NativeSocketAddress.java index c1f813ec5249a..223ff5ffbb914 100644 --- a/src/java.base/share/classes/sun/nio/ch/NativeSocketAddress.java +++ b/src/java.base/share/classes/sun/nio/ch/NativeSocketAddress.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -341,6 +341,6 @@ private static void putAddress(long address, Inet6Address ia) { private static native int offsetSin6FlowInfo(); static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/share/classes/sun/nio/ch/Net.java b/src/java.base/share/classes/sun/nio/ch/Net.java index 9ec7975a35cd3..38270593d7ac5 100644 --- a/src/java.base/share/classes/sun/nio/ch/Net.java +++ b/src/java.base/share/classes/sun/nio/ch/Net.java @@ -488,7 +488,7 @@ static FileDescriptor socket() throws IOException { static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException { boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); - return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK)); + return NIOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK)); } static FileDescriptor serverSocket() { @@ -498,7 +498,7 @@ static FileDescriptor serverSocket() { static FileDescriptor serverSocket(ProtocolFamily family) { boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); - return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK)); + return NIOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK)); } // Due to oddities SO_REUSEADDR on Windows reuse is ignored @@ -770,7 +770,7 @@ static native int blockOrUnblock6(boolean block, FileDescriptor fd, byte[] group static native short pollconnValue(); static { - IOUtil.load(); + NIOUtil.load(); initIDs(); POLLIN = pollinValue(); diff --git a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java index dd81b356738e1..84680c120de62 100644 --- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java @@ -211,7 +211,7 @@ private void configureNonBlockingIfNeeded(FileDescriptor fd, boolean timed) if (!nonBlocking && (timed || Thread.currentThread().isVirtual())) { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); nonBlocking = true; } } @@ -766,7 +766,7 @@ protected void accept(SocketImpl si) throws IOException { InetSocketAddress localAddress; try { localAddress = Net.localAddress(newfd); - IOUtil.configureBlocking(newfd, true); + NIOUtil.configureBlocking(newfd, true); } catch (IOException ioe) { nd.close(newfd); throw ioe; diff --git a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java index b8e25b23b69e8..3ba1136c122d5 100644 --- a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java @@ -484,7 +484,7 @@ private SocketChannel finishAccept(FileDescriptor newfd, SocketAddress sa) { try { // newly accepted socket is initially in blocking mode - IOUtil.configureBlocking(newfd, true); + NIOUtil.configureBlocking(newfd, true); return new SocketChannelImpl(provider(), family, newfd, sa); } catch (Exception e) { nd.close(newfd); @@ -511,7 +511,7 @@ private void lockedConfigureBlocking(boolean block) throws IOException { ensureOpen(); // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } } } @@ -525,7 +525,7 @@ private boolean tryLockedConfigureBlocking(boolean block) throws IOException { synchronized (stateLock) { // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking && isOpen()) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); return true; } else { return false; @@ -541,7 +541,7 @@ private void configureSocketNonBlockingIfVirtualThread() throws IOException { if (!forcedNonBlocking && Thread.currentThread().isVirtual()) { synchronized (stateLock) { ensureOpen(); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); forcedNonBlocking = true; } } diff --git a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java index 37e6a71d80df8..df76a92c6930d 100644 --- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java @@ -725,7 +725,7 @@ private void lockedConfigureBlocking(boolean block) throws IOException { ensureOpen(); // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } } } @@ -739,7 +739,7 @@ private boolean tryLockedConfigureBlocking(boolean block) throws IOException { synchronized (stateLock) { // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking && isOpen()) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); return true; } else { return false; @@ -755,7 +755,7 @@ private void configureSocketNonBlockingIfVirtualThread() throws IOException { if (!forcedNonBlocking && Thread.currentThread().isVirtual()) { synchronized (stateLock) { ensureOpen(); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); forcedNonBlocking = true; } } diff --git a/src/java.base/share/classes/sun/nio/ch/UnixDomainSockets.java b/src/java.base/share/classes/sun/nio/ch/UnixDomainSockets.java index 66c9fa40f2e23..3ea292b26b1ef 100644 --- a/src/java.base/share/classes/sun/nio/ch/UnixDomainSockets.java +++ b/src/java.base/share/classes/sun/nio/ch/UnixDomainSockets.java @@ -76,7 +76,7 @@ static byte[] getPathBytes(Path path) { } static FileDescriptor socket() throws IOException { - return IOUtil.newFD(socket0()); + return NIOUtil.newFD(socket0()); } static void bind(FileDescriptor fd, Path addr) throws IOException { @@ -157,7 +157,7 @@ private static native int accept0(FileDescriptor fd, FileDescriptor newfd, Objec static { // Load all required native libs - IOUtil.load(); + NIOUtil.load(); supported = init(); } } diff --git a/src/java.base/share/native/libnio/ch/nio.h b/src/java.base/share/native/libjava/nio/ch/nio.h similarity index 100% rename from src/java.base/share/native/libnio/ch/nio.h rename to src/java.base/share/native/libjava/nio/ch/nio.h diff --git a/src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java b/src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java index 8f1692f0e4df6..ba5f576125845 100644 --- a/src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java +++ b/src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java @@ -35,10 +35,6 @@ class DatagramDispatcher extends UnixDispatcher { - static { - IOUtil.load(); - } - @Override int read(FileDescriptor fd, long address, int len) throws IOException { return read0(fd, address, len); diff --git a/src/java.base/unix/classes/sun/nio/ch/InheritedChannel.java b/src/java.base/unix/classes/sun/nio/ch/InheritedChannel.java index 06d5ce47c1516..3ecfd227f176a 100644 --- a/src/java.base/unix/classes/sun/nio/ch/InheritedChannel.java +++ b/src/java.base/unix/classes/sun/nio/ch/InheritedChannel.java @@ -265,7 +265,7 @@ private static String unixPeerAddress(int fd) throws IOException { private static native boolean isConnected(int fd); static { - IOUtil.load(); + NIOUtil.load(); initIDs(); } } diff --git a/src/java.base/unix/classes/sun/nio/ch/NativeThread.java b/src/java.base/unix/classes/sun/nio/ch/NativeThread.java index 8d0bcea48d9b5..11860258c9329 100644 --- a/src/java.base/unix/classes/sun/nio/ch/NativeThread.java +++ b/src/java.base/unix/classes/sun/nio/ch/NativeThread.java @@ -104,7 +104,6 @@ static boolean supportPendingSignals() { private static native void init(); static { - IOUtil.load(); init(); } diff --git a/src/java.base/unix/classes/sun/nio/ch/PipeImpl.java b/src/java.base/unix/classes/sun/nio/ch/PipeImpl.java index f4ef6ce147191..bf5516cd95756 100644 --- a/src/java.base/unix/classes/sun/nio/ch/PipeImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/PipeImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -39,7 +39,7 @@ class PipeImpl private final SinkChannel sink; PipeImpl(SelectorProvider sp) throws IOException { - long pipeFds = IOUtil.makePipe(true); + long pipeFds = NIOUtil.makePipe(true); int readFd = (int) (pipeFds >>> 32); int writeFd = (int) pipeFds; FileDescriptor sourcefd = new FileDescriptor(); diff --git a/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java b/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java index d29eacb81c1b0..79af982081e79 100644 --- a/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java @@ -74,7 +74,7 @@ class PollSelectorImpl extends SelectorImpl { this.pollArray = new AllocatedNativeObject(size, false); try { - long fds = IOUtil.makePipe(false); + long fds = NIOUtil.makePipe(false); this.fd0 = (int) (fds >>> 32); this.fd1 = (int) fds; } catch (IOException ioe) { @@ -241,7 +241,7 @@ public Selector wakeup() { synchronized (interruptLock) { if (!interruptTriggered) { try { - IOUtil.write1(fd1, (byte)0); + NIOUtil.write1(fd1, (byte)0); } catch (IOException ioe) { throw new InternalError(ioe); } @@ -253,7 +253,7 @@ public Selector wakeup() { private void clearInterrupt() throws IOException { synchronized (interruptLock) { - IOUtil.drain(fd0); + NIOUtil.drain(fd0); interruptTriggered = false; } } @@ -384,6 +384,6 @@ private int getReventOps(int i) { private static native int poll(long pollAddress, int numfds, int timeout); static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java index b073c287bfb91..ebf1c44c20754 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java @@ -108,7 +108,7 @@ private void configureSocketNonBlockingIfVirtualThread() throws IOException { if (!forcedNonBlocking && Thread.currentThread().isVirtual()) { synchronized (stateLock) { ensureOpen(); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); forcedNonBlocking = true; } } @@ -211,7 +211,7 @@ protected void implConfigureBlocking(boolean block) throws IOException { ensureOpen(); // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } } } finally { diff --git a/src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java b/src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java index ebc4b2e8fbe7f..af924c6731bf1 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java +++ b/src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java @@ -83,8 +83,4 @@ private static native int write0(FileDescriptor fd, long address, int len) private static native long writev0(FileDescriptor fd, long address, int len) throws IOException; - - static { - IOUtil.load(); - } } diff --git a/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java index 571d7f483d20d..035fc3f2ca215 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java @@ -108,7 +108,7 @@ private void configureSocketNonBlockingIfVirtualThread() throws IOException { if (!forcedNonBlocking && Thread.currentThread().isVirtual()) { synchronized (stateLock) { ensureOpen(); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); forcedNonBlocking = true; } } @@ -211,7 +211,7 @@ protected void implConfigureBlocking(boolean block) throws IOException { ensureOpen(); // do nothing if virtual thread has forced the socket to be non-blocking if (!forcedNonBlocking) { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } } } finally { diff --git a/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java index e98756c7e64cb..3f523fdf514b5 100644 --- a/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java @@ -67,7 +67,7 @@ private void enableAccept() { super(port); try { - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); } catch (IOException x) { nd.close(fd); // prevent leak throw x; diff --git a/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java index 5e75e3b2486d6..dafcf57f642a0 100644 --- a/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java @@ -101,7 +101,7 @@ private static enum OpType { CONNECT, READ, WRITE }; // set non-blocking try { - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); } catch (IOException x) { nd.close(fd); throw x; @@ -123,7 +123,7 @@ private static enum OpType { CONNECT, READ, WRITE }; super(port, fd, remote); this.fdVal = IOUtil.fdVal(fd); - IOUtil.configureBlocking(fd, false); + NIOUtil.configureBlocking(fd, false); try { port.register(fdVal, this); @@ -765,6 +765,6 @@ Future implWrite(boolean isGatheringWrite, private static native void checkConnect(int fdVal) throws IOException; static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/unix/classes/sun/nio/ch/UnixDispatcher.java b/src/java.base/unix/classes/sun/nio/ch/UnixDispatcher.java index 4cdd0c400ec2f..fffcf4a4d27b7 100644 --- a/src/java.base/unix/classes/sun/nio/ch/UnixDispatcher.java +++ b/src/java.base/unix/classes/sun/nio/ch/UnixDispatcher.java @@ -61,7 +61,7 @@ void implPreClose(FileDescriptor fd, long reader, long writer) throws IOExceptio static native void init(); static { - IOUtil.load(); + NIOUtil.load(); init(); } } diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java index 09611f23298ea..f84b1a4d9ca43 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java @@ -580,7 +580,6 @@ static boolean fchmodatNoFollowSupported() { private static native int init(); static { - jdk.internal.loader.BootLoader.loadLibrary("nio"); capabilities = init(); } } diff --git a/src/java.base/unix/native/libnio/ch/IOUtil.c b/src/java.base/unix/native/libjava/IOUtil.c similarity index 55% rename from src/java.base/unix/native/libnio/ch/IOUtil.c rename to src/java.base/unix/native/libjava/IOUtil.c index dfa99658fa667..22d34c79f72e4 100644 --- a/src/java.base/unix/native/libnio/ch/IOUtil.c +++ b/src/java.base/unix/native/libjava/IOUtil.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,14 +47,6 @@ Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I")); } -JNIEXPORT jboolean JNICALL -Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz, - jbyteArray randArray) -{ - JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL); - return JNI_FALSE; -} - JNIEXPORT jint JNICALL Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo) { @@ -67,104 +59,6 @@ Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val setfdval(env, fdo, val); } -static int -configureBlocking(int fd, jboolean blocking) -{ - int flags = fcntl(fd, F_GETFL); - int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); - - return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags); -} - -JNIEXPORT void JNICALL -Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, - jobject fdo, jboolean blocking) -{ - if (configureBlocking(fdval(env, fdo), blocking) < 0) - JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); -} - -JNIEXPORT jlong JNICALL -Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking) -{ - int fd[2]; - - if (pipe(fd) < 0) { - JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); - return 0; - } - if (blocking == JNI_FALSE) { - if ((configureBlocking(fd[0], JNI_FALSE) < 0) - || (configureBlocking(fd[1], JNI_FALSE) < 0)) { - JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); - close(fd[0]); - close(fd[1]); - return 0; - } - } - return ((jlong) fd[0] << 32) | (jlong) fd[1]; -} - -JNIEXPORT jint JNICALL -Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b) -{ - char c = (char)b; - return convertReturnVal(env, write(fd, &c, 1), JNI_FALSE); -} - -JNIEXPORT jboolean JNICALL -Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd) -{ - char buf[16]; - int tn = 0; - - for (;;) { - int n = read(fd, buf, sizeof(buf)); - tn += n; - if ((n < 0) && (errno != EAGAIN && errno != EWOULDBLOCK)) - JNU_ThrowIOExceptionWithLastError(env, "Drain"); - if (n == (int)sizeof(buf)) - continue; - return (tn > 0) ? JNI_TRUE : JNI_FALSE; - } -} - -JNIEXPORT jint JNICALL -Java_sun_nio_ch_IOUtil_drain1(JNIEnv *env, jclass cl, jint fd) -{ - int res; - char buf[1]; - - res = read(fd, buf, 1); - if (res < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - res = 0; - } else if (errno == EINTR) { - return IOS_INTERRUPTED; - } else { - JNU_ThrowIOExceptionWithLastError(env, "read"); - return IOS_THROWN; - } - } - return res; -} - -JNIEXPORT jint JNICALL -Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this) -{ - struct rlimit rlp; - if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) { - JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed"); - return -1; - } - if (rlp.rlim_max == RLIM_INFINITY || - rlp.rlim_max > (rlim_t)java_lang_Integer_MAX_VALUE) { - return java_lang_Integer_MAX_VALUE; - } else { - return (jint)rlp.rlim_max; - } -} - JNIEXPORT jint JNICALL Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this) { @@ -197,16 +91,16 @@ Java_sun_nio_ch_IOUtil_writevMax(JNIEnv *env, jclass this) #endif } -/* Declared in nio_util.h for use elsewhere in NIO */ +// Declared in nio_util.h for use elsewhere in NIO -jint +JNIEXPORT jint convertReturnVal(JNIEnv *env, jint n, jboolean reading) { - if (n > 0) /* Number of bytes written */ + if (n > 0) // Number of bytes written return n; else if (n == 0) { if (reading) { - return IOS_EOF; /* EOF is -1 in javaland */ + return IOS_EOF; // EOF is -1 in javaland } else { return 0; } @@ -222,16 +116,16 @@ convertReturnVal(JNIEnv *env, jint n, jboolean reading) } } -/* Declared in nio_util.h for use elsewhere in NIO */ +// Declared in nio_util.h for use elsewhere in NIO -jlong +JNIEXPORT jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) { - if (n > 0) /* Number of bytes written */ + if (n > 0) // Number of bytes written return n; else if (n == 0) { if (reading) { - return IOS_EOF; /* EOF is -1 in javaland */ + return IOS_EOF; // EOF is -1 in javaland } else { return 0; } @@ -247,15 +141,13 @@ convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) } } -jint +JNIEXPORT jint fdval(JNIEnv *env, jobject fdo) { return (*env)->GetIntField(env, fdo, fd_fdID); } -void +JNIEXPORT void setfdval(JNIEnv *env, jobject fdo, jint val) { (*env)->SetIntField(env, fdo, fd_fdID, val); } - - diff --git a/src/java.base/unix/native/libnio/MappedMemoryUtils.c b/src/java.base/unix/native/libjava/nio/MappedMemoryUtils.c similarity index 100% rename from src/java.base/unix/native/libnio/MappedMemoryUtils.c rename to src/java.base/unix/native/libjava/nio/MappedMemoryUtils.c diff --git a/src/java.base/unix/native/libnio/ch/FileKey.c b/src/java.base/unix/native/libjava/nio/ch/FileKey.c similarity index 100% rename from src/java.base/unix/native/libnio/ch/FileKey.c rename to src/java.base/unix/native/libjava/nio/ch/FileKey.c diff --git a/src/java.base/unix/native/libnio/ch/NativeThread.c b/src/java.base/unix/native/libjava/nio/ch/NativeThread.c similarity index 100% rename from src/java.base/unix/native/libnio/ch/NativeThread.c rename to src/java.base/unix/native/libjava/nio/ch/NativeThread.c diff --git a/src/java.base/unix/native/libnio/ch/UnixFileDispatcherImpl.c b/src/java.base/unix/native/libjava/nio/ch/UnixFileDispatcherImpl.c similarity index 100% rename from src/java.base/unix/native/libnio/ch/UnixFileDispatcherImpl.c rename to src/java.base/unix/native/libjava/nio/ch/UnixFileDispatcherImpl.c diff --git a/src/java.base/unix/native/libnio/ch/nio_util.h b/src/java.base/unix/native/libjava/nio/ch/nio_util.h similarity index 65% rename from src/java.base/unix/native/libnio/ch/nio_util.h rename to src/java.base/unix/native/libjava/nio/ch/nio_util.h index 6cd3c858a7bd7..73aab03bd293d 100644 --- a/src/java.base/unix/native/libnio/ch/nio_util.h +++ b/src/java.base/unix/native/libjava/nio/ch/nio_util.h @@ -31,23 +31,6 @@ #include #include -/* Defines SO_REUSEPORT */ -#ifndef SO_REUSEPORT -#ifdef __linux__ -#define SO_REUSEPORT 15 -#elif defined(AIX) || defined(MACOSX) -#define SO_REUSEPORT 0x0200 -#else -#define SO_REUSEPORT 0 -#endif -#endif - -/* 2 bytes to allow for null at end of string and null at start of string - * for abstract name - */ -#define MAX_UNIX_DOMAIN_PATH_LEN \ - (int)(sizeof(((struct sockaddr_un *)0)->sun_path)-2) - /* NIO utility procedures */ @@ -59,19 +42,3 @@ void setfdval(JNIEnv *env, jobject fdo, jint value); jint convertReturnVal(JNIEnv *env, jint n, jboolean reading); jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading); - -/* Defined in Net.c */ - -jint handleSocketError(JNIEnv *env, jint errorValue); - -/* Defined in UnixDomainSockets.c */ - -jbyteArray sockaddrToUnixAddressBytes(JNIEnv *env, - struct sockaddr_un *sa, - socklen_t len); - -jint unixSocketAddressToSockaddr(JNIEnv *env, - jbyteArray uaddr, - struct sockaddr_un *sa, - int *len); - diff --git a/src/java.base/unix/native/libnio/fs/UnixFileSystem.c b/src/java.base/unix/native/libjava/nio/fs/UnixFileSystem.c similarity index 100% rename from src/java.base/unix/native/libnio/fs/UnixFileSystem.c rename to src/java.base/unix/native/libjava/nio/fs/UnixFileSystem.c diff --git a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c b/src/java.base/unix/native/libjava/nio/fs/UnixNativeDispatcher.c similarity index 100% rename from src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c rename to src/java.base/unix/native/libjava/nio/fs/UnixNativeDispatcher.c diff --git a/src/java.base/unix/native/libnio/ch/DatagramChannelImpl.c b/src/java.base/unix/native/libnio/ch/DatagramChannelImpl.c index 902a816419db7..4f2ec0c74f179 100644 --- a/src/java.base/unix/native/libnio/ch/DatagramChannelImpl.c +++ b/src/java.base/unix/native/libnio/ch/DatagramChannelImpl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,6 +41,8 @@ #include "nio.h" #include "nio_util.h" +#include "Net.h" + #include "sun_nio_ch_DatagramChannelImpl.h" JNIEXPORT void JNICALL diff --git a/src/java.base/unix/native/libnio/ch/InheritedChannel.c b/src/java.base/unix/native/libnio/ch/InheritedChannel.c index d56625627930d..8f341c4e87810 100644 --- a/src/java.base/unix/native/libnio/ch/InheritedChannel.c +++ b/src/java.base/unix/native/libnio/ch/InheritedChannel.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -38,6 +38,11 @@ #include "sun_nio_ch_InheritedChannel.h" +/* Defined in UnixDomainSockets.c */ +extern jbyteArray sockaddrToUnixAddressBytes(JNIEnv *env, + struct sockaddr_un *sa, + socklen_t len); + JNIEXPORT void JNICALL Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla) { diff --git a/src/java.base/unix/native/libnio/ch/NIOUtil.c b/src/java.base/unix/native/libnio/ch/NIOUtil.c new file mode 100644 index 0000000000000..fd2c3c31680b0 --- /dev/null +++ b/src/java.base/unix/native/libnio/ch/NIOUtil.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include +#include +#include + +#include "jni.h" +#include "jni_util.h" +#include "jvm.h" +#include "jlong.h" +#include "sun_nio_ch_NIOUtil.h" +#include "java_lang_Integer.h" +#include "java_lang_Long.h" +#include "nio.h" +#include "nio_util.h" + +static int +configureBlocking(int fd, jboolean blocking) +{ + int flags = fcntl(fd, F_GETFL); + int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); + + return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags); +} + +JNIEXPORT void JNICALL +Java_sun_nio_ch_NIOUtil_configureBlocking(JNIEnv *env, jclass clazz, + jobject fdo, jboolean blocking) +{ + if (configureBlocking(fdval(env, fdo), blocking) < 0) + JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); +} + +JNIEXPORT jlong JNICALL +Java_sun_nio_ch_NIOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking) +{ + int fd[2]; + + if (pipe(fd) < 0) { + JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); + return 0; + } + if (blocking == JNI_FALSE) { + if ((configureBlocking(fd[0], JNI_FALSE) < 0) + || (configureBlocking(fd[1], JNI_FALSE) < 0)) { + JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); + close(fd[0]); + close(fd[1]); + return 0; + } + } + return ((jlong) fd[0] << 32) | (jlong) fd[1]; +} + +JNIEXPORT jint JNICALL +Java_sun_nio_ch_NIOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b) +{ + char c = (char)b; + return convertReturnVal(env, write(fd, &c, 1), JNI_FALSE); +} + +JNIEXPORT jboolean JNICALL +Java_sun_nio_ch_NIOUtil_drain(JNIEnv *env, jclass cl, jint fd) +{ + char buf[16]; + int tn = 0; + + for (;;) { + int n = read(fd, buf, sizeof(buf)); + tn += n; + if ((n < 0) && (errno != EAGAIN && errno != EWOULDBLOCK)) + JNU_ThrowIOExceptionWithLastError(env, "Drain"); + if (n == (int)sizeof(buf)) + continue; + return (tn > 0) ? JNI_TRUE : JNI_FALSE; + } +} + +JNIEXPORT jint JNICALL +Java_sun_nio_ch_NIOUtil_drain1(JNIEnv *env, jclass cl, jint fd) +{ + int res; + char buf[1]; + + res = read(fd, buf, 1); + if (res < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + res = 0; + } else if (errno == EINTR) { + return IOS_INTERRUPTED; + } else { + JNU_ThrowIOExceptionWithLastError(env, "read"); + return IOS_THROWN; + } + } + return res; +} + +JNIEXPORT jint JNICALL +Java_sun_nio_ch_NIOUtil_fdLimit(JNIEnv *env, jclass this) +{ + struct rlimit rlp; + if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) { + JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed"); + return -1; + } + if (rlp.rlim_max == RLIM_INFINITY || + rlp.rlim_max > (rlim_t)java_lang_Integer_MAX_VALUE) { + return java_lang_Integer_MAX_VALUE; + } else { + return (jint)rlp.rlim_max; + } +} diff --git a/src/java.base/unix/native/libnio/ch/Net.c b/src/java.base/unix/native/libnio/ch/Net.c index 28c1814f42281..a8629509df55f 100644 --- a/src/java.base/unix/native/libnio/ch/Net.c +++ b/src/java.base/unix/native/libnio/ch/Net.c @@ -42,6 +42,8 @@ #include "nio_util.h" #include "nio.h" +#include "Net.h" + #ifdef _AIX #include #include diff --git a/src/java.base/unix/native/libnio/ch/Net.h b/src/java.base/unix/native/libnio/ch/Net.h new file mode 100644 index 0000000000000..75b1acefecb46 --- /dev/null +++ b/src/java.base/unix/native/libnio/ch/Net.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include "jni.h" + + +/* Defined in Net.c */ + +jint handleSocketError(JNIEnv *env, jint errorValue); diff --git a/src/java.base/unix/native/libnio/ch/UnixAsynchronousSocketChannelImpl.c b/src/java.base/unix/native/libnio/ch/UnixAsynchronousSocketChannelImpl.c index 6aa4adbf5d9d1..cdad353cd9749 100644 --- a/src/java.base/unix/native/libnio/ch/UnixAsynchronousSocketChannelImpl.c +++ b/src/java.base/unix/native/libnio/ch/UnixAsynchronousSocketChannelImpl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -34,6 +34,7 @@ #include "nio_util.h" #include "nio.h" +#include "Net.h" JNIEXPORT void JNICALL Java_sun_nio_ch_UnixAsynchronousSocketChannelImpl_checkConnect(JNIEnv *env, diff --git a/src/java.base/unix/native/libnio/ch/UnixDomainSockets.c b/src/java.base/unix/native/libnio/ch/UnixDomainSockets.c index c43c3b9069529..8e4962e63712f 100644 --- a/src/java.base/unix/native/libnio/ch/UnixDomainSockets.c +++ b/src/java.base/unix/native/libnio/ch/UnixDomainSockets.c @@ -41,6 +41,14 @@ #include "nio_util.h" #include "nio.h" +#include "Net.h" + +/* 2 bytes to allow for null at end of string and null at start of string + * for abstract name + */ +#define MAX_UNIX_DOMAIN_PATH_LEN \ + (int)(sizeof(((struct sockaddr_un *)0)->sun_path)-2) + /* Subtle platform differences in how unnamed sockets (empty path) * are returned from getsockname() */ diff --git a/src/java.base/windows/classes/sun/net/www/protocol/http/ntlm/NTLMAuthentication.java b/src/java.base/windows/classes/sun/net/www/protocol/http/ntlm/NTLMAuthentication.java index a7056082e1291..1274cfca912c6 100644 --- a/src/java.base/windows/classes/sun/net/www/protocol/http/ntlm/NTLMAuthentication.java +++ b/src/java.base/windows/classes/sun/net/www/protocol/http/ntlm/NTLMAuthentication.java @@ -62,6 +62,8 @@ enum TransparentAuth { private static final TransparentAuth authMode; static { + jdk.internal.loader.BootLoader.loadLibrary("net"); + defaultDomain = System.getProperty("http.auth.ntlm.domain", "domain"); String ntlmCacheProp = System.getProperty("jdk.ntlm.cache", "true"); ntlmCache = Boolean.parseBoolean(ntlmCacheProp); diff --git a/src/java.base/windows/classes/sun/nio/ch/DatagramDispatcher.java b/src/java.base/windows/classes/sun/nio/ch/DatagramDispatcher.java index de40be6cda11a..8419d86f9f4b2 100644 --- a/src/java.base/windows/classes/sun/nio/ch/DatagramDispatcher.java +++ b/src/java.base/windows/classes/sun/nio/ch/DatagramDispatcher.java @@ -76,6 +76,6 @@ private static native long writev0(FileDescriptor fd, long address, int len) throws IOException; static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/windows/classes/sun/nio/ch/Iocp.java b/src/java.base/windows/classes/sun/nio/ch/Iocp.java index 9a6a5c66b4432..58932356db0a5 100644 --- a/src/java.base/windows/classes/sun/nio/ch/Iocp.java +++ b/src/java.base/windows/classes/sun/nio/ch/Iocp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -433,7 +433,6 @@ private static native void postQueuedCompletionStatus(long completionPort, private static native String getErrorMessage(int error); static { - IOUtil.load(); initIDs(); } } diff --git a/src/java.base/windows/classes/sun/nio/ch/SocketDispatcher.java b/src/java.base/windows/classes/sun/nio/ch/SocketDispatcher.java index a9c2a132cad19..0da459babf766 100644 --- a/src/java.base/windows/classes/sun/nio/ch/SocketDispatcher.java +++ b/src/java.base/windows/classes/sun/nio/ch/SocketDispatcher.java @@ -91,6 +91,6 @@ private static native long writev0(FileDescriptor fd, long address, int len) private static native void close0(int fdVal) throws IOException; static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/windows/classes/sun/nio/ch/WEPoll.java b/src/java.base/windows/classes/sun/nio/ch/WEPoll.java index 93e25e55f7cf4..af24ff77278bd 100644 --- a/src/java.base/windows/classes/sun/nio/ch/WEPoll.java +++ b/src/java.base/windows/classes/sun/nio/ch/WEPoll.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -53,7 +53,7 @@ private WEPoll() { } * } */ static { - IOUtil.load(); + NIOUtil.load(); } private static final int SIZEOF_EPOLLEVENT = eventSize(); private static final int OFFSETOF_EVENTS = eventsOffset(); diff --git a/src/java.base/windows/classes/sun/nio/ch/WEPollSelectorImpl.java b/src/java.base/windows/classes/sun/nio/ch/WEPollSelectorImpl.java index f2bff88675fdb..dc1bf02417291 100644 --- a/src/java.base/windows/classes/sun/nio/ch/WEPollSelectorImpl.java +++ b/src/java.base/windows/classes/sun/nio/ch/WEPollSelectorImpl.java @@ -232,7 +232,7 @@ public Selector wakeup() { synchronized (interruptLock) { if (!interruptTriggered) { try { - IOUtil.write1(fd1Val, (byte) 0); + NIOUtil.write1(fd1Val, (byte) 0); } catch (IOException ioe) { throw new InternalError(ioe); } @@ -244,7 +244,7 @@ public Selector wakeup() { private void clearInterrupt() throws IOException { synchronized (interruptLock) { - IOUtil.drain(fd0Val); + NIOUtil.drain(fd0Val); interruptTriggered = false; } } diff --git a/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java b/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java index 5df8aef743126..5f924c551ad44 100644 --- a/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java +++ b/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java @@ -762,8 +762,4 @@ private static native int writeFile(long handle, long address, int len, private static native int lockFile(long handle, long position, long size, boolean shared, long overlapped) throws IOException; - - static { - IOUtil.load(); - } } diff --git a/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousServerSocketChannelImpl.java b/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousServerSocketChannelImpl.java index 9f84a5089fa0d..b5cdf3eb616d1 100644 --- a/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousServerSocketChannelImpl.java +++ b/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousServerSocketChannelImpl.java @@ -335,7 +335,7 @@ private static native void updateAcceptContext(long listenSocket, private static native void closesocket0(long socket) throws IOException; static { - IOUtil.load(); + NIOUtil.load(); initIDs(); } } diff --git a/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java b/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java index 36065f040a0a9..29582bc37d95e 100644 --- a/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java +++ b/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java @@ -926,7 +926,7 @@ private static native int write0(long socket, int count, long address, private static native void closesocket0(long socket) throws IOException; static { - IOUtil.load(); + NIOUtil.load(); initIDs(); } } diff --git a/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java b/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java index eb681bc5ab935..9ddd13521b577 100644 --- a/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java +++ b/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java @@ -623,6 +623,6 @@ public Selector wakeup() { } static { - IOUtil.load(); + NIOUtil.load(); } } diff --git a/src/java.base/windows/classes/sun/nio/fs/RegistryFileTypeDetector.java b/src/java.base/windows/classes/sun/nio/fs/RegistryFileTypeDetector.java index 485d99eae00d5..1a7037d65d4df 100644 --- a/src/java.base/windows/classes/sun/nio/fs/RegistryFileTypeDetector.java +++ b/src/java.base/windows/classes/sun/nio/fs/RegistryFileTypeDetector.java @@ -62,10 +62,4 @@ public String implProbeContentType(Path file) throws IOException { } private static native String queryStringValue(long subKey, long name); - - static { - // nio.dll has dependency on net.dll - jdk.internal.loader.BootLoader.loadLibrary("net"); - jdk.internal.loader.BootLoader.loadLibrary("nio"); - } } diff --git a/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java b/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java index 8e351b7e0effa..7248a17a39fab 100644 --- a/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java +++ b/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java @@ -1094,9 +1094,6 @@ static NativeBuffer asNativeBuffer(String s) throws WindowsException { private static native void initIDs(); static { - // nio.dll has dependency on net.dll - jdk.internal.loader.BootLoader.loadLibrary("net"); - jdk.internal.loader.BootLoader.loadLibrary("nio"); initIDs(); } diff --git a/src/java.base/windows/native/libnio/ch/IOUtil.c b/src/java.base/windows/native/libjava/IOUtil.c similarity index 65% rename from src/java.base/windows/native/libnio/ch/IOUtil.c rename to src/java.base/windows/native/libjava/IOUtil.c index 850c237d9e908..946065d31c325 100644 --- a/src/java.base/windows/native/libnio/ch/IOUtil.c +++ b/src/java.base/windows/native/libjava/IOUtil.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -34,7 +34,6 @@ #include "java_lang_Long.h" #include "nio.h" #include "nio_util.h" -#include "net_util.h" #include "sun_nio_ch_IOUtil.h" /* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */ @@ -62,16 +61,6 @@ Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) /************************************************************** * IOUtil.c */ -JNIEXPORT jboolean JNICALL -Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz, - jbyteArray randArray) -{ - return - Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env, - clazz, - randArray); -} - JNIEXPORT jint JNICALL Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this) { @@ -84,7 +73,7 @@ Java_sun_nio_ch_IOUtil_writevMax(JNIEnv *env, jclass this) return java_lang_Long_MAX_VALUE; } -jint +JNIEXPORT jint convertReturnVal(JNIEnv *env, jint n, jboolean reading) { if (n > 0) /* Number of bytes written */ @@ -100,7 +89,7 @@ convertReturnVal(JNIEnv *env, jint n, jboolean reading) return IOS_THROWN; } -jlong +JNIEXPORT jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) { if (n > 0) /* Number of bytes written */ @@ -128,79 +117,22 @@ Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val setfdval(env, fdo, val); } - -#define SET_BLOCKING 0 -#define SET_NONBLOCKING 1 - -JNIEXPORT void JNICALL -Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, - jobject fdo, jboolean blocking) -{ - u_long argp; - int result = 0; - jint fd = fdval(env, fdo); - - if (blocking == JNI_FALSE) { - argp = SET_NONBLOCKING; - } else { - argp = SET_BLOCKING; - /* Blocking fd cannot be registered with EventSelect */ - WSAEventSelect(fd, NULL, 0); - } - result = ioctlsocket(fd, FIONBIO, &argp); - if (result == SOCKET_ERROR) { - NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket"); - } -} - -JNIEXPORT jboolean JNICALL -Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd) -{ - char buf[16]; - jboolean readBytes = JNI_FALSE; - for (;;) { - int n = recv((SOCKET) fd, buf, sizeof(buf), 0); - if (n == SOCKET_ERROR) { - if (WSAGetLastError() != WSAEWOULDBLOCK) { - JNU_ThrowIOExceptionWithLastError(env, "recv failed"); - } - return readBytes; - } - if (n <= 0) - return readBytes; - if (n < (int)sizeof(buf)) - return JNI_TRUE; - readBytes = JNI_TRUE; - } -} - -JNIEXPORT jint JNICALL -Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b) -{ - int n = send((SOCKET) fd, &b, 1, 0); - if (n == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) { - JNU_ThrowIOExceptionWithLastError(env, "send failed"); - return IOS_THROWN; - } - return (n == 1) ? 1 : 0; -} - /* Note: This function returns the int fd value from file descriptor. It is mostly used for sockets which should use the int fd value. */ -jint +JNIEXPORT jint fdval(JNIEnv *env, jobject fdo) { return (*env)->GetIntField(env, fdo, fd_fdID); } -void +JNIEXPORT void setfdval(JNIEnv *env, jobject fdo, jint val) { (*env)->SetIntField(env, fdo, fd_fdID, val); } -jlong +JNIEXPORT jlong handleval(JNIEnv *env, jobject fdo) { return (*env)->GetLongField(env, fdo, handle_fdID); diff --git a/src/java.base/windows/native/libnio/MappedMemoryUtils.c b/src/java.base/windows/native/libjava/nio/MappedMemoryUtils.c similarity index 100% rename from src/java.base/windows/native/libnio/MappedMemoryUtils.c rename to src/java.base/windows/native/libjava/nio/MappedMemoryUtils.c diff --git a/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c b/src/java.base/windows/native/libjava/nio/ch/FileDispatcherImpl.c similarity index 100% rename from src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c rename to src/java.base/windows/native/libjava/nio/ch/FileDispatcherImpl.c diff --git a/src/java.base/windows/native/libnio/ch/FileKey.c b/src/java.base/windows/native/libjava/nio/ch/FileKey.c similarity index 100% rename from src/java.base/windows/native/libnio/ch/FileKey.c rename to src/java.base/windows/native/libjava/nio/ch/FileKey.c diff --git a/src/java.base/windows/native/libnio/ch/Iocp.c b/src/java.base/windows/native/libjava/nio/ch/Iocp.c similarity index 100% rename from src/java.base/windows/native/libnio/ch/Iocp.c rename to src/java.base/windows/native/libjava/nio/ch/Iocp.c diff --git a/src/java.base/windows/native/libnio/ch/WindowsAsynchronousFileChannelImpl.c b/src/java.base/windows/native/libjava/nio/ch/WindowsAsynchronousFileChannelImpl.c similarity index 100% rename from src/java.base/windows/native/libnio/ch/WindowsAsynchronousFileChannelImpl.c rename to src/java.base/windows/native/libjava/nio/ch/WindowsAsynchronousFileChannelImpl.c diff --git a/src/java.base/windows/native/libnio/ch/nio_util.h b/src/java.base/windows/native/libjava/nio/ch/nio_util.h similarity index 68% rename from src/java.base/windows/native/libnio/ch/nio_util.h rename to src/java.base/windows/native/libjava/nio/ch/nio_util.h index e7bef3d39cbe1..4f2f768270c80 100644 --- a/src/java.base/windows/native/libnio/ch/nio_util.h +++ b/src/java.base/windows/native/libjava/nio/ch/nio_util.h @@ -29,29 +29,30 @@ #include "jni.h" -/** - * The maximum buffer size for WSASend/WSARecv. Microsoft recommendation for - * blocking operations is to use buffers no larger than 64k. We need the - * maximum to be less than 128k to support asynchronous close on Windows - * Server 2003 and newer editions of Windows. - */ -#define MAX_BUFFER_SIZE ((128*1024)-1) +/* Defined in IOUtil.c */ -#define MAX_UNIX_DOMAIN_PATH_LEN \ - (int)(sizeof(((struct sockaddr_un *)0)->sun_path)-2) +JNIEXPORT jint fdval(JNIEnv *env, jobject fdo); +JNIEXPORT void setfdval(JNIEnv *env, jobject fdo, jint val); +JNIEXPORT jlong handleval(JNIEnv *env, jobject fdo); +JNIEXPORT jint convertReturnVal(JNIEnv *env, jint n, jboolean r); +JNIEXPORT jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean r); -jint fdval(JNIEnv *env, jobject fdo); -void setfdval(JNIEnv *env, jobject fdo, jint val); -jlong handleval(JNIEnv *env, jobject fdo); -jint convertReturnVal(JNIEnv *env, jint n, jboolean r); -jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean r); -jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd); +#ifdef _WIN64 struct iovec { jlong iov_base; jint iov_len; }; +#else + +struct iovec { + jint iov_base; + jint iov_len; +}; + +#endif + /* Defined in UnixDomainSockets.c */ jbyteArray sockaddrToUnixAddressBytes(JNIEnv *env, struct sockaddr_un *sa, socklen_t len); diff --git a/src/java.base/windows/native/libnio/fs/RegistryFileTypeDetector.c b/src/java.base/windows/native/libjava/nio/fs/RegistryFileTypeDetector.c similarity index 100% rename from src/java.base/windows/native/libnio/fs/RegistryFileTypeDetector.c rename to src/java.base/windows/native/libjava/nio/fs/RegistryFileTypeDetector.c diff --git a/src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c b/src/java.base/windows/native/libjava/nio/fs/WindowsNativeDispatcher.c similarity index 100% rename from src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c rename to src/java.base/windows/native/libjava/nio/fs/WindowsNativeDispatcher.c diff --git a/src/java.base/windows/native/libnio/ch/DatagramDispatcher.c b/src/java.base/windows/native/libnio/ch/DatagramDispatcher.c index 8d3a14ffd91bb..1dbff362c9df4 100644 --- a/src/java.base/windows/native/libnio/ch/DatagramDispatcher.c +++ b/src/java.base/windows/native/libnio/ch/DatagramDispatcher.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,6 +41,9 @@ * DatagramDispatcher.c */ +/* Defined in DatagramChannelImpl.c */ +extern jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd); + JNIEXPORT jint JNICALL Java_sun_nio_ch_DatagramDispatcher_read0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len) diff --git a/src/java.base/windows/native/libnio/ch/NIOUtil.c b/src/java.base/windows/native/libnio/ch/NIOUtil.c new file mode 100644 index 0000000000000..ee8e763cb06c7 --- /dev/null +++ b/src/java.base/windows/native/libnio/ch/NIOUtil.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include +#include +#include +#include "jni.h" +#include "jni_util.h" +#include "jvm.h" +#include "jlong.h" + +#include "java_lang_Long.h" +#include "nio.h" +#include "nio_util.h" +#include "net_util.h" +#include "sun_nio_ch_NIOUtil.h" + +/************************************************************** + * NIOUtil.c + */ + +#define SET_BLOCKING 0 +#define SET_NONBLOCKING 1 + +JNIEXPORT void JNICALL +Java_sun_nio_ch_NIOUtil_configureBlocking(JNIEnv *env, jclass clazz, + jobject fdo, jboolean blocking) +{ + u_long argp; + int result = 0; + jint fd = fdval(env, fdo); + + if (blocking == JNI_FALSE) { + argp = SET_NONBLOCKING; + } else { + argp = SET_BLOCKING; + /* Blocking fd cannot be registered with EventSelect */ + WSAEventSelect(fd, NULL, 0); + } + result = ioctlsocket(fd, FIONBIO, &argp); + if (result == SOCKET_ERROR) { + NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket"); + } +} + +JNIEXPORT jboolean JNICALL +Java_sun_nio_ch_NIOUtil_drain(JNIEnv *env, jclass cl, jint fd) +{ + char buf[16]; + jboolean readBytes = JNI_FALSE; + for (;;) { + int n = recv((SOCKET) fd, buf, sizeof(buf), 0); + if (n == SOCKET_ERROR) { + if (WSAGetLastError() != WSAEWOULDBLOCK) { + JNU_ThrowIOExceptionWithLastError(env, "recv failed"); + } + return readBytes; + } + if (n <= 0) + return readBytes; + if (n < (int)sizeof(buf)) + return JNI_TRUE; + readBytes = JNI_TRUE; + } +} + +JNIEXPORT jint JNICALL +Java_sun_nio_ch_NIOUtil_drain1(JNIEnv *env, jclass cl, jint fd) +{ + JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", "drain1"); + return IOS_THROWN; +} + +JNIEXPORT jint JNICALL +Java_sun_nio_ch_NIOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b) +{ + int n = send((SOCKET) fd, &b, 1, 0); + if (n == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) { + JNU_ThrowIOExceptionWithLastError(env, "send failed"); + return IOS_THROWN; + } + return (n == 1) ? 1 : 0; +} diff --git a/src/java.base/windows/native/libnio/ch/SocketDispatcher.c b/src/java.base/windows/native/libnio/ch/SocketDispatcher.c index ff74a0abf10af..02b14d8a09efc 100644 --- a/src/java.base/windows/native/libnio/ch/SocketDispatcher.c +++ b/src/java.base/windows/native/libnio/ch/SocketDispatcher.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -39,6 +39,15 @@ * SocketDispatcher.c */ +/** + * The maximum buffer size for WSASend/WSARecv. Microsoft recommendation for + * blocking operations is to use buffers no larger than 64k. We need the + * maximum to be less than 128k to support asynchronous close on Windows + * Server 2003 and newer editions of Windows. + */ +#define MAX_BUFFER_SIZE ((128*1024)-1) + + JNIEXPORT jint JNICALL Java_sun_nio_ch_SocketDispatcher_read0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len) diff --git a/src/java.base/windows/native/libnio/ch/UnixDomainSockets.c b/src/java.base/windows/native/libnio/ch/UnixDomainSockets.c index e29d8e28efc72..71d542105857a 100644 --- a/src/java.base/windows/native/libnio/ch/UnixDomainSockets.c +++ b/src/java.base/windows/native/libnio/ch/UnixDomainSockets.c @@ -38,6 +38,12 @@ #include "sun_nio_ch_Net.h" #include "sun_nio_ch_PollArrayWrapper.h" +/* 2 bytes to allow for null at end of string and null at start of string + * for abstract name + */ +#define MAX_UNIX_DOMAIN_PATH_LEN \ + (int)(sizeof(((struct sockaddr_un *)0)->sun_path)-2) + /* The winsock provider ID of the Microsoft AF_UNIX implementation */ static GUID MS_PROVIDER_ID = {0xA00943D9,0x9C2E,0x4633,{0x9B,0x59,0,0x57,0xA3,0x16,0x09,0x94}}; diff --git a/src/jdk.net/share/classes/jdk/nio/Channels.java b/src/jdk.net/share/classes/jdk/nio/Channels.java index fa15cf189fd3a..6fb0278401041 100644 --- a/src/jdk.net/share/classes/jdk/nio/Channels.java +++ b/src/jdk.net/share/classes/jdk/nio/Channels.java @@ -34,6 +34,7 @@ import java.util.Objects; import sun.nio.ch.IOUtil; +import sun.nio.ch.NIOUtil; import sun.nio.ch.Net; import sun.nio.ch.SelChImpl; import sun.nio.ch.SelectionKeyImpl; @@ -233,7 +234,7 @@ public int translateInterestOps(int ops) { @Override protected void implConfigureBlocking(boolean block) throws IOException { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } @Override diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java index 485ebf13f2c23..154fc4df0df83 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java @@ -62,6 +62,7 @@ import sun.nio.ch.IOUtil; import sun.nio.ch.NativeThread; import sun.nio.ch.Net; +import sun.nio.ch.NIOUtil; import sun.nio.ch.SelChImpl; import sun.nio.ch.SelectionKeyImpl; import sun.nio.ch.Util; @@ -532,7 +533,7 @@ public boolean finishConnect() throws IOException { @Override protected void implConfigureBlocking(boolean block) throws IOException { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } @Override @@ -1085,7 +1086,7 @@ static native int send0(int fd, long address, int length, @SuppressWarnings("restricted") private static void loadSctpLibrary() { - IOUtil.load(); /* loads nio & net native libraries */ + NIOUtil.load(); /* loads nio & net native libraries */ System.loadLibrary("sctp"); initIDs(); } diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java index 31e83d72f96df..f6c4f30b5bb1f 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java @@ -63,6 +63,7 @@ import sun.nio.ch.IOStatus; import sun.nio.ch.IOUtil; import sun.nio.ch.Net; +import sun.nio.ch.NIOUtil; import sun.nio.ch.SelChImpl; import sun.nio.ch.SelectionKeyImpl; import sun.nio.ch.Util; @@ -281,7 +282,7 @@ private void senderCleanup() throws IOException { @Override protected void implConfigureBlocking(boolean block) throws IOException { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } @Override diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpNet.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpNet.java index 0be28f526258a..42a6ab9347f2d 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpNet.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpNet.java @@ -35,6 +35,7 @@ import sun.net.util.IPAddressUtil; import sun.nio.ch.IOUtil; import sun.nio.ch.Net; +import sun.nio.ch.NIOUtil; import com.sun.nio.sctp.SctpSocketOption; import static com.sun.nio.sctp.SctpStandardSocketOptions.*; @@ -74,7 +75,7 @@ static void preClose(int fd) throws IOException { */ static FileDescriptor socket(boolean oneToOne) throws IOException { int nativefd = socket0(oneToOne); - return IOUtil.newFD(nativefd); + return NIOUtil.newFD(nativefd); } static void bindx(int fd, InetAddress[] addrs, int port, boolean add) @@ -251,7 +252,7 @@ static void shutdown(int fd, int assocId) static FileDescriptor branch(int fd, int assocId) throws IOException { int nativefd = branch0(fd, assocId); - return IOUtil.newFD(nativefd); + return NIOUtil.newFD(nativefd); } /* Native Methods */ @@ -306,7 +307,7 @@ static native void setInitMsgOption0(int fd, int arg1, int arg2) @SuppressWarnings("restricted") private static void loadSctpLibrary() { - IOUtil.load(); // loads nio & net native libraries + NIOUtil.load(); // loads nio & net native libraries System.loadLibrary("sctp"); init(); } diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java index 4b2be742c6d32..4afa0dc9fee4c 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java @@ -45,6 +45,7 @@ import sun.nio.ch.IOStatus; import sun.nio.ch.IOUtil; import sun.nio.ch.Net; +import sun.nio.ch.NIOUtil; import sun.nio.ch.SelChImpl; import sun.nio.ch.SelectionKeyImpl; @@ -238,14 +239,14 @@ public SctpChannel accept() throws IOException { if (n < 1) return null; - IOUtil.configureBlocking(newfd, true); + NIOUtil.configureBlocking(newfd, true); return new SctpChannelImpl(provider(), newfd); } } @Override protected void implConfigureBlocking(boolean block) throws IOException { - IOUtil.configureBlocking(fd, block); + NIOUtil.configureBlocking(fd, block); } @Override diff --git a/test/jdk/java/net/InetAddress/java.base/java/net/NullCharInHostname.java b/test/jdk/java/net/InetAddress/java.base/java/net/NullCharInHostname.java index a78c41398ce51..84d3e58b130ed 100644 --- a/test/jdk/java/net/InetAddress/java.base/java/net/NullCharInHostname.java +++ b/test/jdk/java/net/InetAddress/java.base/java/net/NullCharInHostname.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,6 +27,10 @@ public class NullCharInHostname { public static void main(String[] args) { + // Manually load the "net" library which is needed for Inet6AddressImpl. + // In normal scenarios this library is loaded automatically. + jdk.internal.loader.BootLoader.loadLibrary("net"); + var name = "foo\u0000bar"; System.out.println("file.encoding = " + System.getProperty("file.encoding")); System.out.println("native.encoding = " + System.getProperty("native.encoding")); diff --git a/test/jdk/jdk/nio/Basic.java b/test/jdk/jdk/nio/Basic.java index 02fcfbc4a4d5a..769f7cba3d662 100644 --- a/test/jdk/jdk/nio/Basic.java +++ b/test/jdk/jdk/nio/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -46,7 +46,7 @@ import jdk.nio.Channels; import jdk.nio.Channels.SelectableChannelCloser; -import sun.nio.ch.IOUtil; +import sun.nio.ch.NIOUtil; import org.testng.annotations.Test; import static org.testng.Assert.*; @@ -237,7 +237,7 @@ public void testImplReleaseChannel() throws IOException { @Test(expectedExceptions = IllegalArgumentException.class) public void testInvalidFileDescriptor() throws IOException { - FileDescriptor fd = IOUtil.newFD(-1); + FileDescriptor fd = NIOUtil.newFD(-1); Channels.readWriteSelectableChannel(fd, new SelectableChannelCloser() { @Override public void implCloseChannel(SelectableChannel sc) { }