# HG changeset patch # User michaelm # Date 1573753387 0 # Node ID fb91b01624be86d90673cf8b8c062331e7894d7f # Parent 5e250ee9259efb743b70f798d46b4d53f730213e unixdomainchannels: more fixes diff -r 5e250ee9259e -r fb91b01624be src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java --- a/src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java Thu Nov 14 15:01:49 2019 +0000 +++ b/src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java Thu Nov 14 17:43:07 2019 +0000 @@ -149,7 +149,6 @@ HashSet> set = new HashSet<>(); set.add(StandardSocketOptions.SO_SNDBUF); set.add(StandardSocketOptions.SO_RCVBUF); - set.add(StandardSocketOptions.SO_KEEPALIVE); set.add(StandardSocketOptions.SO_LINGER); return Collections.unmodifiableSet(set); } diff -r 5e250ee9259e -r fb91b01624be src/java.base/unix/native/libnio/ch/Net.c --- a/src/java.base/unix/native/libnio/ch/Net.c Thu Nov 14 15:01:49 2019 +0000 +++ b/src/java.base/unix/native/libnio/ch/Net.c Thu Nov 14 17:43:07 2019 +0000 @@ -73,20 +73,25 @@ JNIEXPORT jint JNICALL NET_UnixSocketAddressToSockaddr(JNIEnv *env, jobject uaddr, struct sockaddr_un *sa, int *len) { - jstring path = (*env)->GetObjectField(env, uaddr, udsa_pathID); + jstring path; + memset(sa, 0, sizeof(struct sockaddr_un)); + sa->sun_family = AF_UNIX; + if (uaddr == NULL) { + *len = (int)(offsetof(struct sockaddr_un, sun_path)); + return 0; + } + path = (*env)->GetObjectField(env, uaddr, udsa_pathID); jboolean isCopy; int ret; const char* pname = JNU_GetStringPlatformChars(env, path, &isCopy); - memset(sa, 0, sizeof(struct sockaddr_un)); - sa->sun_family = AF_UNIX; - int name_len = strlen(pname) + 1; /* includes null termination */ + size_t name_len = strlen(pname)+1; if (name_len > MAX_UNIX_DOMAIN_PATH_LEN) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "unix domain path too long"); ret = 1; goto finish; } - strncpy(&sa->sun_path[0], pname, name_len); - *len = offsetof(struct sockaddr_un, sun_path) + name_len; + strncpy(sa->sun_path, pname, name_len); + *len = (int)(offsetof(struct sockaddr_un, sun_path) + name_len); ret = 0; finish: if (isCopy) diff -r 5e250ee9259e -r fb91b01624be src/java.base/windows/native/libnio/ch/Net.c --- a/src/java.base/windows/native/libnio/ch/Net.c Thu Nov 14 15:01:49 2019 +0000 +++ b/src/java.base/windows/native/libnio/ch/Net.c Thu Nov 14 17:43:07 2019 +0000 @@ -90,7 +90,7 @@ extern jmethodID udsa_ctorID; extern jfieldID udsa_pathID; -#define PATHLEN(len) (len - offsetof(struct sockaddr_un, sun_path)) +#define PATHLEN_UN(len) (len - offsetof(struct sockaddr_un, sun_path)) JNIEXPORT jobject JNICALL NET_SockaddrToUnixAddress(JNIEnv *env, struct sockaddr_un *sa, socklen_t len) { @@ -98,7 +98,7 @@ if (sa->sun_family == AF_UNIX) { char *name; - if (PATHLEN(len) == 0) { + if (PATHLEN_UN(len) == 0) { name = ""; } else { name = sa->sun_path; @@ -112,12 +112,17 @@ JNIEXPORT jint JNICALL NET_UnixSocketAddressToSockaddr(JNIEnv *env, jobject uaddr, struct sockaddr_un *sa, int *len) { - jstring path = (*env)->GetObjectField(env, uaddr, udsa_pathID); + jstring path; + memset(sa, 0, sizeof(struct sockaddr_un)); + sa->sun_family = AF_UNIX; + if (uaddr == NULL) { + *len = (int)(offsetof(struct sockaddr_un, sun_path)); + return 0; + } + path = (*env)->GetObjectField(env, uaddr, udsa_pathID); jboolean isCopy; int ret; const char* pname = JNU_GetStringPlatformChars(env, path, &isCopy); - memset(sa, 0, sizeof(struct sockaddr_un)); - sa->sun_family = AF_UNIX; size_t name_len = strlen(pname)+1; if (name_len > MAX_UNIX_DOMAIN_PATH_LEN) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "unix domain path too long"); @@ -149,7 +154,7 @@ { SOCKET fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd == INVALID_SOCKET) { - return handleSocketError(env, errno); + return handleSocketError(env, WSAGetLastError()); } return (int)fd; } @@ -161,6 +166,11 @@ int sa_len = 0; int rv = 0; +/* + if (uaddr == NULL) + return; +*/ + if (NET_UnixSocketAddressToSockaddr(env, uaddr, &sa, &sa_len) != 0) return; @@ -243,7 +253,7 @@ struct sockaddr_un sa; socklen_t sa_len = sizeof(sa); if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) { - handleSocketError(env, errno); + handleSocketError(env, WSAGetLastError()); return NULL; } return NET_SockaddrToUnixAddress(env, &sa, sa_len); diff -r 5e250ee9259e -r fb91b01624be test/jdk/java/nio/channels/unixdomain/Basic.java --- a/test/jdk/java/nio/channels/unixdomain/Basic.java Thu Nov 14 15:01:49 2019 +0000 +++ b/test/jdk/java/nio/channels/unixdomain/Basic.java Thu Nov 14 17:43:07 2019 +0000 @@ -35,21 +35,33 @@ import java.nio.channels.*; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.LinkedList; import java.util.List; public class Basic { static int sockRxBufsize, sockTxBufsize; static boolean nagle; + static String tempDir; + + static { + try { + Path parent = Paths.get("."); + Path child = Files.createTempDirectory(parent, null); + tempDir = child.toString(); + } catch (IOException e) { + tempDir = null; + } + } public static void main(String args[]) throws Exception { if (args.length != 3) usage(); - if (!supported()) { - System.out.println("Unix domain channels not supported"); - return; - } + if (!supported()) { + System.out.println("Unix domain channels not supported"); + return; + } sockRxBufsize = getInt(args[0]); sockTxBufsize = getInt(args[1]); if (args[2].equals("nagle-on")) @@ -65,14 +77,14 @@ } static boolean supported() { - try { - SocketChannel.open(StandardProtocolFamily.UNIX); - } catch (UnsupportedAddressTypeException e) { - return false; - } catch (Exception e) { - return true; // continue test to see what problem is - } - return true; + try { + SocketChannel.open(StandardProtocolFamily.UNIX); + } catch (UnsupportedAddressTypeException e) { + return false; + } catch (Exception e) { + return true; // continue test to see what problem is + } + return true; } static int getInt(String s) { @@ -163,16 +175,18 @@ SelectionKey ckey; List toSend = new LinkedList<>(); final int bufsize; + Path sockfile; Server(ProtocolFamily family, int bufsize) throws IOException { - setDaemon(true); + //setDaemon(true); SocketAddress addr; this.bufsize = bufsize; if (family == StandardProtocolFamily.UNIX) { server = ServerSocketChannel.open(family); - Path sockfile = Path.of("server.sock"); + sockfile = Path.of(tempDir, "server.sock"); Files.deleteIfExists(sockfile); addr = new UnixDomainSocketAddress(sockfile); + System.out.println("ADDR = " + addr); } else { server = ServerSocketChannel.open(); addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); @@ -212,7 +226,6 @@ int m = channel.read(buf); if (m == -1) { channel.close(); - server.close(); return; } else { buf.flip(); @@ -236,6 +249,12 @@ } } catch (IOException e) { throw new RuntimeException(e); + } finally { + try { + if (server.isOpen()) server.close(); + if (sockfile != null) + Files.deleteIfExists(sockfile); + } catch (IOException ee) {} } } }