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