src/jdk.attach/solaris/native/libattach/VirtualMachineImpl.c
author hseigel
Thu, 14 Mar 2019 09:38:17 -0400
changeset 54124 5d48ae032588
parent 50333 7cea35f78b50
permissions -rw-r--r--
8219579: Remove redundant signature parsing from the verifier Summary: Change verifier signature checking into asserts because ClassFileParser checks signatures for files being verified. Reviewed-by: lfoltan, coleenp, redestad, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24365
79018fe41ff9 8042859: Build broken for Solaris
emc
parents: 24363
diff changeset
     1
/*
49288
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    25
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    26
#include "jni_util.h"
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    27
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    28
#include <sys/stat.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <door.h>
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    31
#include <errno.h>
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    32
#include <fcntl.h>
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    33
#include <limits.h>
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    34
#include <signal.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <string.h>
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
    37
#include <unistd.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    39
#include "sun_tools_attach_VirtualMachineImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
50333
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 49742
diff changeset
    41
#define ROOT_UID 0
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 49742
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#define RESTARTABLE(_cmd, _result) do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    _result = _cmd; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  } while((_result == -1) && (errno == EINTR)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
} while(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29607
diff changeset
    50
 * Declare library specific JNI_Onload entry if static build
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29607
diff changeset
    51
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29607
diff changeset
    52
DEF_STATIC_JNI_OnLoad
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29607
diff changeset
    53
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29607
diff changeset
    54
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    55
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Method:    open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Signature: (Ljava/lang/String;)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    59
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_open
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  (JNIEnv *env, jclass cls, jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    const char* p = GetStringPlatformChars(env, path, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    if (p == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int err = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        fd = open(p, O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            err = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            JNU_ReleaseStringPlatformChars(env, path, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (err == ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                char* msg = strdup(strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                JNU_ThrowIOException(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    free(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    95
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Method:    checkPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Signature: (Ljava/lang/String;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    99
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_checkPermissions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  (JNIEnv *env, jclass cls, jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    const char* p = GetStringPlatformChars(env, path, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (p != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        struct stat64 sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        uid_t uid, gid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
   109
        memset(&sb, 0, sizeof(struct stat64));
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
   110
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * Check that the path is owned by the effective uid/gid of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * process. Also check that group/other access is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        uid = geteuid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        gid = getegid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        res = stat64(p, &sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            /* save errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            res = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (res == 0) {
29607
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   125
            char msg[100];
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   126
            jboolean isError = JNI_FALSE;
50333
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 49742
diff changeset
   127
            if (sb.st_uid != uid && uid != ROOT_UID) {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
   128
                snprintf(msg, sizeof(msg),
29607
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   129
                    "file should be owned by the current user (which is %d) but is owned by %d", uid, sb.st_uid);
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   130
                isError = JNI_TRUE;
50333
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 49742
diff changeset
   131
            } else if (sb.st_gid != gid && uid != ROOT_UID) {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
   132
                snprintf(msg, sizeof(msg),
29607
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   133
                    "file's group should be the current group (which is %d) but the group is %d", gid, sb.st_gid);
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   134
                isError = JNI_TRUE;
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   135
            } else if ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0) {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
   136
                snprintf(msg, sizeof(msg),
29607
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   137
                    "file should only be readable and writable by the owner but has 0%03o access", sb.st_mode & 0777);
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   138
                isError = JNI_TRUE;
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   139
            }
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   140
            if (isError) {
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   141
                char buf[256];
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49288
diff changeset
   142
                snprintf(buf, sizeof(buf), "well-known file %s is not secure: %s", p, msg);
29607
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   143
                JNU_ThrowIOException(env, buf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            char* msg = strdup(strerror(res));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            JNU_ThrowIOException(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                free(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
29607
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   152
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   153
        if (isCopy) {
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   154
            JNU_ReleaseStringPlatformChars(env, path, p);
659cd463df20 8074812: More specific error message when the .java_pid well-known file is not secure
sla
parents: 28059
diff changeset
   155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   160
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * Method:    close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   164
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_close
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  (JNIEnv *env, jclass cls, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    RESTARTABLE(close(fd), ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   172
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * Method:    read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * Signature: (I[BI)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   176
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  (JNIEnv *env, jclass cls, jint fd, jbyteArray ba, jint off, jint baLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    unsigned char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    size_t len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    ssize_t n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    size_t remaining = (size_t)(baLen - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        len = remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   188
    RESTARTABLE(read(fd, buf, len), n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        JNU_ThrowIOExceptionWithLastError(env, "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            n = -1;     // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   195
            (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   202
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Method:    sigquit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   206
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_sigquit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  (JNIEnv *env, jclass cls, jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if (kill((pid_t)pid, SIGQUIT) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        JNU_ThrowIOExceptionWithLastError(env, "kill");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * A simple table to translate some known errors into reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * error messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jint err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    const char* msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
} const error_messages[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    { 100,      "Bad request" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    { 101,      "Protocol mismatch" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    { 102,      "Resource failure" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    { 103,      "Internal error" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    { 104,      "Permission denied" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * Lookup the given error code and return the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * message. If not found return NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
static const char* translate_error(jint err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    int table_size = sizeof(error_messages) / sizeof(error_messages[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
49288
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   237
    for (i = 0; i < table_size; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (err == error_messages[i].err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return error_messages[i].msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * Current protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
static const char* PROTOCOL_VERSION = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   251
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * Method:    enqueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * Signature: (JILjava/lang/String;[Ljava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   255
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_enqueue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
  (JNIEnv *env, jclass cls, jint fd, jstring cmd, jobjectArray args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    jint arg_count, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    size_t size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    door_arg_t door_args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    char res_buffer[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    jint result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    int rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    const char* cstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    char* buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * First we get the command string and create the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * argument string to send to the target VM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <ver>\0<cmd>\0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    cstr = JNU_GetStringPlatformChars(env, cmd, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    if (cstr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return -1;              /* pending exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    buf = (char*)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    if (buf != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        char* pos = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        strcpy(buf, PROTOCOL_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        pos += strlen(PROTOCOL_VERSION)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        strcpy(pos, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        JNU_ReleaseStringPlatformChars(env, cmd, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    if (buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        JNU_ThrowOutOfMemoryError(env, "malloc failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Next we iterate over the arguments and extend the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * to include them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    arg_count = (*env)->GetArrayLength(env, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
49288
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   299
    for (i = 0; i < arg_count; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        jobject obj = (*env)->GetObjectArrayElement(env, args, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (obj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            cstr = JNU_GetStringPlatformChars(env, obj, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (cstr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                size_t len = strlen(cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                char* newbuf = (char*)realloc(buf, size+len+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (newbuf != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    strcpy(buf+size, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    size += len+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    JNU_ReleaseStringPlatformChars(env, obj, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (newbuf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    JNU_ThrowOutOfMemoryError(env, "realloc failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
49288
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   320
        } else {
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   321
            char* newbuf = (char*)realloc(buf, size + 1);
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   322
            if (newbuf == NULL) {
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   323
                free(buf);
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   324
                JNU_ThrowOutOfMemoryError(env, "realloc failed");
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   325
                return -1;
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   326
            }
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   327
            buf = newbuf;
6e2d71029781 8199924: Solaris: Correctly enqueue null arguments of attach operations
clanger
parents: 47216
diff changeset
   328
            buf[size++] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * The arguments to the door function are in 'buf' so we now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * do the door call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    door_args.data_ptr = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    door_args.data_size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    door_args.desc_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    door_args.desc_num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    door_args.rbuf = (char*)&res_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    door_args.rsize = sizeof(res_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    RESTARTABLE(door_call(fd, &door_args), rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * door_call failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    if (rc == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        JNU_ThrowIOExceptionWithLastError(env, "door_call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        /*
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26216
diff changeset
   356
         * door_call succeeded but the call didn't return the expected jint.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (door_args.data_size < sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            JNU_ThrowIOException(env, "Enqueue error - reason unknown as result is truncated!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            jint* res = (jint*)(door_args.data_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (*res != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                const char* msg = translate_error(*res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    sprintf(buf, "Unable to enqueue command to target VM: %d", *res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    sprintf(buf, "Unable to enqueue command to target VM: %s", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                JNU_ThrowIOException(env, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                 * The door call should return a file descriptor to one end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                 * a socket pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if ((door_args.desc_ptr != NULL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    (door_args.desc_num == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    result = door_args.desc_ptr->d_data.d_desc.d_descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    JNU_ThrowIOException(env, "Reply from enqueue missing descriptor!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
}