1 /*
2 * Copyright (c) 2000, 2009, 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 #include "nio.h"
38 #include "nio_util.h"
39 #include "JNIHelp.h"
40
41 #define NATIVE_METHOD(className, functionName, signature) \
42 { #functionName, signature, (void*)(className ## _ ## functionName) }
43
44 #ifdef _ALLBSD_SOURCE
45 #define stat64 stat
46 #define flock64 flock
47 #define off64_t off_t
48 #define F_SETLKW64 F_SETLKW
49 #define F_SETLK64 F_SETLK
50
51 #define pread64 pread
52 #define pwrite64 pwrite
53 #define ftruncate64 ftruncate
54 #define fstat64 fstat
55
56 #define fdatasync fsync
57 #endif
58
59 JNIEXPORT jint JNICALL
FileDispatcherImpl_read0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)60 FileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
61 jobject fdo, jlong address, jint len)
62 {
63 jint fd = fdval(env, fdo);
64 void *buf = (void *)jlong_to_ptr(address);
65
66 return convertReturnVal(env, read(fd, buf, len), JNI_TRUE);
67 }
68
69 JNIEXPORT jint JNICALL
FileDispatcherImpl_pread0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len,jlong offset)70 FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
71 jlong address, jint len, jlong offset)
72 {
73 jint fd = fdval(env, fdo);
74 void *buf = (void *)jlong_to_ptr(address);
75
76 return convertReturnVal(env, pread64(fd, buf, len, offset), JNI_TRUE);
77 }
78
79 JNIEXPORT jlong JNICALL
FileDispatcherImpl_readv0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)80 FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz,
81 jobject fdo, jlong address, jint len)
82 {
83 jint fd = fdval(env, fdo);
84 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
85 return convertLongReturnVal(env, readv(fd, iov, len), JNI_TRUE);
86 }
87
88 JNIEXPORT jint JNICALL
FileDispatcherImpl_write0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)89 FileDispatcherImpl_write0(JNIEnv *env, jclass clazz,
90 jobject fdo, jlong address, jint len)
91 {
92 jint fd = fdval(env, fdo);
93 void *buf = (void *)jlong_to_ptr(address);
94
95 return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
96 }
97
98 JNIEXPORT jint JNICALL
FileDispatcherImpl_pwrite0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len,jlong offset)99 FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
100 jlong address, jint len, jlong offset)
101 {
102 jint fd = fdval(env, fdo);
103 void *buf = (void *)jlong_to_ptr(address);
104
105 return convertReturnVal(env, pwrite64(fd, buf, len, offset), JNI_FALSE);
106 }
107
108 JNIEXPORT jlong JNICALL
FileDispatcherImpl_writev0(JNIEnv * env,jclass clazz,jobject fdo,jlong address,jint len)109 FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz,
110 jobject fdo, jlong address, jint len)
111 {
112 jint fd = fdval(env, fdo);
113 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
114 return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
115 }
116
117 static jlong
handle(JNIEnv * env,jlong rv,char * msg)118 handle(JNIEnv *env, jlong rv, char *msg)
119 {
120 if (rv >= 0)
121 return rv;
122 if (errno == EINTR)
123 return IOS_INTERRUPTED;
124 JNU_ThrowIOExceptionWithLastError(env, msg);
125 return IOS_THROWN;
126 }
127
128 JNIEXPORT jint JNICALL
FileDispatcherImpl_force0(JNIEnv * env,jobject this,jobject fdo,jboolean md)129 FileDispatcherImpl_force0(JNIEnv *env, jobject this,
130 jobject fdo, jboolean md)
131 {
132 jint fd = fdval(env, fdo);
133 int result = 0;
134
135 if (md == JNI_FALSE) {
136 result = fdatasync(fd);
137 } else {
138 result = fsync(fd);
139 }
140 return handle(env, result, "Force failed");
141 }
142
143 JNIEXPORT jint JNICALL
FileDispatcherImpl_truncate0(JNIEnv * env,jobject this,jobject fdo,jlong size)144 FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
145 jobject fdo, jlong size)
146 {
147 return handle(env,
148 ftruncate64(fdval(env, fdo), size),
149 "Truncation failed");
150 }
151
152 JNIEXPORT jlong JNICALL
FileDispatcherImpl_size0(JNIEnv * env,jobject this,jobject fdo)153 FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
154 {
155 struct stat64 fbuf;
156
157 if (fstat64(fdval(env, fdo), &fbuf) < 0)
158 return handle(env, -1, "Size failed");
159 return fbuf.st_size;
160 }
161
162 JNIEXPORT jint JNICALL
FileDispatcherImpl_lock0(JNIEnv * env,jobject this,jobject fdo,jboolean block,jlong pos,jlong size,jboolean shared)163 FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
164 jboolean block, jlong pos, jlong size,
165 jboolean shared)
166 {
167 jint fd = fdval(env, fdo);
168 jint lockResult = 0;
169 int cmd = 0;
170 struct flock64 fl;
171
172 fl.l_whence = SEEK_SET;
173 if (size == (jlong)java_lang_Long_MAX_VALUE) {
174 fl.l_len = (off64_t)0;
175 } else {
176 fl.l_len = (off64_t)size;
177 }
178 fl.l_start = (off64_t)pos;
179 if (shared == JNI_TRUE) {
180 fl.l_type = F_RDLCK;
181 } else {
182 fl.l_type = F_WRLCK;
183 }
184 if (block == JNI_TRUE) {
185 cmd = F_SETLKW64;
186 } else {
187 cmd = F_SETLK64;
188 }
189 lockResult = fcntl(fd, cmd, &fl);
190 if (lockResult < 0) {
191 if ((cmd == F_SETLK64) && (errno == EAGAIN || errno == EACCES))
192 return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
193 if (errno == EINTR)
194 return sun_nio_ch_FileDispatcherImpl_INTERRUPTED;
195 JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
196 }
197 return 0;
198 }
199
200 JNIEXPORT void JNICALL
FileDispatcherImpl_release0(JNIEnv * env,jobject this,jobject fdo,jlong pos,jlong size)201 FileDispatcherImpl_release0(JNIEnv *env, jobject this,
202 jobject fdo, jlong pos, jlong size)
203 {
204 jint fd = fdval(env, fdo);
205 jint lockResult = 0;
206 struct flock64 fl;
207 int cmd = F_SETLK64;
208
209 fl.l_whence = SEEK_SET;
210 if (size == (jlong)java_lang_Long_MAX_VALUE) {
211 fl.l_len = (off64_t)0;
212 } else {
213 fl.l_len = (off64_t)size;
214 }
215 fl.l_start = (off64_t)pos;
216 fl.l_type = F_UNLCK;
217 lockResult = fcntl(fd, cmd, &fl);
218 if (lockResult < 0) {
219 JNU_ThrowIOExceptionWithLastError(env, "Release failed");
220 }
221 }
222
223
closeFileDescriptor(JNIEnv * env,int fd)224 static void closeFileDescriptor(JNIEnv *env, int fd) {
225 if (fd != -1) {
226 int result = close(fd);
227 if (result < 0)
228 JNU_ThrowIOExceptionWithLastError(env, "Close failed");
229 }
230 }
231
232 JNIEXPORT void JNICALL
FileDispatcherImpl_close0(JNIEnv * env,jclass clazz,jobject fdo)233 FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
234 {
235 jint fd = fdval(env, fdo);
236 closeFileDescriptor(env, fd);
237 }
238
239 JNIEXPORT void JNICALL
FileDispatcherImpl_preClose0(JNIEnv * env,jclass clazz,jobject fdo)240 FileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
241 {
242 jint fd = fdval(env, fdo);
243 int preCloseFD = open("/dev/null", O_RDWR | O_CLOEXEC);
244 if (preCloseFD < 0) {
245 JNU_ThrowIOExceptionWithLastError(env, "open(\"/dev/null\") failed");
246 return;
247 }
248 if (dup2(preCloseFD, fd) < 0) {
249 JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
250 }
251 close(preCloseFD);
252 }
253
254 JNIEXPORT void JNICALL
FileDispatcherImpl_closeIntFD(JNIEnv * env,jclass clazz,jint fd)255 FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
256 {
257 closeFileDescriptor(env, fd);
258 }
259
260 static JNINativeMethod gMethods[] = {
261 NATIVE_METHOD(FileDispatcherImpl, closeIntFD, "(I)V"),
262 NATIVE_METHOD(FileDispatcherImpl, preClose0, "(Ljava/io/FileDescriptor;)V"),
263 NATIVE_METHOD(FileDispatcherImpl, close0, "(Ljava/io/FileDescriptor;)V"),
264 NATIVE_METHOD(FileDispatcherImpl, release0, "(Ljava/io/FileDescriptor;JJ)V"),
265 NATIVE_METHOD(FileDispatcherImpl, lock0, "(Ljava/io/FileDescriptor;ZJJZ)I"),
266 NATIVE_METHOD(FileDispatcherImpl, size0, "(Ljava/io/FileDescriptor;)J"),
267 NATIVE_METHOD(FileDispatcherImpl, truncate0, "(Ljava/io/FileDescriptor;J)I"),
268 NATIVE_METHOD(FileDispatcherImpl, force0, "(Ljava/io/FileDescriptor;Z)I"),
269 NATIVE_METHOD(FileDispatcherImpl, writev0, "(Ljava/io/FileDescriptor;JI)J"),
270 NATIVE_METHOD(FileDispatcherImpl, pwrite0, "(Ljava/io/FileDescriptor;JIJ)I"),
271 NATIVE_METHOD(FileDispatcherImpl, write0, "(Ljava/io/FileDescriptor;JI)I"),
272 NATIVE_METHOD(FileDispatcherImpl, readv0, "(Ljava/io/FileDescriptor;JI)J"),
273 NATIVE_METHOD(FileDispatcherImpl, pread0, "(Ljava/io/FileDescriptor;JIJ)I"),
274 NATIVE_METHOD(FileDispatcherImpl, read0, "(Ljava/io/FileDescriptor;JI)I"),
275 };
276
register_sun_nio_ch_FileDispatcherImpl(JNIEnv * env)277 void register_sun_nio_ch_FileDispatcherImpl(JNIEnv* env) {
278 jniRegisterNativeMethods(env, "sun/nio/ch/FileDispatcherImpl", gMethods, NELEM(gMethods));
279 }
280