< prev index next >

src/solaris/native/sun/nio/ch/IOUtil.c

Print this page
rev 13048 : 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
Reviewed-by: alanb
   1 /*
   2  * Copyright (c) 2000, 2012, 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


  98         if ((configureBlocking(fd[0], JNI_FALSE) < 0)
  99             || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
 100             JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
 101             close(fd[0]);
 102             close(fd[1]);
 103             return 0;
 104         }
 105     }
 106     return ((jlong) fd[0] << 32) | (jlong) fd[1];
 107 }
 108 
 109 JNIEXPORT jboolean JNICALL
 110 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
 111 {
 112     char buf[128];
 113     int tn = 0;
 114 
 115     for (;;) {
 116         int n = read(fd, buf, sizeof(buf));
 117         tn += n;
 118         if ((n < 0) && (errno != EAGAIN))
 119             JNU_ThrowIOExceptionWithLastError(env, "Drain");
 120         if (n == (int)sizeof(buf))
 121             continue;
 122         return (tn > 0) ? JNI_TRUE : JNI_FALSE;
 123     }
 124 }
 125 
 126 JNIEXPORT jint JNICALL
 127 Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
 128 {
 129     struct rlimit rlp;
 130     if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
 131         JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
 132         return -1;
 133     }
 134     if (rlp.rlim_max < 0 || rlp.rlim_max > java_lang_Integer_MAX_VALUE) {
 135         return java_lang_Integer_MAX_VALUE;
 136     } else {
 137         return (jint)rlp.rlim_max;
 138     }


 144     jlong iov_max = sysconf(_SC_IOV_MAX);
 145     if (iov_max == -1)
 146         iov_max = 16;
 147     return (jint)iov_max;
 148 }
 149 
 150 /* Declared in nio_util.h for use elsewhere in NIO */
 151 
 152 jint
 153 convertReturnVal(JNIEnv *env, jint n, jboolean reading)
 154 {
 155     if (n > 0) /* Number of bytes written */
 156         return n;
 157     else if (n == 0) {
 158         if (reading) {
 159             return IOS_EOF; /* EOF is -1 in javaland */
 160         } else {
 161             return 0;
 162         }
 163     }
 164     else if (errno == EAGAIN)
 165         return IOS_UNAVAILABLE;
 166     else if (errno == EINTR)
 167         return IOS_INTERRUPTED;
 168     else {
 169         const char *msg = reading ? "Read failed" : "Write failed";
 170         JNU_ThrowIOExceptionWithLastError(env, msg);
 171         return IOS_THROWN;
 172     }
 173 }
 174 
 175 /* Declared in nio_util.h for use elsewhere in NIO */
 176 
 177 jlong
 178 convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
 179 {
 180     if (n > 0) /* Number of bytes written */
 181         return n;
 182     else if (n == 0) {
 183         if (reading) {
 184             return IOS_EOF; /* EOF is -1 in javaland */
 185         } else {
 186             return 0;
 187         }
 188     }
 189     else if (errno == EAGAIN)
 190         return IOS_UNAVAILABLE;
 191     else if (errno == EINTR)
 192         return IOS_INTERRUPTED;
 193     else {
 194         const char *msg = reading ? "Read failed" : "Write failed";
 195         JNU_ThrowIOExceptionWithLastError(env, msg);
 196         return IOS_THROWN;
 197     }
 198 }
 199 
 200 jint
 201 fdval(JNIEnv *env, jobject fdo)
 202 {
 203     return (*env)->GetIntField(env, fdo, fd_fdID);
 204 }
   1 /*
   2  * Copyright (c) 2000, 2018, 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


  98         if ((configureBlocking(fd[0], JNI_FALSE) < 0)
  99             || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
 100             JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
 101             close(fd[0]);
 102             close(fd[1]);
 103             return 0;
 104         }
 105     }
 106     return ((jlong) fd[0] << 32) | (jlong) fd[1];
 107 }
 108 
 109 JNIEXPORT jboolean JNICALL
 110 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
 111 {
 112     char buf[128];
 113     int tn = 0;
 114 
 115     for (;;) {
 116         int n = read(fd, buf, sizeof(buf));
 117         tn += n;
 118         if ((n < 0) && (errno != EAGAIN && errno != EWOULDBLOCK))
 119             JNU_ThrowIOExceptionWithLastError(env, "Drain");
 120         if (n == (int)sizeof(buf))
 121             continue;
 122         return (tn > 0) ? JNI_TRUE : JNI_FALSE;
 123     }
 124 }
 125 
 126 JNIEXPORT jint JNICALL
 127 Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
 128 {
 129     struct rlimit rlp;
 130     if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
 131         JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
 132         return -1;
 133     }
 134     if (rlp.rlim_max < 0 || rlp.rlim_max > java_lang_Integer_MAX_VALUE) {
 135         return java_lang_Integer_MAX_VALUE;
 136     } else {
 137         return (jint)rlp.rlim_max;
 138     }


 144     jlong iov_max = sysconf(_SC_IOV_MAX);
 145     if (iov_max == -1)
 146         iov_max = 16;
 147     return (jint)iov_max;
 148 }
 149 
 150 /* Declared in nio_util.h for use elsewhere in NIO */
 151 
 152 jint
 153 convertReturnVal(JNIEnv *env, jint n, jboolean reading)
 154 {
 155     if (n > 0) /* Number of bytes written */
 156         return n;
 157     else if (n == 0) {
 158         if (reading) {
 159             return IOS_EOF; /* EOF is -1 in javaland */
 160         } else {
 161             return 0;
 162         }
 163     }
 164     else if (errno == EAGAIN || errno == EWOULDBLOCK)
 165         return IOS_UNAVAILABLE;
 166     else if (errno == EINTR)
 167         return IOS_INTERRUPTED;
 168     else {
 169         const char *msg = reading ? "Read failed" : "Write failed";
 170         JNU_ThrowIOExceptionWithLastError(env, msg);
 171         return IOS_THROWN;
 172     }
 173 }
 174 
 175 /* Declared in nio_util.h for use elsewhere in NIO */
 176 
 177 jlong
 178 convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
 179 {
 180     if (n > 0) /* Number of bytes written */
 181         return n;
 182     else if (n == 0) {
 183         if (reading) {
 184             return IOS_EOF; /* EOF is -1 in javaland */
 185         } else {
 186             return 0;
 187         }
 188     }
 189     else if (errno == EAGAIN || errno == EWOULDBLOCK)
 190         return IOS_UNAVAILABLE;
 191     else if (errno == EINTR)
 192         return IOS_INTERRUPTED;
 193     else {
 194         const char *msg = reading ? "Read failed" : "Write failed";
 195         JNU_ThrowIOExceptionWithLastError(env, msg);
 196         return IOS_THROWN;
 197     }
 198 }
 199 
 200 jint
 201 fdval(JNIEnv *env, jobject fdo)
 202 {
 203     return (*env)->GetIntField(env, fdo, fd_fdID);
 204 }
< prev index next >