jdk/src/jdk.attach/solaris/native/libattach/VirtualMachineImpl.c
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 26216 5e46c782b43c
child 29607 659cd463df20
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24365
79018fe41ff9 8042859: Build broken for Solaris
emc
parents: 24363
diff changeset
     1
/*
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2014, 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <door.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <signal.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jni_util.h"
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#define RESTARTABLE(_cmd, _result) do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    _result = _cmd; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  } while((_result == -1) && (errno == EINTR)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
} while(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    48
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Method:    open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Signature: (Ljava/lang/String;)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    52
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_open
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  (JNIEnv *env, jclass cls, jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    const char* p = GetStringPlatformChars(env, path, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    if (p == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int err = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        fd = open(p, O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            err = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            JNU_ReleaseStringPlatformChars(env, path, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (err == ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                char* msg = strdup(strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                JNU_ThrowIOException(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    free(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    88
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Method:    checkPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Signature: (Ljava/lang/String;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    92
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_checkPermissions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  (JNIEnv *env, jclass cls, jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    const char* p = GetStringPlatformChars(env, path, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (p != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        struct stat64 sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        uid_t uid, gid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * Check that the path is owned by the effective uid/gid of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * process. Also check that group/other access is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        uid = geteuid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        gid = getegid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        res = stat64(p, &sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            /* save errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            res = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /* release p here before we throw an I/O exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            JNU_ReleaseStringPlatformChars(env, path, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if ( (sb.st_uid != uid) || (sb.st_gid != gid) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                 ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                JNU_ThrowIOException(env, "well-known file is not secure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            char* msg = strdup(strerror(res));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            JNU_ThrowIOException(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                free(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   136
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * Method:    close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   140
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_close
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  (JNIEnv *env, jclass cls, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    RESTARTABLE(close(fd), ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   148
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * Method:    read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * Signature: (I[BI)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   152
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  (JNIEnv *env, jclass cls, jint fd, jbyteArray ba, jint off, jint baLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    unsigned char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    size_t len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    ssize_t n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    size_t remaining = (size_t)(baLen - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        len = remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   164
    RESTARTABLE(read(fd, buf, len), n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        JNU_ThrowIOExceptionWithLastError(env, "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            n = -1;     // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   171
            (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   178
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * Method:    sigquit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   182
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_sigquit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
  (JNIEnv *env, jclass cls, jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    if (kill((pid_t)pid, SIGQUIT) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        JNU_ThrowIOExceptionWithLastError(env, "kill");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * A simple table to translate some known errors into reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * error messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    jint err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    const char* msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
} const error_messages[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    { 100,      "Bad request" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    { 101,      "Protocol mismatch" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    { 102,      "Resource failure" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    { 103,      "Internal error" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    { 104,      "Permission denied" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * Lookup the given error code and return the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * message. If not found return NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
static const char* translate_error(jint err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    int table_size = sizeof(error_messages) / sizeof(error_messages[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    for (i=0; i<table_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (err == error_messages[i].err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return error_messages[i].msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * Current protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
static const char* PROTOCOL_VERSION = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   227
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * Method:    enqueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * Signature: (JILjava/lang/String;[Ljava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   231
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_enqueue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
  (JNIEnv *env, jclass cls, jint fd, jstring cmd, jobjectArray args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    jint arg_count, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    size_t size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    door_arg_t door_args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    char res_buffer[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    jint result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    int rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    const char* cstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    char* buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * First we get the command string and create the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * argument string to send to the target VM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <ver>\0<cmd>\0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    cstr = JNU_GetStringPlatformChars(env, cmd, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    if (cstr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return -1;              /* pending exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    buf = (char*)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    if (buf != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        char* pos = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        strcpy(buf, PROTOCOL_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        pos += strlen(PROTOCOL_VERSION)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        strcpy(pos, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        JNU_ReleaseStringPlatformChars(env, cmd, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    if (buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        JNU_ThrowOutOfMemoryError(env, "malloc failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Next we iterate over the arguments and extend the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * to include them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    arg_count = (*env)->GetArrayLength(env, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    for (i=0; i<arg_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        jobject obj = (*env)->GetObjectArrayElement(env, args, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (obj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            cstr = JNU_GetStringPlatformChars(env, obj, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (cstr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                size_t len = strlen(cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                char* newbuf = (char*)realloc(buf, size+len+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (newbuf != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    strcpy(buf+size, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    size += len+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    JNU_ReleaseStringPlatformChars(env, obj, cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (newbuf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    JNU_ThrowOutOfMemoryError(env, "realloc failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * The arguments to the door function are in 'buf' so we now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * do the door call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    door_args.data_ptr = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    door_args.data_size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    door_args.desc_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    door_args.desc_num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    door_args.rbuf = (char*)&res_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    door_args.rsize = sizeof(res_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    RESTARTABLE(door_call(fd, &door_args), rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * door_call failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (rc == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        JNU_ThrowIOExceptionWithLastError(env, "door_call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /*
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26216
diff changeset
   323
         * door_call succeeded but the call didn't return the expected jint.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (door_args.data_size < sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            JNU_ThrowIOException(env, "Enqueue error - reason unknown as result is truncated!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            jint* res = (jint*)(door_args.data_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (*res != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                const char* msg = translate_error(*res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    sprintf(buf, "Unable to enqueue command to target VM: %d", *res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    sprintf(buf, "Unable to enqueue command to target VM: %s", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                JNU_ThrowIOException(env, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                 * The door call should return a file descriptor to one end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                 * a socket pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if ((door_args.desc_ptr != NULL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    (door_args.desc_num == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    result = door_args.desc_ptr->d_data.d_desc.d_descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    JNU_ThrowIOException(env, "Reply from enqueue missing descriptor!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}