1 /*
   2  * Copyright (c) 2001, 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 #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     jobject fileDescriptor = (*env)->GetObjectField(env, (this), (fid));
 128     if (fileDescriptor == NULL) {
 129         return;
 130     }
 131     fileDescriptorClose(env, fileDescriptor);
 132 }
 133 
 134 // Function to close the fd held by this FileDescriptor and set fd to -1.
 135 void
 136 fileDescriptorClose(JNIEnv *env, jobject this)
 137 {
 138     FD fd = (*env)->GetIntField(env, this, IO_fd_fdID);
 139     if ((*env)->ExceptionOccurred(env)) {
 140         return;
 141     }
 142 
 143     if (fd == -1) {
 144         return;     // already closed and set to -1
 145     }
 146 
 147     /* Set the fd to -1 before closing it so that the timing window
 148      * of other threads using the wrong fd (closed but recycled fd,
 149      * that gets re-opened with some other filename) is reduced.
 150      * Practically the chance of its occurance is low, however, we are
 151      * taking extra precaution over here.
 152      */
 153     (*env)->SetIntField(env, this, IO_fd_fdID, -1);
 154     if ((*env)->ExceptionOccurred(env)) {
 155         return;
 156     }
 157     /*
 158      * Don't close file descriptors 0, 1, or 2. If we close these stream
 159      * then a subsequent file open or socket will use them. Instead we
 160      * just redirect these file descriptors to /dev/null.
 161      */
 162     if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
 163         int devnull = open("/dev/null", O_WRONLY);
 164         if (devnull < 0) {
 165             (*env)->SetIntField(env, this, IO_fd_fdID, fd);
 166             JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
 167         } else {
 168             dup2(devnull, fd);
 169             close(devnull);
 170         }
 171     } else if (close(fd) == -1) {
 172         JNU_ThrowIOExceptionWithLastError(env, "close failed");
 173     }
 174 }
 175 
 176 ssize_t
 177 handleRead(FD fd, void *buf, jint len)
 178 {
 179     ssize_t result;
 180     RESTARTABLE(read(fd, buf, len), result);
 181     return result;
 182 }
 183 
 184 ssize_t
 185 handleWrite(FD fd, const void *buf, jint len)
 186 {
 187     ssize_t result;
 188     RESTARTABLE(write(fd, buf, len), result);
 189     return result;
 190 }
 191 
 192 jint
 193 handleAvailable(FD fd, jlong *pbytes)
 194 {
 195     int mode;
 196     struct stat64 buf64;
 197     jlong size = -1, current = -1;
 198 
 199     int result;
 200     RESTARTABLE(fstat64(fd, &buf64), result);
 201     if (result != -1) {
 202         mode = buf64.st_mode;
 203         if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
 204             int n;
 205             int result;
 206             RESTARTABLE(ioctl(fd, FIONREAD, &n), result);
 207             if (result >= 0) {
 208                 *pbytes = n;
 209                 return 1;
 210             }
 211         } else if (S_ISREG(mode)) {
 212             size = buf64.st_size;
 213         }
 214     }
 215 
 216     if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) {
 217         return 0;
 218     }
 219 
 220     if (size < current) {
 221         if ((size = lseek64(fd, 0, SEEK_END)) == -1)
 222             return 0;
 223         else if (lseek64(fd, current, SEEK_SET) == -1)
 224             return 0;
 225     }
 226 
 227     *pbytes = size - current;
 228     return 1;
 229 }
 230 
 231 jint
 232 handleSetLength(FD fd, jlong length)
 233 {
 234     int result;
 235     RESTARTABLE(ftruncate64(fd, length), result);
 236     return result;
 237 }
 238 
 239 jlong
 240 handleGetLength(FD fd)
 241 {
 242     struct stat64 sb;
 243     if (fstat64(fd, &sb) == 0) {
 244         return sb.st_size;
 245     } else {
 246         return -1;
 247     }
 248 }