1 /*
   2  * Copyright (c) 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 package java.io;
  27 
  28 import jdk.internal.access.JavaIOFileDescriptorAccess;
  29 import jdk.internal.access.SharedSecrets;
  30 import jdk.internal.ref.CleanerFactory;
  31 import jdk.internal.ref.PhantomCleanable;
  32 
  33 import java.lang.ref.Cleaner;
  34 
  35 /**
  36  * Cleanable for a FileDescriptor when it becomes phantom reachable.
  37  * For regular fds on Unix and regular handles on Windows
  38  * register a cleanup if fd != -1 or handle != -1.
  39  * <p>
  40  * Subclassed from {@code PhantomCleanable} so that {@code clear} can be
  41  * called to disable the cleanup when the handle is closed by any means other
  42  * than calling {@link FileDescriptor#close}.
  43  * Otherwise, it might incorrectly close the handle after it has been reused.
  44  */
  45 final class FileCleanable extends PhantomCleanable<FileDescriptor> {
  46 
  47     // Access to FileDescriptor private fields;
  48     // avoids making fd and handle package private
  49     private static final JavaIOFileDescriptorAccess fdAccess =
  50             SharedSecrets.getJavaIOFileDescriptorAccess();
  51 
  52     /*
  53      * Raw close of the file fd and/or handle.
  54      * Used only for last chance cleanup.
  55      */
  56     private static native void cleanupClose0(int fd, long handle) throws IOException;
  57 
  58     // The raw fd to close
  59     private final int fd;
  60 
  61     // The handle to close
  62     private final long handle;
  63 
  64     /**
  65      * Register a Cleanable with the FileDescriptor
  66      * if the FileDescriptor is non-null and valid.
  67      * @implNote
  68      * A exception (OutOfMemoryException) will leave the FileDescriptor
  69      * having allocated resources and leak the fd/handle.
  70      *
  71      * @param fdo the FileDescriptor; may be null
  72      */
  73     static void register(FileDescriptor fdo) {
  74         if (fdo != null && fdo.valid()) {
  75             int fd = fdAccess.get(fdo);
  76             long handle = fdAccess.getHandle(fdo);
  77             fdo.registerCleanup(new FileCleanable(fdo, CleanerFactory.cleaner(), fd, handle));
  78         }
  79     }
  80 
  81     /**
  82      * Unregister a Cleanable from the FileDescriptor.
  83      * @param fdo the FileDescriptor; may be null
  84      */
  85     static void unregister(FileDescriptor fdo) {
  86         if (fdo != null) {
  87             fdo.unregisterCleanup();
  88         }
  89     }
  90 
  91     /**
  92      * Constructor for a phantom cleanable reference.
  93      *
  94      * @param obj     the object to monitor
  95      * @param cleaner the cleaner
  96      * @param fd      file descriptor to close
  97      * @param handle  handle to close
  98      */
  99     private FileCleanable(FileDescriptor obj, Cleaner cleaner, int fd, long handle) {
 100         super(obj, cleaner);
 101         this.fd = fd;
 102         this.handle = handle;
 103     }
 104 
 105     /**
 106      * Close the native handle or fd.
 107      */
 108     @Override
 109     protected void performCleanup() {
 110         try {
 111             cleanupClose0(fd, handle);
 112         } catch (IOException ioe) {
 113             throw new UncheckedIOException("close", ioe);
 114         }
 115     }
 116 }