|
28 | 28 | #include <sys/socket.h>
|
29 | 29 | #include <sys/stat.h>
|
30 | 30 | #include <sys/syslimits.h>
|
| 31 | +#include <sys/sysctl.h> |
31 | 32 | #include <sys/types.h>
|
32 | 33 | #include <sys/un.h>
|
33 | 34 | #include <errno.h>
|
34 | 35 | #include <fcntl.h>
|
35 | 36 | #include <signal.h>
|
| 37 | +#include <stdbool.h> |
36 | 38 | #include <stdio.h>
|
37 | 39 | #include <stdlib.h>
|
38 | 40 | #include <string.h>
|
@@ -110,15 +112,54 @@ JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_connect
|
110 | 112 |
|
111 | 113 | /*
|
112 | 114 | * Class: sun_tools_attach_VirtualMachineImpl
|
113 |
| - * Method: sendQuitTo |
| 115 | + * Method: checkCatchesAndSendQuitTo |
114 | 116 | * Signature: (I)V
|
115 | 117 | */
|
116 |
| -JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_sendQuitTo |
117 |
| - (JNIEnv *env, jclass cls, jint pid) |
| 118 | +JNIEXPORT jboolean JNICALL Java_sun_tools_attach_VirtualMachineImpl_checkCatchesAndSendQuitTo |
| 119 | + (JNIEnv *env, jclass cls, jint pid, jboolean throwIfNotReady) |
118 | 120 | {
|
119 |
| - if (kill((pid_t)pid, SIGQUIT)) { |
120 |
| - JNU_ThrowIOExceptionWithLastError(env, "kill"); |
| 121 | + int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, (int)pid }; |
| 122 | + |
| 123 | + struct kinfo_proc kiproc; |
| 124 | + size_t kipsz = sizeof(struct kinfo_proc); |
| 125 | + |
| 126 | + /* |
| 127 | + * Early in the lifetime of a JVM it has not yet initialized its signal handlers, in particular the QUIT |
| 128 | + * handler, note that the default behavior of QUIT is to terminate the receiving process, if unhandled. |
| 129 | + * |
| 130 | + * Since we use QUIT to initiate an attach operation, if we signal a JVM during this period early in its |
| 131 | + * lifetime before it has initialized its QUIT handler, such a signal delivery will terminate the JVM we |
| 132 | + * are attempting to attach to! |
| 133 | + * |
| 134 | + * The following code guards the QUIT delivery by testing the current signal masks. It is okay to send QUIT |
| 135 | + * if the signal is caught but not ignored, as that implies a handler has been installed. |
| 136 | + */ |
| 137 | + |
| 138 | + if (sysctl(mib, sizeof(mib) / sizeof(int), &kiproc, &kipsz, NULL, 0) == 0) { |
| 139 | + const bool ignored = (kiproc.kp_proc.p_sigignore & sigmask(SIGQUIT)) != 0; |
| 140 | + const bool caught = (kiproc.kp_proc.p_sigcatch & sigmask(SIGQUIT)) != 0; |
| 141 | + |
| 142 | + // note: obviously the masks could change between testing and signalling however this is not the |
| 143 | + // observed behavior of the current JVM implementation. |
| 144 | + |
| 145 | + if (caught && !ignored) { |
| 146 | + if (kill((pid_t)pid, SIGQUIT) != 0) { |
| 147 | + JNU_ThrowIOExceptionWithLastError(env, "kill"); |
| 148 | + } else { |
| 149 | + return JNI_TRUE; |
| 150 | + } |
| 151 | + } else if (throwIfNotReady) { |
| 152 | + char msg[100]; |
| 153 | + |
| 154 | + snprintf(msg, sizeof(msg), "pid: %d, state is not ready to participate in attach handshake!", (int)pid); |
| 155 | + |
| 156 | + JNU_ThrowByName(env, "com/sun/tools/attach/AttachNotSupportedException", msg); |
| 157 | + } |
| 158 | + } else { |
| 159 | + JNU_ThrowIOExceptionWithLastError(env, "sysctl"); |
121 | 160 | }
|
| 161 | + |
| 162 | + return JNI_FALSE; |
122 | 163 | }
|
123 | 164 |
|
124 | 165 | /*
|
|
0 commit comments