< prev index next >

src/java.base/unix/native/libnio/ch/IOUtil.c

Print this page

  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

 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         }

  1 /*
  2  * Copyright (c) 2000, 2025, 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

 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, jint fd, jboolean blocking)

 81 {
 82     if (configureBlocking(fd, blocking) < 0)
 83         JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
 84 }
 85 
 86 JNIEXPORT jlong JNICALL
 87 Java_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         }
< prev index next >