< prev index next >

test/lib/jdk/test/lib/util/FileUtils.java

Print this page
rev 51058 : 8205610: [TESTLIB] Improve listing of open file descriptors
Reviewed-by:

@@ -37,10 +37,11 @@
 import java.nio.file.Paths;
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.time.Instant;
 import java.time.Duration;
+import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.ArrayDeque;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;

@@ -240,20 +241,19 @@
      * List the open file descriptors (if supported by the 'lsof' command).
      * @param ps a printStream to send the output to
      * @throws UncheckedIOException if an error occurs
      */
     public static void listFileDescriptors(PrintStream ps) {
-        List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
-        Optional<Path> lsof = lsofDirs.stream()
-                .map(s -> Paths.get(s, "lsof"))
-                .filter(f -> Files.isExecutable(f))
+
+        Optional<String[]> lsof = Arrays.stream(lsCommands)
+                .filter(args -> Files.isExecutable(Path.of(args[0])))
                 .findFirst();
-        lsof.ifPresent(exe -> {
+        lsof.ifPresent(args -> {
             try {
                 ps.printf("Open File Descriptors:%n");
                 long pid = ProcessHandle.current().pid();
-                ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid));
+                ProcessBuilder pb = new ProcessBuilder(args[0], args[1], Integer.toString((int) pid));
                 pb.redirectErrorStream(true);   // combine stderr and stdout
                 pb.redirectOutput(Redirect.PIPE);
 
                 Process p = pb.start();
                 Instant start = Instant.now();

@@ -271,6 +271,16 @@
             } catch (IOException ioe) {
                 throw new UncheckedIOException("error listing file descriptors", ioe);
             }
         });
     }
+
+    // Possible command locations and arguments
+    static String[][] lsCommands = new String[][] {
+            {"/usr/bin/lsof", "-p"},
+            {"/usr/sbin/lsof", "-p"},
+            {"/bin/lsof", "-p"},
+            {"/sbin/lsof", "-p"},
+            {"/usr/local/bin/lsof", "-p"},
+            {"/usr/bin/pfiles", "-F"},   // Solaris
+    };
 }
< prev index next >