Path: blob/master/src/java.base/unix/native/libnio/ch/IOUtil.c
41133 views
/*1* Copyright (c) 2000, 2019, 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"3738static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */394041JNIEXPORT void JNICALL42Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)43{44CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));45CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));46}4748JNIEXPORT jboolean JNICALL49Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,50jbyteArray randArray)51{52JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL);53return JNI_FALSE;54}5556JNIEXPORT jint JNICALL57Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)58{59return (*env)->GetIntField(env, fdo, fd_fdID);60}6162JNIEXPORT void JNICALL63Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)64{65setfdval(env, fdo, val);66}6768static int69configureBlocking(int fd, jboolean blocking)70{71int flags = fcntl(fd, F_GETFL);72int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);7374return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);75}7677JNIEXPORT void JNICALL78Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,79jobject fdo, jboolean blocking)80{81if (configureBlocking(fdval(env, fdo), blocking) < 0)82JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");83}8485JNIEXPORT jlong JNICALL86Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)87{88int fd[2];8990if (pipe(fd) < 0) {91JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");92return 0;93}94if (blocking == JNI_FALSE) {95if ((configureBlocking(fd[0], JNI_FALSE) < 0)96|| (configureBlocking(fd[1], JNI_FALSE) < 0)) {97JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");98close(fd[0]);99close(fd[1]);100return 0;101}102}103return ((jlong) fd[0] << 32) | (jlong) fd[1];104}105106JNIEXPORT jint JNICALL107Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b)108{109char c = (char)b;110return convertReturnVal(env, write(fd, &c, 1), JNI_FALSE);111}112113JNIEXPORT jboolean JNICALL114Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)115{116char buf[16];117int tn = 0;118119for (;;) {120int n = read(fd, buf, sizeof(buf));121tn += n;122if ((n < 0) && (errno != EAGAIN && errno != EWOULDBLOCK))123JNU_ThrowIOExceptionWithLastError(env, "Drain");124if (n == (int)sizeof(buf))125continue;126return (tn > 0) ? JNI_TRUE : JNI_FALSE;127}128}129130JNIEXPORT jint JNICALL131Java_sun_nio_ch_IOUtil_drain1(JNIEnv *env, jclass cl, jint fd)132{133int res;134char buf[1];135136res = read(fd, buf, 1);137if (res < 0) {138if (errno == EAGAIN || errno == EWOULDBLOCK) {139res = 0;140} else if (errno == EINTR) {141return IOS_INTERRUPTED;142} else {143JNU_ThrowIOExceptionWithLastError(env, "read");144return IOS_THROWN;145}146}147return res;148}149150JNIEXPORT jint JNICALL151Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)152{153struct rlimit rlp;154if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {155JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");156return -1;157}158if (rlp.rlim_max == RLIM_INFINITY ||159rlp.rlim_max > (rlim_t)java_lang_Integer_MAX_VALUE) {160return java_lang_Integer_MAX_VALUE;161} else {162return (jint)rlp.rlim_max;163}164}165166JNIEXPORT jint JNICALL167Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)168{169jlong iov_max = sysconf(_SC_IOV_MAX);170if (iov_max == -1)171iov_max = 16;172return (jint)iov_max;173}174175/* Declared in nio_util.h for use elsewhere in NIO */176177jint178convertReturnVal(JNIEnv *env, jint n, jboolean reading)179{180if (n > 0) /* Number of bytes written */181return n;182else if (n == 0) {183if (reading) {184return IOS_EOF; /* EOF is -1 in javaland */185} else {186return 0;187}188}189else if (errno == EAGAIN || errno == EWOULDBLOCK)190return IOS_UNAVAILABLE;191else if (errno == EINTR)192return IOS_INTERRUPTED;193else {194const char *msg = reading ? "Read failed" : "Write failed";195JNU_ThrowIOExceptionWithLastError(env, msg);196return IOS_THROWN;197}198}199200/* Declared in nio_util.h for use elsewhere in NIO */201202jlong203convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)204{205if (n > 0) /* Number of bytes written */206return n;207else if (n == 0) {208if (reading) {209return IOS_EOF; /* EOF is -1 in javaland */210} else {211return 0;212}213}214else if (errno == EAGAIN || errno == EWOULDBLOCK)215return IOS_UNAVAILABLE;216else if (errno == EINTR)217return IOS_INTERRUPTED;218else {219const char *msg = reading ? "Read failed" : "Write failed";220JNU_ThrowIOExceptionWithLastError(env, msg);221return IOS_THROWN;222}223}224225jint226fdval(JNIEnv *env, jobject fdo)227{228return (*env)->GetIntField(env, fdo, fd_fdID);229}230231void232setfdval(JNIEnv *env, jobject fdo, jint val) {233(*env)->SetIntField(env, fdo, fd_fdID, val);234}235236237238239