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