< prev index next >

test/hotspot/jtreg/runtime/8176717/TestInheritFD.java

8205054: Could not find "lsof" on test machine
Reviewed-by:

0 import static java.io.File.createTempFile;                                                                                 
1 import static java.lang.Long.parseLong;                                                                                    
2 import static java.lang.System.getProperty;                                                                                
3 import static java.nio.file.Files.readAllBytes;                                                                            
4 import static java.util.Arrays.stream;                                                                                     
                                                                                                                           
5 import static java.util.stream.Collectors.joining;                                                                         
6 import static java.util.stream.Collectors.toList;                                                                          
7 import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder;                                                  
8 
9 import java.io.BufferedReader;                                                                                             
10 import java.io.File;                                                                                                       
11 import java.io.FileNotFoundException;                                                                                      
12 import java.io.FileOutputStream;                                                                                           
13 import java.io.IOException;                                                                                                
14 import java.io.InputStreamReader;                                                                                          
15 import java.util.Collection;                                                                                               
                                                                                                                           
16 import java.util.stream.Stream;                                                                                            
17 
18 /*                                                                                                                         
19  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.                                                  
20  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.                                                           
21  *                                                                                                                         
22  * This code is free software; you can redistribute it and/or modify it                                                    
23  * under the terms of the GNU General Public License version 2 only, as                                                    
24  * published by the Free Software Foundation.                                                                              
25  *                                                                                                                         
26  * This code is distributed in the hope that it will be useful, but WITHOUT                                                
27  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or                                                   
28  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License                                                   
29  * version 2 for more details (a copy is included in the LICENSE file that                                                 
30  * accompanied this code).                                                                                                 
31  *                                                                                                                         
32  * You should have received a copy of the GNU General Public License version                                               
33  * 2 along with this work; if not, write to the Free Software Foundation,                                                  
34  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                                                           

0 import static java.io.File.createTempFile;
1 import static java.lang.Long.parseLong;
2 import static java.lang.System.getProperty;
3 import static java.nio.file.Files.readAllBytes;
4 import static java.util.Arrays.stream;
5 import static java.util.Collections.emptyList;
6 import static java.util.stream.Collectors.joining;
7 import static java.util.stream.Collectors.toList;
8 import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder;
9 
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.FileNotFoundException;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.util.Collection;
17 import java.util.Optional;
18 import java.util.stream.Stream;
19 
20 /*
21  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23  *
24  * This code is free software; you can redistribute it and/or modify it
25  * under the terms of the GNU General Public License version 2 only, as
26  * published by the Free Software Foundation.
27  *
28  * This code is distributed in the hope that it will be useful, but WITHOUT
29  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31  * version 2 for more details (a copy is included in the LICENSE file that
32  * accompanied this code).
33  *
34  * You should have received a copy of the GNU General Public License version
35  * 2 along with this work; if not, write to the Free Software Foundation,
36  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

145     @SuppressWarnings("resource")                                                                                          
146     static void fakeLeakyJVM(boolean fake) {                                                                               
147         if (fake) {                                                                                                        
148             try {                                                                                                          
149                 new FileOutputStream("fakeLeakyJVM" + LOG_SUFFIX, false);                                                  
150             } catch (FileNotFoundException e) {                                                                            
151             }                                                                                                              
152         }                                                                                                                  
153     }                                                                                                                      
154 
155     static Stream<String> run(String... args){                                                                             
156         try {                                                                                                              
157             return new BufferedReader(new InputStreamReader(new ProcessBuilder(args).start().getInputStream())).lines();   
158         } catch (IOException e) {                                                                                          
159             throw new RuntimeException(e);                                                                                 
160         }                                                                                                                  
161     }                                                                                                                      
162 
163     static Collection<String> outputContainingFilenames() {                                                                
164         long pid = ProcessHandle.current().pid();                                                                          
165         String[] command = stream(new String[][]{                                                                          
166                 {"/usr/bin/lsof", "-p"},                                                                                   
167                 {"/usr/sbin/lsof", "-p"},                                                                                  
168                 {"/bin/lsof", "-p"},                                                                                       
169                 {"/sbin/lsof", "-p"},                                                                                      
170                 {"/usr/local/bin/lsof", "-p"},                                                                             
171                 {"/usr/bin/pfiles", "-F"}}) // Solaris                                                                     
172             .filter(args -> new File(args[0]).exists())                                                                    
173             .findFirst()                                                                                                   
174             .orElseThrow(() -> new RuntimeException("could not find lsof-like command"));                                  
175 
176         System.out.println("using command: " + command[0] + " " + command[1]);                                             
177         return run(command[0], command[1], "" + pid).collect(toList());                                                    
178     }                                                                                                                      
179 
180     static boolean findOpenLogFile(Collection<String> fileNames) {                                                         
181         return fileNames.stream()                                                                                          
182             .filter(fileName -> fileName.contains(LOG_SUFFIX))                                                             
183             .findAny()                                                                                                     
184             .isPresent();                                                                                                  
185     }                                                                                                                      
186 
187     static void windows(File f, long parentPid) throws InterruptedException {                                              
188         System.out.println("waiting for pid: " + parentPid);                                                               
189         ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join());                                           
190         System.out.println("trying to rename file to the same name: " + f);                                                
191         System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by p
192     }                                                                                                                      

147     @SuppressWarnings("resource")
148     static void fakeLeakyJVM(boolean fake) {
149         if (fake) {
150             try {
151                 new FileOutputStream("fakeLeakyJVM" + LOG_SUFFIX, false);
152             } catch (FileNotFoundException e) {
153             }
154         }
155     }
156 
157     static Stream<String> run(String... args){
158         try {
159             return new BufferedReader(new InputStreamReader(new ProcessBuilder(args).start().getInputStream())).lines();
160         } catch (IOException e) {
161             throw new RuntimeException(e);
162         }
163     }
164 
165     static Collection<String> outputContainingFilenames() {
166         long pid = ProcessHandle.current().pid();
167         Optional<String[]> command = stream(new String[][]{
168                 {"/usr/bin/lsof", "-p"},
169                 {"/usr/sbin/lsof", "-p"},
170                 {"/bin/lsof", "-p"},
171                 {"/sbin/lsof", "-p"},
172                 {"/usr/local/bin/lsof", "-p"},
173                 {"/usr/bin/pfiles", "-F"}}) // Solaris
174             .filter(args -> new File(args[0]).exists())
175             .findFirst();

176 
177         System.out.println("using command: " + command.map((c) -> c[0] + " " + c[1]).orElse("<not found>"));
178         return command.map(c -> run(c[0], c[1], "" + pid).collect(toList())).orElse(emptyList()); // if command can not be 
179     }
180 
181     static boolean findOpenLogFile(Collection<String> fileNames) {
182         return fileNames.stream()
183             .filter(fileName -> fileName.contains(LOG_SUFFIX))
184             .findAny()
185             .isPresent();
186     }
187 
188     static void windows(File f, long parentPid) throws InterruptedException {
189         System.out.println("waiting for pid: " + parentPid);
190         ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join());
191         System.out.println("trying to rename file to the same name: " + f);
192         System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by p
193     }
< prev index next >