1 /*
2 * Copyright (c) 2000, 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 "jlong.h"
30 #include "sun_nio_ch_FileDispatcherImpl.h"
31 #include "java_lang_Long.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <fcntl.h>
35 #include <sys/uio.h>
36 #include <unistd.h>
37 #if defined(__linux__)
38 #include <linux/fs.h>
39 #include <sys/ioctl.h>
40 #endif
41 #include "nio.h"
42 #include "nio_util.h"
43 #include <nativehelper/JNIHelp.h>
44
45 #define NATIVE_METHOD(className, functionName, signature) \
46 { #functionName, signature, (void*)(className ## _ ## functionName) }
47
48 // Android-changed: Fuchsia: Alias *64 on Fuchsia builds. http://b/119496969
49 // #ifdef _ALLBSD_SOURCE
50 #if defined(_ALLBSD_SOURCE) || defined(__Fuchsia__)
51 #define stat64 stat
52 #define flock64 flock
53 #define off64_t off_t
54 #define F_SETLKW64 F_SETLKW
55 #define F_SETLK64 F_SETLK
56
57 #define pread64 pread
58 #define pwrite64 pwrite
59 #define ftruncate64 ftruncate
60 #define fstat64 fstat
61
62 #define fdatasync fsync
63 #endif
64
65 JNIEXPORT jint JNICALL
FileDispatcherImpl_read0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)66 FileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
67 jobject fdo, jlong address, jint len)
68 {
69 jint fd = fdval(env, fdo);
70 void *buf = (void *)jlong_to_ptr(address);
71
72 return convertReturnVal(env, read(fd, buf, len), JNI_TRUE);
73 }
74
75 JNIEXPORT jint JNICALL
FileDispatcherImpl_pread0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len,jlong offset)76 FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
77 jlong address, jint len, jlong offset)
78 {
79 jint fd = fdval(env, fdo);
80 void *buf = (void *)jlong_to_ptr(address);
81
82 return convertReturnVal(env, pread64(fd, buf, len, offset), JNI_TRUE);
83 }
84
85 JNIEXPORT jlong JNICALL
FileDispatcherImpl_readv0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)86 FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz,
87 jobject fdo, jlong address, jint len)
88 {
89 jint fd = fdval(env, fdo);
90 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
91 return convertLongReturnVal(env, readv(fd, iov, len), JNI_TRUE);
92 }
93
94 JNIEXPORT jint JNICALL
FileDispatcherImpl_write0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)95 FileDispatcherImpl_write0(JNIEnv *env, jclass clazz,
96 jobject fdo, jlong address, jint len)
97 {
98 jint fd = fdval(env, fdo);
99 void *buf = (void *)jlong_to_ptr(address);
100
101 return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
102 }
103
104 JNIEXPORT jint JNICALL
FileDispatcherImpl_pwrite0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len,jlong offset)105 FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
106 jlong address, jint len, jlong offset)
107 {
108 jint fd = fdval(env, fdo);
109 void *buf = (void *)jlong_to_ptr(address);
110
111 return convertReturnVal(env, pwrite64(fd, buf, len, offset), JNI_FALSE);
112 }
113
114 JNIEXPORT jlong JNICALL
FileDispatcherImpl_writev0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)115 FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz,
116 jobject fdo, jlong address, jint len)
117 {
118 jint fd = fdval(env, fdo);
119 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
120 return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
121 }
122
123 static jlong
handle(JNIEnv * env,jlong rv,char * msg)124 handle(JNIEnv *env, jlong rv, char *msg)
125 {
126 if (rv >= 0)
127 return rv;
128 if (errno == EINTR)
129 return IOS_INTERRUPTED;
130 JNU_ThrowIOExceptionWithLastError(env, msg);
131 return IOS_THROWN;
132 }
133
134 JNIEXPORT jint JNICALL
FileDispatcherImpl_force0(JNIEnv * env,jobject this,jobject fdo,jboolean md)135 FileDispatcherImpl_force0(JNIEnv *env, jobject this,
136 jobject fdo, jboolean md)
137 {
138 jint fd = fdval(env, fdo);
139 int result = 0;
140
141 if (md == JNI_FALSE) {
142 result = fdatasync(fd);
143 } else {
144 #ifdef _AIX
145 /* On AIX, calling fsync on a file descriptor that is opened only for
146 * reading results in an error ("EBADF: The FileDescriptor parameter is
147 * not a valid file descriptor open for writing.").
148 * However, at this point it is not possibly anymore to read the
149 * 'writable' attribute of the corresponding file channel so we have to
150 * use 'fcntl'.
151 */
152 int getfl = fcntl(fd, F_GETFL);
153 if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
154 return 0;
155 }
156 #endif
157 result = fsync(fd);
158 }
159 return handle(env, result, "Force failed");
160 }
161
162 JNIEXPORT jint JNICALL
FileDispatcherImpl_truncate0(JNIEnv * env,jobject this,jobject fdo,jlong size)163 FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
164 jobject fdo, jlong size)
165 {
166 return handle(env,
167 ftruncate64(fdval(env, fdo), size),
168 "Truncation failed");
169 }
170
171 JNIEXPORT jlong JNICALL
FileDispatcherImpl_size0(JNIEnv * env,jobject this,jobject fdo)172 FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
173 {
174 jint fd = fdval(env, fdo);
175 struct stat64 fbuf;
176
177 if (fstat64(fd, &fbuf) < 0)
178 return handle(env, -1, "Size failed");
179
180 #ifdef BLKGETSIZE64
181 if (S_ISBLK(fbuf.st_mode)) {
182 uint64_t size;
183 if (ioctl(fd, BLKGETSIZE64, &size) < 0)
184 return handle(env, -1, "Size failed");
185 return (jlong)size;
186 }
187 #endif
188
189 return fbuf.st_size;
190 }
191
192 JNIEXPORT jint JNICALL
FileDispatcherImpl_lock0(JNIEnv * env,jobject this,jobject fdo,jboolean block,jlong pos,jlong size,jboolean shared)193 FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
194 jboolean block, jlong pos, jlong size,
195 jboolean shared)
196 {
197 jint fd = fdval(env, fdo);
198 jint lockResult = 0;
199 int cmd = 0;
200 struct flock64 fl;
201
202 fl.l_whence = SEEK_SET;
203 if (size == (jlong)java_lang_Long_MAX_VALUE) {
204 fl.l_len = (off64_t)0;
205 } else {
206 fl.l_len = (off64_t)size;
207 }
208 fl.l_start = (off64_t)pos;
209 if (shared == JNI_TRUE) {
210 fl.l_type = F_RDLCK;
211 } else {
212 fl.l_type = F_WRLCK;
213 }
214 if (block == JNI_TRUE) {
215 cmd = F_SETLKW64;
216 } else {
217 cmd = F_SETLK64;
218 }
219 lockResult = fcntl(fd, cmd, &fl);
220 if (lockResult < 0) {
221 if ((cmd == F_SETLK64) && (errno == EAGAIN || errno == EACCES))
222 return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
223 if (errno == EINTR)
224 return sun_nio_ch_FileDispatcherImpl_INTERRUPTED;
225 JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
226 }
227 return 0;
228 }
229
230 JNIEXPORT void JNICALL
FileDispatcherImpl_release0(JNIEnv * env,jobject this,jobject fdo,jlong pos,jlong size)231 FileDispatcherImpl_release0(JNIEnv *env, jobject this,
232 jobject fdo, jlong pos, jlong size)
233 {
234 jint fd = fdval(env, fdo);
235 jint lockResult = 0;
236 struct flock64 fl;
237 int cmd = F_SETLK64;
238
239 fl.l_whence = SEEK_SET;
240 if (size == (jlong)java_lang_Long_MAX_VALUE) {
241 fl.l_len = (off64_t)0;
242 } else {
243 fl.l_len = (off64_t)size;
244 }
245 fl.l_start = (off64_t)pos;
246 fl.l_type = F_UNLCK;
247 lockResult = fcntl(fd, cmd, &fl);
248 if (lockResult < 0) {
249 JNU_ThrowIOExceptionWithLastError(env, "Release failed");
250 }
251 }
252
253
closeFileDescriptor(JNIEnv * env,int fd)254 static void closeFileDescriptor(JNIEnv *env, int fd) {
255 if (fd != -1) {
256 int result = close(fd);
257 if (result < 0)
258 JNU_ThrowIOExceptionWithLastError(env, "Close failed");
259 }
260 }
261
262 JNIEXPORT void JNICALL
FileDispatcherImpl_close0(JNIEnv * env,jclass clazz,jobject fdo)263 FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
264 {
265 jint fd = fdval(env, fdo);
266 closeFileDescriptor(env, fd);
267 }
268
269 JNIEXPORT void JNICALL
FileDispatcherImpl_preClose0(JNIEnv * env,jclass clazz,jobject fdo)270 FileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
271 {
272 jint fd = fdval(env, fdo);
273 int preCloseFD = open("/dev/null", O_RDWR | O_CLOEXEC);
274 if (preCloseFD < 0) {
275 JNU_ThrowIOExceptionWithLastError(env, "open(\"/dev/null\") failed");
276 return;
277 }
278 if (dup2(preCloseFD, fd) < 0) {
279 JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
280 }
281 close(preCloseFD);
282 }
283
284 JNIEXPORT void JNICALL
FileDispatcherImpl_closeIntFD(JNIEnv * env,jclass clazz,jint fd)285 FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
286 {
287 closeFileDescriptor(env, fd);
288 }
289
290 static JNINativeMethod gMethods[] = {
291 NATIVE_METHOD(FileDispatcherImpl, closeIntFD, "(I)V"),
292 NATIVE_METHOD(FileDispatcherImpl, preClose0, "(Ljava/io/FileDescriptor;)V"),
293 NATIVE_METHOD(FileDispatcherImpl, close0, "(Ljava/io/FileDescriptor;)V"),
294 NATIVE_METHOD(FileDispatcherImpl, release0, "(Ljava/io/FileDescriptor;JJ)V"),
295 NATIVE_METHOD(FileDispatcherImpl, lock0, "(Ljava/io/FileDescriptor;ZJJZ)I"),
296 NATIVE_METHOD(FileDispatcherImpl, size0, "(Ljava/io/FileDescriptor;)J"),
297 NATIVE_METHOD(FileDispatcherImpl, truncate0, "(Ljava/io/FileDescriptor;J)I"),
298 NATIVE_METHOD(FileDispatcherImpl, force0, "(Ljava/io/FileDescriptor;Z)I"),
299 NATIVE_METHOD(FileDispatcherImpl, writev0, "(Ljava/io/FileDescriptor;JI)J"),
300 NATIVE_METHOD(FileDispatcherImpl, pwrite0, "(Ljava/io/FileDescriptor;JIJ)I"),
301 NATIVE_METHOD(FileDispatcherImpl, write0, "(Ljava/io/FileDescriptor;JI)I"),
302 NATIVE_METHOD(FileDispatcherImpl, readv0, "(Ljava/io/FileDescriptor;JI)J"),
303 NATIVE_METHOD(FileDispatcherImpl, pread0, "(Ljava/io/FileDescriptor;JIJ)I"),
304 NATIVE_METHOD(FileDispatcherImpl, read0, "(Ljava/io/FileDescriptor;JI)I"),
305 };
306
register_sun_nio_ch_FileDispatcherImpl(JNIEnv * env)307 void register_sun_nio_ch_FileDispatcherImpl(JNIEnv* env) {
308 jniRegisterNativeMethods(env, "sun/nio/ch/FileDispatcherImpl", gMethods, NELEM(gMethods));
309 }
310