Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/ch/FileChannelImpl.c
32288 views
1
/*
2
* Copyright (c) 2000, 2018, 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/mman.h>
27
#include <sys/stat.h>
28
#include <fcntl.h>
29
#include <sys/types.h>
30
#include <unistd.h>
31
32
#if defined(__ANDROID__) || defined(__linux__) || defined(__solaris__)
33
# include <sys/sendfile.h>
34
#elif defined(_AIX)
35
# include <sys/socket.h>
36
#elif defined(_ALLBSD_SOURCE) || defined(__ANDROID__)
37
# if defined(_ALLBSD_SOURCE)
38
# include <sys/socket.h>
39
# include <sys/uio.h>
40
# endif
41
# define lseek64 lseek
42
# define mmap64 mmap
43
#endif
44
45
#include "jni.h"
46
#include "jni_util.h"
47
#include "jlong.h"
48
#include "nio.h"
49
#include "nio_util.h"
50
#include "sun_nio_ch_FileChannelImpl.h"
51
#include "java_lang_Integer.h"
52
53
static jfieldID chan_fd; /* jobject 'fd' in sun.io.FileChannelImpl */
54
55
JNIEXPORT jlong JNICALL
56
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
57
{
58
jlong pageSize = sysconf(_SC_PAGESIZE);
59
chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
60
return pageSize;
61
}
62
63
static jlong
64
handle(JNIEnv *env, jlong rv, char *msg)
65
{
66
if (rv >= 0)
67
return rv;
68
if (errno == EINTR)
69
return IOS_INTERRUPTED;
70
JNU_ThrowIOExceptionWithLastError(env, msg);
71
return IOS_THROWN;
72
}
73
74
75
JNIEXPORT jlong JNICALL
76
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
77
jint prot, jlong off, jlong len)
78
{
79
void *mapAddress = 0;
80
jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
81
jint fd = fdval(env, fdo);
82
int protections = 0;
83
int flags = 0;
84
85
if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
86
protections = PROT_READ;
87
flags = MAP_SHARED;
88
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
89
protections = PROT_WRITE | PROT_READ;
90
flags = MAP_SHARED;
91
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
92
protections = PROT_WRITE | PROT_READ;
93
flags = MAP_PRIVATE;
94
}
95
96
mapAddress = mmap64(
97
0, /* Let OS decide location */
98
len, /* Number of bytes to map */
99
protections, /* File permissions */
100
flags, /* Changes are shared */
101
fd, /* File descriptor of mapped file */
102
off); /* Offset into file */
103
104
if (mapAddress == MAP_FAILED) {
105
if (errno == ENOMEM) {
106
JNU_ThrowOutOfMemoryError(env, "Map failed");
107
return IOS_THROWN;
108
}
109
return handle(env, -1, "Map failed");
110
}
111
112
return ((jlong) (unsigned long) mapAddress);
113
}
114
115
116
JNIEXPORT jint JNICALL
117
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
118
jlong address, jlong len)
119
{
120
void *a = (void *)jlong_to_ptr(address);
121
return handle(env,
122
munmap(a, (size_t)len),
123
"Unmap failed");
124
}
125
126
127
JNIEXPORT void JNICALL
128
Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
129
{
130
jint fd = fdval(env, fdo);
131
if (fd != -1) {
132
jlong result = close(fd);
133
if (result < 0) {
134
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
135
}
136
}
137
}
138
139
JNIEXPORT jlong JNICALL
140
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
141
jobject srcFDO,
142
jlong position, jlong count,
143
jobject dstFDO)
144
{
145
jint srcFD = fdval(env, srcFDO);
146
jint dstFD = fdval(env, dstFDO);
147
148
#if defined(__linux__)
149
off64_t offset = (off64_t)position;
150
jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
151
if (n < 0) {
152
if (errno == EAGAIN)
153
return IOS_UNAVAILABLE;
154
if ((errno == EINVAL) && ((ssize_t)count >= 0))
155
return IOS_UNSUPPORTED_CASE;
156
if (errno == EINTR) {
157
return IOS_INTERRUPTED;
158
}
159
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
160
return IOS_THROWN;
161
}
162
return n;
163
#elif defined (__solaris__)
164
sendfilevec64_t sfv;
165
size_t numBytes = 0;
166
jlong result;
167
168
sfv.sfv_fd = srcFD;
169
sfv.sfv_flag = 0;
170
sfv.sfv_off = (off64_t)position;
171
sfv.sfv_len = count;
172
173
result = sendfilev64(dstFD, &sfv, 1, &numBytes);
174
175
/* Solaris sendfilev() will return -1 even if some bytes have been
176
* transferred, so we check numBytes first.
177
*/
178
if (numBytes > 0)
179
return numBytes;
180
if (result < 0) {
181
if (errno == EAGAIN)
182
return IOS_UNAVAILABLE;
183
if (errno == EOPNOTSUPP)
184
return IOS_UNSUPPORTED_CASE;
185
if ((errno == EINVAL) && ((ssize_t)count >= 0))
186
return IOS_UNSUPPORTED_CASE;
187
if (errno == EINTR)
188
return IOS_INTERRUPTED;
189
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
190
return IOS_THROWN;
191
}
192
return result;
193
#elif defined(__APPLE__)
194
off_t numBytes;
195
int result;
196
197
numBytes = count;
198
199
result = sendfile(srcFD, dstFD, position, &numBytes, NULL, 0);
200
201
if (numBytes > 0)
202
return numBytes;
203
204
if (result == -1) {
205
if (errno == EAGAIN)
206
return IOS_UNAVAILABLE;
207
if (errno == EOPNOTSUPP || errno == ENOTSOCK || errno == ENOTCONN)
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
217
return result;
218
219
#elif defined(_AIX)
220
jlong max = (jlong)java_lang_Integer_MAX_VALUE;
221
struct sf_parms sf_iobuf;
222
jlong result;
223
224
if (position > max)
225
return IOS_UNSUPPORTED_CASE;
226
227
if (count > max)
228
count = max;
229
230
memset(&sf_iobuf, 0, sizeof(sf_iobuf));
231
sf_iobuf.file_descriptor = srcFD;
232
sf_iobuf.file_offset = (off_t)position;
233
sf_iobuf.file_bytes = count;
234
235
result = send_file(&dstFD, &sf_iobuf, SF_SYNC_CACHE);
236
237
/* AIX send_file() will return 0 when this operation complete successfully,
238
* return 1 when partial bytes transfered and return -1 when an error has
239
* Occured.
240
*/
241
if (result == -1) {
242
if (errno == EWOULDBLOCK)
243
return IOS_UNAVAILABLE;
244
if ((errno == EINVAL) && ((ssize_t)count >= 0))
245
return IOS_UNSUPPORTED_CASE;
246
if (errno == EINTR)
247
return IOS_INTERRUPTED;
248
if (errno == ENOTSOCK)
249
return IOS_UNSUPPORTED;
250
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
251
return IOS_THROWN;
252
}
253
254
if (sf_iobuf.bytes_sent > 0)
255
return (jlong)sf_iobuf.bytes_sent;
256
257
return IOS_UNSUPPORTED_CASE;
258
#else
259
return IOS_UNSUPPORTED_CASE;
260
#endif
261
}
262
263
264