1 /*
  2  * Copyright (c) 2000, 2021, 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 "java_lang_Long.h"
 37 #include "nio.h"
 38 #include "nio_util.h"
 39 
 40 static jfieldID fd_fdID;        /* for jint 'fd' in java.io.FileDescriptor */
 41 
 42 
 43 JNIEXPORT void JNICALL
 44 Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
 45 {
 46     CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
 47     CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
 48 }
 49 
 50 JNIEXPORT jboolean JNICALL
 51 Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,
 52                                   jbyteArray randArray)
 53 {
 54     JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL);
 55     return JNI_FALSE;
 56 }
 57 
 58 JNIEXPORT jint JNICALL
 59 Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)
 60 {
 61     return (*env)->GetIntField(env, fdo, fd_fdID);
 62 }
 63 
 64 JNIEXPORT void JNICALL
 65 Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)
 66 {
 67     setfdval(env, fdo, val);
 68 }
 69 
 70 static int
 71 configureBlocking(int fd, jboolean blocking)
 72 {
 73     int flags = fcntl(fd, F_GETFL);
 74     int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
 75 
 76     return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);
 77 }
 78 
 79 JNIEXPORT void JNICALL
 80 Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
 81                                          jobject fdo, jboolean blocking)
 82 {
 83     if (configureBlocking(fdval(env, fdo), blocking) < 0)
 84         JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
 85 }
 86 
 87 JNIEXPORT jlong JNICALL
 88 Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
 89 {
 90     int fd[2];
 91 
 92     if (pipe(fd) < 0) {
 93         JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
 94         return 0;
 95     }
 96     if (blocking == JNI_FALSE) {
 97         if ((configureBlocking(fd[0], JNI_FALSE) < 0)
 98             || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
 99             JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
100             close(fd[0]);
101             close(fd[1]);
102             return 0;
103         }
104     }
105     return ((jlong) fd[0] << 32) | (jlong) fd[1];
106 }
107 
108 JNIEXPORT jint JNICALL
109 Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b)
110 {
111     char c = (char)b;
112     return convertReturnVal(env, write(fd, &c, 1), JNI_FALSE);
113 }
114 
115 JNIEXPORT jboolean JNICALL
116 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
117 {
118     char buf[16];
119     int tn = 0;
120 
121     for (;;) {
122         int n = read(fd, buf, sizeof(buf));
123         tn += n;
124         if ((n < 0) && (errno != EAGAIN && errno != EWOULDBLOCK))
125             JNU_ThrowIOExceptionWithLastError(env, "Drain");
126         if (n == (int)sizeof(buf))
127             continue;
128         return (tn > 0) ? JNI_TRUE : JNI_FALSE;
129     }
130 }
131 
132 JNIEXPORT jint JNICALL
133 Java_sun_nio_ch_IOUtil_drain1(JNIEnv *env, jclass cl, jint fd)
134 {
135     int res;
136     char buf[1];
137 
138     res = read(fd, buf, 1);
139     if (res < 0) {
140         if (errno == EAGAIN || errno == EWOULDBLOCK) {
141             res = 0;
142         } else if (errno == EINTR) {
143             return IOS_INTERRUPTED;
144         } else {
145             JNU_ThrowIOExceptionWithLastError(env, "read");
146             return IOS_THROWN;
147         }
148     }
149     return res;
150 }
151 
152 JNIEXPORT jint JNICALL
153 Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
154 {
155     struct rlimit rlp;
156     if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
157         JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
158         return -1;
159     }
160     if (rlp.rlim_max == RLIM_INFINITY ||
161         rlp.rlim_max > (rlim_t)java_lang_Integer_MAX_VALUE) {
162         return java_lang_Integer_MAX_VALUE;
163     } else {
164         return (jint)rlp.rlim_max;
165     }
166 }
167 
168 JNIEXPORT jint JNICALL
169 Java_sun_nio_ch_IOUtil_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 JNIEXPORT jlong JNICALL
178 Java_sun_nio_ch_IOUtil_writevMax(JNIEnv *env, jclass this)
179 {
180 #if defined(MACOSX) || defined(__linux__)
181     //
182     // The man pages of writev() on both Linux and macOS specify this
183     // constraint on the sum of all byte lengths in the iovec array:
184     //
185     // [EINVAL] The sum of the iov_len values in the iov array
186     //          overflows a 32-bit integer.
187     //
188     // As of macOS 11 Big Sur, Darwin version 20, writev() started to
189     // actually enforce the constraint which had been previously ignored.
190     //
191     // In practice on Linux writev() has been observed not to write more
192     // than 0x7fff0000 (aarch64) or 0x7ffff000 (x64) bytes in one call.
193     //
194     return java_lang_Integer_MAX_VALUE;
195 #else
196     return java_lang_Long_MAX_VALUE;
197 #endif
198 }
199 
200 /* Declared in nio_util.h for use elsewhere in NIO */
201 
202 jint
203 convertReturnVal(JNIEnv *env, jint n, jboolean reading)
204 {
205     if (n > 0) /* Number of bytes written */
206         return n;
207     else if (n == 0) {
208         if (reading) {
209             return IOS_EOF; /* EOF is -1 in javaland */
210         } else {
211             return 0;
212         }
213     }
214     else if (errno == EAGAIN || errno == EWOULDBLOCK)
215         return IOS_UNAVAILABLE;
216     else if (errno == EINTR)
217         return IOS_INTERRUPTED;
218     else {
219         const char *msg = reading ? "Read failed" : "Write failed";
220         JNU_ThrowIOExceptionWithLastError(env, msg);
221         return IOS_THROWN;
222     }
223 }
224 
225 /* Declared in nio_util.h for use elsewhere in NIO */
226 
227 jlong
228 convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
229 {
230     if (n > 0) /* Number of bytes written */
231         return n;
232     else if (n == 0) {
233         if (reading) {
234             return IOS_EOF; /* EOF is -1 in javaland */
235         } else {
236             return 0;
237         }
238     }
239     else if (errno == EAGAIN || errno == EWOULDBLOCK)
240         return IOS_UNAVAILABLE;
241     else if (errno == EINTR)
242         return IOS_INTERRUPTED;
243     else {
244         const char *msg = reading ? "Read failed" : "Write failed";
245         JNU_ThrowIOExceptionWithLastError(env, msg);
246         return IOS_THROWN;
247     }
248 }
249 
250 jint
251 fdval(JNIEnv *env, jobject fdo)
252 {
253     return (*env)->GetIntField(env, fdo, fd_fdID);
254 }
255 
256 void
257 setfdval(JNIEnv *env, jobject fdo, jint val) {
258     (*env)->SetIntField(env, fdo, fd_fdID, val);
259 }
260 
261