1 /*
   2  * Copyright (c) 2001, 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 #include "jni.h"
  26 #include "jni_util.h"
  27 #include "jvm.h"
  28 #include "io_util.h"
  29 #include "io_util_md.h"
  30 #include <string.h>
  31 #include <unistd.h>
  32 
  33 #ifdef __solaris__
  34 #include <sys/filio.h>
  35 #endif
  36 
  37 #if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
  38 #include <sys/ioctl.h>
  39 #endif
  40 
  41 #ifdef MACOSX
  42 
  43 #include <CoreFoundation/CoreFoundation.h>
  44 
  45 __private_extern__
  46 jstring newStringPlatform(JNIEnv *env, const char* str)
  47 {
  48     jstring rv = NULL;
  49     CFMutableStringRef csref = CFStringCreateMutable(NULL, 0);
  50     if (csref == NULL) {
  51         JNU_ThrowOutOfMemoryError(env, "native heap");
  52     } else {
  53         CFStringAppendCString(csref, str, kCFStringEncodingUTF8);
  54         CFStringNormalize(csref, kCFStringNormalizationFormC);
  55         int clen = CFStringGetLength(csref);
  56         int ulen = (clen + 1) * 2;        // utf16 + zero padding
  57         char* chars = malloc(ulen);
  58         if (chars == NULL) {
  59             CFRelease(csref);
  60             JNU_ThrowOutOfMemoryError(env, "native heap");
  61         } else {
  62             if (CFStringGetCString(csref, chars, ulen, kCFStringEncodingUTF16)) {
  63                 rv = (*env)->NewString(env, (jchar*)chars, clen);
  64             }
  65             free(chars);
  66             CFRelease(csref);
  67         }
  68     }
  69     return rv;
  70 }
  71 #endif
  72 
  73 FD
  74 handleOpen(const char *path, int oflag, int mode) {
  75     FD fd;
  76     RESTARTABLE(open64(path, oflag, mode), fd);
  77     if (fd != -1) {
  78         struct stat64 buf64;
  79         int result;
  80         RESTARTABLE(fstat64(fd, &buf64), result);
  81         if (result != -1) {
  82             if (S_ISDIR(buf64.st_mode)) {
  83                 close(fd);
  84                 errno = EISDIR;
  85                 fd = -1;
  86             }
  87         } else {
  88             close(fd);
  89             fd = -1;
  90         }
  91     }
  92     return fd;
  93 }
  94 
  95 void
  96 fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
  97 {
  98     WITH_PLATFORM_STRING(env, path, ps) {
  99         FD fd;
 100 
 101 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
 102         /* Remove trailing slashes, since the kernel won't */
 103         char *p = (char *)ps + strlen(ps) - 1;
 104         while ((p > ps) && (*p == '/'))
 105             *p-- = '\0';
 106 #endif
 107         fd = handleOpen(ps, flags, 0666);
 108         if (fd != -1) {
 109             jobject fdobj;
 110             jboolean append;
 111             SET_FD(this, fd, fid);
 112 
 113             fdobj = (*env)->GetObjectField(env, this, fid);
 114             if (fdobj != NULL) {
 115                 append = (flags & O_APPEND) == 0 ? JNI_FALSE : JNI_TRUE;
 116                 (*env)->SetBooleanField(env, fdobj, IO_append_fdID, append);
 117             }
 118         } else {
 119             throwFileNotFoundException(env, path);
 120         }
 121     } END_PLATFORM_STRING(env, ps);
 122 }
 123 
 124 void
 125 fileClose(JNIEnv *env, jobject this, jfieldID fid)
 126 {
 127     FD fd = GET_FD(this, fid);
 128     if (fd == -1) {
 129         return;
 130     }
 131 
 132     /* Set the fd to -1 before closing it so that the timing window
 133      * of other threads using the wrong fd (closed but recycled fd,
 134      * that gets re-opened with some other filename) is reduced.
 135      * Practically the chance of its occurance is low, however, we are
 136      * taking extra precaution over here.
 137      */
 138     SET_FD(this, -1, fid);
 139 
 140     /*
 141      * Don't close file descriptors 0, 1, or 2. If we close these stream
 142      * then a subsequent file open or socket will use them. Instead we
 143      * just redirect these file descriptors to /dev/null.
 144      */
 145     if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
 146         int devnull = open("/dev/null", O_WRONLY);
 147         if (devnull < 0) {
 148             SET_FD(this, fd, fid); // restore fd
 149             JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
 150         } else {
 151             dup2(devnull, fd);
 152             close(devnull);
 153         }
 154     } else if (close(fd) == -1) {
 155         JNU_ThrowIOExceptionWithLastError(env, "close failed");
 156     }
 157 }
 158 
 159 ssize_t
 160 handleRead(FD fd, void *buf, jint len)
 161 {
 162     ssize_t result;
 163     RESTARTABLE(read(fd, buf, len), result);
 164     return result;
 165 }
 166 
 167 ssize_t
 168 handleWrite(FD fd, const void *buf, jint len)
 169 {
 170     ssize_t result;
 171     RESTARTABLE(write(fd, buf, len), result);
 172     return result;
 173 }
 174 
 175 jint
 176 handleAvailable(FD fd, jlong *pbytes)
 177 {
 178     int mode;
 179     struct stat64 buf64;
 180     jlong size = -1, current = -1;
 181 
 182     int result;
 183     RESTARTABLE(fstat64(fd, &buf64), result);
 184     if (result != -1) {
 185         mode = buf64.st_mode;
 186         if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
 187             int n;
 188             int result;
 189             RESTARTABLE(ioctl(fd, FIONREAD, &n), result);
 190             if (result >= 0) {
 191                 *pbytes = n;
 192                 return 1;
 193             }
 194         } else if (S_ISREG(mode)) {
 195             size = buf64.st_size;
 196         }
 197     }
 198 
 199     if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) {
 200         return 0;
 201     }
 202 
 203     if (size < current) {
 204         if ((size = lseek64(fd, 0, SEEK_END)) == -1)
 205             return 0;
 206         else if (lseek64(fd, current, SEEK_SET) == -1)
 207             return 0;
 208     }
 209 
 210     *pbytes = size - current;
 211     return 1;
 212 }
 213 
 214 jint
 215 handleSetLength(FD fd, jlong length)
 216 {
 217     int result;
 218 #if defined(__linux__)
 219     /*
 220      * On Linux, if the file size is being increased, then ftruncate64()
 221      * will modify the metadata value of the size without actually allocating
 222      * any blocks which can cause a SIGBUS error if the file is subsequently
 223      * memory-mapped.
 224      */
 225     struct stat64 sb;
 226 
 227     if (fstat64(fd, &sb) == 0 && length > sb.st_blocks*512) {
 228         RESTARTABLE(fallocate64(fd, 0, 0, length), result);
 229         return result;
 230     }
 231 #endif
 232     RESTARTABLE(ftruncate64(fd, length), result);
 233     return result;
 234 }
 235 
 236 jlong
 237 handleGetLength(FD fd)
 238 {
 239     struct stat64 sb;
 240     if (fstat64(fd, &sb) == 0) {
 241         return sb.st_size;
 242     } else {
 243         return -1;
 244     }
 245 }