Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c
41133 views
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 flock64 flock
47
#define off64_t off_t
48
#define F_SETLKW64 F_SETLKW
49
#define F_SETLK64 F_SETLK
50
#define pread64 pread
51
#define pwrite64 pwrite
52
#define ftruncate64 ftruncate
53
#define fstat64 fstat
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
#ifdef MACOSX
173
result = fcntl(fd, F_FULLFSYNC);
174
if (result == -1) {
175
struct statfs fbuf;
176
int errno_fcntl = errno;
177
if (fstatfs(fd, &fbuf) == 0) {
178
if ((fbuf.f_flags & MNT_LOCAL) == 0) {
179
/* Try fsync() in case file is not local. */
180
result = fsync(fd);
181
}
182
} else {
183
/* fstatfs() failed so restore errno from fcntl(). */
184
errno = errno_fcntl;
185
}
186
}
187
#else /* end MACOSX, begin not-MACOSX */
188
if (md == JNI_FALSE) {
189
result = fdatasync(fd);
190
} else {
191
#ifdef _AIX
192
/* On AIX, calling fsync on a file descriptor that is opened only for
193
* reading results in an error ("EBADF: The FileDescriptor parameter is
194
* not a valid file descriptor open for writing.").
195
* However, at this point it is not possibly anymore to read the
196
* 'writable' attribute of the corresponding file channel so we have to
197
* use 'fcntl'.
198
*/
199
int getfl = fcntl(fd, F_GETFL);
200
if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
201
return 0;
202
}
203
#endif /* _AIX */
204
result = fsync(fd);
205
}
206
#endif /* not-MACOSX */
207
return handle(env, result, "Force failed");
208
}
209
210
JNIEXPORT jint JNICALL
211
Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
212
jobject fdo, jlong size)
213
{
214
return handle(env,
215
ftruncate64(fdval(env, fdo), size),
216
"Truncation failed");
217
}
218
219
JNIEXPORT jlong JNICALL
220
Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
221
{
222
jint fd = fdval(env, fdo);
223
struct stat64 fbuf;
224
225
if (fstat64(fd, &fbuf) < 0)
226
return handle(env, -1, "Size failed");
227
228
#ifdef BLKGETSIZE64
229
if (S_ISBLK(fbuf.st_mode)) {
230
uint64_t size;
231
if (ioctl(fd, BLKGETSIZE64, &size) < 0)
232
return handle(env, -1, "Size failed");
233
return (jlong)size;
234
}
235
#endif
236
237
return fbuf.st_size;
238
}
239
240
JNIEXPORT jint JNICALL
241
Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
242
jboolean block, jlong pos, jlong size,
243
jboolean shared)
244
{
245
jint fd = fdval(env, fdo);
246
jint lockResult = 0;
247
int cmd = 0;
248
struct flock64 fl;
249
250
fl.l_whence = SEEK_SET;
251
if (size == (jlong)java_lang_Long_MAX_VALUE) {
252
fl.l_len = (off64_t)0;
253
} else {
254
fl.l_len = (off64_t)size;
255
}
256
fl.l_start = (off64_t)pos;
257
if (shared == JNI_TRUE) {
258
fl.l_type = F_RDLCK;
259
} else {
260
fl.l_type = F_WRLCK;
261
}
262
if (block == JNI_TRUE) {
263
cmd = F_SETLKW64;
264
} else {
265
cmd = F_SETLK64;
266
}
267
lockResult = fcntl(fd, cmd, &fl);
268
if (lockResult < 0) {
269
if ((cmd == F_SETLK64) && (errno == EAGAIN || errno == EACCES))
270
return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
271
if (errno == EINTR)
272
return sun_nio_ch_FileDispatcherImpl_INTERRUPTED;
273
JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
274
}
275
return 0;
276
}
277
278
JNIEXPORT void JNICALL
279
Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
280
jobject fdo, jlong pos, jlong size)
281
{
282
jint fd = fdval(env, fdo);
283
jint lockResult = 0;
284
struct flock64 fl;
285
int cmd = F_SETLK64;
286
287
fl.l_whence = SEEK_SET;
288
if (size == (jlong)java_lang_Long_MAX_VALUE) {
289
fl.l_len = (off64_t)0;
290
} else {
291
fl.l_len = (off64_t)size;
292
}
293
fl.l_start = (off64_t)pos;
294
fl.l_type = F_UNLCK;
295
lockResult = fcntl(fd, cmd, &fl);
296
if (lockResult < 0) {
297
JNU_ThrowIOExceptionWithLastError(env, "Release failed");
298
}
299
}
300
301
302
static void closeFileDescriptor(JNIEnv *env, int fd) {
303
if (fd != -1) {
304
int result = close(fd);
305
if (result < 0)
306
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
307
}
308
}
309
310
JNIEXPORT void JNICALL
311
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
312
{
313
jint fd = fdval(env, fdo);
314
closeFileDescriptor(env, fd);
315
}
316
317
JNIEXPORT void JNICALL
318
Java_sun_nio_ch_FileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
319
{
320
jint fd = fdval(env, fdo);
321
if (preCloseFD >= 0) {
322
if (dup2(preCloseFD, fd) < 0)
323
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
324
}
325
}
326
327
JNIEXPORT void JNICALL
328
Java_sun_nio_ch_FileDispatcherImpl_dup0(JNIEnv *env, jobject this, jobject fdo1, jobject fdo2)
329
{
330
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
331
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
332
}
333
}
334
335
JNIEXPORT void JNICALL
336
Java_sun_nio_ch_FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
337
{
338
closeFileDescriptor(env, fd);
339
}
340
341
JNIEXPORT jint JNICALL
342
Java_sun_nio_ch_FileDispatcherImpl_setDirect0(JNIEnv *env, jclass clazz,
343
jobject fdo)
344
{
345
jint fd = fdval(env, fdo);
346
jint result;
347
#ifdef MACOSX
348
struct statvfs file_stat;
349
#else
350
struct statvfs64 file_stat;
351
#endif
352
353
#if defined(O_DIRECT) || defined(F_NOCACHE) || defined(DIRECTIO_ON)
354
#ifdef O_DIRECT
355
jint orig_flag;
356
orig_flag = fcntl(fd, F_GETFL);
357
if (orig_flag == -1) {
358
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
359
return -1;
360
}
361
result = fcntl(fd, F_SETFL, orig_flag | O_DIRECT);
362
if (result == -1) {
363
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
364
return result;
365
}
366
#elif defined(F_NOCACHE)
367
result = fcntl(fd, F_NOCACHE, 1);
368
if (result == -1) {
369
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
370
return result;
371
}
372
#elif defined(DIRECTIO_ON)
373
result = directio(fd, DIRECTIO_ON);
374
if (result == -1) {
375
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
376
return result;
377
}
378
#endif
379
#ifdef MACOSX
380
result = fstatvfs(fd, &file_stat);
381
#else
382
result = fstatvfs64(fd, &file_stat);
383
#endif
384
if(result == -1) {
385
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
386
return result;
387
} else {
388
result = (int)file_stat.f_frsize;
389
}
390
#else
391
result = -1;
392
#endif
393
return result;
394
}
395
396