IOUtil.c revision 74062e1c405d0943159bb52dcacd4df240e60621
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#define NATIVE_METHOD(className, functionName, signature) \ 40{ #functionName, signature, (void*)(className ## _ ## functionName) } 41 42static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */ 43 44 45JNIEXPORT void JNICALL 46IOUtil_initIDs(JNIEnv *env, jclass clazz) 47{ 48 clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); 49 fd_fdID = (*env)->GetFieldID(env, clazz, "descriptor", "I"); 50} 51 52JNIEXPORT jboolean JNICALL 53IOUtil_randomBytes(JNIEnv *env, jclass clazz, 54 jbyteArray randArray) 55{ 56 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL); 57 return JNI_FALSE; 58} 59 60JNIEXPORT jint JNICALL 61IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo) 62{ 63 return (*env)->GetIntField(env, fdo, fd_fdID); 64} 65 66JNIEXPORT void JNICALL 67IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val) 68{ 69 (*env)->SetIntField(env, fdo, fd_fdID, val); 70} 71 72static int 73configureBlocking(int fd, jboolean blocking) 74{ 75 int flags = fcntl(fd, F_GETFL); 76 int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); 77 78 return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags); 79} 80 81JNIEXPORT void JNICALL 82IOUtil_configureBlocking(JNIEnv *env, jclass clazz, 83 jobject fdo, jboolean blocking) 84{ 85 if (configureBlocking(fdval(env, fdo), blocking) < 0) 86 JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); 87} 88 89JNIEXPORT jlong JNICALL 90IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking) 91{ 92 int fd[2]; 93 94 if (pipe(fd) < 0) { 95 JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); 96 return 0; 97 } 98 if (blocking == JNI_FALSE) { 99 if ((configureBlocking(fd[0], JNI_FALSE) < 0) 100 || (configureBlocking(fd[1], JNI_FALSE) < 0)) { 101 JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); 102 close(fd[0]); 103 close(fd[1]); 104 return 0; 105 } 106 } 107 return ((jlong) fd[0] << 32) | (jlong) fd[1]; 108} 109 110JNIEXPORT jboolean JNICALL 111IOUtil_drain(JNIEnv *env, jclass cl, jint fd) 112{ 113 char buf[128]; 114 int tn = 0; 115 116 for (;;) { 117 int n = read(fd, buf, sizeof(buf)); 118 tn += n; 119 if ((n < 0) && (errno != EAGAIN)) 120 JNU_ThrowIOExceptionWithLastError(env, "Drain"); 121 if (n == (int)sizeof(buf)) 122 continue; 123 return (tn > 0) ? JNI_TRUE : JNI_FALSE; 124 } 125} 126 127JNIEXPORT jint JNICALL 128IOUtil_fdLimit(JNIEnv *env, jclass this) 129{ 130 struct rlimit rlp; 131 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) { 132 JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed"); 133 return -1; 134 } 135 // Android-changed: Remove the rlimit < 0 check. 136 if (rlp.rlim_max > java_lang_Integer_MAX_VALUE) { 137 return java_lang_Integer_MAX_VALUE; 138 } else { 139 return (jint)rlp.rlim_max; 140 } 141} 142 143/* Declared in nio_util.h for use elsewhere in NIO */ 144 145jint 146convertReturnVal(JNIEnv *env, jint n, jboolean reading) 147{ 148 if (n > 0) /* Number of bytes written */ 149 return n; 150 else if (n == 0) { 151 if (reading) { 152 return IOS_EOF; /* EOF is -1 in javaland */ 153 } else { 154 return 0; 155 } 156 } 157 else if (errno == EAGAIN) 158 return IOS_UNAVAILABLE; 159 else if (errno == EINTR) 160 return IOS_INTERRUPTED; 161 else { 162 const char *msg = reading ? "Read failed" : "Write failed"; 163 JNU_ThrowIOExceptionWithLastError(env, msg); 164 return IOS_THROWN; 165 } 166} 167 168JNIEXPORT jint JNICALL 169IOUtil_iovMax(JNIEnv *env, jclass this) 170{ 171 jlong iov_max = sysconf(_SC_IOV_MAX); 172 if (iov_max == -1) 173 iov_max = 16; 174 return (jint)iov_max; 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 NATIVE_METHOD(IOUtil, initIDs, "()V"), 219}; 220 221void register_sun_nio_ch_IOUtil(JNIEnv* env) { 222 jniRegisterNativeMethods(env, "sun/nio/ch/IOUtil", gMethods, NELEM(gMethods)); 223} 224