/*
* 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 <unistd.h>
#include <signal.h>
#ifndef UNIX_PATH_MAX
#endif
// The attach mechanism on Linux uses a UNIX domain socket. An attach listener
// thread is created at startup or is created on-demand via a signal from
// the client tool. The attach listener creates a socket and binds it to a file
// in the filesystem. The attach listener then acts as a simple (single-
// threaded) server - it waits for a client to connect, reads the request,
// executes it, and returns the response to the client via the socket
// connection.
//
// As the socket is a UNIX domain socket it means that only clients on the
// local machine can connect. In addition there are two other aspects to
// the security:
// 1. The well known file that the socket is bound to has permission 400
// 2. When a client connect, the SO_PEERCRED socket option is used to
// obtain the credentials of client. We check that the effective uid
// of the client matches this process.
// forward reference
class LinuxAttachOperation;
private:
// the path to which we bind the UNIX domain socket
static bool _has_path;
// the file descriptor for the listening socket
static int _listener;
_has_path = false;
} else {
_has_path = true;
}
}
// reads a request from the given connected socket
static LinuxAttachOperation* read_request(int s);
public:
enum {
};
enum {
};
// initialize the listener, returns 0 if okay
static int init();
// write the given buffer to a socket
static LinuxAttachOperation* dequeue();
};
private:
// the connection to the client
int _socket;
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;
}
};
// atexit hook to stop listener and unlink the file that it is
// bound too.
extern "C" {
static void listener_cleanup() {
static int cleanup_done;
if (!cleanup_done) {
cleanup_done = 1;
int s = LinuxAttachListener::listener();
if (s != -1) {
::close(s);
}
if (LinuxAttachListener::has_path()) {
}
}
}
}
// Initialization - create a listener socket and bind it to a file
// register function to cleanup
if (n < (int)UNIX_PATH_MAX) {
}
if (n >= (int)UNIX_PATH_MAX) {
return -1;
}
// create the listener socket
if (listener == -1) {
return -1;
}
// bind socket
::unlink(initial_path);
if (res == -1) {
return -1;
}
// put in listen mode, set permissions, and rename into place
if (res == 0) {
if (res == 0) {
}
}
if (res == -1) {
::unlink(initial_path);
return -1;
}
return 0;
}
// Given a socket that is connected to a peer we read the request and
// create an AttachOperation. As the socket is blocking there is potential
// for a denial-of-service if the peer does not response. However this happens
// after the peer credentials have been checked and in the worst case it just
// means that the attach listener thread is blocked.
//
// The request is a sequence of strings so we first figure out the
// expected count and the maximum possible length of the request.
// The request is:
// <ver>0<cmd>0<arg>0<arg>0<arg>0
// where <ver> is the protocol version (1), <cmd> is the command
// name ("load", "datadump", ...), and <arg> is an argument
int str_count = 0;
// Read until all (expected) strings have been read, the buffer is
// full, or EOF.
int off = 0;
do {
int n;
if (n == -1) {
return NULL; // reset by peer or other error
}
if (n == 0) {
break;
}
for (int i=0; i<n; i++) {
// EOS found
str_count++;
// The first string is <ver> so check it now to
// check for protocol mis-match
if (str_count == 1) {
return NULL;
}
}
}
}
off += n;
left -= n;
if (str_count != expected_str_count) {
return NULL; // incomplete request
}
// parse request
// version already checked
return NULL;
}
for (int i=0; i<AttachOperation::arg_count_max; i++) {
} else {
delete op;
return NULL;
}
}
}
op->set_socket(s);
return op;
}
// Dequeue an operation
//
// In the Linux implementation there is only a single operation and clients
// cannot queue commands (except at the socket level).
//
for (;;) {
int s;
// wait for client to connect
if (s == -1) {
return NULL; // log a warning?
}
// - check with jeff on this.
int res;
continue;
}
int res;
continue;
}
// peer credential look okay so we read the request
int res;
continue;
} else {
return op;
}
}
}
// write the given buffer to the 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.
// 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) {
}
// 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() via check_and_wait_while_suspended()
// 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;
}
// if VM aborts then remove listener
}
}
return NULL;
}
return JNI_ERR;
}
// do nothing for now
}