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