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.
37 *
38 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
39 * or visit www.oracle.com if you need additional information or have any
40 * questions.
41 */
42
43 /*
44 * @test TestInheritFD
45 * @bug 8176717 8176809
46 * @summary a new process should not inherit open file descriptors
47 * @library /test/lib
48 * @modules java.base/jdk.internal.misc
49 * java.management
50 */
51
52 /**
53 * Test that HotSpot does not leak logging file descriptors.
54 *
55 * This test is performed in three steps. The first VM starts a second VM with
56 * gc logging enabled. The second VM starts a third VM and redirects the third
57 * VMs output to the first VM, it then exits and hopefully closes its log file.
58 *
59 * The third VM waits for the second to exit and close its log file. After that,
60 * the third VM tries to rename the log file of the second VM. If it succeeds in
61 * doing so it means that the third VM did not inherit the open log file
|
0 /*
1 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 */
22
23 import static java.io.File.createTempFile;
24 import static java.lang.Long.parseLong;
25 import static java.lang.System.getProperty;
26 import static java.nio.file.Files.readAllBytes;
27 import static java.util.Arrays.stream;
28 import static java.util.Collections.emptyList;
29 import static java.util.stream.Collectors.joining;
30 import static java.util.stream.Collectors.toList;
31 import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder;
32
33 import java.io.BufferedReader;
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.util.Collection;
40 import java.util.Optional;
41 import java.util.stream.Stream;
42
43 /*
44 * @test TestInheritFD
45 * @bug 8176717 8176809
46 * @summary a new process should not inherit open file descriptors
47 * @library /test/lib
48 * @modules java.base/jdk.internal.misc
49 * java.management
50 */
51
52 /**
53 * Test that HotSpot does not leak logging file descriptors.
54 *
55 * This test is performed in three steps. The first VM starts a second VM with
56 * gc logging enabled. The second VM starts a third VM and redirects the third
57 * VMs output to the first VM, it then exits and hopefully closes its log file.
58 *
59 * The third VM waits for the second to exit and close its log file. After that,
60 * the third VM tries to rename the log file of the second VM. If it succeeds in
61 * doing so it means that the third VM did not inherit the open log file
|
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 }
|
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 // if command can not be found return list without log file (some machines does not have "lsof" in the expected pla
179 return command.map(c -> run(c[0], c[1], "" + pid).collect(toList())).orElse(emptyList());
180 }
181
182 static boolean findOpenLogFile(Collection<String> fileNames) {
183 return fileNames.stream()
184 .filter(fileName -> fileName.contains(LOG_SUFFIX))
185 .findAny()
186 .isPresent();
187 }
188
189 static void windows(File f, long parentPid) throws InterruptedException {
190 System.out.println("waiting for pid: " + parentPid);
191 ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join());
192 System.out.println("trying to rename file to the same name: " + f);
193 System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by p
194 }
|