Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java
64478 views
1
/*
2
* Copyright (c) 2018, 2022, 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 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.stream.Collectors.joining;
29
import static java.util.stream.Collectors.toList;
30
import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder;
31
import static jdk.test.lib.Platform.isWindows;
32
import jdk.test.lib.Utils;
33
import jdk.test.lib.Platform;
34
import jtreg.SkippedException;
35
36
import java.io.BufferedReader;
37
import java.io.File;
38
import java.io.FileNotFoundException;
39
import java.io.FileOutputStream;
40
import java.io.IOException;
41
import java.io.InputStreamReader;
42
import java.util.Collection;
43
import java.util.Optional;
44
import java.util.stream.Stream;
45
46
/*
47
* @test TestInheritFD
48
* @bug 8176717 8176809 8222500
49
* @summary a new process should not inherit open file descriptors
50
* @comment On Aix lsof requires root privileges.
51
* @requires os.family != "aix"
52
* @library /test/lib
53
* @modules java.base/jdk.internal.misc
54
* java.management
55
* @run driver TestInheritFD
56
*/
57
58
/**
59
* Test that HotSpot does not leak logging file descriptors.
60
*
61
* This test is performed in three steps. The first VM starts a second VM with
62
* gc logging enabled. The second VM starts a third VM and redirects the third
63
* VMs output to the first VM, it then exits and hopefully closes its log file.
64
*
65
* The third VM waits for the second to exit and close its log file. After that,
66
* the third VM tries to rename the log file of the second VM. If it succeeds in
67
* doing so it means that the third VM did not inherit the open log file
68
* (windows can not rename opened files easily)
69
*
70
* The third VM communicates the success to rename the file by printing "CLOSED
71
* FD". The first VM checks that the string was printed by the third VM.
72
*
73
* On unix like systems "lsof" is used.
74
*/
75
76
public class TestInheritFD {
77
78
public static final String LEAKS_FD = "VM RESULT => LEAKS FD";
79
public static final String RETAINS_FD = "VM RESULT => RETAINS FD";
80
public static final String EXIT = "VM RESULT => VM EXIT";
81
public static final String LOG_SUFFIX = ".strangelogsuffixthatcanbecheckedfor";
82
public static final String USER_DIR = System.getProperty("user.dir");
83
84
// first VM
85
public static void main(String[] args) throws Exception {
86
String logPath = Utils.createTempFile("logging", LOG_SUFFIX).toFile().getName();
87
File commFile = Utils.createTempFile("communication", ".txt").toFile();
88
89
if (!isWindows() && !lsofCommand().isPresent()) {
90
throw new SkippedException("Could not find lsof like command");
91
}
92
93
ProcessBuilder pb = createJavaProcessBuilder(
94
"-Xlog:gc:\"" + logPath + "\"",
95
"-Dtest.jdk=" + getProperty("test.jdk"),
96
VMStartedWithLogging.class.getName(),
97
logPath);
98
99
pb.redirectOutput(commFile); // use temp file to communicate between processes
100
pb.start();
101
102
String out = "";
103
do {
104
out = new String(readAllBytes(commFile.toPath()));
105
Thread.sleep(100);
106
System.out.println("SLEEP 100 millis");
107
} while (!out.contains(EXIT));
108
109
System.out.println(out);
110
if (out.contains(RETAINS_FD)) {
111
System.out.println("Log file was not inherited by third VM");
112
} else {
113
throw new RuntimeException("could not match: " + RETAINS_FD);
114
}
115
}
116
117
static class VMStartedWithLogging {
118
// second VM
119
public static void main(String[] args) throws IOException, InterruptedException {
120
ProcessBuilder pb = createJavaProcessBuilder(
121
"-Dtest.jdk=" + getProperty("test.jdk"),
122
VMShouldNotInheritFileDescriptors.class.getName(),
123
args[0],
124
"" + ProcessHandle.current().pid());
125
pb.inheritIO(); // in future, redirect information from third VM to first VM
126
pb.start();
127
128
if (!isWindows()) {
129
System.out.println("(Second VM) Open file descriptors:\n" + outputContainingFilenames().stream().collect(joining("\n")));
130
}
131
}
132
}
133
134
static class VMShouldNotInheritFileDescriptors {
135
// third VM
136
public static void main(String[] args) throws InterruptedException {
137
try {
138
File logFile = new File(args[0]);
139
long parentPid = parseLong(args[1]);
140
fakeLeakyJVM(false); // for debugging of test case
141
142
if (isWindows()) {
143
windows(logFile, parentPid);
144
} else {
145
Collection<String> output = outputContainingFilenames();
146
System.out.println("(Third VM) Open file descriptors:\n" + output.stream().collect(joining("\n")));
147
System.out.println(findOpenLogFile(output) ? LEAKS_FD : RETAINS_FD);
148
}
149
} catch (Exception e) {
150
System.out.println(e.toString());
151
} finally {
152
System.out.println(EXIT);
153
}
154
}
155
}
156
157
// for debugging of test case
158
@SuppressWarnings("resource")
159
static void fakeLeakyJVM(boolean fake) {
160
if (fake) {
161
try {
162
new FileOutputStream("fakeLeakyJVM" + LOG_SUFFIX, false);
163
} catch (FileNotFoundException e) {
164
}
165
}
166
}
167
168
static Stream<String> run(String... args){
169
try {
170
return new BufferedReader(new InputStreamReader(new ProcessBuilder(args).start().getInputStream())).lines();
171
} catch (IOException e) {
172
throw new RuntimeException(e);
173
}
174
}
175
176
static Optional<String[]> lsofCommandCache = stream(new String[][]{
177
{"/usr/bin/lsof", "-p"},
178
{"/usr/sbin/lsof", "-p"},
179
{"/bin/lsof", "-p"},
180
{"/sbin/lsof", "-p"},
181
{"/usr/local/bin/lsof", "-p"}})
182
.filter(args -> new File(args[0]).exists())
183
.findFirst();
184
185
static Optional<String[]> lsofCommand() {
186
return lsofCommandCache;
187
}
188
189
static Collection<String> outputContainingFilenames() {
190
long pid = ProcessHandle.current().pid();
191
String[] command = lsofCommand().orElseThrow(() -> new RuntimeException("lsof like command not found"));
192
// Only search the directory in which the VM is running (user.dir property).
193
System.out.println("using command: " + command[0] + " -a +d " + USER_DIR + " " + command[1] + " " + pid);
194
return run(command[0], "-a", "+d", USER_DIR, command[1], "" + pid).collect(toList());
195
}
196
197
static boolean findOpenLogFile(Collection<String> fileNames) {
198
String pid = Long.toString(ProcessHandle.current().pid());
199
String[] command = lsofCommand().orElseThrow(() ->
200
new RuntimeException("lsof like command not found"));
201
String lsof = command[0];
202
boolean isBusybox = Platform.isBusybox(lsof);
203
return fileNames.stream()
204
// lsof from busybox does not support "-p" option
205
.filter(fileName -> !isBusybox || fileName.contains(pid))
206
.filter(fileName -> fileName.contains(LOG_SUFFIX))
207
.findAny()
208
.isPresent();
209
}
210
211
static void windows(File f, long parentPid) throws InterruptedException {
212
System.out.println("waiting for pid: " + parentPid);
213
ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join());
214
System.out.println("trying to rename file to the same name: " + f);
215
System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by printing "VM RESULT => RETAINS FD"
216
}
217
}
218
219
220