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