Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java
67703 views
1
/*
2
* Copyright (c) 2008, 2021, 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
// Keep canonical version of File, to delete, in case target process ends and /proc link has gone:
75
File f = createAttachFile(pid).getCanonicalFile();
76
try {
77
sendQuitTo(pid);
78
79
// give the target VM time to start the attach mechanism
80
final int delay_step = 100;
81
final long timeout = attachTimeout();
82
long time_spend = 0;
83
long delay = 0;
84
do {
85
// Increase timeout on each attempt to reduce polling
86
delay += delay_step;
87
try {
88
Thread.sleep(delay);
89
} catch (InterruptedException x) { }
90
91
time_spend += delay;
92
if (time_spend > timeout/2 && !socket_file.exists()) {
93
// Send QUIT again to give target VM the last chance to react
94
sendQuitTo(pid);
95
}
96
} while (time_spend <= timeout && !socket_file.exists());
97
if (!socket_file.exists()) {
98
throw new AttachNotSupportedException(
99
String.format("Unable to open socket file %s: " +
100
"target process %d doesn't respond within %dms " +
101
"or HotSpot VM not loaded", socket_path, pid,
102
time_spend));
103
}
104
} finally {
105
f.delete();
106
}
107
}
108
109
// Check that the file owner/permission to avoid attaching to
110
// bogus process
111
checkPermissions(socket_path);
112
113
// Check that we can connect to the process
114
// - this ensures we throw the permission denied error now rather than
115
// later when we attempt to enqueue a command.
116
int s = socket();
117
try {
118
connect(s, socket_path);
119
} finally {
120
close(s);
121
}
122
}
123
124
/**
125
* Detach from the target VM
126
*/
127
public void detach() throws IOException {
128
synchronized (this) {
129
if (socket_path != null) {
130
socket_path = null;
131
}
132
}
133
}
134
135
// protocol version
136
private final static String PROTOCOL_VERSION = "1";
137
138
// known errors
139
private final static int ATTACH_ERROR_BADVERSION = 101;
140
141
/**
142
* Execute the given command in the target VM.
143
*/
144
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
145
assert args.length <= 3; // includes null
146
147
// did we detach?
148
synchronized (this) {
149
if (socket_path == null) {
150
throw new IOException("Detached from target VM");
151
}
152
}
153
154
// create UNIX socket
155
int s = socket();
156
157
// connect to target VM
158
try {
159
connect(s, socket_path);
160
} catch (IOException x) {
161
close(s);
162
throw x;
163
}
164
165
IOException ioe = null;
166
167
// connected - write request
168
// <ver> <cmd> <args...>
169
try {
170
writeString(s, PROTOCOL_VERSION);
171
writeString(s, cmd);
172
173
for (int i = 0; i < 3; i++) {
174
if (i < args.length && args[i] != null) {
175
writeString(s, (String)args[i]);
176
} else {
177
writeString(s, "");
178
}
179
}
180
} catch (IOException x) {
181
ioe = x;
182
}
183
184
185
// Create an input stream to read reply
186
SocketInputStream sis = new SocketInputStream(s);
187
188
// Read the command completion status
189
int completionStatus;
190
try {
191
completionStatus = readInt(sis);
192
} catch (IOException x) {
193
sis.close();
194
if (ioe != null) {
195
throw ioe;
196
} else {
197
throw x;
198
}
199
}
200
201
if (completionStatus != 0) {
202
// read from the stream and use that as the error message
203
String message = readErrorMessage(sis);
204
sis.close();
205
206
// In the event of a protocol mismatch then the target VM
207
// returns a known error so that we can throw a reasonable
208
// error.
209
if (completionStatus == ATTACH_ERROR_BADVERSION) {
210
throw new IOException("Protocol mismatch with target VM");
211
}
212
213
// Special-case the "load" command so that the right exception is
214
// thrown.
215
if (cmd.equals("load")) {
216
String msg = "Failed to load agent library";
217
if (!message.isEmpty())
218
msg += ": " + message;
219
throw new AgentLoadException(msg);
220
} else {
221
if (message.isEmpty())
222
message = "Command failed in target VM";
223
throw new AttachOperationFailedException(message);
224
}
225
}
226
227
// Return the input stream so that the command output can be read
228
return sis;
229
}
230
231
/*
232
* InputStream for the socket connection to get target VM
233
*/
234
private class SocketInputStream extends InputStream {
235
int s;
236
237
public SocketInputStream(int s) {
238
this.s = s;
239
}
240
241
public synchronized int read() throws IOException {
242
byte b[] = new byte[1];
243
int n = this.read(b, 0, 1);
244
if (n == 1) {
245
return b[0] & 0xff;
246
} else {
247
return -1;
248
}
249
}
250
251
public synchronized int read(byte[] bs, int off, int len) throws IOException {
252
if ((off < 0) || (off > bs.length) || (len < 0) ||
253
((off + len) > bs.length) || ((off + len) < 0)) {
254
throw new IndexOutOfBoundsException();
255
} else if (len == 0)
256
return 0;
257
258
return VirtualMachineImpl.read(s, bs, off, len);
259
}
260
261
public synchronized void close() throws IOException {
262
if (s != -1) {
263
int toClose = s;
264
s = -1;
265
VirtualMachineImpl.close(toClose);
266
}
267
}
268
}
269
270
// On Aix a simple handshake is used to start the attach mechanism
271
// if not already started. The client creates a .attach_pid<pid> file in the
272
// target VM's working directory (or temp directory), and the SIGQUIT handler
273
// checks for the file.
274
private File createAttachFile(int pid) throws IOException {
275
String fn = ".attach_pid" + pid;
276
String path = "/proc/" + pid + "/cwd/" + fn;
277
File f = new File(path);
278
try {
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