never@3156: /* sla@3648: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. never@3156: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. never@3156: * never@3156: * This code is free software; you can redistribute it and/or modify it never@3156: * under the terms of the GNU General Public License version 2 only, as never@3156: * published by the Free Software Foundation. never@3156: * never@3156: * This code is distributed in the hope that it will be useful, but WITHOUT never@3156: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or never@3156: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License never@3156: * version 2 for more details (a copy is included in the LICENSE file that never@3156: * accompanied this code). never@3156: * never@3156: * You should have received a copy of the GNU General Public License version never@3156: * 2 along with this work; if not, write to the Free Software Foundation, never@3156: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. never@3156: * never@3156: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA never@3156: * or visit www.oracle.com if you need additional information or have any never@3156: * questions. never@3156: * never@3156: */ never@3156: never@3156: #include "precompiled.hpp" never@3156: #include "runtime/interfaceSupport.hpp" never@3156: #include "runtime/os.hpp" never@3156: #include "services/attachListener.hpp" never@3156: #include "services/dtraceAttacher.hpp" never@3156: never@3156: #include never@3156: #include never@3156: #include never@3156: #include never@3156: #include never@3156: #include never@3156: never@3156: #ifndef UNIX_PATH_MAX never@3156: #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path) never@3156: #endif never@3156: never@3156: // The attach mechanism on Bsd uses a UNIX domain socket. An attach listener never@3156: // thread is created at startup or is created on-demand via a signal from never@3156: // the client tool. The attach listener creates a socket and binds it to a file never@3156: // in the filesystem. The attach listener then acts as a simple (single- never@3156: // threaded) server - it waits for a client to connect, reads the request, never@3156: // executes it, and returns the response to the client via the socket never@3156: // connection. never@3156: // never@3156: // As the socket is a UNIX domain socket it means that only clients on the never@3156: // local machine can connect. In addition there are two other aspects to never@3156: // the security: never@3156: // 1. The well known file that the socket is bound to has permission 400 never@3156: // 2. When a client connect, the SO_PEERCRED socket option is used to never@3156: // obtain the credentials of client. We check that the effective uid never@3156: // of the client matches this process. never@3156: never@3156: // forward reference never@3156: class BsdAttachOperation; never@3156: never@3156: class BsdAttachListener: AllStatic { never@3156: private: never@3156: // the path to which we bind the UNIX domain socket never@3156: static char _path[UNIX_PATH_MAX]; never@3156: static bool _has_path; never@3156: never@3156: // the file descriptor for the listening socket never@3156: static int _listener; never@3156: never@3156: static void set_path(char* path) { never@3156: if (path == NULL) { never@3156: _has_path = false; never@3156: } else { never@3156: strncpy(_path, path, UNIX_PATH_MAX); never@3156: _path[UNIX_PATH_MAX-1] = '\0'; never@3156: _has_path = true; never@3156: } never@3156: } never@3156: never@3156: static void set_listener(int s) { _listener = s; } never@3156: never@3156: // reads a request from the given connected socket never@3156: static BsdAttachOperation* read_request(int s); never@3156: never@3156: public: never@3156: enum { never@3156: ATTACH_PROTOCOL_VER = 1 // protocol version never@3156: }; never@3156: enum { never@3156: ATTACH_ERROR_BADVERSION = 101 // error codes never@3156: }; never@3156: never@3156: // initialize the listener, returns 0 if okay never@3156: static int init(); never@3156: never@3156: static char* path() { return _path; } never@3156: static bool has_path() { return _has_path; } never@3156: static int listener() { return _listener; } never@3156: never@3156: // write the given buffer to a socket never@3156: static int write_fully(int s, char* buf, int len); never@3156: never@3156: static BsdAttachOperation* dequeue(); never@3156: }; never@3156: never@3156: class BsdAttachOperation: public AttachOperation { never@3156: private: never@3156: // the connection to the client never@3156: int _socket; never@3156: never@3156: public: never@3156: void complete(jint res, bufferedStream* st); never@3156: never@3156: void set_socket(int s) { _socket = s; } never@3156: int socket() const { return _socket; } never@3156: never@3156: BsdAttachOperation(char* name) : AttachOperation(name) { never@3156: set_socket(-1); never@3156: } never@3156: }; never@3156: never@3156: // statics never@3156: char BsdAttachListener::_path[UNIX_PATH_MAX]; never@3156: bool BsdAttachListener::_has_path; never@3156: int BsdAttachListener::_listener = -1; never@3156: never@3156: // Supporting class to help split a buffer into individual components never@3156: class ArgumentIterator : public StackObj { never@3156: private: never@3156: char* _pos; never@3156: char* _end; never@3156: public: never@3156: ArgumentIterator(char* arg_buffer, size_t arg_size) { never@3156: _pos = arg_buffer; never@3156: _end = _pos + arg_size - 1; never@3156: } never@3156: char* next() { never@3156: if (*_pos == '\0') { never@3156: return NULL; never@3156: } never@3156: char* res = _pos; never@3156: char* next_pos = strchr(_pos, '\0'); never@3156: if (next_pos < _end) { never@3156: next_pos++; never@3156: } never@3156: _pos = next_pos; never@3156: return res; never@3156: } never@3156: }; never@3156: never@3156: never@3156: // atexit hook to stop listener and unlink the file that it is never@3156: // bound too. never@3156: extern "C" { never@3156: static void listener_cleanup() { never@3156: static int cleanup_done; never@3156: if (!cleanup_done) { never@3156: cleanup_done = 1; never@3156: int s = BsdAttachListener::listener(); never@3156: if (s != -1) { never@3156: ::close(s); never@3156: } never@3156: if (BsdAttachListener::has_path()) { never@3156: ::unlink(BsdAttachListener::path()); never@3156: } never@3156: } never@3156: } never@3156: } never@3156: never@3156: // Initialization - create a listener socket and bind it to a file never@3156: never@3156: int BsdAttachListener::init() { never@3156: char path[UNIX_PATH_MAX]; // socket file never@3156: char initial_path[UNIX_PATH_MAX]; // socket file during setup never@3156: int listener; // listener socket (file descriptor) never@3156: never@3156: // register function to cleanup never@3156: ::atexit(listener_cleanup); never@3156: never@3156: int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d", never@3156: os::get_temp_directory(), os::current_process_id()); never@3156: if (n < (int)UNIX_PATH_MAX) { never@3156: n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path); never@3156: } never@3156: if (n >= (int)UNIX_PATH_MAX) { never@3156: return -1; never@3156: } never@3156: never@3156: // create the listener socket never@3156: listener = ::socket(PF_UNIX, SOCK_STREAM, 0); never@3156: if (listener == -1) { never@3156: return -1; never@3156: } never@3156: never@3156: // bind socket never@3156: struct sockaddr_un addr; never@3156: addr.sun_family = AF_UNIX; never@3156: strcpy(addr.sun_path, initial_path); never@3156: ::unlink(initial_path); never@3156: int res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr)); never@3156: if (res == -1) { never@3156: RESTARTABLE(::close(listener), res); never@3156: return -1; never@3156: } never@3156: never@3156: // put in listen mode, set permissions, and rename into place never@3156: res = ::listen(listener, 5); never@3156: if (res == 0) { sla@3648: RESTARTABLE(::chmod(initial_path, S_IREAD|S_IWRITE), res); sla@3648: if (res == 0) { sla@3648: // make sure the file is owned by the effective user and effective group sla@3648: // (this is the default on linux, but not on mac os) sla@3648: RESTARTABLE(::chown(initial_path, geteuid(), getegid()), res); never@3156: if (res == 0) { sla@3648: res = ::rename(initial_path, path); never@3156: } sla@3648: } never@3156: } never@3156: if (res == -1) { never@3156: RESTARTABLE(::close(listener), res); never@3156: ::unlink(initial_path); never@3156: return -1; never@3156: } never@3156: set_path(path); never@3156: set_listener(listener); never@3156: never@3156: return 0; never@3156: } never@3156: never@3156: // Given a socket that is connected to a peer we read the request and never@3156: // create an AttachOperation. As the socket is blocking there is potential never@3156: // for a denial-of-service if the peer does not response. However this happens never@3156: // after the peer credentials have been checked and in the worst case it just never@3156: // means that the attach listener thread is blocked. never@3156: // never@3156: BsdAttachOperation* BsdAttachListener::read_request(int s) { never@3156: char ver_str[8]; never@3156: sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER); never@3156: never@3156: // The request is a sequence of strings so we first figure out the never@3156: // expected count and the maximum possible length of the request. never@3156: // The request is: never@3156: // 00000 never@3156: // where is the protocol version (1), is the command never@3156: // name ("load", "datadump", ...), and is an argument never@3156: int expected_str_count = 2 + AttachOperation::arg_count_max; never@3156: const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) + never@3156: AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1); never@3156: never@3156: char buf[max_len]; never@3156: int str_count = 0; never@3156: never@3156: // Read until all (expected) strings have been read, the buffer is never@3156: // full, or EOF. never@3156: never@3156: int off = 0; never@3156: int left = max_len; never@3156: never@3156: do { never@3156: int n; never@3156: RESTARTABLE(read(s, buf+off, left), n); never@3156: if (n == -1) { never@3156: return NULL; // reset by peer or other error never@3156: } never@3156: if (n == 0) { never@3156: break; never@3156: } never@3156: for (int i=0; i so check it now to never@3156: // check for protocol mis-match never@3156: if (str_count == 1) { never@3156: if ((strlen(buf) != strlen(ver_str)) || never@3156: (atoi(buf) != ATTACH_PROTOCOL_VER)) { never@3156: char msg[32]; never@3156: sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION); never@3156: write_fully(s, msg, strlen(msg)); never@3156: return NULL; never@3156: } never@3156: } never@3156: } never@3156: } never@3156: off += n; never@3156: left -= n; never@3156: } while (left > 0 && str_count < expected_str_count); never@3156: never@3156: if (str_count != expected_str_count) { never@3156: return NULL; // incomplete request never@3156: } never@3156: never@3156: // parse request never@3156: never@3156: ArgumentIterator args(buf, (max_len)-left); never@3156: never@3156: // version already checked never@3156: char* v = args.next(); never@3156: never@3156: char* name = args.next(); never@3156: if (name == NULL || strlen(name) > AttachOperation::name_length_max) { never@3156: return NULL; never@3156: } never@3156: never@3156: BsdAttachOperation* op = new BsdAttachOperation(name); never@3156: never@3156: for (int i=0; iset_arg(i, NULL); never@3156: } else { never@3156: if (strlen(arg) > AttachOperation::arg_length_max) { never@3156: delete op; never@3156: return NULL; never@3156: } never@3156: op->set_arg(i, arg); never@3156: } never@3156: } never@3156: never@3156: op->set_socket(s); never@3156: return op; never@3156: } never@3156: never@3156: never@3156: // Dequeue an operation never@3156: // never@3156: // In the Bsd implementation there is only a single operation and clients never@3156: // cannot queue commands (except at the socket level). never@3156: // never@3156: BsdAttachOperation* BsdAttachListener::dequeue() { never@3156: for (;;) { never@3156: int s; never@3156: never@3156: // wait for client to connect never@3156: struct sockaddr addr; never@3156: socklen_t len = sizeof(addr); never@3156: RESTARTABLE(::accept(listener(), &addr, &len), s); never@3156: if (s == -1) { never@3156: return NULL; // log a warning? never@3156: } never@3156: never@3156: // get the credentials of the peer and check the effective uid/guid never@3156: // - check with jeff on this. never@3156: uid_t puid; never@3156: gid_t pgid; never@3156: if (::getpeereid(s, &puid, &pgid) != 0) { never@3156: int res; never@3156: RESTARTABLE(::close(s), res); never@3156: continue; never@3156: } never@3156: uid_t euid = geteuid(); never@3156: gid_t egid = getegid(); never@3156: never@3156: if (puid != euid || pgid != egid) { never@3156: int res; never@3156: RESTARTABLE(::close(s), res); never@3156: continue; never@3156: } never@3156: never@3156: // peer credential look okay so we read the request never@3156: BsdAttachOperation* op = read_request(s); never@3156: if (op == NULL) { never@3156: int res; never@3156: RESTARTABLE(::close(s), res); never@3156: continue; never@3156: } else { never@3156: return op; never@3156: } never@3156: } never@3156: } never@3156: never@3156: // write the given buffer to the socket never@3156: int BsdAttachListener::write_fully(int s, char* buf, int len) { never@3156: do { never@3156: int n = ::write(s, buf, len); never@3156: if (n == -1) { never@3156: if (errno != EINTR) return -1; never@3156: } else { never@3156: buf += n; never@3156: len -= n; never@3156: } never@3156: } never@3156: while (len > 0); never@3156: return 0; never@3156: } never@3156: never@3156: // Complete an operation by sending the operation result and any result never@3156: // output to the client. At this time the socket is in blocking mode so never@3156: // potentially we can block if there is a lot of data and the client is never@3156: // non-responsive. For most operations this is a non-issue because the never@3156: // default send buffer is sufficient to buffer everything. In the future never@3156: // if there are operations that involves a very big reply then it the never@3156: // socket could be made non-blocking and a timeout could be used. never@3156: never@3156: void BsdAttachOperation::complete(jint result, bufferedStream* st) { never@3156: JavaThread* thread = JavaThread::current(); never@3156: ThreadBlockInVM tbivm(thread); never@3156: never@3156: thread->set_suspend_equivalent(); never@3156: // cleared by handle_special_suspend_equivalent_condition() or never@3156: // java_suspend_self() via check_and_wait_while_suspended() never@3156: never@3156: // write operation result never@3156: char msg[32]; never@3156: sprintf(msg, "%d\n", result); never@3156: int rc = BsdAttachListener::write_fully(this->socket(), msg, strlen(msg)); never@3156: never@3156: // write any result data never@3156: if (rc == 0) { never@3156: BsdAttachListener::write_fully(this->socket(), (char*) st->base(), st->size()); never@3156: ::shutdown(this->socket(), 2); never@3156: } never@3156: never@3156: // done never@3156: RESTARTABLE(::close(this->socket()), rc); never@3156: never@3156: // were we externally suspended while we were waiting? never@3156: thread->check_and_wait_while_suspended(); never@3156: never@3156: delete this; never@3156: } never@3156: never@3156: never@3156: // AttachListener functions never@3156: never@3156: AttachOperation* AttachListener::dequeue() { never@3156: JavaThread* thread = JavaThread::current(); never@3156: ThreadBlockInVM tbivm(thread); never@3156: never@3156: thread->set_suspend_equivalent(); never@3156: // cleared by handle_special_suspend_equivalent_condition() or never@3156: // java_suspend_self() via check_and_wait_while_suspended() never@3156: never@3156: AttachOperation* op = BsdAttachListener::dequeue(); never@3156: never@3156: // were we externally suspended while we were waiting? never@3156: thread->check_and_wait_while_suspended(); never@3156: never@3156: return op; never@3156: } never@3156: never@3156: int AttachListener::pd_init() { never@3156: JavaThread* thread = JavaThread::current(); never@3156: ThreadBlockInVM tbivm(thread); never@3156: never@3156: thread->set_suspend_equivalent(); never@3156: // cleared by handle_special_suspend_equivalent_condition() or never@3156: // java_suspend_self() via check_and_wait_while_suspended() never@3156: never@3156: int ret_code = BsdAttachListener::init(); never@3156: never@3156: // were we externally suspended while we were waiting? never@3156: thread->check_and_wait_while_suspended(); never@3156: never@3156: return ret_code; never@3156: } never@3156: never@3156: // Attach Listener is started lazily except in the case when never@3156: // +ReduseSignalUsage is used never@3156: bool AttachListener::init_at_startup() { never@3156: if (ReduceSignalUsage) { never@3156: return true; never@3156: } else { never@3156: return false; never@3156: } never@3156: } never@3156: never@3156: // If the file .attach_pid exists in the working directory never@3156: // or /tmp then this is the trigger to start the attach mechanism never@3156: bool AttachListener::is_init_trigger() { never@3156: if (init_at_startup() || is_initialized()) { never@3156: return false; // initialized at startup or already initialized never@3156: } never@3156: char path[PATH_MAX + 1]; never@3156: int ret; never@3156: struct stat st; never@3156: never@3156: snprintf(path, PATH_MAX + 1, "%s/.attach_pid%d", never@3156: os::get_temp_directory(), os::current_process_id()); never@3156: RESTARTABLE(::stat(path, &st), ret); never@3156: if (ret == 0) { never@3156: // simple check to avoid starting the attach mechanism when never@3156: // a bogus user creates the file never@3156: if (st.st_uid == geteuid()) { never@3156: init(); never@3156: return true; never@3156: } never@3156: } never@3156: return false; never@3156: } never@3156: never@3156: // if VM aborts then remove listener never@3156: void AttachListener::abort() { never@3156: listener_cleanup(); never@3156: } never@3156: never@3156: void AttachListener::pd_data_dump() { never@3156: os::signal_notify(SIGQUIT); never@3156: } never@3156: never@3156: AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) { never@3156: return NULL; never@3156: } never@3156: never@3156: jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) { never@3156: out->print_cr("flag '%s' cannot be changed", op->arg(0)); never@3156: return JNI_ERR; never@3156: } never@3156: never@3156: void AttachListener::pd_detachall() { never@3156: // do nothing for now never@3156: }