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