• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000, 2011, 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 "jvm_md.h"
30 #include "jlong.h"
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include "sun_nio_ch_FileChannelImpl.h"
35 #include "nio.h"
36 #include "nio_util.h"
37 #include <dlfcn.h>
38 #include <nativehelper/JNIHelp.h>
39 
40 #define NATIVE_METHOD(className, functionName, signature) \
41 { #functionName, signature, (void*)(className ## _ ## functionName) }
42 
43 #if defined(__linux__) || defined(__solaris__)
44 #include <sys/sendfile.h>
45 #elif defined(_AIX)
46 #include <sys/socket.h>
47 #elif defined(_ALLBSD_SOURCE)
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/uio.h>
51 
52 #define lseek64 lseek
53 #define mmap64 mmap
54 // BEGIN Android-added: Alias *64 on Fuchsia builds. http://b/119496969
55 #elif defined(__Fuchsia__)
56 #define lseek64 lseek
57 #define mmap64 mmap
58 // END Android-added: Alias *64 on Fuchsia builds. http://b/119496969
59 #endif
60 
61 static jfieldID chan_fd;        /* jobject 'fd' in sun.io.FileChannelImpl */
62 
63 JNIEXPORT jlong JNICALL
FileChannelImpl_initIDs(JNIEnv * env,jclass clazz)64 FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
65 {
66     jlong pageSize = sysconf(_SC_PAGESIZE);
67     chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
68     return pageSize;
69 }
70 
71 static jlong
handle(JNIEnv * env,jlong rv,char * msg)72 handle(JNIEnv *env, jlong rv, char *msg)
73 {
74     if (rv >= 0)
75         return rv;
76     if (errno == EINTR)
77         return IOS_INTERRUPTED;
78     JNU_ThrowIOExceptionWithLastError(env, msg);
79     return IOS_THROWN;
80 }
81 
82 
83 JNIEXPORT jlong JNICALL
FileChannelImpl_map0(JNIEnv * env,jobject this,jint prot,jlong off,jlong len)84 FileChannelImpl_map0(JNIEnv *env, jobject this,
85                                      jint prot, jlong off, jlong len)
86 {
87     void *mapAddress = 0;
88     jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
89     jint fd = fdval(env, fdo);
90     int protections = 0;
91     int flags = 0;
92 
93     if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
94         protections = PROT_READ;
95         flags = MAP_SHARED;
96     } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
97         protections = PROT_WRITE | PROT_READ;
98         flags = MAP_SHARED;
99     } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
100         protections =  PROT_WRITE | PROT_READ;
101         flags = MAP_PRIVATE;
102     }
103 
104     mapAddress = mmap64(
105         0,                    /* Let OS decide location */
106         len,                  /* Number of bytes to map */
107         protections,          /* File permissions */
108         flags,                /* Changes are shared */
109         fd,                   /* File descriptor of mapped file */
110         off);                 /* Offset into file */
111 
112     if (mapAddress == MAP_FAILED) {
113         if (errno == ENOMEM) {
114             JNU_ThrowOutOfMemoryError(env, "Map failed");
115             return IOS_THROWN;
116         }
117         return handle(env, -1, "Map failed");
118     }
119 
120     return ((jlong) (unsigned long) mapAddress);
121 }
122 
123 
124 JNIEXPORT jint JNICALL
FileChannelImpl_unmap0(JNIEnv * env,jobject this,jlong address,jlong len)125 FileChannelImpl_unmap0(JNIEnv *env, jobject this,
126                                        jlong address, jlong len)
127 {
128     void *a = (void *)jlong_to_ptr(address);
129     return handle(env,
130                   munmap(a, (size_t)len),
131                   "Unmap failed");
132 }
133 
134 
135 JNIEXPORT jlong JNICALL
FileChannelImpl_position0(JNIEnv * env,jobject this,jobject fdo,jlong offset)136 FileChannelImpl_position0(JNIEnv *env, jobject this,
137                                           jobject fdo, jlong offset)
138 {
139     jint fd = fdval(env, fdo);
140     jlong result = 0;
141 
142     if (offset < 0) {
143         result = lseek64(fd, 0, SEEK_CUR);
144     } else {
145         result = lseek64(fd, offset, SEEK_SET);
146     }
147     return handle(env, result, "Position failed");
148 }
149 
150 
151 JNIEXPORT void JNICALL
FileChannelImpl_close0(JNIEnv * env,jobject this,jobject fdo)152 FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
153 {
154     jint fd = fdval(env, fdo);
155     if (fd != -1) {
156         jlong result = close(fd);
157         if (result < 0) {
158             JNU_ThrowIOExceptionWithLastError(env, "Close failed");
159         }
160     }
161 }
162 
163 JNIEXPORT jlong JNICALL
FileChannelImpl_transferTo0(JNIEnv * env,jobject this,jobject srcFDO,jlong position,jlong count,jobject dstFDO)164 FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
165                                             jobject srcFDO,
166                                             jlong position, jlong count,
167                                             jobject dstFDO)
168 {
169     jint srcFD = fdval(env, srcFDO);
170     jint dstFD = fdval(env, dstFDO);
171 
172 #if defined(__linux__)
173     off64_t offset = (off64_t)position;
174     jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
175     if (n < 0) {
176         if (errno == EAGAIN)
177             return IOS_UNAVAILABLE;
178         if ((errno == EINVAL) && ((ssize_t)count >= 0))
179             return IOS_UNSUPPORTED_CASE;
180         if (errno == EINTR) {
181             return IOS_INTERRUPTED;
182         }
183         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
184         return IOS_THROWN;
185     }
186     return n;
187 #elif defined (__solaris__)
188     sendfilevec64_t sfv;
189     size_t numBytes = 0;
190     jlong result;
191 
192     sfv.sfv_fd = srcFD;
193     sfv.sfv_flag = 0;
194     sfv.sfv_off = (off64_t)position;
195     sfv.sfv_len = count;
196 
197     result = sendfilev64(dstFD, &sfv, 1, &numBytes);
198 
199     /* Solaris sendfilev() will return -1 even if some bytes have been
200      * transferred, so we check numBytes first.
201      */
202     if (numBytes > 0)
203         return numBytes;
204     if (result < 0) {
205         if (errno == EAGAIN)
206             return IOS_UNAVAILABLE;
207         if (errno == EOPNOTSUPP)
208             return IOS_UNSUPPORTED_CASE;
209         if ((errno == EINVAL) && ((ssize_t)count >= 0))
210             return IOS_UNSUPPORTED_CASE;
211         if (errno == EINTR)
212             return IOS_INTERRUPTED;
213         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
214         return IOS_THROWN;
215     }
216     return result;
217 #elif defined(__APPLE__)
218     off_t numBytes;
219     int result;
220 
221     numBytes = count;
222 
223     result = sendfile(srcFD, dstFD, position, &numBytes, NULL, 0);
224 
225     if (numBytes > 0)
226         return numBytes;
227 
228     if (result == -1) {
229         if (errno == EAGAIN)
230             return IOS_UNAVAILABLE;
231         if (errno == EOPNOTSUPP || errno == ENOTSOCK || errno == ENOTCONN)
232             return IOS_UNSUPPORTED_CASE;
233         if ((errno == EINVAL) && ((ssize_t)count >= 0))
234             return IOS_UNSUPPORTED_CASE;
235         if (errno == EINTR)
236             return IOS_INTERRUPTED;
237         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
238         return IOS_THROWN;
239     }
240 
241     return result;
242 
243 #elif defined(_AIX)
244     jlong max = (jlong)java_lang_Integer_MAX_VALUE;
245     struct sf_parms sf_iobuf;
246     jlong result;
247 
248     if (position > max)
249         return IOS_UNSUPPORTED_CASE;
250 
251     if (count > max)
252         count = max;
253 
254     memset(&sf_iobuf, 0, sizeof(sf_iobuf));
255     sf_iobuf.file_descriptor = srcFD;
256     sf_iobuf.file_offset = (off_t)position;
257     sf_iobuf.file_bytes = count;
258 
259     result = send_file(&dstFD, &sf_iobuf, SF_SYNC_CACHE);
260 
261     /* AIX send_file() will return 0 when this operation complete successfully,
262      * return 1 when partial bytes transfered and return -1 when an error has
263      * Occured.
264      */
265     if (result == -1) {
266         if (errno == EWOULDBLOCK)
267             return IOS_UNAVAILABLE;
268         if ((errno == EINVAL) && ((ssize_t)count >= 0))
269             return IOS_UNSUPPORTED_CASE;
270         if (errno == EINTR)
271             return IOS_INTERRUPTED;
272         if (errno == ENOTSOCK)
273             return IOS_UNSUPPORTED;
274         JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
275         return IOS_THROWN;
276     }
277 
278     if (sf_iobuf.bytes_sent > 0)
279         return (jlong)sf_iobuf.bytes_sent;
280 
281     return IOS_UNSUPPORTED_CASE;
282 #else
283     return IOS_UNSUPPORTED_CASE;
284 #endif
285 }
286 
287 static JNINativeMethod gMethods[] = {
288   NATIVE_METHOD(FileChannelImpl, initIDs, "()J"),
289   NATIVE_METHOD(FileChannelImpl, map0, "(IJJ)J"),
290   NATIVE_METHOD(FileChannelImpl, unmap0, "(JJ)I"),
291   NATIVE_METHOD(FileChannelImpl, position0, "(Ljava/io/FileDescriptor;J)J"),
292   NATIVE_METHOD(FileChannelImpl, transferTo0, "(Ljava/io/FileDescriptor;JJLjava/io/FileDescriptor;)J"),
293 };
294 
register_sun_nio_ch_FileChannelImpl(JNIEnv * env)295 void register_sun_nio_ch_FileChannelImpl(JNIEnv* env) {
296   jniRegisterNativeMethods(env, "sun/nio/ch/FileChannelImpl", gMethods, NELEM(gMethods));
297 }
298