8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset

Reviewed-by: bpb
This commit is contained in:
Alan Bateman 2019-03-22 11:35:35 +00:00
parent cc590f5765
commit 3a4d5db248
13 changed files with 507 additions and 88 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2019, 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,13 +34,28 @@ import java.io.IOException;
*/
class SocketDispatcher extends NativeDispatcher {
SocketDispatcher() { }
/**
* Reads up to len bytes from a socket with special handling for "connection
* reset".
*
* @throws sun.net.ConnectionResetException if connection reset is detected
* @throws IOException if another I/O error occurs
*/
int read(FileDescriptor fd, long address, int len) throws IOException {
return FileDispatcherImpl.read0(fd, address, len);
return read0(fd, address, len);
}
/**
* Scattering read from a socket into len buffers with special handling for
* "connection reset".
*
* @throws sun.net.ConnectionResetException if connection reset is detected
* @throws IOException if another I/O error occurs
*/
long readv(FileDescriptor fd, long address, int len) throws IOException {
return FileDispatcherImpl.readv0(fd, address, len);
return readv0(fd, address, len);
}
int write(FileDescriptor fd, long address, int len) throws IOException {
@ -58,4 +73,16 @@ class SocketDispatcher extends NativeDispatcher {
void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
}
// -- Native methods --
private static native int read0(FileDescriptor fd, long address, int len)
throws IOException;
private static native long readv0(FileDescriptor fd, long address, int len)
throws IOException;
static {
IOUtil.load();
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, 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
@ -31,6 +31,8 @@ import java.net.*;
import java.util.concurrent.*;
import java.io.IOException;
import java.io.FileDescriptor;
import sun.net.ConnectionResetException;
import sun.net.NetHooks;
import sun.net.util.SocketExceptions;
import sun.security.action.GetPropertyAction;
@ -415,6 +417,8 @@ class UnixAsynchronousSocketChannelImpl
enableReading();
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
if (x instanceof ConnectionResetException)
x = new IOException(x.getMessage());
exc = x;
} finally {
// restart poll in case of concurrent write
@ -546,6 +550,8 @@ class UnixAsynchronousSocketChannelImpl
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
if (x instanceof ConnectionResetException)
x = new IOException(x.getMessage());
exc = x;
} finally {
if (!pending)

View file

@ -804,7 +804,7 @@ Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlo
}
}
JNIEXPORT jint JNICALL
JNIEXPORT jboolean JNICALL
Java_sun_nio_ch_Net_pollConnect(JNIEnv *env, jobject this, jobject fdo, jlong timeout)
{
jint fd = fdval(env, fdo);
@ -828,23 +828,22 @@ Java_sun_nio_ch_Net_pollConnect(JNIEnv *env, jobject this, jobject fdo, jlong ti
errno = 0;
result = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &n);
if (result < 0) {
return handleSocketError(env, errno);
handleSocketError(env, errno);
return JNI_FALSE;
} else if (error) {
return handleSocketError(env, error);
handleSocketError(env, error);
return JNI_FALSE;
} else if ((poller.revents & POLLHUP) != 0) {
return handleSocketError(env, ENOTCONN);
handleSocketError(env, ENOTCONN);
return JNI_FALSE;
}
// connected
return 1;
} else if (result == 0) {
return 0;
return JNI_TRUE;
} else if (result == 0 || errno == EINTR) {
return JNI_FALSE;
} else {
if (errno == EINTR) {
return IOS_INTERRUPTED;
} else {
JNU_ThrowIOExceptionWithLastError(env, "poll failed");
return IOS_THROWN;
}
JNU_ThrowIOExceptionWithLastError(env, "poll failed");
return JNI_FALSE;
}
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2019, 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 <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "jni.h"
#include "jni_util.h"
#include "jlong.h"
#include "nio.h"
#include "nio_util.h"
#include "sun_nio_ch_SocketDispatcher.h"
JNIEXPORT jint JNICALL
Java_sun_nio_ch_SocketDispatcher_read0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
{
jint fd = fdval(env, fdo);
void *buf = (void *)jlong_to_ptr(address);
jint n = read(fd, buf, len);
if ((n == -1) && (errno == ECONNRESET || errno == EPIPE)) {
JNU_ThrowByName(env, "sun/net/ConnectionResetException", "Connection reset");
return IOS_THROWN;
} else {
return convertReturnVal(env, n, JNI_TRUE);
}
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_SocketDispatcher_readv0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
{
jint fd = fdval(env, fdo);
struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
jlong n = readv(fd, iov, len);
if ((n == -1) && (errno == ECONNRESET || errno == EPIPE)) {
JNU_ThrowByName(env, "sun/net/ConnectionResetException", "Connection reset");
return IOS_THROWN;
} else {
return convertLongReturnVal(env, n, JNI_TRUE);
}
}