1/*
2 * Copyright (c) 2000, 2015, 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
39static jfieldID fd_fdID;        /* for jint 'fd' in java.io.FileDescriptor */
40
41
42JNIEXPORT void JNICALL
43Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
44{
45    CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
46    CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
47}
48
49JNIEXPORT jboolean JNICALL
50Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,
51                                  jbyteArray randArray)
52{
53    JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL);
54    return JNI_FALSE;
55}
56
57JNIEXPORT jint JNICALL
58Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)
59{
60    return (*env)->GetIntField(env, fdo, fd_fdID);
61}
62
63JNIEXPORT void JNICALL
64Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)
65{
66    (*env)->SetIntField(env, fdo, fd_fdID, val);
67}
68
69static int
70configureBlocking(int fd, jboolean blocking)
71{
72    int flags = fcntl(fd, F_GETFL);
73    int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
74
75    return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);
76}
77
78JNIEXPORT void JNICALL
79Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
80                                         jobject fdo, jboolean blocking)
81{
82    if (configureBlocking(fdval(env, fdo), blocking) < 0)
83        JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
84}
85
86JNIEXPORT jlong JNICALL
87Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
88{
89    int fd[2];
90
91    if (pipe(fd) < 0) {
92        JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
93        return 0;
94    }
95    if (blocking == JNI_FALSE) {
96        if ((configureBlocking(fd[0], JNI_FALSE) < 0)
97            || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
98            JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
99            close(fd[0]);
100            close(fd[1]);
101            return 0;
102        }
103    }
104    return ((jlong) fd[0] << 32) | (jlong) fd[1];
105}
106
107JNIEXPORT jboolean JNICALL
108Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
109{
110    char buf[128];
111    int tn = 0;
112
113    for (;;) {
114        int n = read(fd, buf, sizeof(buf));
115        tn += n;
116        if ((n < 0) && (errno != EAGAIN))
117            JNU_ThrowIOExceptionWithLastError(env, "Drain");
118        if (n == (int)sizeof(buf))
119            continue;
120        return (tn > 0) ? JNI_TRUE : JNI_FALSE;
121    }
122}
123
124JNIEXPORT jint JNICALL
125Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
126{
127    struct rlimit rlp;
128    if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
129        JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
130        return -1;
131    }
132    if (rlp.rlim_max == RLIM_INFINITY ||
133        rlp.rlim_max > (rlim_t)java_lang_Integer_MAX_VALUE) {
134        return java_lang_Integer_MAX_VALUE;
135    } else {
136        return (jint)rlp.rlim_max;
137    }
138}
139
140JNIEXPORT jint JNICALL
141Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)
142{
143    jlong iov_max = sysconf(_SC_IOV_MAX);
144    if (iov_max == -1)
145        iov_max = 16;
146    return (jint)iov_max;
147}
148
149/* Declared in nio_util.h for use elsewhere in NIO */
150
151jint
152convertReturnVal(JNIEnv *env, jint n, jboolean reading)
153{
154    if (n > 0) /* Number of bytes written */
155        return n;
156    else if (n == 0) {
157        if (reading) {
158            return IOS_EOF; /* EOF is -1 in javaland */
159        } else {
160            return 0;
161        }
162    }
163    else if (errno == EAGAIN)
164        return IOS_UNAVAILABLE;
165    else if (errno == EINTR)
166        return IOS_INTERRUPTED;
167    else {
168        const char *msg = reading ? "Read failed" : "Write failed";
169        JNU_ThrowIOExceptionWithLastError(env, msg);
170        return IOS_THROWN;
171    }
172}
173
174/* Declared in nio_util.h for use elsewhere in NIO */
175
176jlong
177convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
178{
179    if (n > 0) /* Number of bytes written */
180        return n;
181    else if (n == 0) {
182        if (reading) {
183            return IOS_EOF; /* EOF is -1 in javaland */
184        } else {
185            return 0;
186        }
187    }
188    else if (errno == EAGAIN)
189        return IOS_UNAVAILABLE;
190    else if (errno == EINTR)
191        return IOS_INTERRUPTED;
192    else {
193        const char *msg = reading ? "Read failed" : "Write failed";
194        JNU_ThrowIOExceptionWithLastError(env, msg);
195        return IOS_THROWN;
196    }
197}
198
199jint
200fdval(JNIEnv *env, jobject fdo)
201{
202    return (*env)->GetIntField(env, fdo, fd_fdID);
203}
204