1 /*
2  * Copyright (c) 2000, 2019, 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 <sys/types.h>
27 #include <sys/socket.h>
28 #include <fcntl.h>
29 #include <sys/uio.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/statvfs.h>
33 
34 #if defined(__linux__)
35 #include <linux/fs.h>
36 #include <sys/ioctl.h>
37 #endif
38 
39 #if defined(_ALLBSD_SOURCE)
40 #define lseek64 lseek
41 #define stat64 stat
42 #define statvfs64 statvfs
43 #define flock64 flock
44 #define off64_t off_t
45 #define F_SETLKW64 F_SETLKW
46 #define F_SETLK64 F_SETLK
47 #define pread64 pread
48 #define pwrite64 pwrite
49 #define ftruncate64 ftruncate
50 #define fstat64 fstat
51 #define fstatvfs64 fstatvfs
52 #define fdatasync fsync
53 #endif
54 
55 #include "jni.h"
56 #include "jni_util.h"
57 #include "jvm.h"
58 #include "jlong.h"
59 #include "nio.h"
60 #include "nio_util.h"
61 #include "sun_nio_ch_FileDispatcherImpl.h"
62 #include "java_lang_Long.h"
63 
64 static int preCloseFD = -1;     /* File descriptor to which we dup other fd's
65                                    before closing them for real */
66 
67 
68 JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_init(JNIEnv * env,jclass cl)69 Java_sun_nio_ch_FileDispatcherImpl_init(JNIEnv *env, jclass cl)
70 {
71     int sp[2];
72     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
73         JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
74         return;
75     }
76     preCloseFD = sp[0];
77     close(sp[1]);
78 }
79 
80 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)81 Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
82                              jobject fdo, jlong address, jint len)
83 {
84     jint fd = fdval(env, fdo);
85     void *buf = (void *)jlong_to_ptr(address);
86 
87     return convertReturnVal(env, read(fd, buf, len), JNI_TRUE);
88 }
89 
90 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len,jlong offset)91 Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
92                             jlong address, jint len, jlong offset)
93 {
94     jint fd = fdval(env, fdo);
95     void *buf = (void *)jlong_to_ptr(address);
96 
97     return convertReturnVal(env, pread64(fd, buf, len, offset), JNI_TRUE);
98 }
99 
100 JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)101 Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz,
102                               jobject fdo, jlong address, jint len)
103 {
104     jint fd = fdval(env, fdo);
105     struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
106     return convertLongReturnVal(env, readv(fd, iov, len), JNI_TRUE);
107 }
108 
109 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)110 Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz,
111                               jobject fdo, jlong address, jint len)
112 {
113     jint fd = fdval(env, fdo);
114     void *buf = (void *)jlong_to_ptr(address);
115 
116     return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
117 }
118 
119 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len,jlong offset)120 Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
121                             jlong address, jint len, jlong offset)
122 {
123     jint fd = fdval(env, fdo);
124     void *buf = (void *)jlong_to_ptr(address);
125 
126     return convertReturnVal(env, pwrite64(fd, buf, len, offset), JNI_FALSE);
127 }
128 
129 JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)130 Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz,
131                                        jobject fdo, jlong address, jint len)
132 {
133     jint fd = fdval(env, fdo);
134     struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
135     return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
136 }
137 
138 static jlong
handle(JNIEnv * env,jlong rv,char * msg)139 handle(JNIEnv *env, jlong rv, char *msg)
140 {
141     if (rv >= 0)
142         return rv;
143     if (errno == EINTR)
144         return IOS_INTERRUPTED;
145     JNU_ThrowIOExceptionWithLastError(env, msg);
146     return IOS_THROWN;
147 }
148 
149 JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_seek0(JNIEnv * env,jclass clazz,jobject fdo,jlong offset)150 Java_sun_nio_ch_FileDispatcherImpl_seek0(JNIEnv *env, jclass clazz,
151                                          jobject fdo, jlong offset)
152 {
153     jint fd = fdval(env, fdo);
154     off64_t result;
155     if (offset < 0) {
156         result = lseek64(fd, 0, SEEK_CUR);
157     } else {
158         result = lseek64(fd, offset, SEEK_SET);
159     }
160     return handle(env, (jlong)result, "lseek64 failed");
161 }
162 
163 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv * env,jobject this,jobject fdo,jboolean md)164 Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this,
165                                           jobject fdo, jboolean md)
166 {
167     jint fd = fdval(env, fdo);
168     int result = 0;
169 
170 #ifdef MACOSX
171     result = fcntl(fd, F_FULLFSYNC);
172     if (result == -1 && errno == ENOTSUP) {
173         /* Try fsync() in case F_FULLSYUNC is not implemented on the file system. */
174         result = fsync(fd);
175     }
176 #else /* end MACOSX, begin not-MACOSX */
177     if (md == JNI_FALSE) {
178         result = fdatasync(fd);
179     } else {
180 #ifdef _AIX
181         /* On AIX, calling fsync on a file descriptor that is opened only for
182          * reading results in an error ("EBADF: The FileDescriptor parameter is
183          * not a valid file descriptor open for writing.").
184          * However, at this point it is not possibly anymore to read the
185          * 'writable' attribute of the corresponding file channel so we have to
186          * use 'fcntl'.
187          */
188         int getfl = fcntl(fd, F_GETFL);
189         if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
190             return 0;
191         }
192 #endif /* _AIX */
193         result = fsync(fd);
194     }
195 #endif /* not-MACOSX */
196     return handle(env, result, "Force failed");
197 }
198 
199 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv * env,jobject this,jobject fdo,jlong size)200 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
201                                              jobject fdo, jlong size)
202 {
203     return handle(env,
204                   ftruncate64(fdval(env, fdo), size),
205                   "Truncation failed");
206 }
207 
208 JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv * env,jobject this,jobject fdo)209 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
210 {
211     jint fd = fdval(env, fdo);
212     struct stat64 fbuf;
213 
214     if (fstat64(fd, &fbuf) < 0)
215         return handle(env, -1, "Size failed");
216 
217 #ifdef BLKGETSIZE64
218     if (S_ISBLK(fbuf.st_mode)) {
219         uint64_t size;
220         if (ioctl(fd, BLKGETSIZE64, &size) < 0)
221             return handle(env, -1, "Size failed");
222         return (jlong)size;
223     }
224 #endif
225 
226     return fbuf.st_size;
227 }
228 
229 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv * env,jobject this,jobject fdo,jboolean block,jlong pos,jlong size,jboolean shared)230 Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
231                                       jboolean block, jlong pos, jlong size,
232                                       jboolean shared)
233 {
234     jint fd = fdval(env, fdo);
235     jint lockResult = 0;
236     int cmd = 0;
237     struct flock64 fl;
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     if (shared == JNI_TRUE) {
247         fl.l_type = F_RDLCK;
248     } else {
249         fl.l_type = F_WRLCK;
250     }
251     if (block == JNI_TRUE) {
252         cmd = F_SETLKW64;
253     } else {
254         cmd = F_SETLK64;
255     }
256     lockResult = fcntl(fd, cmd, &fl);
257     if (lockResult < 0) {
258         if ((cmd == F_SETLK64) && (errno == EAGAIN || errno == EACCES))
259             return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
260         if (errno == EINTR)
261             return sun_nio_ch_FileDispatcherImpl_INTERRUPTED;
262         JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
263     }
264     return 0;
265 }
266 
267 JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv * env,jobject this,jobject fdo,jlong pos,jlong size)268 Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
269                                          jobject fdo, jlong pos, jlong size)
270 {
271     jint fd = fdval(env, fdo);
272     jint lockResult = 0;
273     struct flock64 fl;
274     int cmd = F_SETLK64;
275 
276     fl.l_whence = SEEK_SET;
277     if (size == (jlong)java_lang_Long_MAX_VALUE) {
278         fl.l_len = (off64_t)0;
279     } else {
280         fl.l_len = (off64_t)size;
281     }
282     fl.l_start = (off64_t)pos;
283     fl.l_type = F_UNLCK;
284     lockResult = fcntl(fd, cmd, &fl);
285     if (lockResult < 0) {
286         JNU_ThrowIOExceptionWithLastError(env, "Release failed");
287     }
288 }
289 
290 
closeFileDescriptor(JNIEnv * env,int fd)291 static void closeFileDescriptor(JNIEnv *env, int fd) {
292     if (fd != -1) {
293         int result = close(fd);
294         if (result < 0 && errno != ECONNRESET)
295             JNU_ThrowIOExceptionWithLastError(env, "Close failed");
296     }
297 }
298 
299 JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv * env,jclass clazz,jobject fdo)300 Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
301 {
302     jint fd = fdval(env, fdo);
303     closeFileDescriptor(env, fd);
304 }
305 
306 JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_preClose0(JNIEnv * env,jclass clazz,jobject fdo)307 Java_sun_nio_ch_FileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
308 {
309     jint fd = fdval(env, fdo);
310     if (preCloseFD >= 0) {
311         if (dup2(preCloseFD, fd) < 0)
312             JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
313     }
314 }
315 
316 JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_dup0(JNIEnv * env,jobject this,jobject fdo1,jobject fdo2)317 Java_sun_nio_ch_FileDispatcherImpl_dup0(JNIEnv *env, jobject this, jobject fdo1, jobject fdo2)
318 {
319     if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
320         JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
321     }
322 }
323 
324 JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_closeIntFD(JNIEnv * env,jclass clazz,jint fd)325 Java_sun_nio_ch_FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
326 {
327     closeFileDescriptor(env, fd);
328 }
329 
330 JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_setDirect0(JNIEnv * env,jclass clazz,jobject fdo)331 Java_sun_nio_ch_FileDispatcherImpl_setDirect0(JNIEnv *env, jclass clazz,
332                                            jobject fdo)
333 {
334     jint fd = fdval(env, fdo);
335     jint result;
336     struct statvfs64 file_stat;
337 
338 #if defined(O_DIRECT) || defined(F_NOCACHE) || defined(DIRECTIO_ON)
339 #ifdef O_DIRECT
340     jint orig_flag;
341     orig_flag = fcntl(fd, F_GETFL);
342     if (orig_flag == -1) {
343         JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
344         return -1;
345     }
346     result = fcntl(fd, F_SETFL, orig_flag | O_DIRECT);
347     if (result == -1) {
348         JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
349         return result;
350     }
351 #elif defined(F_NOCACHE)
352     result = fcntl(fd, F_NOCACHE, 1);
353     if (result == -1) {
354         JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
355         return result;
356     }
357 #elif defined(DIRECTIO_ON)
358     result = directio(fd, DIRECTIO_ON);
359     if (result == -1) {
360         JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
361         return result;
362     }
363 #endif
364     result = fstatvfs64(fd, &file_stat);
365     if(result == -1) {
366         JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
367         return result;
368     } else {
369         result = (int)file_stat.f_frsize;
370     }
371 #else
372     JNU_ThrowIOException(env, "DirectIO unsupported");
373     result = -1;
374 #endif
375     return result;
376 }
377