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