1 /*
2 * Copyright (c) 2001, 2010, 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__
newStringPlatform(JNIEnv * env,const char * str)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 FD
handleOpen(const char * path,int oflag,int mode)66 handleOpen(const char *path, int oflag, int mode) {
67 FD fd;
68 RESTARTABLE(open64(path, oflag, mode), fd);
69 if (fd != -1) {
70 struct stat64 buf64;
71 int result;
72 RESTARTABLE(fstat64(fd, &buf64), result);
73 if (result != -1) {
74 if (S_ISDIR(buf64.st_mode)) {
75 close(fd);
76 errno = EISDIR;
77 fd = -1;
78 }
79 } else {
80 close(fd);
81 fd = -1;
82 }
83 }
84 return fd;
85 }
86
87
88 void
fileOpen(JNIEnv * env,jobject this,jstring path,jfieldID fid,int flags)89 fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
90 {
91 WITH_PLATFORM_STRING(env, path, ps) {
92 FD fd;
93
94 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
95 /* Remove trailing slashes, since the kernel won't */
96 char *p = (char *)ps + strlen(ps) - 1;
97 while ((p > ps) && (*p == '/'))
98 *p-- = '\0';
99 #endif
100 fd = handleOpen(ps, flags, 0666);
101 if (fd != -1) {
102 SET_FD(this, fd, fid);
103 } else {
104 throwFileNotFoundException(env, path);
105 }
106 } END_PLATFORM_STRING(env, ps);
107 }
108
109
110 void
fileClose(JNIEnv * env,jobject this,jfieldID fid)111 fileClose(JNIEnv *env, jobject this, jfieldID fid)
112 {
113 FD fd = GET_FD(this, fid);
114 if (fd == -1) {
115 return;
116 }
117
118 /* Set the fd to -1 before closing it so that the timing window
119 * of other threads using the wrong fd (closed but recycled fd,
120 * that gets re-opened with some other filename) is reduced.
121 * Practically the chance of its occurance is low, however, we are
122 * taking extra precaution over here.
123 */
124 SET_FD(this, -1, fid);
125
126 /*
127 * Don't close file descriptors 0, 1, or 2. If we close these stream
128 * then a subsequent file open or socket will use them. Instead we
129 * just redirect these file descriptors to /dev/null.
130 */
131 if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
132 int devnull = open("/dev/null", O_WRONLY);
133 if (devnull < 0) {
134 SET_FD(this, fd, fid); // restore fd
135 JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
136 } else {
137 dup2(devnull, fd);
138 close(devnull);
139 }
140 } else if (JVM_Close(fd) == -1) {
141 JNU_ThrowIOExceptionWithLastError(env, "close failed");
142 }
143 }
144