/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "runtime/interfaceSupport.hpp"
#include "services/attachListener.hpp"
#include "services/dtraceAttacher.hpp"
#include <door.h>
#include <string.h>
#include <signal.h>
// stropts.h uses STR in stream ioctl defines
#include <stropts.h>
#define STR(a) #a
// The attach mechanism on Solaris is implemented using the Doors IPC
// mechanism. The first tool to attempt to attach causes the attach
// listener thread to startup. This thread creats a door that is
// associated with a function that enqueues an operation to the attach
// listener. The door is attached to a file in the file system so that
// client (tools) can locate it. To enqueue an operation to the VM the
// client calls through the door which invokes the enqueue function in
// this process. The credentials of the client are checked and if the
// effective uid matches this process then the operation is enqueued.
// When an operation completes the attach listener is required to send the
// operation result and any result data to the client. In this implementation
// the result is returned via a UNIX domain socket. A pair of connected
// sockets (socketpair) is created in the enqueue function and the file
// descriptor for one of the sockets is returned to the client as the
// return from the door call. The other end is retained in this process.
// When the operation completes the result is sent to the client and
// the socket is closed.
// forward reference
class SolarisAttachOperation;
private:
// the path to which we attach the door file descriptor
static volatile bool _has_door_path;
// door descriptor returned by door_create
static int _door_descriptor;
_has_door_path = false;
} else {
_has_door_path = true;
}
}
// mutex to protect operation list
// semaphore to wakeup listener thread
// enqueued operation list
// create the door
static int create_door();
public:
enum {
};
enum {
};
// initialize the listener
static int init();
// enqueue an operation
// dequeue an operation
static SolarisAttachOperation* dequeue();
};
// SolarisAttachOperation is an AttachOperation that additionally encapsulates
// can additionally be held in a linked list.
private:
friend class SolarisAttachListener;
// connection to client
int _socket;
// linked list support
public:
set_socket(-1);
}
};
// statics
// Supporting class to help split a buffer into individual components
private:
char* _pos;
char* _end;
public:
_pos = arg_buffer;
}
char* next() {
if (*_pos == '\0') {
return NULL;
}
next_pos++;
}
return res;
}
};
// Calls from the door function to check that the client credentials
// match this process. Returns 0 if credentials okay, otherwise -1.
static int check_credentials() {
// get client credentials
return -1; // unable to get them
}
return 0; // okay
} else {
return -1; // denied
}
}
// Parses the argument buffer to create an AttachOperation that we should
// enqueue to the attach listener.
// The buffer is expected to be formatted as follows:
// <ver>0<cmd>0<arg>0<arg>0<arg>0
// where <ver> is the version number (must be "1"), <cmd> is the command
// name ("load, "datadump", ...) and <arg> is an argument.
//
// assume bad request until parsed
return NULL; // no ver or not null terminated
}
// Use supporting class to iterate over the buffer
// First check the protocol version
return NULL;
}
return NULL;
}
// Get command name and create the operation
return NULL;
}
// Iterate over the arguments
for (int i=0; i<AttachOperation::arg_count_max; i++) {
} else {
delete op;
return NULL;
}
}
}
// return operation
*err = 0;
return op;
}
// create special operation to indicate all clients have detached
}
// This is door function which the client executes via a door_call.
extern "C" {
{
// no listener
if (!AttachListener::is_initialized()) {
// how did we get here?
}
// check client credentials
if (res == 0) {
if (check_credentials() != 0) {
}
}
// if we are stopped at ShowMessageBoxOnError then maybe we can
// load a diagnostic library
if (res == 0 && is_error_reported()) {
if (ShowMessageBoxOnError) {
// TBD - support loading of diagnostic library here
}
// can't enqueue operation after fatal error
}
// create the operation
if (res == 0) {
int err;
}
// create a pair of connected sockets. Store the file descriptor
// for one end in the operation and enqueue the operation. The
// file descriptor for the other end wil be returned to the client.
if (res == 0) {
int s[2];
delete op;
} else {
op->set_socket(s[0]);
return_fd = s[1];
}
}
// Return 0 (success) + file descriptor, or non-0 (error)
if (res == 0) {
// DOOR_RELEASE flag makes sure fd is closed after passing it to
// the client. See door_return(3DOOR) man page.
} else {
}
}
}
// atexit hook to detach the door and remove the file
extern "C" {
static void listener_cleanup() {
static int cleanup_done;
if (!cleanup_done) {
cleanup_done = 1;
if (dd >= 0) {
}
if (SolarisAttachListener::has_door_path()) {
}
}
}
}
// Create the door
// register exit function
// create the door descriptor
if (dd < 0) {
return -1;
}
// create initial file to attach door descriptor
if (fd == -1) {
::door_revoke(dd);
return -1;
}
// attach the door descriptor to the file
// if busy then detach and try again
::fdetach(initial_path);
}
if (res == -1) {
::door_revoke(dd);
dd = -1;
}
}
// rename file so that clients can attach
if (dd >= 0) {
::fdetach(initial_path);
dd = -1;
}
}
if (dd >= 0) {
} else {
// unable to create door, attach it to file, or rename file into place
::unlink(initial_path);
return -1;
}
return 0;
}
// Initialization - create the door, locks, and other initialization
if (create_door()) {
return -1;
}
return 0;
}
// Dequeue an operation
for (;;) {
int res;
// wait for somebody to enqueue something
;
if (res) {
return NULL;
}
// lock the list
// remove the head of the list
}
}
// unlock
// if we got an operation when return it.
return op;
}
}
}
// Enqueue an operation
// lock list
// enqueue at tail
} else {
}
// wakeup the attach listener
// unlock
}
// support function - writes the (entire) buffer to a socket
do {
if (n == -1) {
} else {
buf += n;
len -= n;
}
}
while (len > 0);
return 0;
}
// Complete an operation by sending the operation result and any result
// output to the client. At this time the socket is in blocking mode so
// potentially we can block if there is a lot of data and the client is
// non-responsive. For most operations this is a non-issue because the
// default send buffer is sufficient to buffer everything. In the future
// if there are operations that involves a very big reply then it the
// socket could be made non-blocking and a timeout could be used.
if (this->socket() >= 0) {
// cleared by handle_special_suspend_equivalent_condition() or
// java_suspend_self() via check_and_wait_while_suspended()
// write operation result
// write any result data
if (rc == 0) {
}
// close socket and we're done
// were we externally suspended while we were waiting?
}
delete this;
}
// AttachListener functions
// cleared by handle_special_suspend_equivalent_condition() or
// java_suspend_self() via check_and_wait_while_suspended()
// were we externally suspended while we were waiting?
return op;
}
// cleared by handle_special_suspend_equivalent_condition() or
// java_suspend_self()
// were we externally suspended while we were waiting?
return ret_code;
}
// Attach Listener is started lazily except in the case when
// +ReduseSignalUsage is used
if (ReduceSignalUsage) {
return true;
} else {
return false;
}
}
// If the file .attach_pid<pid> exists in the working directory
// or /tmp then this is the trigger to start the attach mechanism
if (init_at_startup() || is_initialized()) {
return false; // initialized at startup or already initialized
}
int ret;
if (ret == -1) {
}
if (ret == 0) {
// simple check to avoid starting the attach mechanism when
// a bogus user creates the file
init();
return true;
}
}
return false;
}
}
}
return JNI_ERR;
} else {
if (errno) {
return JNI_ERR;
} else {
return JNI_OK;
}
}
}
// platform specific operations table
{ "enabledprobes", enable_dprobes },
};
int i;
return &funcs[i];
}
}
return NULL;
}
// Solaris specific global flag set. Currently, we support only
// changing ExtendedDTraceProbes flag.
bool flag = true;
const char* arg1;
if (errno) {
return JNI_ERR;
}
}
return JNI_OK;
}
return JNI_OK;
}
return JNI_ERR;
}
}