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