Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/ch/ServerSocketChannelImpl.c
32288 views
/*1* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <netdb.h>27#include <sys/types.h>28#include <sys/socket.h>2930#if __linux__31#include <netinet/in.h>32#endif3334#if defined(__solaris__) && !defined(_SOCKLEN_T)35typedef size_t socklen_t; /* New in SunOS 5.7, so need this for 5.6 */36#endif3738#include "jni.h"39#include "jni_util.h"40#include "net_util.h"41#include "jvm.h"42#include "jlong.h"43#include "sun_nio_ch_ServerSocketChannelImpl.h"44#include "nio.h"45#include "nio_util.h"464748static jfieldID fd_fdID; /* java.io.FileDescriptor.fd */49static jclass isa_class; /* java.net.InetSocketAddress */50static jmethodID isa_ctorID; /* .InetSocketAddress(InetAddress, int) */515253JNIEXPORT void JNICALL54Java_sun_nio_ch_ServerSocketChannelImpl_initIDs(JNIEnv *env, jclass c)55{56jclass cls;5758cls = (*env)->FindClass(env, "java/io/FileDescriptor");59CHECK_NULL(cls);60fd_fdID = (*env)->GetFieldID(env, cls, "fd", "I");61CHECK_NULL(fd_fdID);6263cls = (*env)->FindClass(env, "java/net/InetSocketAddress");64CHECK_NULL(cls);65isa_class = (*env)->NewGlobalRef(env, cls);66if (isa_class == NULL) {67JNU_ThrowOutOfMemoryError(env, NULL);68return;69}70isa_ctorID = (*env)->GetMethodID(env, cls, "<init>",71"(Ljava/net/InetAddress;I)V");72CHECK_NULL(isa_ctorID);73}7475JNIEXPORT jint JNICALL76Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this,77jobject ssfdo, jobject newfdo,78jobjectArray isaa)79{80jint ssfd = (*env)->GetIntField(env, ssfdo, fd_fdID);81jint newfd;82struct sockaddr *sa;83int alloc_len;84jobject remote_ia = 0;85jobject isa;86jint remote_port = 0;8788NET_AllocSockaddr(&sa, &alloc_len);89if (sa == NULL) {90JNU_ThrowOutOfMemoryError(env, NULL);91return IOS_THROWN;92}9394/*95* accept connection but ignore ECONNABORTED indicating that96* a connection was eagerly accepted but was reset before97* accept() was called.98*/99for (;;) {100socklen_t sa_len = alloc_len;101newfd = accept(ssfd, sa, &sa_len);102if (newfd >= 0) {103break;104}105if (errno != ECONNABORTED) {106break;107}108/* ECONNABORTED => restart accept */109}110111if (newfd < 0) {112free((void *)sa);113if (errno == EAGAIN)114return IOS_UNAVAILABLE;115if (errno == EINTR)116return IOS_INTERRUPTED;117JNU_ThrowIOExceptionWithLastError(env, "Accept failed");118return IOS_THROWN;119}120121(*env)->SetIntField(env, newfdo, fd_fdID, newfd);122remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);123free((void *)sa);124CHECK_NULL_RETURN(remote_ia, IOS_THROWN);125isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port);126CHECK_NULL_RETURN(isa, IOS_THROWN);127(*env)->SetObjectArrayElement(env, isaa, 0, isa);128return 1;129}130131132