Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/serviceability/attach/AttachWithStalePidFile.java
64476 views
1
/*
2
* Copyright (c) 2013, 2021, 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
/*
25
* @test
26
* @bug 7162400
27
* @summary Regression test for attach issue where stale pid files in /tmp lead to connection issues
28
* @requires os.family != "windows"
29
* @requires vm.flagless
30
* @modules java.base/jdk.internal.misc:open
31
* @modules java.base/java.lang:open
32
* @modules jdk.attach/sun.tools.attach
33
* @library /test/lib
34
* @run driver AttachWithStalePidFile
35
*/
36
37
import jdk.test.lib.Platform;
38
import jdk.test.lib.process.ProcessTools;
39
import com.sun.tools.attach.VirtualMachine;
40
import sun.tools.attach.HotSpotVirtualMachine;
41
import java.lang.reflect.Field;
42
import java.nio.file.*;
43
import java.nio.file.attribute.*;
44
import java.io.*;
45
46
public class AttachWithStalePidFile {
47
public static void main(String... args) throws Exception {
48
49
// Since there might be stale pid-files owned by different
50
// users on the system we may need to retry the test in case we
51
// are unable to remove the existing file.
52
int retries = 5;
53
while(!runTest() && --retries > 0);
54
55
if(retries == 0) {
56
throw new RuntimeException("Test failed after 5 retries. " +
57
"Remove any /tmp/.java_pid* files and retry.");
58
}
59
}
60
61
public static boolean runTest() throws Exception {
62
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
63
"-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
64
Process target = pb.start();
65
Path pidFile = null;
66
67
try {
68
int pid = getUnixProcessId(target);
69
70
// create the stale .java_pid file. use hard-coded /tmp path as in th VM
71
pidFile = createJavaPidFile(pid);
72
if(pidFile == null) {
73
return false;
74
}
75
76
// wait for vm.paused file to be created and delete it once we find it.
77
waitForAndResumeVM(pid);
78
79
waitForTargetReady(target);
80
81
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
82
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
83
String line = null;
84
while((line = remoteDataReader.readLine()) != null);
85
86
vm.detach();
87
return true;
88
}
89
finally {
90
target.destroy();
91
target.waitFor();
92
93
if(pidFile != null && Files.exists(pidFile)) {
94
Files.delete(pidFile);
95
}
96
}
97
}
98
99
private static void waitForTargetReady(Process target) throws IOException {
100
BufferedReader br = new BufferedReader(new InputStreamReader(target.getInputStream()));
101
String line = br.readLine();
102
// wait for the ready message having been printed or EOF (line == null)
103
while (line != null && !line.equals(AttachWithStalePidFileTarget.READY_MSG)) {
104
line = br.readLine();
105
}
106
// target VM ready
107
}
108
109
private static Path createJavaPidFile(int pid) throws Exception {
110
Path pidFile = Paths.get("/tmp/.java_pid" + pid);
111
if(Files.exists(pidFile)) {
112
try {
113
Files.delete(pidFile);
114
}
115
catch(FileSystemException e) {
116
if(e.getReason().matches("Operation not permitted|Not owner")) {
117
System.out.println("Unable to remove exisiting stale PID file" + pidFile);
118
System.out.println("===================================================");
119
e.printStackTrace(System.out);
120
return null;
121
}
122
throw e;
123
}
124
}
125
return Files.createFile(pidFile,
126
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
127
}
128
129
private static void waitForAndResumeVM(int pid) throws Exception {
130
Path pauseFile = Paths.get("vm.paused." + pid);
131
int retries = 60;
132
while(!Files.exists(pauseFile) && --retries > 0) {
133
Thread.sleep(1000);
134
}
135
if(retries == 0) {
136
throw new RuntimeException("Timeout waiting for VM to start. " +
137
"vm.paused file not created within 60 seconds.");
138
}
139
Files.delete(pauseFile);
140
}
141
142
private static int getUnixProcessId(Process unixProcess) throws Exception {
143
Field pidField = unixProcess.getClass().getDeclaredField("pid");
144
pidField.setAccessible(true);
145
return (Integer)pidField.get(unixProcess);
146
}
147
}
148
149