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