8187631: Refactor FileDescriptor close implementation

Reviewed-by: bpb, alanb
This commit is contained in:
Roger Riggs 2017-09-21 11:41:12 -04:00
parent 8088bd6350
commit 5e55e5e2ee
20 changed files with 184 additions and 305 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 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
@ -33,16 +33,13 @@ import jdk.internal.misc.SharedSecrets;
/**
* Instances of the file descriptor class serve as an opaque handle
* to the underlying machine-specific structure representing an open
* file, an open socket, or another source or sink of bytes. The
* main practical use for a file descriptor is to create a
* <code>FileInputStream</code> or <code>FileOutputStream</code> to
* contain it.
* file, an open socket, or another source or sink of bytes.
* The main practical use for a file descriptor is to create a
* {@link FileInputStream} or {@link FileOutputStream} to contain it.
* <p>
* Applications should not create their own file descriptors.
*
* @author Pavani Diwanji
* @see java.io.FileInputStream
* @see java.io.FileOutputStream
* @since 1.0
*/
public final class FileDescriptor {
@ -58,6 +55,45 @@ public final class FileDescriptor {
*/
private boolean append;
static {
initIDs();
}
// Set up JavaIOFileDescriptorAccess in SharedSecrets
static {
SharedSecrets.setJavaIOFileDescriptorAccess(
new JavaIOFileDescriptorAccess() {
public void set(FileDescriptor fdo, int fd) {
fdo.fd = fd;
}
public int get(FileDescriptor fdo) {
return fdo.fd;
}
public void setAppend(FileDescriptor fdo, boolean append) {
fdo.append = append;
}
public boolean getAppend(FileDescriptor fdo) {
return fdo.append;
}
public void close(FileDescriptor fdo) {
fdo.close();
}
public void setHandle(FileDescriptor fdo, long handle) {
throw new UnsupportedOperationException();
}
public long getHandle(FileDescriptor fdo) {
throw new UnsupportedOperationException();
}
}
);
}
/**
* Constructs an (invalid) FileDescriptor
* object.
@ -74,7 +110,7 @@ public final class FileDescriptor {
/**
* A handle to the standard input stream. Usually, this file
* descriptor is not used directly, but rather via the input stream
* known as <code>System.in</code>.
* known as {@code System.in}.
*
* @see java.lang.System#in
*/
@ -83,7 +119,7 @@ public final class FileDescriptor {
/**
* A handle to the standard output stream. Usually, this file
* descriptor is not used directly, but rather via the output stream
* known as <code>System.out</code>.
* known as {@code System.out}.
* @see java.lang.System#out
*/
public static final FileDescriptor out = new FileDescriptor(1);
@ -91,7 +127,7 @@ public final class FileDescriptor {
/**
* A handle to the standard error stream. Usually, this file
* descriptor is not used directly, but rather via the output stream
* known as <code>System.err</code>.
* known as {@code System.err}.
*
* @see java.lang.System#err
*/
@ -100,9 +136,9 @@ public final class FileDescriptor {
/**
* Tests if this file descriptor object is valid.
*
* @return <code>true</code> if the file descriptor object represents a
* @return {@code true} if the file descriptor object represents a
* valid, open file, socket, or other active I/O connection;
* <code>false</code> otherwise.
* {@code false} otherwise.
*/
public boolean valid() {
return fd != -1;
@ -141,46 +177,18 @@ public final class FileDescriptor {
/* This routine initializes JNI field offsets for the class */
private static native void initIDs();
static {
initIDs();
}
// Set up JavaIOFileDescriptorAccess in SharedSecrets
static {
SharedSecrets.setJavaIOFileDescriptorAccess(
new JavaIOFileDescriptorAccess() {
public void set(FileDescriptor obj, int fd) {
obj.fd = fd;
}
public int get(FileDescriptor obj) {
return obj.fd;
}
public void setAppend(FileDescriptor obj, boolean append) {
obj.append = append;
}
public boolean getAppend(FileDescriptor obj) {
return obj.append;
}
public void setHandle(FileDescriptor obj, long handle) {
throw new UnsupportedOperationException();
}
public long getHandle(FileDescriptor obj) {
throw new UnsupportedOperationException();
}
}
);
}
/**
* Returns true, if the file was opened for appending.
*/
private static native boolean getAppend(int fd);
/**
* Close the raw file descriptor or handle, if it has not already been closed
* and set the fd and handle to -1.
* Package private to allow it to be used in java.io.
*/
native void close();
/*
* Package private methods to track referents.
* If multiple streams point to the same FileDescriptor, we cycle

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -28,6 +28,9 @@ package sun.nio.ch;
import java.io.FileDescriptor;
import java.io.IOException;
import jdk.internal.misc.JavaIOFileDescriptorAccess;
import jdk.internal.misc.SharedSecrets;
class FileDispatcherImpl extends FileDispatcher {
static {
@ -35,6 +38,9 @@ class FileDispatcherImpl extends FileDispatcher {
init();
}
private static final JavaIOFileDescriptorAccess fdAccess =
SharedSecrets.getJavaIOFileDescriptorAccess();
FileDispatcherImpl() {
}
@ -95,7 +101,7 @@ class FileDispatcherImpl extends FileDispatcher {
}
void close(FileDescriptor fd) throws IOException {
close0(fd);
fdAccess.close(fd);
}
void preClose(FileDescriptor fd) throws IOException {
@ -153,6 +159,8 @@ class FileDispatcherImpl extends FileDispatcher {
static native void release0(FileDescriptor fd, long pos, long size)
throws IOException;
// Shared with SocketDispatcher and DatagramDispatcher but
// NOT used by FileDispatcherImpl
static native void close0(FileDescriptor fd) throws IOException;
static native void preClose0(FileDescriptor fd) throws IOException;

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);

View file

@ -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);