mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8187631: Refactor FileDescriptor close implementation
Reviewed-by: bpb, alanb
This commit is contained in:
parent
8088bd6350
commit
5e55e5e2ee
20 changed files with 184 additions and 305 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -70,3 +70,9 @@ Java_java_io_FileDescriptor_getAppend(JNIEnv *env, jclass fdClass, jint fd) {
|
|||
int flags = fcntl(fd, F_GETFL);
|
||||
return ((flags & O_APPEND) == 0) ? JNI_FALSE : JNI_TRUE;
|
||||
}
|
||||
|
||||
// instance method close0 for FileDescriptor
|
||||
JNIEXPORT void JNICALL
|
||||
Java_java_io_FileDescriptor_close(JNIEnv *env, jobject this) {
|
||||
fileDescriptorClose(env, this);
|
||||
}
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include "jni_util.h"
|
||||
#include "jvm.h"
|
||||
#include "io_util.h"
|
||||
#include "io_util_md.h"
|
||||
|
||||
#include "java_io_FileInputStream.h"
|
||||
|
||||
extern jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
|
||||
|
||||
/*********************************************************************
|
||||
* Platform specific implementation of input stream native methods
|
||||
*/
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_java_io_FileInputStream_close0(JNIEnv *env, jobject this) {
|
||||
fileClose(env, this, fis_fd);
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include "jni_util.h"
|
||||
#include "jvm.h"
|
||||
#include "io_util.h"
|
||||
#include "io_util_md.h"
|
||||
|
||||
#include "java_io_RandomAccessFile.h"
|
||||
|
||||
extern jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
|
||||
|
||||
/*********************************************************************
|
||||
* Platform specific implementation of input stream native methods
|
||||
*/
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_java_io_RandomAccessFile_close0(JNIEnv *env, jobject this) {
|
||||
fileClose(env, this, raf_fd);
|
||||
}
|
|
@ -124,19 +124,31 @@ fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
|
|||
void
|
||||
fileClose(JNIEnv *env, jobject this, jfieldID fid)
|
||||
{
|
||||
FD fd = GET_FD(this, fid);
|
||||
if (fd == -1) {
|
||||
jobject fileDescriptor = (*env)->GetObjectField(env, (this), (fid));
|
||||
if (fileDescriptor == NULL) {
|
||||
return;
|
||||
}
|
||||
fileDescriptorClose(env, fileDescriptor);
|
||||
}
|
||||
|
||||
// Function to close the fd held by this FileDescriptor and set fd to -1.
|
||||
void
|
||||
fileDescriptorClose(JNIEnv *env, jobject this)
|
||||
{
|
||||
FD fd = (*env)->GetIntField(env, this, IO_fd_fdID);
|
||||
if ((*env)->ExceptionOccurred(env)) {
|
||||
return;
|
||||
}
|
||||
/* Set the fd to -1 before closing it so that the timing window
|
||||
* of other threads using the wrong fd (closed but recycled fd,
|
||||
* that gets re-opened with some other filename) is reduced.
|
||||
* Practically the chance of its occurance is low, however, we are
|
||||
* taking extra precaution over here.
|
||||
*/
|
||||
SET_FD(this, -1, fid);
|
||||
|
||||
(*env)->SetIntField(env, this, IO_fd_fdID, -1);
|
||||
if ((*env)->ExceptionOccurred(env)) {
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* Don't close file descriptors 0, 1, or 2. If we close these stream
|
||||
* then a subsequent file open or socket will use them. Instead we
|
||||
|
@ -145,7 +157,7 @@ fileClose(JNIEnv *env, jobject this, jfieldID fid)
|
|||
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
|
||||
int devnull = open("/dev/null", O_WRONLY);
|
||||
if (devnull < 0) {
|
||||
SET_FD(this, fd, fid); // restore fd
|
||||
(*env)->SetIntField(env, this, IO_fd_fdID, fd);
|
||||
JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
|
||||
} else {
|
||||
dup2(devnull, fd);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -103,6 +103,7 @@ FD handleOpen(const char *path, int oflag, int mode);
|
|||
* IO helper function(s)
|
||||
*/
|
||||
void fileClose(JNIEnv *env, jobject this, jfieldID fid);
|
||||
void fileDescriptorClose(JNIEnv *env, jobject this);
|
||||
|
||||
#ifdef MACOSX
|
||||
jstring newStringPlatform(JNIEnv *env, const char* str);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue