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