1 /* 2 * Copyright (c) 2001, 2020, 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 FD getFD(JNIEnv *env, jobject obj, jfieldID fid) { 96 jobject fdo = (*env)->GetObjectField(env, obj, fid); 97 if (fdo == NULL) { 98 return -1; 99 } 100 return (*env)->GetIntField(env, fdo, IO_fd_fdID); 101 } 102 103 void 104 fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) 105 { 106 WITH_PLATFORM_STRING(env, path, ps) { 107 FD fd; 108 109 #if defined(__linux__) || defined(_ALLBSD_SOURCE) 110 /* Remove trailing slashes, since the kernel won't */ 111 char *p = (char *)ps + strlen(ps) - 1; 112 while ((p > ps) && (*p == '/')) 113 *p-- = '\0'; 114 #endif 115 fd = handleOpen(ps, flags, 0666); 116 if (fd != -1) { 117 jobject fdobj; 118 jboolean append; 119 fdobj = (*env)->GetObjectField(env, this, fid); 120 if (fdobj != NULL) { 121 // Set FD 122 (*env)->SetIntField(env, fdobj, IO_fd_fdID, fd); 123 append = (flags & O_APPEND) == 0 ? JNI_FALSE : JNI_TRUE; 124 (*env)->SetBooleanField(env, fdobj, IO_append_fdID, append); 125 } 126 } else { 127 throwFileNotFoundException(env, path); 128 } 129 } END_PLATFORM_STRING(env, ps); 130 } 131 132 // Function to close the fd held by this FileDescriptor and set fd to -1. 133 void 134 fileDescriptorClose(JNIEnv *env, jobject this) 135 { 136 FD fd = (*env)->GetIntField(env, this, IO_fd_fdID); 137 if ((*env)->ExceptionOccurred(env)) { 138 return; 139 } 140 141 if (fd == -1) { 142 return; // already closed and set to -1 143 } 144 145 /* Set the fd to -1 before closing it so that the timing window 146 * of other threads using the wrong fd (closed but recycled fd, 147 * that gets re-opened with some other filename) is reduced. 148 * Practically the chance of its occurance is low, however, we are 149 * taking extra precaution over here. 150 */ 151 (*env)->SetIntField(env, this, IO_fd_fdID, -1); 152 if ((*env)->ExceptionOccurred(env)) { 153 return; 154 } 155 /* 156 * Don't close file descriptors 0, 1, or 2. If we close these stream 157 * then a subsequent file open or socket will use them. Instead we 158 * just redirect these file descriptors to /dev/null. 159 */ 160 if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) { 161 int devnull = open("/dev/null", O_WRONLY); 162 if (devnull < 0) { 163 (*env)->SetIntField(env, this, IO_fd_fdID, fd); 164 JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed"); 165 } else { 166 dup2(devnull, fd); 167 close(devnull); 168 } 169 } else if (close(fd) == -1) { 170 JNU_ThrowIOExceptionWithLastError(env, "close failed"); 171 } 172 } 173 174 ssize_t 175 handleRead(FD fd, void *buf, jint len) 176 { 177 ssize_t result; 178 RESTARTABLE(read(fd, buf, len), result); 179 return result; 180 } 181 182 ssize_t 183 handleWrite(FD fd, const void *buf, jint len) 184 { 185 ssize_t result; 186 RESTARTABLE(write(fd, buf, len), result); 187 return result; 188 } 189 190 jint 191 handleAvailable(FD fd, jlong *pbytes) 192 { 193 int mode; 194 struct stat64 buf64; 195 jlong size = -1, current = -1; 196 197 int result; 198 RESTARTABLE(fstat64(fd, &buf64), result); 199 if (result != -1) { 200 mode = buf64.st_mode; 201 if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) { 202 int n; 203 int result; 204 RESTARTABLE(ioctl(fd, FIONREAD, &n), result); 205 if (result >= 0) { 206 *pbytes = n; 207 return 1; 208 } 209 } else if (S_ISREG(mode)) { 210 size = buf64.st_size; 211 } 212 } 213 214 if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) { 215 return 0; 216 } 217 218 if (size < current) { 219 if ((size = lseek64(fd, 0, SEEK_END)) == -1) 220 return 0; 221 else if (lseek64(fd, current, SEEK_SET) == -1) 222 return 0; 223 } 224 225 *pbytes = size - current; 226 return 1; 227 } 228 229 jint 230 handleSetLength(FD fd, jlong length) 231 { 232 int result; 233 RESTARTABLE(ftruncate64(fd, length), result); 234 return result; 235 } 236 237 jlong 238 handleGetLength(FD fd) 239 { 240 struct stat64 sb; 241 if (fstat64(fd, &sb) == 0) { 242 return sb.st_size; 243 } else { 244 return -1; 245 } 246 }