IOUtil.c revision 3d43823e17818b3c5f45831f830884917b181454
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 "sun_nio_ch_IOUtil.h" 35#include "java_lang_Integer.h" 36#include "nio.h" 37#include "nio_util.h" 38#include "JNIHelp.h" 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 45 46JNIEXPORT void JNICALL 47IOUtil_initIDs(JNIEnv *env, jclass clazz) 48{ 49 clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); 50 fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"); 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 144/* Declared in nio_util.h for use elsewhere in NIO */ 145 146jint 147convertReturnVal(JNIEnv *env, jint n, jboolean reading) 148{ 149 if (n > 0) /* Number of bytes written */ 150 return n; 151 else if (n == 0) { 152 if (reading) { 153 return IOS_EOF; /* EOF is -1 in javaland */ 154 } else { 155 return 0; 156 } 157 } 158 else if (errno == EAGAIN) 159 return IOS_UNAVAILABLE; 160 else if (errno == EINTR) 161 return IOS_INTERRUPTED; 162 else { 163 const char *msg = reading ? "Read failed" : "Write failed"; 164 JNU_ThrowIOExceptionWithLastError(env, msg); 165 return IOS_THROWN; 166 } 167} 168 169JNIEXPORT jint JNICALL 170IOUtil_iovMax(JNIEnv *env, jclass this) 171{ 172 jlong iov_max = sysconf(_SC_IOV_MAX); 173 if (iov_max == -1) 174 iov_max = 16; 175 return (jint)iov_max; 176} 177 178 179/* Declared in nio_util.h for use elsewhere in NIO */ 180 181jlong 182convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) 183{ 184 if (n > 0) /* Number of bytes written */ 185 return n; 186 else if (n == 0) { 187 if (reading) { 188 return IOS_EOF; /* EOF is -1 in javaland */ 189 } else { 190 return 0; 191 } 192 } 193 else if (errno == EAGAIN) 194 return IOS_UNAVAILABLE; 195 else if (errno == EINTR) 196 return IOS_INTERRUPTED; 197 else { 198 const char *msg = reading ? "Read failed" : "Write failed"; 199 JNU_ThrowIOExceptionWithLastError(env, msg); 200 return IOS_THROWN; 201 } 202} 203 204jint 205fdval(JNIEnv *env, jobject fdo) 206{ 207 return (*env)->GetIntField(env, fdo, fd_fdID); 208} 209 210static JNINativeMethod gMethods[] = { 211 NATIVE_METHOD(IOUtil, iovMax, "()I"), 212 NATIVE_METHOD(IOUtil, fdLimit, "()I"), 213 NATIVE_METHOD(IOUtil, drain, "(I)Z"), 214 NATIVE_METHOD(IOUtil, makePipe, "(Z)J"), 215 NATIVE_METHOD(IOUtil, configureBlocking, "(Ljava/io/FileDescriptor;Z)V"), 216 NATIVE_METHOD(IOUtil, setfdVal, "(Ljava/io/FileDescriptor;I)V"), 217 NATIVE_METHOD(IOUtil, fdVal, "(Ljava/io/FileDescriptor;)I"), 218 NATIVE_METHOD(IOUtil, randomBytes, "([B)Z"), 219 NATIVE_METHOD(IOUtil, initIDs, "()V"), 220}; 221 222void register_sun_nio_ch_IOUtil(JNIEnv* env) { 223 jniRegisterNativeMethods(env, "sun/nio/ch/IOUtil", gMethods, NELEM(gMethods)); 224} 225