1/* 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26#include <sys/types.h> 27#include <string.h> 28#include <sys/resource.h> 29 30#include "jni.h" 31#include "jni_util.h" 32#include "jvm.h" 33#include "jlong.h" 34#include "java_lang_Integer.h" 35#include "nio.h" 36#include "nio_util.h" 37#include "JNIHelp.h" 38 39 40#define NATIVE_METHOD(className, functionName, signature) \ 41{ #functionName, signature, (void*)(className ## _ ## functionName) } 42 43static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */ 44 45static void IOUtil_initIDs(JNIEnv *env) 46{ 47 jclass clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); 48 CHECK_NULL(clazz); 49 fd_fdID = (*env)->GetFieldID(env, clazz, "descriptor", "I"); 50 CHECK_NULL(fd_fdID); 51} 52 53JNIEXPORT jboolean JNICALL 54IOUtil_randomBytes(JNIEnv *env, jclass clazz, 55 jbyteArray randArray) 56{ 57 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL); 58 return JNI_FALSE; 59} 60 61JNIEXPORT jint JNICALL 62IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo) 63{ 64 return (*env)->GetIntField(env, fdo, fd_fdID); 65} 66 67JNIEXPORT void JNICALL 68IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val) 69{ 70 (*env)->SetIntField(env, fdo, fd_fdID, val); 71} 72 73static int 74configureBlocking(int fd, jboolean blocking) 75{ 76 int flags = fcntl(fd, F_GETFL); 77 int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); 78 79 return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags); 80} 81 82JNIEXPORT void JNICALL 83IOUtil_configureBlocking(JNIEnv *env, jclass clazz, 84 jobject fdo, jboolean blocking) 85{ 86 if (configureBlocking(fdval(env, fdo), blocking) < 0) 87 JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); 88} 89 90JNIEXPORT jlong JNICALL 91IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking) 92{ 93 int fd[2]; 94 95 if (pipe(fd) < 0) { 96 JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); 97 return 0; 98 } 99 if (blocking == JNI_FALSE) { 100 if ((configureBlocking(fd[0], JNI_FALSE) < 0) 101 || (configureBlocking(fd[1], JNI_FALSE) < 0)) { 102 JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); 103 close(fd[0]); 104 close(fd[1]); 105 return 0; 106 } 107 } 108 return ((jlong) fd[0] << 32) | (jlong) fd[1]; 109} 110 111JNIEXPORT jboolean JNICALL 112IOUtil_drain(JNIEnv *env, jclass cl, jint fd) 113{ 114 char buf[128]; 115 int tn = 0; 116 117 for (;;) { 118 int n = read(fd, buf, sizeof(buf)); 119 tn += n; 120 if ((n < 0) && (errno != EAGAIN)) 121 JNU_ThrowIOExceptionWithLastError(env, "Drain"); 122 if (n == (int)sizeof(buf)) 123 continue; 124 return (tn > 0) ? JNI_TRUE : JNI_FALSE; 125 } 126} 127 128JNIEXPORT jint JNICALL 129IOUtil_fdLimit(JNIEnv *env, jclass this) 130{ 131 struct rlimit rlp; 132 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) { 133 JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed"); 134 return -1; 135 } 136 // Android-changed: Remove the rlimit < 0 check. 137 if (rlp.rlim_max > java_lang_Integer_MAX_VALUE) { 138 return java_lang_Integer_MAX_VALUE; 139 } else { 140 return (jint)rlp.rlim_max; 141 } 142} 143 144JNIEXPORT jint JNICALL 145IOUtil_iovMax(JNIEnv *env, jclass this) 146{ 147 jlong iov_max = sysconf(_SC_IOV_MAX); 148 if (iov_max == -1) 149 iov_max = 16; 150 return (jint)iov_max; 151} 152 153/* Declared in nio_util.h for use elsewhere in NIO */ 154 155jint 156convertReturnVal(JNIEnv *env, jint n, jboolean reading) 157{ 158 if (n > 0) /* Number of bytes written */ 159 return n; 160 else if (n == 0) { 161 if (reading) { 162 return IOS_EOF; /* EOF is -1 in javaland */ 163 } else { 164 return 0; 165 } 166 } 167 else if (errno == EAGAIN) 168 return IOS_UNAVAILABLE; 169 else if (errno == EINTR) 170 return IOS_INTERRUPTED; 171 else { 172 const char *msg = reading ? "Read failed" : "Write failed"; 173 JNU_ThrowIOExceptionWithLastError(env, msg); 174 return IOS_THROWN; 175 } 176} 177 178/* Declared in nio_util.h for use elsewhere in NIO */ 179 180jlong 181convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) 182{ 183 if (n > 0) /* Number of bytes written */ 184 return n; 185 else if (n == 0) { 186 if (reading) { 187 return IOS_EOF; /* EOF is -1 in javaland */ 188 } else { 189 return 0; 190 } 191 } 192 else if (errno == EAGAIN) 193 return IOS_UNAVAILABLE; 194 else if (errno == EINTR) 195 return IOS_INTERRUPTED; 196 else { 197 const char *msg = reading ? "Read failed" : "Write failed"; 198 JNU_ThrowIOExceptionWithLastError(env, msg); 199 return IOS_THROWN; 200 } 201} 202 203jint 204fdval(JNIEnv *env, jobject fdo) 205{ 206 return (*env)->GetIntField(env, fdo, fd_fdID); 207} 208 209static JNINativeMethod gMethods[] = { 210 NATIVE_METHOD(IOUtil, iovMax, "()I"), 211 NATIVE_METHOD(IOUtil, fdLimit, "()I"), 212 NATIVE_METHOD(IOUtil, drain, "(I)Z"), 213 NATIVE_METHOD(IOUtil, makePipe, "(Z)J"), 214 NATIVE_METHOD(IOUtil, configureBlocking, "(Ljava/io/FileDescriptor;Z)V"), 215 NATIVE_METHOD(IOUtil, setfdVal, "(Ljava/io/FileDescriptor;I)V"), 216 NATIVE_METHOD(IOUtil, fdVal, "(Ljava/io/FileDescriptor;)I"), 217 NATIVE_METHOD(IOUtil, randomBytes, "([B)Z"), 218}; 219 220void register_sun_nio_ch_IOUtil(JNIEnv* env) { 221 jniRegisterNativeMethods(env, "sun/nio/ch/IOUtil", gMethods, NELEM(gMethods)); 222 223 IOUtil_initIDs(env); 224} 225