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
  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 <sys/socket.h>
  36 #elif defined(_ALLBSD_SOURCE)
  37 #include <sys/socket.h>
  38 #include <sys/uio.h>
  39 #define lseek64 lseek
  40 #define mmap64 mmap
  41 #endif
  42 
  43 #include "jni.h"
  44 #include "jni_util.h"
  45 #include "jlong.h"
  46 #include "nio.h"
  47 #include "nio_util.h"
  48 #include "sun_nio_ch_FileChannelImpl.h"
  49 #include "java_lang_Integer.h"
  50 #include <assert.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, jboolean map_sync)
  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     // should never be called with map_sync and prot == PRIVATE
  85     assert((prot != sun_nio_ch_FileChannelImpl_MAP_PV) ||! map_sync);
  86     
  87     if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
  88         protections = PROT_READ;
  89         flags = MAP_SHARED;
  90     } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
  91         protections = PROT_WRITE | PROT_READ;
  92         flags = MAP_SHARED;
  93     } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
  94         protections =  PROT_WRITE | PROT_READ;
  95         flags = MAP_PRIVATE;
  96     }
  97 
  98     // if MAP_SYNC and MAP_SHARED_VALIDATE are not defined then it is
  99     // best to define them here. This ensures the code compiles on old
 100     // OS releases which do not provide the relevant headers. If run
 101     // on the same machine then it will work if the kernel contains
 102     // the necessary support otherwise mmap should fail with an
 103     // invalid argument error
 104 
 105 #ifndef MAP_SYNC
 106 #define MAP_SYNC 0x80000
 107 #endif
 108 #ifndef MAP_SHARED_VALIDATE
 109 #define MAP_SHARED_VALIDATE 0x03
 110 #endif
 111     
 112     if (map_sync) {
 113         // ensure
 114         //  1) this is Linux on AArch64 or x86_64
 115         //  2) the mmap APIs are available/ at compile time
 116 #if !defined(LINUX) || ! (defined(aarch64) || (defined(amd64) && defined(_LP64)))
 117         // TODO - implement for solaris/AIX/BSD/WINDOWS and for 32 bit
 118         JNU_ThrowIOException(env, "map with persistent mode is not implemented");
 119         return IOS_THROWN;
 120 #else
 121         flags |= MAP_SYNC | MAP_SHARED_VALIDATE;
 122 #endif
 123     }
 124 
 125     mapAddress = mmap64(
 126         0,                    /* Let OS decide location */
 127         len,                  /* Number of bytes to map */
 128         protections,          /* File permissions */
 129         flags,                /* Changes are shared */
 130         fd,                   /* File descriptor of mapped file */
 131         off);                 /* Offset into file */
 132 
 133     if (mapAddress == MAP_FAILED) {
 134         if (map_sync && errno == ENOTSUP) {
 135             JNU_ThrowIOExceptionWithLastError(env, "map with persistent mode is not supported");
 136             return IOS_THROWN;
 137         }
 138 
 139         if (errno == ENOMEM) {
 140             JNU_ThrowOutOfMemoryError(env, "Map failed");
 141             return IOS_THROWN;
 142         }
 143         return handle(env, -1, "Map failed");
 144     }
 145 
 146     return ((jlong) (unsigned long) mapAddress);
 147 }
 148 
 149 
 150 JNIEXPORT jint JNICALL
 151 Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
 152                                        jlong address, jlong len)
 153 {
 154     void *a = (void *)jlong_to_ptr(address);
 155     return handle(env,
 156                   munmap(a, (size_t)len),
 157                   "Unmap failed");
 158 }
 159 
 160 JNIEXPORT jlong JNICALL
 161 Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
 162                                             jobject srcFDO,
 163                                             jlong position, jlong count,
 164                                             jobject dstFDO)
 165 {
 166     jint srcFD = fdval(env, srcFDO);
 167     jint dstFD = fdval(env, dstFDO);
 168 
 169 #if defined(__linux__)
 170     off64_t offset = (off64_t)position;
 171     jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
 172     if (n < 0) {
 173         if (errno == EAGAIN)
 174             return IOS_UNAVAILABLE;
 175         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 176             return IOS_UNSUPPORTED_CASE;
 177         if (errno == EINTR) {
 178             return IOS_INTERRUPTED;
 179         }
 180         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 181         return IOS_THROWN;
 182     }
 183     return n;
 184 #elif defined (__solaris__)
 185     sendfilevec64_t sfv;
 186     size_t numBytes = 0;
 187     jlong result;
 188 
 189     sfv.sfv_fd = srcFD;
 190     sfv.sfv_flag = 0;
 191     sfv.sfv_off = (off64_t)position;
 192     sfv.sfv_len = count;
 193 
 194     result = sendfilev64(dstFD, &sfv, 1, &numBytes);
 195 
 196     /* Solaris sendfilev() will return -1 even if some bytes have been
 197      * transferred, so we check numBytes first.
 198      */
 199     if (numBytes > 0)
 200         return numBytes;
 201     if (result < 0) {
 202         if (errno == EAGAIN)
 203             return IOS_UNAVAILABLE;
 204         if (errno == EOPNOTSUPP)
 205             return IOS_UNSUPPORTED_CASE;
 206         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 207             return IOS_UNSUPPORTED_CASE;
 208         if (errno == EINTR)
 209             return IOS_INTERRUPTED;
 210         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 211         return IOS_THROWN;
 212     }
 213     return result;
 214 #elif defined(__APPLE__)
 215     off_t numBytes;
 216     int result;
 217 
 218     numBytes = count;
 219 
 220     result = sendfile(srcFD, dstFD, position, &numBytes, NULL, 0);
 221 
 222     if (numBytes > 0)
 223         return numBytes;
 224 
 225     if (result == -1) {
 226         if (errno == EAGAIN)
 227             return IOS_UNAVAILABLE;
 228         if (errno == EOPNOTSUPP || errno == ENOTSOCK || errno == ENOTCONN)
 229             return IOS_UNSUPPORTED_CASE;
 230         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 231             return IOS_UNSUPPORTED_CASE;
 232         if (errno == EINTR)
 233             return IOS_INTERRUPTED;
 234         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 235         return IOS_THROWN;
 236     }
 237 
 238     return result;
 239 
 240 #elif defined(_AIX)
 241     jlong max = (jlong)java_lang_Integer_MAX_VALUE;
 242     struct sf_parms sf_iobuf;
 243     jlong result;
 244 
 245     if (position > max)
 246         return IOS_UNSUPPORTED_CASE;
 247 
 248     if (count > max)
 249         count = max;
 250 
 251     memset(&sf_iobuf, 0, sizeof(sf_iobuf));
 252     sf_iobuf.file_descriptor = srcFD;
 253     sf_iobuf.file_offset = (off_t)position;
 254     sf_iobuf.file_bytes = count;
 255 
 256     result = send_file(&dstFD, &sf_iobuf, SF_SYNC_CACHE);
 257 
 258     /* AIX send_file() will return 0 when this operation complete successfully,
 259      * return 1 when partial bytes transfered and return -1 when an error has
 260      * Occured.
 261      */
 262     if (result == -1) {
 263         if (errno == EWOULDBLOCK)
 264             return IOS_UNAVAILABLE;
 265         if ((errno == EINVAL) && ((ssize_t)count >= 0))
 266             return IOS_UNSUPPORTED_CASE;
 267         if (errno == EINTR)
 268             return IOS_INTERRUPTED;
 269         if (errno == ENOTSOCK)
 270             return IOS_UNSUPPORTED;
 271         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
 272         return IOS_THROWN;
 273     }
 274 
 275     if (sf_iobuf.bytes_sent > 0)
 276         return (jlong)sf_iobuf.bytes_sent;
 277 
 278     return IOS_UNSUPPORTED_CASE;
 279 #else
 280     return IOS_UNSUPPORTED_CASE;
 281 #endif
 282 }
 283