1 /*
   2  * Copyright (c) 2007, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.FileDescriptor;
  26 import java.io.FileNotFoundException;
  27 import java.io.IOException;
  28 import java.io.RandomAccessFile;
  29 import java.lang.management.ManagementFactory;
  30 import java.lang.management.OperatingSystemMXBean;
  31 import java.lang.ref.Cleaner;
  32 import java.lang.ref.Reference;
  33 import java.lang.ref.ReferenceQueue;
  34 import java.lang.ref.WeakReference;
  35 import java.lang.reflect.Field;
  36 import java.nio.file.Path;
  37 import java.util.ArrayDeque;
  38 import java.util.HashSet;
  39 
  40 import com.sun.management.UnixOperatingSystemMXBean;
  41 
  42 import jdk.test.lib.util.FileUtils;
  43 
  44 /**
  45  * @test
  46  * @bug 8080225
  47  * @library /test/lib
  48  * @build jdk.test.lib.util.FileUtils UnreferencedRAFClosesFd
  49  * @modules java.base/java.io:open
  50  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
  51  * @run main/othervm UnreferencedRAFClosesFd
  52  */
  53 public class UnreferencedRAFClosesFd {
  54 
  55     static final String FILE_NAME = "empty.txt";
  56 
  57     /* standalone interface */
  58     public static void main(String argv[]) throws Exception {
  59 
  60         File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
  61         inFile.createNewFile();
  62         inFile.deleteOnExit();
  63 
  64         FileUtils.listFileDescriptors(System.out);
  65         long fdCount0 = getFdCount();
  66 
  67         String name = inFile.getPath();
  68         RandomAccessFile raf;
  69         try {
  70             // raf is explicitly *not* closed to allow cleaner to work
  71             raf = new RandomAccessFile(name, "rw");
  72         } catch (FileNotFoundException e) {
  73             System.out.println("Unexpected exception " + e);
  74             throw(e);
  75         }
  76         FileDescriptor fd = raf.getFD();
  77 
  78         Field fdField = FileDescriptor.class.getDeclaredField("cleanup");
  79         fdField.setAccessible(true);
  80         Cleaner.Cleanable cleanup = (Cleaner.Cleanable)fdField.get(fd);
  81 
  82         // Prepare to wait for FOS, FD, Cleanup to be reclaimed
  83         ReferenceQueue<Object> queue = new ReferenceQueue<>();
  84         HashSet<Reference<?>> pending = new HashSet<>(3);
  85         pending.add(new WeakReference<>(cleanup, queue));
  86         pending.add(new WeakReference<>(raf, queue));
  87         pending.add(new WeakReference<>(fd, queue));
  88 
  89         Reference<?> r;
  90         while (((r = queue.remove(10L)) != null)
  91                 || !pending.isEmpty()) {
  92             System.out.printf("r: %s, pending: %d%n", r, pending.size());
  93             if (r != null) {
  94                 pending.remove(r);
  95             } else {
  96                 cleanup = null;
  97                 raf = null;
  98                 fd = null;
  99                 System.gc();  // attempt to reclaim the RAF, cleanup, and fd
 100             }
 101         }
 102 
 103         // Keep these variables in scope as gc roots
 104         Reference.reachabilityFence(cleanup);
 105         Reference.reachabilityFence(fd);
 106         Reference.reachabilityFence(raf);
 107         Reference.reachabilityFence(pending);
 108 
 109         // Check the final count of open file descriptors
 110         long fdCount = getFdCount();
 111         if (fdCount != fdCount0) {
 112             System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
 113             System.out.printf("final count of open file descriptors: %d%n", fdCount);
 114             FileUtils.listFileDescriptors(System.out);
 115         }
 116     }
 117 
 118 
 119     // Get the count of open file descriptors, or -1 if not available
 120     private static long getFdCount() {
 121         OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
 122         return  (mxBean instanceof UnixOperatingSystemMXBean)
 123                 ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
 124                 : -1L;
 125     }
 126 }