author | tschatzl |
Thu, 07 Jun 2018 11:20:18 +0200 | |
changeset 50440 | cfdd37095f66 |
parent 49742 | 1196aa0be8be |
child 52817 | 981eb3c1b90d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49742
1196aa0be8be
8201247: Various cleanups in the attach framework
clanger
parents:
48485
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package sun.tools.attach; |
|
26 |
||
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
27 |
import com.sun.tools.attach.AttachOperationFailedException; |
2 | 28 |
import com.sun.tools.attach.AgentLoadException; |
29 |
import com.sun.tools.attach.AttachNotSupportedException; |
|
30 |
import com.sun.tools.attach.spi.AttachProvider; |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
31 |
|
2 | 32 |
import java.io.InputStream; |
33 |
import java.io.IOException; |
|
34 |
import java.io.File; |
|
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
35 |
import java.nio.charset.StandardCharsets; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
36 |
import java.nio.file.Path; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
37 |
import java.nio.file.Paths; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
38 |
import java.nio.file.Files; |
2 | 39 |
|
40 |
/* |
|
41 |
* Linux implementation of HotSpotVirtualMachine |
|
42 |
*/ |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
43 |
public class VirtualMachineImpl extends HotSpotVirtualMachine { |
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
44 |
// "/tmp" is used as a global well-known location for the files |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
45 |
// .java_pid<pid>. and .attach_pid<pid>. It is important that this |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
46 |
// location is the same for all processes, otherwise the tools |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
47 |
// will not be able to find all Hotspot processes. |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
48 |
// Any changes to this needs to be synchronized with HotSpot. |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
49 |
private static final String tmpdir = "/tmp"; |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
50 |
String socket_path; |
2 | 51 |
/** |
52 |
* Attaches to the target VM |
|
53 |
*/ |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
54 |
VirtualMachineImpl(AttachProvider provider, String vmid) |
2 | 55 |
throws AttachNotSupportedException, IOException |
56 |
{ |
|
57 |
super(provider, vmid); |
|
58 |
||
59 |
// This provider only understands pids |
|
60 |
int pid; |
|
61 |
try { |
|
62 |
pid = Integer.parseInt(vmid); |
|
63 |
} catch (NumberFormatException x) { |
|
64 |
throw new AttachNotSupportedException("Invalid process identifier"); |
|
65 |
} |
|
66 |
||
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
67 |
// Try to resolve to the "inner most" pid namespace |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
68 |
int ns_pid = getNamespacePid(pid); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
69 |
|
2 | 70 |
// Find the socket file. If not found then we attempt to start the |
71 |
// attach mechanism in the target VM by sending it a QUIT signal. |
|
72 |
// Then we attempt to find the socket file again. |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
73 |
File socket_file = findSocketFile(pid, ns_pid); |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
74 |
socket_path = socket_file.getPath(); |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
75 |
if (!socket_file.exists()) { |
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
76 |
File f = createAttachFile(pid, ns_pid); |
2 | 77 |
try { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
78 |
sendQuitTo(pid); |
2 | 79 |
|
80 |
// give the target VM time to start the attach mechanism |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
81 |
final int delay_step = 100; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
82 |
final long timeout = attachTimeout(); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
83 |
long time_spend = 0; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
84 |
long delay = 0; |
2 | 85 |
do { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
86 |
// Increase timeout on each attempt to reduce polling |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
87 |
delay += delay_step; |
2 | 88 |
try { |
89 |
Thread.sleep(delay); |
|
90 |
} catch (InterruptedException x) { } |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
91 |
|
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
92 |
time_spend += delay; |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
93 |
if (time_spend > timeout/2 && !socket_file.exists()) { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
94 |
// Send QUIT again to give target VM the last chance to react |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
95 |
sendQuitTo(pid); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
96 |
} |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
97 |
} while (time_spend <= timeout && !socket_file.exists()); |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
98 |
if (!socket_file.exists()) { |
2 | 99 |
throw new AttachNotSupportedException( |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
100 |
String.format("Unable to open socket file %s: " + |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
101 |
"target process %d doesn't respond within %dms " + |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
102 |
"or HotSpot VM not loaded", socket_path, pid, |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
103 |
time_spend)); |
2 | 104 |
} |
105 |
} finally { |
|
106 |
f.delete(); |
|
107 |
} |
|
49742
1196aa0be8be
8201247: Various cleanups in the attach framework
clanger
parents:
48485
diff
changeset
|
108 |
} |
2 | 109 |
|
110 |
// Check that the file owner/permission to avoid attaching to |
|
111 |
// bogus process |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
112 |
checkPermissions(socket_path); |
2 | 113 |
|
114 |
// Check that we can connect to the process |
|
115 |
// - this ensures we throw the permission denied error now rather than |
|
116 |
// later when we attempt to enqueue a command. |
|
117 |
int s = socket(); |
|
118 |
try { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
119 |
connect(s, socket_path); |
2 | 120 |
} finally { |
121 |
close(s); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Detach from the target VM |
|
127 |
*/ |
|
128 |
public void detach() throws IOException { |
|
129 |
synchronized (this) { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
130 |
if (socket_path != null) { |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
131 |
socket_path = null; |
2 | 132 |
} |
133 |
} |
|
134 |
} |
|
135 |
||
136 |
// protocol version |
|
137 |
private final static String PROTOCOL_VERSION = "1"; |
|
138 |
||
139 |
// known errors |
|
140 |
private final static int ATTACH_ERROR_BADVERSION = 101; |
|
141 |
||
142 |
/** |
|
143 |
* Execute the given command in the target VM. |
|
144 |
*/ |
|
145 |
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException { |
|
146 |
assert args.length <= 3; // includes null |
|
147 |
||
148 |
// did we detach? |
|
149 |
synchronized (this) { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
150 |
if (socket_path == null) { |
2 | 151 |
throw new IOException("Detached from target VM"); |
152 |
} |
|
153 |
} |
|
154 |
||
155 |
// create UNIX socket |
|
156 |
int s = socket(); |
|
157 |
||
158 |
// connect to target VM |
|
159 |
try { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
160 |
connect(s, socket_path); |
2 | 161 |
} catch (IOException x) { |
162 |
close(s); |
|
163 |
throw x; |
|
164 |
} |
|
165 |
||
166 |
IOException ioe = null; |
|
167 |
||
168 |
// connected - write request |
|
169 |
// <ver> <cmd> <args...> |
|
170 |
try { |
|
171 |
writeString(s, PROTOCOL_VERSION); |
|
172 |
writeString(s, cmd); |
|
173 |
||
174 |
for (int i=0; i<3; i++) { |
|
175 |
if (i < args.length && args[i] != null) { |
|
176 |
writeString(s, (String)args[i]); |
|
177 |
} else { |
|
178 |
writeString(s, ""); |
|
179 |
} |
|
180 |
} |
|
181 |
} catch (IOException x) { |
|
182 |
ioe = x; |
|
183 |
} |
|
184 |
||
185 |
||
186 |
// Create an input stream to read reply |
|
187 |
SocketInputStream sis = new SocketInputStream(s); |
|
188 |
||
189 |
// Read the command completion status |
|
190 |
int completionStatus; |
|
191 |
try { |
|
192 |
completionStatus = readInt(sis); |
|
193 |
} catch (IOException x) { |
|
194 |
sis.close(); |
|
195 |
if (ioe != null) { |
|
196 |
throw ioe; |
|
197 |
} else { |
|
198 |
throw x; |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
if (completionStatus != 0) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
203 |
// read from the stream and use that as the error message |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
204 |
String message = readErrorMessage(sis); |
2 | 205 |
sis.close(); |
206 |
||
207 |
// In the event of a protocol mismatch then the target VM |
|
208 |
// returns a known error so that we can throw a reasonable |
|
209 |
// error. |
|
210 |
if (completionStatus == ATTACH_ERROR_BADVERSION) { |
|
211 |
throw new IOException("Protocol mismatch with target VM"); |
|
212 |
} |
|
213 |
||
214 |
// Special-case the "load" command so that the right exception is |
|
215 |
// thrown. |
|
216 |
if (cmd.equals("load")) { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
217 |
String msg = "Failed to load agent library"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
218 |
if (!message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
219 |
msg += ": " + message; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
220 |
throw new AgentLoadException(msg); |
2 | 221 |
} else { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
222 |
if (message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
223 |
message = "Command failed in target VM"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
224 |
throw new AttachOperationFailedException(message); |
2 | 225 |
} |
226 |
} |
|
227 |
||
228 |
// Return the input stream so that the command output can be read |
|
229 |
return sis; |
|
230 |
} |
|
231 |
||
232 |
/* |
|
233 |
* InputStream for the socket connection to get target VM |
|
234 |
*/ |
|
235 |
private class SocketInputStream extends InputStream { |
|
236 |
int s; |
|
237 |
||
238 |
public SocketInputStream(int s) { |
|
239 |
this.s = s; |
|
240 |
} |
|
241 |
||
242 |
public synchronized int read() throws IOException { |
|
243 |
byte b[] = new byte[1]; |
|
244 |
int n = this.read(b, 0, 1); |
|
245 |
if (n == 1) { |
|
246 |
return b[0] & 0xff; |
|
247 |
} else { |
|
248 |
return -1; |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
253 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
254 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
255 |
throw new IndexOutOfBoundsException(); |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
256 |
} else if (len == 0) { |
2 | 257 |
return 0; |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
258 |
} |
2 | 259 |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
260 |
return VirtualMachineImpl.read(s, bs, off, len); |
2 | 261 |
} |
262 |
||
263 |
public void close() throws IOException { |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
264 |
VirtualMachineImpl.close(s); |
2 | 265 |
} |
266 |
} |
|
267 |
||
268 |
// Return the socket file for the given process. |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
269 |
private File findSocketFile(int pid, int ns_pid) { |
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
270 |
// A process may not exist in the same mount namespace as the caller. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
271 |
// Instead, attach relative to the target root filesystem as exposed by |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
272 |
// procfs regardless of namespaces. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
273 |
String root = "/proc/" + pid + "/root/" + tmpdir; |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47540
diff
changeset
|
274 |
return new File(root, ".java_pid" + ns_pid); |
2 | 275 |
} |
276 |
||
49742
1196aa0be8be
8201247: Various cleanups in the attach framework
clanger
parents:
48485
diff
changeset
|
277 |
// On Linux a simple handshake is used to start the attach mechanism |
2 | 278 |
// if not already started. The client creates a .attach_pid<pid> file in the |
5771
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
279 |
// target VM's working directory (or temp directory), and the SIGQUIT handler |
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
280 |
// checks for the file. |
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
281 |
private File createAttachFile(int pid, int ns_pid) throws IOException { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
282 |
String fn = ".attach_pid" + ns_pid; |
2 | 283 |
String path = "/proc/" + pid + "/cwd/" + fn; |
284 |
File f = new File(path); |
|
285 |
try { |
|
286 |
f.createNewFile(); |
|
287 |
} catch (IOException x) { |
|
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
288 |
String root; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
289 |
if (pid != ns_pid) { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
290 |
// A process may not exist in the same mount namespace as the caller. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
291 |
// Instead, attach relative to the target root filesystem as exposed by |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
292 |
// procfs regardless of namespaces. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
293 |
root = "/proc/" + pid + "/root/" + tmpdir; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
294 |
} else { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
295 |
root = tmpdir; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
296 |
} |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
297 |
f = new File(root, fn); |
2 | 298 |
f.createNewFile(); |
299 |
} |
|
300 |
return f; |
|
301 |
} |
|
302 |
||
303 |
/* |
|
304 |
* Write/sends the given to the target VM. String is transmitted in |
|
305 |
* UTF-8 encoding. |
|
306 |
*/ |
|
307 |
private void writeString(int fd, String s) throws IOException { |
|
308 |
if (s.length() > 0) { |
|
309 |
byte b[]; |
|
310 |
try { |
|
311 |
b = s.getBytes("UTF-8"); |
|
312 |
} catch (java.io.UnsupportedEncodingException x) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
7668
diff
changeset
|
313 |
throw new InternalError(x); |
2 | 314 |
} |
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
315 |
VirtualMachineImpl.write(fd, b, 0, b.length); |
2 | 316 |
} |
317 |
byte b[] = new byte[1]; |
|
318 |
b[0] = 0; |
|
319 |
write(fd, b, 0, 1); |
|
320 |
} |
|
321 |
||
322 |
||
47540
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
323 |
// Return the inner most namespaced PID if there is one, |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
324 |
// otherwise return the original PID. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
325 |
private int getNamespacePid(int pid) throws AttachNotSupportedException, IOException { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
326 |
// Assuming a real procfs sits beneath, reading this doesn't block |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
327 |
// nor will it consume a lot of memory. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
328 |
String statusFile = "/proc/" + pid + "/status"; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
329 |
File f = new File(statusFile); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
330 |
if (!f.exists()) { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
331 |
return pid; // Likely a bad pid, but this is properly handled later. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
332 |
} |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
333 |
|
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
334 |
Path statusPath = Paths.get(statusFile); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
335 |
|
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
336 |
try { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
337 |
for (String line : Files.readAllLines(statusPath, StandardCharsets.UTF_8)) { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
338 |
String[] parts = line.split(":"); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
339 |
if (parts.length == 2 && parts[0].trim().equals("NSpid")) { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
340 |
parts = parts[1].trim().split("\\s+"); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
341 |
// The last entry represents the PID the JVM "thinks" it is. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
342 |
// Even in non-namespaced pids these entries should be |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
343 |
// valid. You could refer to it as the inner most pid. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
344 |
int ns_pid = Integer.parseInt(parts[parts.length - 1]); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
345 |
return ns_pid; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
346 |
} |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
347 |
} |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
348 |
// Old kernels may not have NSpid field (i.e. 3.10). |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
349 |
// Fallback to original pid in the event we cannot deduce. |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
350 |
return pid; |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
351 |
} catch (NumberFormatException | IOException x) { |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
352 |
throw new AttachNotSupportedException("Unable to parse namespace"); |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
353 |
} |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
354 |
} |
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
355 |
|
7242ab15b6a5
8179498: attach in linux should be relative to /proc/pid/root and namespace aware
cjplummer
parents:
47216
diff
changeset
|
356 |
|
2 | 357 |
//-- native methods |
358 |
||
359 |
static native void sendQuitTo(int pid) throws IOException; |
|
360 |
||
361 |
static native void checkPermissions(String path) throws IOException; |
|
362 |
||
363 |
static native int socket() throws IOException; |
|
364 |
||
365 |
static native void connect(int fd, String path) throws IOException; |
|
366 |
||
367 |
static native void close(int fd) throws IOException; |
|
368 |
||
369 |
static native int read(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
370 |
||
371 |
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
372 |
||
373 |
static { |
|
374 |
System.loadLibrary("attach"); |
|
375 |
} |
|
376 |
} |