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.
35 *
36 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
37 * or visit www.oracle.com if you need additional information or have any
38 * questions.
39 */
40
41 /*
42 * @test TestInheritFD
43 * @bug 8176717 8176809
44 * @summary a new process should not inherit open file descriptors
45 * @library /test/lib
46 * @modules java.base/jdk.internal.misc
47 * java.management
48 */
49
50 /**
51 * Test that HotSpot does not leak logging file descriptors.
52 *
53 * This test is performed in three steps. The first VM starts a second VM with
54 * gc logging enabled. The second VM starts a third VM and redirects the third
55 * VMs output to the first VM, it then exits and hopefully closes its log file.
56 *
57 * The third VM waits for the second to exit and close its log file. After that,
58 * the third VM tries to rename the log file of the second VM. If it succeeds in
59 * 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
|
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 // 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 }
|