Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/ch/IOUtil.c
32288 views
/*1* Copyright (c) 2000, 2012, 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 <sys/types.h>26#include <string.h>27#include <sys/resource.h>2829#include "jni.h"30#include "jni_util.h"31#include "jvm.h"32#include "jlong.h"33#include "sun_nio_ch_IOUtil.h"34#include "java_lang_Integer.h"35#include "nio.h"36#include "nio_util.h"37#include "net_util.h"3839static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */404142JNIEXPORT void JNICALL43Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)44{45CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));46CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));47initInetAddressIDs(env);48}4950JNIEXPORT jboolean JNICALL51Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,52jbyteArray randArray)53{54JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL);55return JNI_FALSE;56}5758JNIEXPORT jint JNICALL59Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)60{61return (*env)->GetIntField(env, fdo, fd_fdID);62}6364JNIEXPORT void JNICALL65Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)66{67(*env)->SetIntField(env, fdo, fd_fdID, val);68}6970static int71configureBlocking(int fd, jboolean blocking)72{73int flags = fcntl(fd, F_GETFL);74int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);7576return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);77}7879JNIEXPORT void JNICALL80Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,81jobject fdo, jboolean blocking)82{83if (configureBlocking(fdval(env, fdo), blocking) < 0)84JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");85}8687JNIEXPORT jlong JNICALL88Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)89{90int fd[2];9192if (pipe(fd) < 0) {93JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");94return 0;95}96if (blocking == JNI_FALSE) {97if ((configureBlocking(fd[0], JNI_FALSE) < 0)98|| (configureBlocking(fd[1], JNI_FALSE) < 0)) {99JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");100close(fd[0]);101close(fd[1]);102return 0;103}104}105return ((jlong) fd[0] << 32) | (jlong) fd[1];106}107108JNIEXPORT jboolean JNICALL109Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)110{111char buf[128];112int tn = 0;113114for (;;) {115int n = read(fd, buf, sizeof(buf));116tn += n;117if ((n < 0) && (errno != EAGAIN))118JNU_ThrowIOExceptionWithLastError(env, "Drain");119if (n == (int)sizeof(buf))120continue;121return (tn > 0) ? JNI_TRUE : JNI_FALSE;122}123}124125JNIEXPORT jint JNICALL126Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)127{128struct rlimit rlp;129if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {130JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");131return -1;132}133if (rlp.rlim_max < 0 || rlp.rlim_max > java_lang_Integer_MAX_VALUE) {134return java_lang_Integer_MAX_VALUE;135} else {136return (jint)rlp.rlim_max;137}138}139140JNIEXPORT jint JNICALL141Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)142{143jlong iov_max = sysconf(_SC_IOV_MAX);144if (iov_max == -1)145iov_max = 16;146return (jint)iov_max;147}148149/* Declared in nio_util.h for use elsewhere in NIO */150151jint152convertReturnVal(JNIEnv *env, jint n, jboolean reading)153{154if (n > 0) /* Number of bytes written */155return n;156else if (n == 0) {157if (reading) {158return IOS_EOF; /* EOF is -1 in javaland */159} else {160return 0;161}162}163else if (errno == EAGAIN)164return IOS_UNAVAILABLE;165else if (errno == EINTR)166return IOS_INTERRUPTED;167else {168const char *msg = reading ? "Read failed" : "Write failed";169JNU_ThrowIOExceptionWithLastError(env, msg);170return IOS_THROWN;171}172}173174/* Declared in nio_util.h for use elsewhere in NIO */175176jlong177convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)178{179if (n > 0) /* Number of bytes written */180return n;181else if (n == 0) {182if (reading) {183return IOS_EOF; /* EOF is -1 in javaland */184} else {185return 0;186}187}188else if (errno == EAGAIN)189return IOS_UNAVAILABLE;190else if (errno == EINTR)191return IOS_INTERRUPTED;192else {193const char *msg = reading ? "Read failed" : "Write failed";194JNU_ThrowIOExceptionWithLastError(env, msg);195return IOS_THROWN;196}197}198199jint200fdval(JNIEnv *env, jobject fdo)201{202return (*env)->GetIntField(env, fdo, fd_fdID);203}204205206