Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java
40983 views
1
/*
2
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2015, 2019 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation. Oracle designates this
9
* particular file as subject to the "Classpath" exception as provided
10
* by Oracle in the LICENSE file that accompanied this code.
11
*
12
* This code is distributed in the hope that it will be useful, but WITHOUT
13
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15
* version 2 for more details (a copy is included in the LICENSE file that
16
* accompanied this code).
17
*
18
* You should have received a copy of the GNU General Public License version
19
* 2 along with this work; if not, write to the Free Software Foundation,
20
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
*
22
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23
* or visit www.oracle.com if you need additional information or have any
24
* questions.
25
*/
26
package sun.tools.attach;
27
28
import com.sun.tools.attach.AttachOperationFailedException;
29
import com.sun.tools.attach.AgentLoadException;
30
import com.sun.tools.attach.AttachNotSupportedException;
31
import com.sun.tools.attach.spi.AttachProvider;
32
33
import java.io.InputStream;
34
import java.io.IOException;
35
import java.io.File;
36
37
/*
38
* Aix implementation of HotSpotVirtualMachine
39
*/
40
public class VirtualMachineImpl extends HotSpotVirtualMachine {
41
// "/tmp" is used as a global well-known location for the files
42
// .java_pid<pid>. and .attach_pid<pid>. It is important that this
43
// location is the same for all processes, otherwise the tools
44
// will not be able to find all Hotspot processes.
45
// Any changes to this needs to be synchronized with HotSpot.
46
private static final String tmpdir = "/tmp";
47
String socket_path;
48
49
/**
50
* Attaches to the target VM
51
*/
52
VirtualMachineImpl(AttachProvider provider, String vmid)
53
throws AttachNotSupportedException, IOException
54
{
55
super(provider, vmid);
56
57
// This provider only understands pids
58
int pid;
59
try {
60
pid = Integer.parseInt(vmid);
61
if (pid < 1) {
62
throw new NumberFormatException();
63
}
64
} catch (NumberFormatException x) {
65
throw new AttachNotSupportedException("Invalid process identifier: " + vmid);
66
}
67
68
// Find the socket file. If not found then we attempt to start the
69
// attach mechanism in the target VM by sending it a QUIT signal.
70
// Then we attempt to find the socket file again.
71
File socket_file = new File(tmpdir, ".java_pid" + pid);
72
socket_path = socket_file.getPath();
73
if (!socket_file.exists()) {
74
File f = createAttachFile(pid);
75
try {
76
sendQuitTo(pid);
77
78
// give the target VM time to start the attach mechanism
79
final int delay_step = 100;
80
final long timeout = attachTimeout();
81
long time_spend = 0;
82
long delay = 0;
83
do {
84
// Increase timeout on each attempt to reduce polling
85
delay += delay_step;
86
try {
87
Thread.sleep(delay);
88
} catch (InterruptedException x) { }
89
90
time_spend += delay;
91
if (time_spend > timeout/2 && !socket_file.exists()) {
92
// Send QUIT again to give target VM the last chance to react
93
sendQuitTo(pid);
94
}
95
} while (time_spend <= timeout && !socket_file.exists());
96
if (!socket_file.exists()) {
97
throw new AttachNotSupportedException(
98
String.format("Unable to open socket file %s: " +
99
"target process %d doesn't respond within %dms " +
100
"or HotSpot VM not loaded", socket_path, pid,
101
time_spend));
102
}
103
} finally {
104
f.delete();
105
}
106
}
107
108
// Check that the file owner/permission to avoid attaching to
109
// bogus process
110
checkPermissions(socket_path);
111
112
// Check that we can connect to the process
113
// - this ensures we throw the permission denied error now rather than
114
// later when we attempt to enqueue a command.
115
int s = socket();
116
try {
117
connect(s, socket_path);
118
} finally {
119
close(s);
120
}
121
}
122
123
/**
124
* Detach from the target VM
125
*/
126
public void detach() throws IOException {
127
synchronized (this) {
128
if (socket_path != null) {
129
socket_path = null;
130
}
131
}
132
}
133
134
// protocol version
135
private final static String PROTOCOL_VERSION = "1";
136
137
// known errors
138
private final static int ATTACH_ERROR_BADVERSION = 101;
139
140
/**
141
* Execute the given command in the target VM.
142
*/
143
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
144
assert args.length <= 3; // includes null
145
146
// did we detach?
147
synchronized (this) {
148
if (socket_path == null) {
149
throw new IOException("Detached from target VM");
150
}
151
}
152
153
// create UNIX socket
154
int s = socket();
155
156
// connect to target VM
157
try {
158
connect(s, socket_path);
159
} catch (IOException x) {
160
close(s);
161
throw x;
162
}
163
164
IOException ioe = null;
165
166
// connected - write request
167
// <ver> <cmd> <args...>
168
try {
169
writeString(s, PROTOCOL_VERSION);
170
writeString(s, cmd);
171
172
for (int i = 0; i < 3; i++) {
173
if (i < args.length && args[i] != null) {
174
writeString(s, (String)args[i]);
175
} else {
176
writeString(s, "");
177
}
178
}
179
} catch (IOException x) {
180
ioe = x;
181
}
182
183
184
// Create an input stream to read reply
185
SocketInputStream sis = new SocketInputStream(s);
186
187
// Read the command completion status
188
int completionStatus;
189
try {
190
completionStatus = readInt(sis);
191
} catch (IOException x) {
192
sis.close();
193
if (ioe != null) {
194
throw ioe;
195
} else {
196
throw x;
197
}
198
}
199
200
if (completionStatus != 0) {
201
// read from the stream and use that as the error message
202
String message = readErrorMessage(sis);
203
sis.close();
204
205
// In the event of a protocol mismatch then the target VM
206
// returns a known error so that we can throw a reasonable
207
// error.
208
if (completionStatus == ATTACH_ERROR_BADVERSION) {
209
throw new IOException("Protocol mismatch with target VM");
210
}
211
212
// Special-case the "load" command so that the right exception is
213
// thrown.
214
if (cmd.equals("load")) {
215
String msg = "Failed to load agent library";
216
if (!message.isEmpty())
217
msg += ": " + message;
218
throw new AgentLoadException(msg);
219
} else {
220
if (message.isEmpty())
221
message = "Command failed in target VM";
222
throw new AttachOperationFailedException(message);
223
}
224
}
225
226
// Return the input stream so that the command output can be read
227
return sis;
228
}
229
230
/*
231
* InputStream for the socket connection to get target VM
232
*/
233
private class SocketInputStream extends InputStream {
234
int s;
235
236
public SocketInputStream(int s) {
237
this.s = s;
238
}
239
240
public synchronized int read() throws IOException {
241
byte b[] = new byte[1];
242
int n = this.read(b, 0, 1);
243
if (n == 1) {
244
return b[0] & 0xff;
245
} else {
246
return -1;
247
}
248
}
249
250
public synchronized int read(byte[] bs, int off, int len) throws IOException {
251
if ((off < 0) || (off > bs.length) || (len < 0) ||
252
((off + len) > bs.length) || ((off + len) < 0)) {
253
throw new IndexOutOfBoundsException();
254
} else if (len == 0)
255
return 0;
256
257
return VirtualMachineImpl.read(s, bs, off, len);
258
}
259
260
public synchronized void close() throws IOException {
261
if (s != -1) {
262
int toClose = s;
263
s = -1;
264
VirtualMachineImpl.close(toClose);
265
}
266
}
267
}
268
269
// On Aix a simple handshake is used to start the attach mechanism
270
// if not already started. The client creates a .attach_pid<pid> file in the
271
// target VM's working directory (or temp directory), and the SIGQUIT handler
272
// checks for the file.
273
private File createAttachFile(int pid) throws IOException {
274
String fn = ".attach_pid" + pid;
275
String path = "/proc/" + pid + "/cwd/" + fn;
276
File f = new File(path);
277
try {
278
f = f.getCanonicalFile();
279
f.createNewFile();
280
} catch (IOException x) {
281
f = new File(tmpdir, fn);
282
f.createNewFile();
283
}
284
return f;
285
}
286
287
/*
288
* Write/sends the given to the target VM. String is transmitted in
289
* UTF-8 encoding.
290
*/
291
private void writeString(int fd, String s) throws IOException {
292
if (s.length() > 0) {
293
byte b[];
294
try {
295
b = s.getBytes("UTF-8");
296
} catch (java.io.UnsupportedEncodingException x) {
297
throw new InternalError(x);
298
}
299
VirtualMachineImpl.write(fd, b, 0, b.length);
300
}
301
byte b[] = new byte[1];
302
b[0] = 0;
303
write(fd, b, 0, 1);
304
}
305
306
307
//-- native methods
308
309
static native void sendQuitTo(int pid) throws IOException;
310
311
static native void checkPermissions(String path) throws IOException;
312
313
static native int socket() throws IOException;
314
315
static native void connect(int fd, String path) throws IOException;
316
317
static native void close(int fd) throws IOException;
318
319
static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;
320
321
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
322
323
static {
324
System.loadLibrary("attach");
325
}
326
}
327
328