unixdomainchannels: unix domain automatic bind unixdomainchannels
authormichaelm
Thu, 14 Nov 2019 10:16:28 +0000
branchunixdomainchannels
changeset 59076 998df1368cca
parent 59074 7917f95f74c4
child 59077 85df93a18fe0
unixdomainchannels: unix domain automatic bind
src/java.base/share/classes/java/nio/channels/ServerSocketChannel.java
src/java.base/share/classes/sun/nio/ch/Net.java
src/java.base/share/classes/sun/nio/ch/UnixDomainServerSocketChannelImpl.java
src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java
src/java.base/unix/native/libnio/ch/Net.c
--- a/src/java.base/share/classes/java/nio/channels/ServerSocketChannel.java	Thu Nov 14 09:06:10 2019 +0000
+++ b/src/java.base/share/classes/java/nio/channels/ServerSocketChannel.java	Thu Nov 14 10:16:28 2019 +0000
@@ -218,16 +218,19 @@
      * <p> Note, for <i>Unix Domain</i> channels, a file is created in the file-system
      * with the same path name as this channel's bound {@link UnixDomainSocketAddress}.
      * This file persists after the channel is closed, and must be removed before
-     * another channel can bind to the same name.
+     * another channel can bind to the same name. Also, <i>Unix Domain</i> ServerSocketChannels
+     * must be bound to an explicit address. 
      *
      * @param   local
-     *          The address to bind the socket, or {@code null} to bind to an
-     *          automatically assigned socket address
+     *          The address to bind the socket, or {@code null} to bind an <i>IP</i> channel to
+     *          an automatically assigned socket address
      * @param   backlog
      *          The maximum number of pending connections
      *
      * @return  This channel
      *
+     * @throws  BindException
+     *          If this is a <i>Unix domain</i> channel and {@code local} is {@code null}
      * @throws  AlreadyBoundException
      *          If the socket is already bound
      * @throws  UnsupportedAddressTypeException
--- a/src/java.base/share/classes/sun/nio/ch/Net.java	Thu Nov 14 09:06:10 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/Net.java	Thu Nov 14 10:16:28 2019 +0000
@@ -711,7 +711,7 @@
 
     public static UnixDomainSocketAddress checkUnixAddress(SocketAddress sa) {
         if (sa == null)
-            throw new NullPointerException();
+            return null;
         if (!(sa instanceof UnixDomainSocketAddress))
             throw new UnsupportedAddressTypeException();
         UnixDomainSocketAddress usa = (UnixDomainSocketAddress)sa;
--- a/src/java.base/share/classes/sun/nio/ch/UnixDomainServerSocketChannelImpl.java	Thu Nov 14 09:06:10 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/UnixDomainServerSocketChannelImpl.java	Thu Nov 14 10:16:28 2019 +0000
@@ -27,6 +27,7 @@
 
 import java.io.FileDescriptor;
 import java.io.IOException;
+import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.SocketAddress;
 import java.net.SocketOption;
@@ -135,6 +136,9 @@
 
     @Override
     public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
+	if (local == null)
+	    throw new BindException("automatic bind not possible for Unix domain servers");
+
         synchronized (stateLock) {
             ensureOpen();
             if (localAddress != null)
--- a/src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java	Thu Nov 14 09:06:10 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java	Thu Nov 14 10:16:28 2019 +0000
@@ -172,8 +172,6 @@
                         throw new ConnectionPendingException();
                     if (localAddress != null)
                         throw new AlreadyBoundException();
-                    if (local == null)
-                        throw new NullPointerException(); // TODO: ??
                     UnixDomainSocketAddress usa = Net.checkUnixAddress(local);
                     Net.unixDomainBind(fd, usa);
                     localAddress = Net.localUnixAddress(fd);
--- a/src/java.base/unix/native/libnio/ch/Net.c	Thu Nov 14 09:06:10 2019 +0000
+++ b/src/java.base/unix/native/libnio/ch/Net.c	Thu Nov 14 10:16:28 2019 +0000
@@ -110,6 +110,9 @@
     int sa_len = 0;
     int rv = 0;
 
+    if (uaddr == NULL) 
+	return; /* Rely on implicit bind */
+
     if (NET_UnixSocketAddressToSockaddr(env, uaddr, &sa, &sa_len) != 0)
         return;