< prev index next >
open/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java
Print this page
*** 45,58 ****
// .java_pid<pid>. and .attach_pid<pid>. It is important that this
// location is the same for all processes, otherwise the tools
// will not be able to find all Hotspot processes.
// Any changes to this needs to be synchronized with HotSpot.
private static final String tmpdir = "/tmp";
!
! // The patch to the socket file created by the target VM
! String path;
!
/**
* Attaches to the target VM
*/
VirtualMachineImpl(AttachProvider provider, String vmid)
throws AttachNotSupportedException, IOException
--- 45,55 ----
// .java_pid<pid>. and .attach_pid<pid>. It is important that this
// location is the same for all processes, otherwise the tools
// will not be able to find all Hotspot processes.
// Any changes to this needs to be synchronized with HotSpot.
private static final String tmpdir = "/tmp";
! String socket_name;
/**
* Attaches to the target VM
*/
VirtualMachineImpl(AttachProvider provider, String vmid)
throws AttachNotSupportedException, IOException
*** 71,82 ****
int ns_pid = getNamespacePid(pid);
// Find the socket file. If not found then we attempt to start the
// attach mechanism in the target VM by sending it a QUIT signal.
// Then we attempt to find the socket file again.
! path = findSocketFile(pid, ns_pid);
! if (path == null) {
File f = createAttachFile(pid, ns_pid);
try {
sendQuitTo(pid);
// give the target VM time to start the attach mechanism
--- 68,80 ----
int ns_pid = getNamespacePid(pid);
// Find the socket file. If not found then we attempt to start the
// attach mechanism in the target VM by sending it a QUIT signal.
// Then we attempt to find the socket file again.
! File path = findSocketFile(pid, ns_pid);
! socket_name = path.getPath();
! if (!path.exists()) {
File f = createAttachFile(pid, ns_pid);
try {
sendQuitTo(pid);
// give the target VM time to start the attach mechanism
*** 88,140 ****
// Increase timeout on each attempt to reduce polling
delay += delay_step;
try {
Thread.sleep(delay);
} catch (InterruptedException x) { }
- path = findSocketFile(pid, ns_pid);
time_spend += delay;
! if (time_spend > timeout/2 && path == null) {
// Send QUIT again to give target VM the last chance to react
sendQuitTo(pid);
}
! } while (time_spend <= timeout && path == null);
! if (path == null) {
throw new AttachNotSupportedException(
String.format("Unable to open socket file %s: " +
"target process %d doesn't respond within %dms " +
! "or HotSpot VM not loaded", f.getPath(), pid, time_spend));
}
} finally {
f.delete();
}
}
// Check that the file owner/permission to avoid attaching to
// bogus process
! checkPermissions(path);
// Check that we can connect to the process
// - this ensures we throw the permission denied error now rather than
// later when we attempt to enqueue a command.
int s = socket();
try {
! connect(s, path);
} finally {
close(s);
}
}
/**
* Detach from the target VM
*/
public void detach() throws IOException {
- synchronized (this) {
- if (this.path != null) {
- this.path = null;
- }
- }
}
// protocol version
private final static String PROTOCOL_VERSION = "1";
--- 86,133 ----
// Increase timeout on each attempt to reduce polling
delay += delay_step;
try {
Thread.sleep(delay);
} catch (InterruptedException x) { }
time_spend += delay;
! if (time_spend > timeout/2 && !path.exists()) {
// Send QUIT again to give target VM the last chance to react
sendQuitTo(pid);
}
! } while (time_spend <= timeout && !path.exists());
! if (!path.exists()) {
throw new AttachNotSupportedException(
String.format("Unable to open socket file %s: " +
"target process %d doesn't respond within %dms " +
! "or HotSpot VM not loaded", socket_name, pid,
! time_spend));
}
} finally {
f.delete();
}
}
// Check that the file owner/permission to avoid attaching to
// bogus process
! checkPermissions(socket_name);
// Check that we can connect to the process
// - this ensures we throw the permission denied error now rather than
// later when we attempt to enqueue a command.
int s = socket();
try {
! connect(s, socket_name);
} finally {
close(s);
}
}
/**
* Detach from the target VM
*/
public void detach() throws IOException {
}
// protocol version
private final static String PROTOCOL_VERSION = "1";
*** 145,169 ****
* Execute the given command in the target VM.
*/
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
assert args.length <= 3; // includes null
- // did we detach?
- String p;
- synchronized (this) {
- if (this.path == null) {
- throw new IOException("Detached from target VM");
- }
- p = this.path;
- }
-
// create UNIX socket
int s = socket();
// connect to target VM
try {
! connect(s, p);
} catch (IOException x) {
close(s);
throw x;
}
--- 138,153 ----
* Execute the given command in the target VM.
*/
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
assert args.length <= 3; // includes null
// create UNIX socket
int s = socket();
// connect to target VM
try {
! connect(s, socket_name);
} catch (IOException x) {
close(s);
throw x;
}
*** 267,286 ****
VirtualMachineImpl.close(s);
}
}
// Return the socket file for the given process.
! private String findSocketFile(int pid, int ns_pid) {
// A process may not exist in the same mount namespace as the caller.
// Instead, attach relative to the target root filesystem as exposed by
// procfs regardless of namespaces.
String root = "/proc/" + pid + "/root/" + tmpdir;
! File f = new File(root, ".java_pid" + ns_pid);
! if (!f.exists()) {
! return null;
! }
! return f.getPath();
}
// On Solaris/Linux a simple handshake is used to start the attach mechanism
// if not already started. The client creates a .attach_pid<pid> file in the
// target VM's working directory (or temp directory), and the SIGQUIT handler
--- 251,266 ----
VirtualMachineImpl.close(s);
}
}
// Return the socket file for the given process.
! private File findSocketFile(int pid, int ns_pid) {
// A process may not exist in the same mount namespace as the caller.
// Instead, attach relative to the target root filesystem as exposed by
// procfs regardless of namespaces.
String root = "/proc/" + pid + "/root/" + tmpdir;
! return new File(root, ".java_pid" + ns_pid);
}
// On Solaris/Linux a simple handshake is used to start the attach mechanism
// if not already started. The client creates a .attach_pid<pid> file in the
// target VM's working directory (or temp directory), and the SIGQUIT handler
< prev index next >