1 /*
   2  * Copyright (c) 2000, 2019, 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/mman.h>
  27 #include <sys/stat.h>
  28 #include <fcntl.h>
  29 #include <sys/types.h>
  30 #include <unistd.h>
  31 
  32 #if defined(__linux__) || defined(__solaris__)
  33 #include <sys/sendfile.h>
  34 #elif defined(_AIX)
  35 #include <string.h>
  36 #include <sys/socket.h>
  37 #elif defined(_ALLBSD_SOURCE)
  38 #include <sys/socket.h>
  39 #include <sys/uio.h>
  40 #define lseek64 lseek
  41 #define mmap64 mmap
  42 #endif
  43 
  44 #include "jni.h"
  45 #include "jni_util.h"
  46 #include "jlong.h"
  47 #include "nio.h"
  48 #include "nio_util.h"
  49 #include "sun_nio_ch_FileChannelImpl.h"
  50 #include "java_lang_Integer.h"
  51 
  52 static jfieldID chan_fd;        /* jobject 'fd' in sun.nio.ch.FileChannelImpl */
  53 
  54 JNIEXPORT jlong JNICALL
  55 Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
  56 {
  57     jlong pageSize = sysconf(_SC_PAGESIZE);
  58     chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
  59     return pageSize;
  60 }
  61 
  62 static jlong
  63 handle(JNIEnv *env, jlong rv, char *msg)
  64 {
  65     if (rv >= 0)
  66         return rv;
  67     if (errno == EINTR)
  68         return IOS_INTERRUPTED;
  69     JNU_ThrowIOExceptionWithLastError(env, msg);
  70     return IOS_THROWN;
  71 }
  72 
  73 
  74 JNIEXPORT jlong JNICALL
  75 Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
  76                                      jint prot, jlong off, jlong len)
  77 {
  78     void *mapAddress = 0;
  79     jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
  80     jint fd = fdval(env, fdo);
  81     int protections = 0;
  82     int flags = 0;
  83 
  84     if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
  85         protections = PROT_READ;
  86         flags = MAP_SHARED;
  87     } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
  88         protections = PROT_WRITE | PROT_READ;
  89         flags = MAP_SHARED;
  90     } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
  91         protections =  PROT_WRITE | PROT_READ;
  92         flags = MAP_PRIVATE;
  93     }
  94 
  95     mapAddress = mmap64(
  96         0,                    /* Let OS decide location */
  97         len,                  /* Number of bytes to map */
  98         protections,          /* File permissions */
  99         flags,                /* Changes are shared */
 100         fd,                   /* File descriptor of mapped file */
 101         off);                 /* Offset into file */
 102 
 103     if (mapAddress == MAP_FAILED) {
 104         if (errno == ENOMEM) {
 105             JNU_ThrowOutOfMemoryError(env, "Map failed");
 106             return IOS_THROWN;
 107         }
 108         return handle(env, -1, "Map failed");
 109     }
 110 
 111     return ((jlong) (unsigned long) mapAddress);
 112 }
 113 
 114 
 115 JNIEXPORT jint JNICALL
 116 Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
 117                                        jlong address, jlong len)
 118 {
 119     void *a = (void *)jlong_to_ptr(address);
 120     return handle(env,
 121                   munmap(a, (size_t)len),
 122                   "Unmap failed");
 123 }
 124 
 125 JNIEXPORT jlong JNICALL
 126 Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
 127                                             jobject srcFDO,
 128                                             jlong position, jlong count,
 129                                             jobject dstFDO)
 130 {
 131     jint srcFD = fdval(env, srcFDO);
 132     jint dstFD = fdval(env, dstFDO);
 133 
 134 #if defined(__linux__)
 135     off64_t offset = (off64_t)position;
 136     jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
 137     if (n < 0) {
 138         if (errno == EAGAIN)
 139             return IOS_UNAVAILABLE;
 140         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 141             return IOS_UNSUPPORTED_CASE;
 142         if (errno == EINTR) {
 143             return IOS_INTERRUPTED;
 144         }
 145         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 146         return IOS_THROWN;
 147     }
 148     return n;
 149 #elif defined (__solaris__)
 150     sendfilevec64_t sfv;
 151     size_t numBytes = 0;
 152     jlong result;
 153 
 154     sfv.sfv_fd = srcFD;
 155     sfv.sfv_flag = 0;
 156     sfv.sfv_off = (off64_t)position;
 157     sfv.sfv_len = count;
 158 
 159     result = sendfilev64(dstFD, &sfv, 1, &numBytes);
 160 
 161     /* Solaris sendfilev() will return -1 even if some bytes have been
 162      * transferred, so we check numBytes first.
 163      */
 164     if (numBytes > 0)
 165         return numBytes;
 166     if (result < 0) {
 167         if (errno == EAGAIN)
 168             return IOS_UNAVAILABLE;
 169         if (errno == EOPNOTSUPP)
 170             return IOS_UNSUPPORTED_CASE;
 171         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 172             return IOS_UNSUPPORTED_CASE;
 173         if (errno == EINTR)
 174             return IOS_INTERRUPTED;
 175         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 176         return IOS_THROWN;
 177     }
 178     return result;
 179 #elif defined(__APPLE__)
 180     off_t numBytes;
 181     int result;
 182 
 183     numBytes = count;
 184 
 185     result = sendfile(srcFD, dstFD, position, &numBytes, NULL, 0);
 186 
 187     if (numBytes > 0)
 188         return numBytes;
 189 
 190     if (result == -1) {
 191         if (errno == EAGAIN)
 192             return IOS_UNAVAILABLE;
 193         if (errno == EOPNOTSUPP || errno == ENOTSOCK || errno == ENOTCONN)
 194             return IOS_UNSUPPORTED_CASE;
 195         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 196             return IOS_UNSUPPORTED_CASE;
 197         if (errno == EINTR)
 198             return IOS_INTERRUPTED;
 199         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 200         return IOS_THROWN;
 201     }
 202 
 203     return result;
 204 
 205 #elif defined(_AIX)
 206     jlong max = (jlong)java_lang_Integer_MAX_VALUE;
 207     struct sf_parms sf_iobuf;
 208     jlong result;
 209 
 210     if (position > max)
 211         return IOS_UNSUPPORTED_CASE;
 212 
 213     if (count > max)
 214         count = max;
 215 
 216     memset(&sf_iobuf, 0, sizeof(sf_iobuf));
 217     sf_iobuf.file_descriptor = srcFD;
 218     sf_iobuf.file_offset = (off_t)position;
 219     sf_iobuf.file_bytes = count;
 220 
 221     result = send_file(&dstFD, &sf_iobuf, SF_SYNC_CACHE);
 222 
 223     /* AIX send_file() will return 0 when this operation complete successfully,
 224      * return 1 when partial bytes transfered and return -1 when an error has
 225      * Occured.
 226      */
 227     if (result == -1) {
 228         if (errno == EWOULDBLOCK)
 229             return IOS_UNAVAILABLE;
 230         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 231             return IOS_UNSUPPORTED_CASE;
 232         if (errno == EINTR)
 233             return IOS_INTERRUPTED;
 234         if (errno == ENOTSOCK)
 235             return IOS_UNSUPPORTED;
 236         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 237         return IOS_THROWN;
 238     }
 239 
 240     if (sf_iobuf.bytes_sent > 0)
 241         return (jlong)sf_iobuf.bytes_sent;
 242 
 243     return IOS_UNSUPPORTED_CASE;
 244 #else
 245     return IOS_UNSUPPORTED_CASE;
 246 #endif
 247 }
 248