@@ -82,8 +82,10 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
8282 // Keep canonical version of File, to delete, in case target process ends and /proc link has gone:
8383 File f = createAttachFile (pid , ns_pid ).getCanonicalFile ();
8484
85+ boolean sentQuit = false ;
86+
8587 try {
86- checkCatchesAndSendQuitTo (pid , false );
88+ sentQuit = checkCatchesAndSendQuitTo (pid , false );
8789
8890 // give the target VM time to start the attach mechanism
8991 final int delay_step = 100 ;
@@ -100,14 +102,15 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
100102 time_spend += delay ;
101103 if (time_spend > timeout /2 && !socket_file .exists ()) {
102104 // Send QUIT again to give target VM the last chance to react
103- checkCatchesAndSendQuitTo (pid , false );
105+ sentQuit = checkCatchesAndSendQuitTo (pid , false );
104106 }
105107 } while (time_spend <= timeout && !socket_file .exists ());
108+
106109 if (!socket_file .exists ()) {
107110 throw new AttachNotSupportedException (
108111 String .format ("Unable to open socket file %s: " +
109- "target process %d doesn't respond within %dms " +
110- "or HotSpot VM not loaded" , socket_path , pid ,
112+ "target process %d doesn't %s within %dms " +
113+ "or HotSpot VM not loaded" , socket_path , pid , sentQuit ? "respond" : "become ready" ,
111114 time_spend ));
112115 }
113116 } finally {
@@ -253,16 +256,16 @@ private String findTargetProcessTmpDirectory(long pid, long ns_pid) throws Attac
253256 final var procPidRoot = PROC .resolve (Long .toString (pid )).resolve (ROOT_TMP );
254257
255258 /*
256- * if target is elevated, we cant use /proc/<pid>/... so we have to fallback to /tmp, but that may not be shared
259+ * if target is elevated, we cant use /proc/<pid>/... so we have to fallback to /tmp, but that may not be shared
257260 * with the target/attachee process, we can try, except in the case where the ns_pid also exists in this pid ns
258- * which is ambiguous, if we share /tmp with the intended target, the attach will succeed, if we do not,
261+ * which is ambiguous, if we share /tmp with the intended target, the attach will succeed, if we do not,
259262 * then we will potentially attempt to attach to some arbitrary process with the same pid (in this pid ns)
260263 * as that of the intended target (in its * pid ns).
261264 *
262265 * so in that case we should prehaps throw - or risk sending SIGQUIT to some arbitrary process... which could kill it
263266 *
264267 * however we can also check the target pid's signal masks to see if it catches SIGQUIT and only do so if in
265- * fact it does ... this reduces the risk of killing an innocent process in the current ns as opposed to
268+ * fact it does ... this reduces the risk of killing an innocent process in the current ns as opposed to
266269 * attaching to the actual target JVM ... c.f: checkCatchesAndSendQuitTo() below.
267270 *
268271 * note that if pid == ns_pid we are in a shared pid ns with the target and may (potentially) share /tmp
@@ -377,7 +380,7 @@ private static boolean checkCatchesAndSendQuitTo(int pid, boolean throwIfNotRead
377380 cmd = cmdline .get ();
378381 cmd = cmd .substring (0 , cmd .length () - 1 ); // remove trailing \0
379382 }
380-
383+
381384 throw new AttachNotSupportedException ("pid: " + pid + " cmd: '" + cmd + "' state is not ready to participate in attach handshake!" );
382385 }
383386
0 commit comments