1 /*
   2  * Copyright (c) 1997, 2016, 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 <stdlib.h>
  27 #include <errno.h>
  28 #include <string.h>
  29 #include <sys/types.h>
  30 #include <sys/socket.h>
  31 #include <sys/time.h>
  32 #include "jvm.h"
  33 #include "jni_util.h"
  34 #include "net_util.h"
  35 
  36 #include "java_net_SocketInputStream.h"
  37 
  38 /************************************************************************
  39  * SocketInputStream
  40  */
  41 
  42 static jfieldID IO_fd_fdID;
  43 
  44 /*
  45  * Class:     java_net_SocketInputStream
  46  * Method:    init
  47  * Signature: ()V
  48  */
  49 JNIEXPORT void JNICALL
  50 Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
  51     IO_fd_fdID = NET_GetFileDescriptorID(env);
  52 }
  53 
  54 static long getCurrentTime() {
  55     struct timeval time;
  56     gettimeofday(&time, NULL);
  57     return (time.tv_sec * 1000 + time.tv_usec / 1000);
  58 }
  59 
  60 static int NET_TimeoutWithCurrentTime(int s, long timeout, long current_time) {
  61     int result;
  62     long prevtime = current_time, newtime;
  63     struct pollfd pfd;
  64     pfd.fd = s;
  65     pfd.events = POLLIN;
  66     for (;;) {
  67         result = poll(&pfd, 1, timeout);
  68         if (result < 0 && errno == EINTR) {
  69             if (timeout > 0) {
  70                 newtime = getCurrentTime();
  71                 timeout -= newtime - prevtime;
  72                 if (timeout <= 0)
  73                     return 0;
  74                 prevtime = newtime;
  75             }
  76         } else {
  77             return result;
  78         }
  79     }
  80 }
  81 
  82 static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
  83     int result;
  84     long prevtime = getCurrentTime(), newtime;
  85     while (timeout > 0) {
  86         result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
  87         if (result <= 0) {
  88             if (result == 0) {
  89                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", "Read timed out");
  90             } else if (result == -1) {
  91                 if (errno == EBADF) {
  92                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
  93                 } else if (errno == ENOMEM) {
  94                     JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
  95                 } else {
  96                     JNU_ThrowByNameWithMessageAndLastError
  97                             (env, JNU_JAVANETPKG "SocketException", "select/poll failed");
  98                 }
  99             }
 100             return -1;
 101         }
 102         result = NET_NonBlockingRead(fd, bufP, len);
 103         if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
 104             newtime = getCurrentTime();
 105             timeout -= newtime - prevtime;
 106             if (timeout > 0) {
 107                 prevtime = newtime;
 108             }
 109         } else {
 110             break;
 111         }
 112     }
 113     return result;
 114 }
 115 
 116 /*
 117  * Class:     java_net_SocketInputStream
 118  * Method:    socketRead0
 119  * Signature: (Ljava/io/FileDescriptor;[BIII)I
 120  */
 121 JNIEXPORT jint JNICALL
 122 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
 123                                             jobject fdObj, jbyteArray data,
 124                                             jint off, jint len, jint timeout)
 125 {
 126     char BUF[MAX_BUFFER_LEN];
 127     char *bufP;
 128     jint fd, nread;
 129     
 130     if (IS_NULL(fdObj)) {
 131         /* shouldn't this be a NullPointerException? -br */
 132         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 133                         "Socket closed");
 134         return -1;
 135     } else {
 136         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
 137         /* Bug 4086704 - If the Socket associated with this file descriptor
 138          * was closed (sysCloseFD), then the file descriptor is set to -1.
 139          */
 140         if (fd == -1) {
 141             JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
 142             return -1;
 143         }
 144     }
 145 
 146     /*
 147      * If the read is greater than our stack allocated buffer then
 148      * we allocate from the heap (up to a limit)
 149      */
 150     if (len > MAX_BUFFER_LEN) {
 151         if (len > MAX_HEAP_BUFFER_LEN) {
 152             len = MAX_HEAP_BUFFER_LEN;
 153         }
 154         bufP = (char *)malloc((size_t)len);
 155         if (bufP == NULL) {
 156             bufP = BUF;
 157             len = MAX_BUFFER_LEN;
 158         }
 159     } else {
 160         bufP = BUF;
 161     }
 162     if (timeout) {
 163         nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);
 164     } else {
 165         nread = NET_Read(fd, bufP, len);
 166     }
 167     if (nread <= 0) {
 168         if (nread < 0) {
 169 
 170             switch (errno) {
 171                 case ECONNRESET:
 172                 case EPIPE:
 173                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 174                         "Connection reset");
 175                     break;
 176 
 177                 case EBADF:
 178                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 179                         "Socket closed");
 180                     break;
 181 
 182                 case EINTR:
 183                      JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
 184                            "Operation interrupted");
 185                      break;
 186                 default:
 187                     JNU_ThrowByNameWithMessageAndLastError
 188                         (env, JNU_JAVANETPKG "SocketException", "Read failed");
 189             }
 190         }
 191     } else {
 192         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
 193     }
 194 
 195     if (bufP != BUF) {
 196         free(bufP);
 197     }
 198     return nread;
 199 }