src/os/bsd/vm/attachListener_bsd.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/os/bsd/vm/attachListener_bsd.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,534 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "runtime/interfaceSupport.hpp"
    1.30 +#include "runtime/os.hpp"
    1.31 +#include "services/attachListener.hpp"
    1.32 +#include "services/dtraceAttacher.hpp"
    1.33 +
    1.34 +#include <unistd.h>
    1.35 +#include <signal.h>
    1.36 +#include <sys/types.h>
    1.37 +#include <sys/socket.h>
    1.38 +#include <sys/un.h>
    1.39 +#include <sys/stat.h>
    1.40 +
    1.41 +#ifndef UNIX_PATH_MAX
    1.42 +#define UNIX_PATH_MAX   sizeof(((struct sockaddr_un *)0)->sun_path)
    1.43 +#endif
    1.44 +
    1.45 +// The attach mechanism on Bsd uses a UNIX domain socket. An attach listener
    1.46 +// thread is created at startup or is created on-demand via a signal from
    1.47 +// the client tool. The attach listener creates a socket and binds it to a file
    1.48 +// in the filesystem. The attach listener then acts as a simple (single-
    1.49 +// threaded) server - it waits for a client to connect, reads the request,
    1.50 +// executes it, and returns the response to the client via the socket
    1.51 +// connection.
    1.52 +//
    1.53 +// As the socket is a UNIX domain socket it means that only clients on the
    1.54 +// local machine can connect. In addition there are two other aspects to
    1.55 +// the security:
    1.56 +// 1. The well known file that the socket is bound to has permission 400
    1.57 +// 2. When a client connect, the SO_PEERCRED socket option is used to
    1.58 +//    obtain the credentials of client. We check that the effective uid
    1.59 +//    of the client matches this process.
    1.60 +
    1.61 +// forward reference
    1.62 +class BsdAttachOperation;
    1.63 +
    1.64 +class BsdAttachListener: AllStatic {
    1.65 + private:
    1.66 +  // the path to which we bind the UNIX domain socket
    1.67 +  static char _path[UNIX_PATH_MAX];
    1.68 +  static bool _has_path;
    1.69 +
    1.70 +  // the file descriptor for the listening socket
    1.71 +  static int _listener;
    1.72 +
    1.73 +  static void set_path(char* path) {
    1.74 +    if (path == NULL) {
    1.75 +      _has_path = false;
    1.76 +    } else {
    1.77 +      strncpy(_path, path, UNIX_PATH_MAX);
    1.78 +      _path[UNIX_PATH_MAX-1] = '\0';
    1.79 +      _has_path = true;
    1.80 +    }
    1.81 +  }
    1.82 +
    1.83 +  static void set_listener(int s)               { _listener = s; }
    1.84 +
    1.85 +  // reads a request from the given connected socket
    1.86 +  static BsdAttachOperation* read_request(int s);
    1.87 +
    1.88 + public:
    1.89 +  enum {
    1.90 +    ATTACH_PROTOCOL_VER = 1                     // protocol version
    1.91 +  };
    1.92 +  enum {
    1.93 +    ATTACH_ERROR_BADVERSION     = 101           // error codes
    1.94 +  };
    1.95 +
    1.96 +  // initialize the listener, returns 0 if okay
    1.97 +  static int init();
    1.98 +
    1.99 +  static char* path()                   { return _path; }
   1.100 +  static bool has_path()                { return _has_path; }
   1.101 +  static int listener()                 { return _listener; }
   1.102 +
   1.103 +  // write the given buffer to a socket
   1.104 +  static int write_fully(int s, char* buf, int len);
   1.105 +
   1.106 +  static BsdAttachOperation* dequeue();
   1.107 +};
   1.108 +
   1.109 +class BsdAttachOperation: public AttachOperation {
   1.110 + private:
   1.111 +  // the connection to the client
   1.112 +  int _socket;
   1.113 +
   1.114 + public:
   1.115 +  void complete(jint res, bufferedStream* st);
   1.116 +
   1.117 +  void set_socket(int s)                                { _socket = s; }
   1.118 +  int socket() const                                    { return _socket; }
   1.119 +
   1.120 +  BsdAttachOperation(char* name) : AttachOperation(name) {
   1.121 +    set_socket(-1);
   1.122 +  }
   1.123 +};
   1.124 +
   1.125 +// statics
   1.126 +char BsdAttachListener::_path[UNIX_PATH_MAX];
   1.127 +bool BsdAttachListener::_has_path;
   1.128 +int BsdAttachListener::_listener = -1;
   1.129 +
   1.130 +// Supporting class to help split a buffer into individual components
   1.131 +class ArgumentIterator : public StackObj {
   1.132 + private:
   1.133 +  char* _pos;
   1.134 +  char* _end;
   1.135 + public:
   1.136 +  ArgumentIterator(char* arg_buffer, size_t arg_size) {
   1.137 +    _pos = arg_buffer;
   1.138 +    _end = _pos + arg_size - 1;
   1.139 +  }
   1.140 +  char* next() {
   1.141 +    if (*_pos == '\0') {
   1.142 +      return NULL;
   1.143 +    }
   1.144 +    char* res = _pos;
   1.145 +    char* next_pos = strchr(_pos, '\0');
   1.146 +    if (next_pos < _end)  {
   1.147 +      next_pos++;
   1.148 +    }
   1.149 +    _pos = next_pos;
   1.150 +    return res;
   1.151 +  }
   1.152 +};
   1.153 +
   1.154 +
   1.155 +// atexit hook to stop listener and unlink the file that it is
   1.156 +// bound too.
   1.157 +extern "C" {
   1.158 +  static void listener_cleanup() {
   1.159 +    static int cleanup_done;
   1.160 +    if (!cleanup_done) {
   1.161 +      cleanup_done = 1;
   1.162 +      int s = BsdAttachListener::listener();
   1.163 +      if (s != -1) {
   1.164 +        ::close(s);
   1.165 +      }
   1.166 +      if (BsdAttachListener::has_path()) {
   1.167 +        ::unlink(BsdAttachListener::path());
   1.168 +      }
   1.169 +    }
   1.170 +  }
   1.171 +}
   1.172 +
   1.173 +// Initialization - create a listener socket and bind it to a file
   1.174 +
   1.175 +int BsdAttachListener::init() {
   1.176 +  char path[UNIX_PATH_MAX];          // socket file
   1.177 +  char initial_path[UNIX_PATH_MAX];  // socket file during setup
   1.178 +  int listener;                      // listener socket (file descriptor)
   1.179 +
   1.180 +  // register function to cleanup
   1.181 +  ::atexit(listener_cleanup);
   1.182 +
   1.183 +  int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
   1.184 +                   os::get_temp_directory(), os::current_process_id());
   1.185 +  if (n < (int)UNIX_PATH_MAX) {
   1.186 +    n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
   1.187 +  }
   1.188 +  if (n >= (int)UNIX_PATH_MAX) {
   1.189 +    return -1;
   1.190 +  }
   1.191 +
   1.192 +  // create the listener socket
   1.193 +  listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
   1.194 +  if (listener == -1) {
   1.195 +    return -1;
   1.196 +  }
   1.197 +
   1.198 +  // bind socket
   1.199 +  struct sockaddr_un addr;
   1.200 +  addr.sun_family = AF_UNIX;
   1.201 +  strcpy(addr.sun_path, initial_path);
   1.202 +  ::unlink(initial_path);
   1.203 +  int res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr));
   1.204 +  if (res == -1) {
   1.205 +    ::close(listener);
   1.206 +    return -1;
   1.207 +  }
   1.208 +
   1.209 +  // put in listen mode, set permissions, and rename into place
   1.210 +  res = ::listen(listener, 5);
   1.211 +  if (res == 0) {
   1.212 +    RESTARTABLE(::chmod(initial_path, S_IREAD|S_IWRITE), res);
   1.213 +    if (res == 0) {
   1.214 +      // make sure the file is owned by the effective user and effective group
   1.215 +      // (this is the default on linux, but not on mac os)
   1.216 +      RESTARTABLE(::chown(initial_path, geteuid(), getegid()), res);
   1.217 +      if (res == 0) {
   1.218 +        res = ::rename(initial_path, path);
   1.219 +      }
   1.220 +    }
   1.221 +  }
   1.222 +  if (res == -1) {
   1.223 +    ::close(listener);
   1.224 +    ::unlink(initial_path);
   1.225 +    return -1;
   1.226 +  }
   1.227 +  set_path(path);
   1.228 +  set_listener(listener);
   1.229 +
   1.230 +  return 0;
   1.231 +}
   1.232 +
   1.233 +// Given a socket that is connected to a peer we read the request and
   1.234 +// create an AttachOperation. As the socket is blocking there is potential
   1.235 +// for a denial-of-service if the peer does not response. However this happens
   1.236 +// after the peer credentials have been checked and in the worst case it just
   1.237 +// means that the attach listener thread is blocked.
   1.238 +//
   1.239 +BsdAttachOperation* BsdAttachListener::read_request(int s) {
   1.240 +  char ver_str[8];
   1.241 +  sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
   1.242 +
   1.243 +  // The request is a sequence of strings so we first figure out the
   1.244 +  // expected count and the maximum possible length of the request.
   1.245 +  // The request is:
   1.246 +  //   <ver>0<cmd>0<arg>0<arg>0<arg>0
   1.247 +  // where <ver> is the protocol version (1), <cmd> is the command
   1.248 +  // name ("load", "datadump", ...), and <arg> is an argument
   1.249 +  int expected_str_count = 2 + AttachOperation::arg_count_max;
   1.250 +  const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) +
   1.251 +    AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1);
   1.252 +
   1.253 +  char buf[max_len];
   1.254 +  int str_count = 0;
   1.255 +
   1.256 +  // Read until all (expected) strings have been read, the buffer is
   1.257 +  // full, or EOF.
   1.258 +
   1.259 +  int off = 0;
   1.260 +  int left = max_len;
   1.261 +
   1.262 +  do {
   1.263 +    int n;
   1.264 +    RESTARTABLE(read(s, buf+off, left), n);
   1.265 +    if (n == -1) {
   1.266 +      return NULL;      // reset by peer or other error
   1.267 +    }
   1.268 +    if (n == 0) {
   1.269 +      break;
   1.270 +    }
   1.271 +    for (int i=0; i<n; i++) {
   1.272 +      if (buf[off+i] == 0) {
   1.273 +        // EOS found
   1.274 +        str_count++;
   1.275 +
   1.276 +        // The first string is <ver> so check it now to
   1.277 +        // check for protocol mis-match
   1.278 +        if (str_count == 1) {
   1.279 +          if ((strlen(buf) != strlen(ver_str)) ||
   1.280 +              (atoi(buf) != ATTACH_PROTOCOL_VER)) {
   1.281 +            char msg[32];
   1.282 +            sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
   1.283 +            write_fully(s, msg, strlen(msg));
   1.284 +            return NULL;
   1.285 +          }
   1.286 +        }
   1.287 +      }
   1.288 +    }
   1.289 +    off += n;
   1.290 +    left -= n;
   1.291 +  } while (left > 0 && str_count < expected_str_count);
   1.292 +
   1.293 +  if (str_count != expected_str_count) {
   1.294 +    return NULL;        // incomplete request
   1.295 +  }
   1.296 +
   1.297 +  // parse request
   1.298 +
   1.299 +  ArgumentIterator args(buf, (max_len)-left);
   1.300 +
   1.301 +  // version already checked
   1.302 +  char* v = args.next();
   1.303 +
   1.304 +  char* name = args.next();
   1.305 +  if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
   1.306 +    return NULL;
   1.307 +  }
   1.308 +
   1.309 +  BsdAttachOperation* op = new BsdAttachOperation(name);
   1.310 +
   1.311 +  for (int i=0; i<AttachOperation::arg_count_max; i++) {
   1.312 +    char* arg = args.next();
   1.313 +    if (arg == NULL) {
   1.314 +      op->set_arg(i, NULL);
   1.315 +    } else {
   1.316 +      if (strlen(arg) > AttachOperation::arg_length_max) {
   1.317 +        delete op;
   1.318 +        return NULL;
   1.319 +      }
   1.320 +      op->set_arg(i, arg);
   1.321 +    }
   1.322 +  }
   1.323 +
   1.324 +  op->set_socket(s);
   1.325 +  return op;
   1.326 +}
   1.327 +
   1.328 +
   1.329 +// Dequeue an operation
   1.330 +//
   1.331 +// In the Bsd implementation there is only a single operation and clients
   1.332 +// cannot queue commands (except at the socket level).
   1.333 +//
   1.334 +BsdAttachOperation* BsdAttachListener::dequeue() {
   1.335 +  for (;;) {
   1.336 +    int s;
   1.337 +
   1.338 +    // wait for client to connect
   1.339 +    struct sockaddr addr;
   1.340 +    socklen_t len = sizeof(addr);
   1.341 +    RESTARTABLE(::accept(listener(), &addr, &len), s);
   1.342 +    if (s == -1) {
   1.343 +      return NULL;      // log a warning?
   1.344 +    }
   1.345 +
   1.346 +    // get the credentials of the peer and check the effective uid/guid
   1.347 +    // - check with jeff on this.
   1.348 +    uid_t puid;
   1.349 +    gid_t pgid;
   1.350 +    if (::getpeereid(s, &puid, &pgid) != 0) {
   1.351 +      ::close(s);
   1.352 +      continue;
   1.353 +    }
   1.354 +    uid_t euid = geteuid();
   1.355 +    gid_t egid = getegid();
   1.356 +
   1.357 +    if (puid != euid || pgid != egid) {
   1.358 +      ::close(s);
   1.359 +      continue;
   1.360 +    }
   1.361 +
   1.362 +    // peer credential look okay so we read the request
   1.363 +    BsdAttachOperation* op = read_request(s);
   1.364 +    if (op == NULL) {
   1.365 +      ::close(s);
   1.366 +      continue;
   1.367 +    } else {
   1.368 +      return op;
   1.369 +    }
   1.370 +  }
   1.371 +}
   1.372 +
   1.373 +// write the given buffer to the socket
   1.374 +int BsdAttachListener::write_fully(int s, char* buf, int len) {
   1.375 +  do {
   1.376 +    int n = ::write(s, buf, len);
   1.377 +    if (n == -1) {
   1.378 +      if (errno != EINTR) return -1;
   1.379 +    } else {
   1.380 +      buf += n;
   1.381 +      len -= n;
   1.382 +    }
   1.383 +  }
   1.384 +  while (len > 0);
   1.385 +  return 0;
   1.386 +}
   1.387 +
   1.388 +// Complete an operation by sending the operation result and any result
   1.389 +// output to the client. At this time the socket is in blocking mode so
   1.390 +// potentially we can block if there is a lot of data and the client is
   1.391 +// non-responsive. For most operations this is a non-issue because the
   1.392 +// default send buffer is sufficient to buffer everything. In the future
   1.393 +// if there are operations that involves a very big reply then it the
   1.394 +// socket could be made non-blocking and a timeout could be used.
   1.395 +
   1.396 +void BsdAttachOperation::complete(jint result, bufferedStream* st) {
   1.397 +  JavaThread* thread = JavaThread::current();
   1.398 +  ThreadBlockInVM tbivm(thread);
   1.399 +
   1.400 +  thread->set_suspend_equivalent();
   1.401 +  // cleared by handle_special_suspend_equivalent_condition() or
   1.402 +  // java_suspend_self() via check_and_wait_while_suspended()
   1.403 +
   1.404 +  // write operation result
   1.405 +  char msg[32];
   1.406 +  sprintf(msg, "%d\n", result);
   1.407 +  int rc = BsdAttachListener::write_fully(this->socket(), msg, strlen(msg));
   1.408 +
   1.409 +  // write any result data
   1.410 +  if (rc == 0) {
   1.411 +    BsdAttachListener::write_fully(this->socket(), (char*) st->base(), st->size());
   1.412 +    ::shutdown(this->socket(), 2);
   1.413 +  }
   1.414 +
   1.415 +  // done
   1.416 +  ::close(this->socket());
   1.417 +
   1.418 +  // were we externally suspended while we were waiting?
   1.419 +  thread->check_and_wait_while_suspended();
   1.420 +
   1.421 +  delete this;
   1.422 +}
   1.423 +
   1.424 +
   1.425 +// AttachListener functions
   1.426 +
   1.427 +AttachOperation* AttachListener::dequeue() {
   1.428 +  JavaThread* thread = JavaThread::current();
   1.429 +  ThreadBlockInVM tbivm(thread);
   1.430 +
   1.431 +  thread->set_suspend_equivalent();
   1.432 +  // cleared by handle_special_suspend_equivalent_condition() or
   1.433 +  // java_suspend_self() via check_and_wait_while_suspended()
   1.434 +
   1.435 +  AttachOperation* op = BsdAttachListener::dequeue();
   1.436 +
   1.437 +  // were we externally suspended while we were waiting?
   1.438 +  thread->check_and_wait_while_suspended();
   1.439 +
   1.440 +  return op;
   1.441 +}
   1.442 +
   1.443 +
   1.444 +// Performs initialization at vm startup
   1.445 +// For BSD we remove any stale .java_pid file which could cause
   1.446 +// an attaching process to think we are ready to receive on the
   1.447 +// domain socket before we are properly initialized
   1.448 +
   1.449 +void AttachListener::vm_start() {
   1.450 +  char fn[UNIX_PATH_MAX];
   1.451 +  struct stat st;
   1.452 +  int ret;
   1.453 +
   1.454 +  int n = snprintf(fn, UNIX_PATH_MAX, "%s/.java_pid%d",
   1.455 +           os::get_temp_directory(), os::current_process_id());
   1.456 +  assert(n < (int)UNIX_PATH_MAX, "java_pid file name buffer overflow");
   1.457 +
   1.458 +  RESTARTABLE(::stat(fn, &st), ret);
   1.459 +  if (ret == 0) {
   1.460 +    ret = ::unlink(fn);
   1.461 +    if (ret == -1) {
   1.462 +      debug_only(warning("failed to remove stale attach pid file at %s", fn));
   1.463 +    }
   1.464 +  }
   1.465 +}
   1.466 +
   1.467 +int AttachListener::pd_init() {
   1.468 +  JavaThread* thread = JavaThread::current();
   1.469 +  ThreadBlockInVM tbivm(thread);
   1.470 +
   1.471 +  thread->set_suspend_equivalent();
   1.472 +  // cleared by handle_special_suspend_equivalent_condition() or
   1.473 +  // java_suspend_self() via check_and_wait_while_suspended()
   1.474 +
   1.475 +  int ret_code = BsdAttachListener::init();
   1.476 +
   1.477 +  // were we externally suspended while we were waiting?
   1.478 +  thread->check_and_wait_while_suspended();
   1.479 +
   1.480 +  return ret_code;
   1.481 +}
   1.482 +
   1.483 +// Attach Listener is started lazily except in the case when
   1.484 +// +ReduseSignalUsage is used
   1.485 +bool AttachListener::init_at_startup() {
   1.486 +  if (ReduceSignalUsage) {
   1.487 +    return true;
   1.488 +  } else {
   1.489 +    return false;
   1.490 +  }
   1.491 +}
   1.492 +
   1.493 +// If the file .attach_pid<pid> exists in the working directory
   1.494 +// or /tmp then this is the trigger to start the attach mechanism
   1.495 +bool AttachListener::is_init_trigger() {
   1.496 +  if (init_at_startup() || is_initialized()) {
   1.497 +    return false;               // initialized at startup or already initialized
   1.498 +  }
   1.499 +  char path[PATH_MAX + 1];
   1.500 +  int ret;
   1.501 +  struct stat st;
   1.502 +
   1.503 +  snprintf(path, PATH_MAX + 1, "%s/.attach_pid%d",
   1.504 +           os::get_temp_directory(), os::current_process_id());
   1.505 +  RESTARTABLE(::stat(path, &st), ret);
   1.506 +  if (ret == 0) {
   1.507 +    // simple check to avoid starting the attach mechanism when
   1.508 +    // a bogus user creates the file
   1.509 +    if (st.st_uid == geteuid()) {
   1.510 +      init();
   1.511 +      return true;
   1.512 +    }
   1.513 +  }
   1.514 +  return false;
   1.515 +}
   1.516 +
   1.517 +// if VM aborts then remove listener
   1.518 +void AttachListener::abort() {
   1.519 +  listener_cleanup();
   1.520 +}
   1.521 +
   1.522 +void AttachListener::pd_data_dump() {
   1.523 +  os::signal_notify(SIGQUIT);
   1.524 +}
   1.525 +
   1.526 +AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
   1.527 +  return NULL;
   1.528 +}
   1.529 +
   1.530 +jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
   1.531 +  out->print_cr("flag '%s' cannot be changed", op->arg(0));
   1.532 +  return JNI_ERR;
   1.533 +}
   1.534 +
   1.535 +void AttachListener::pd_detachall() {
   1.536 +  // do nothing for now
   1.537 +}

mercurial