src/os/linux/vm/attachListener_linux.cpp

Thu, 13 Mar 2014 14:57:01 -0700

author
kvn
date
Thu, 13 Mar 2014 14:57:01 -0700
changeset 6513
bbfbe9b06038
parent 5412
2e8f19c2feef
child 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

duke@435 1 /*
allwin@5412 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "runtime/interfaceSupport.hpp"
stefank@2314 27 #include "runtime/os.hpp"
stefank@2314 28 #include "services/attachListener.hpp"
stefank@2314 29 #include "services/dtraceAttacher.hpp"
duke@435 30
duke@435 31 #include <unistd.h>
duke@435 32 #include <signal.h>
duke@435 33 #include <sys/types.h>
duke@435 34 #include <sys/socket.h>
duke@435 35 #include <sys/un.h>
duke@435 36 #include <sys/stat.h>
duke@435 37
alanb@2030 38 #ifndef UNIX_PATH_MAX
alanb@2030 39 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
alanb@2030 40 #endif
alanb@2030 41
duke@435 42 // The attach mechanism on Linux uses a UNIX domain socket. An attach listener
duke@435 43 // thread is created at startup or is created on-demand via a signal from
duke@435 44 // the client tool. The attach listener creates a socket and binds it to a file
duke@435 45 // in the filesystem. The attach listener then acts as a simple (single-
alanb@2030 46 // threaded) server - it waits for a client to connect, reads the request,
duke@435 47 // executes it, and returns the response to the client via the socket
duke@435 48 // connection.
duke@435 49 //
duke@435 50 // As the socket is a UNIX domain socket it means that only clients on the
duke@435 51 // local machine can connect. In addition there are two other aspects to
duke@435 52 // the security:
duke@435 53 // 1. The well known file that the socket is bound to has permission 400
duke@435 54 // 2. When a client connect, the SO_PEERCRED socket option is used to
duke@435 55 // obtain the credentials of client. We check that the effective uid
duke@435 56 // of the client matches this process.
duke@435 57
duke@435 58 // forward reference
duke@435 59 class LinuxAttachOperation;
duke@435 60
duke@435 61 class LinuxAttachListener: AllStatic {
duke@435 62 private:
duke@435 63 // the path to which we bind the UNIX domain socket
alanb@2030 64 static char _path[UNIX_PATH_MAX];
duke@435 65 static bool _has_path;
duke@435 66
duke@435 67 // the file descriptor for the listening socket
duke@435 68 static int _listener;
duke@435 69
duke@435 70 static void set_path(char* path) {
duke@435 71 if (path == NULL) {
duke@435 72 _has_path = false;
duke@435 73 } else {
alanb@2030 74 strncpy(_path, path, UNIX_PATH_MAX);
alanb@2030 75 _path[UNIX_PATH_MAX-1] = '\0';
duke@435 76 _has_path = true;
duke@435 77 }
duke@435 78 }
duke@435 79
duke@435 80 static void set_listener(int s) { _listener = s; }
duke@435 81
duke@435 82 // reads a request from the given connected socket
duke@435 83 static LinuxAttachOperation* read_request(int s);
duke@435 84
duke@435 85 public:
duke@435 86 enum {
duke@435 87 ATTACH_PROTOCOL_VER = 1 // protocol version
duke@435 88 };
duke@435 89 enum {
duke@435 90 ATTACH_ERROR_BADVERSION = 101 // error codes
duke@435 91 };
duke@435 92
duke@435 93 // initialize the listener, returns 0 if okay
duke@435 94 static int init();
duke@435 95
duke@435 96 static char* path() { return _path; }
duke@435 97 static bool has_path() { return _has_path; }
duke@435 98 static int listener() { return _listener; }
duke@435 99
duke@435 100 // write the given buffer to a socket
duke@435 101 static int write_fully(int s, char* buf, int len);
duke@435 102
duke@435 103 static LinuxAttachOperation* dequeue();
duke@435 104 };
duke@435 105
duke@435 106 class LinuxAttachOperation: public AttachOperation {
duke@435 107 private:
duke@435 108 // the connection to the client
duke@435 109 int _socket;
duke@435 110
duke@435 111 public:
duke@435 112 void complete(jint res, bufferedStream* st);
duke@435 113
duke@435 114 void set_socket(int s) { _socket = s; }
duke@435 115 int socket() const { return _socket; }
duke@435 116
duke@435 117 LinuxAttachOperation(char* name) : AttachOperation(name) {
duke@435 118 set_socket(-1);
duke@435 119 }
duke@435 120 };
duke@435 121
duke@435 122 // statics
alanb@2030 123 char LinuxAttachListener::_path[UNIX_PATH_MAX];
duke@435 124 bool LinuxAttachListener::_has_path;
duke@435 125 int LinuxAttachListener::_listener = -1;
duke@435 126
duke@435 127 // Supporting class to help split a buffer into individual components
duke@435 128 class ArgumentIterator : public StackObj {
duke@435 129 private:
duke@435 130 char* _pos;
duke@435 131 char* _end;
duke@435 132 public:
duke@435 133 ArgumentIterator(char* arg_buffer, size_t arg_size) {
duke@435 134 _pos = arg_buffer;
duke@435 135 _end = _pos + arg_size - 1;
duke@435 136 }
duke@435 137 char* next() {
duke@435 138 if (*_pos == '\0') {
duke@435 139 return NULL;
duke@435 140 }
duke@435 141 char* res = _pos;
duke@435 142 char* next_pos = strchr(_pos, '\0');
duke@435 143 if (next_pos < _end) {
duke@435 144 next_pos++;
duke@435 145 }
duke@435 146 _pos = next_pos;
duke@435 147 return res;
duke@435 148 }
duke@435 149 };
duke@435 150
duke@435 151
duke@435 152 // atexit hook to stop listener and unlink the file that it is
duke@435 153 // bound too.
duke@435 154 extern "C" {
duke@435 155 static void listener_cleanup() {
duke@435 156 static int cleanup_done;
duke@435 157 if (!cleanup_done) {
duke@435 158 cleanup_done = 1;
duke@435 159 int s = LinuxAttachListener::listener();
duke@435 160 if (s != -1) {
duke@435 161 ::close(s);
duke@435 162 }
duke@435 163 if (LinuxAttachListener::has_path()) {
duke@435 164 ::unlink(LinuxAttachListener::path());
duke@435 165 }
duke@435 166 }
duke@435 167 }
duke@435 168 }
duke@435 169
duke@435 170 // Initialization - create a listener socket and bind it to a file
duke@435 171
duke@435 172 int LinuxAttachListener::init() {
alanb@2030 173 char path[UNIX_PATH_MAX]; // socket file
alanb@2030 174 char initial_path[UNIX_PATH_MAX]; // socket file during setup
alanb@2030 175 int listener; // listener socket (file descriptor)
duke@435 176
duke@435 177 // register function to cleanup
duke@435 178 ::atexit(listener_cleanup);
duke@435 179
alanb@2030 180 int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
alanb@2030 181 os::get_temp_directory(), os::current_process_id());
bobv@2223 182 if (n < (int)UNIX_PATH_MAX) {
alanb@2030 183 n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
alanb@2030 184 }
bobv@2223 185 if (n >= (int)UNIX_PATH_MAX) {
alanb@2030 186 return -1;
alanb@2030 187 }
alanb@2030 188
duke@435 189 // create the listener socket
duke@435 190 listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
duke@435 191 if (listener == -1) {
duke@435 192 return -1;
duke@435 193 }
duke@435 194
alanb@2030 195 // bind socket
duke@435 196 struct sockaddr_un addr;
duke@435 197 addr.sun_family = AF_UNIX;
alanb@2030 198 strcpy(addr.sun_path, initial_path);
alanb@2030 199 ::unlink(initial_path);
alanb@2030 200 int res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr));
duke@435 201 if (res == -1) {
rdurbin@5264 202 ::close(listener);
duke@435 203 return -1;
duke@435 204 }
duke@435 205
alanb@2030 206 // put in listen mode, set permissions, and rename into place
alanb@2030 207 res = ::listen(listener, 5);
alanb@2030 208 if (res == 0) {
alanb@2030 209 RESTARTABLE(::chmod(initial_path, S_IREAD|S_IWRITE), res);
alanb@2030 210 if (res == 0) {
alanb@2030 211 res = ::rename(initial_path, path);
alanb@2030 212 }
alanb@2030 213 }
alanb@2030 214 if (res == -1) {
rdurbin@5264 215 ::close(listener);
alanb@2030 216 ::unlink(initial_path);
duke@435 217 return -1;
duke@435 218 }
alanb@2030 219 set_path(path);
duke@435 220 set_listener(listener);
duke@435 221
duke@435 222 return 0;
duke@435 223 }
duke@435 224
duke@435 225 // Given a socket that is connected to a peer we read the request and
duke@435 226 // create an AttachOperation. As the socket is blocking there is potential
duke@435 227 // for a denial-of-service if the peer does not response. However this happens
duke@435 228 // after the peer credentials have been checked and in the worst case it just
duke@435 229 // means that the attach listener thread is blocked.
duke@435 230 //
duke@435 231 LinuxAttachOperation* LinuxAttachListener::read_request(int s) {
duke@435 232 char ver_str[8];
duke@435 233 sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
duke@435 234
duke@435 235 // The request is a sequence of strings so we first figure out the
duke@435 236 // expected count and the maximum possible length of the request.
duke@435 237 // The request is:
duke@435 238 // <ver>0<cmd>0<arg>0<arg>0<arg>0
duke@435 239 // where <ver> is the protocol version (1), <cmd> is the command
duke@435 240 // name ("load", "datadump", ...), and <arg> is an argument
duke@435 241 int expected_str_count = 2 + AttachOperation::arg_count_max;
dcubed@485 242 const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) +
duke@435 243 AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1);
duke@435 244
duke@435 245 char buf[max_len];
duke@435 246 int str_count = 0;
duke@435 247
duke@435 248 // Read until all (expected) strings have been read, the buffer is
duke@435 249 // full, or EOF.
duke@435 250
duke@435 251 int off = 0;
duke@435 252 int left = max_len;
duke@435 253
duke@435 254 do {
duke@435 255 int n;
duke@435 256 RESTARTABLE(read(s, buf+off, left), n);
duke@435 257 if (n == -1) {
duke@435 258 return NULL; // reset by peer or other error
duke@435 259 }
duke@435 260 if (n == 0) {
duke@435 261 break;
duke@435 262 }
duke@435 263 for (int i=0; i<n; i++) {
duke@435 264 if (buf[off+i] == 0) {
duke@435 265 // EOS found
duke@435 266 str_count++;
duke@435 267
duke@435 268 // The first string is <ver> so check it now to
duke@435 269 // check for protocol mis-match
duke@435 270 if (str_count == 1) {
duke@435 271 if ((strlen(buf) != strlen(ver_str)) ||
duke@435 272 (atoi(buf) != ATTACH_PROTOCOL_VER)) {
duke@435 273 char msg[32];
duke@435 274 sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
duke@435 275 write_fully(s, msg, strlen(msg));
duke@435 276 return NULL;
duke@435 277 }
duke@435 278 }
duke@435 279 }
duke@435 280 }
duke@435 281 off += n;
duke@435 282 left -= n;
duke@435 283 } while (left > 0 && str_count < expected_str_count);
duke@435 284
duke@435 285 if (str_count != expected_str_count) {
duke@435 286 return NULL; // incomplete request
duke@435 287 }
duke@435 288
duke@435 289 // parse request
duke@435 290
duke@435 291 ArgumentIterator args(buf, (max_len)-left);
duke@435 292
duke@435 293 // version already checked
duke@435 294 char* v = args.next();
duke@435 295
duke@435 296 char* name = args.next();
duke@435 297 if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
duke@435 298 return NULL;
duke@435 299 }
duke@435 300
duke@435 301 LinuxAttachOperation* op = new LinuxAttachOperation(name);
duke@435 302
duke@435 303 for (int i=0; i<AttachOperation::arg_count_max; i++) {
duke@435 304 char* arg = args.next();
duke@435 305 if (arg == NULL) {
duke@435 306 op->set_arg(i, NULL);
duke@435 307 } else {
duke@435 308 if (strlen(arg) > AttachOperation::arg_length_max) {
duke@435 309 delete op;
duke@435 310 return NULL;
duke@435 311 }
duke@435 312 op->set_arg(i, arg);
duke@435 313 }
duke@435 314 }
duke@435 315
duke@435 316 op->set_socket(s);
duke@435 317 return op;
duke@435 318 }
duke@435 319
duke@435 320
duke@435 321 // Dequeue an operation
duke@435 322 //
duke@435 323 // In the Linux implementation there is only a single operation and clients
duke@435 324 // cannot queue commands (except at the socket level).
duke@435 325 //
duke@435 326 LinuxAttachOperation* LinuxAttachListener::dequeue() {
duke@435 327 for (;;) {
duke@435 328 int s;
duke@435 329
duke@435 330 // wait for client to connect
duke@435 331 struct sockaddr addr;
duke@435 332 socklen_t len = sizeof(addr);
duke@435 333 RESTARTABLE(::accept(listener(), &addr, &len), s);
duke@435 334 if (s == -1) {
duke@435 335 return NULL; // log a warning?
duke@435 336 }
duke@435 337
duke@435 338 // get the credentials of the peer and check the effective uid/guid
duke@435 339 // - check with jeff on this.
duke@435 340 struct ucred cred_info;
duke@435 341 socklen_t optlen = sizeof(cred_info);
duke@435 342 if (::getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void*)&cred_info, &optlen) == -1) {
rdurbin@5264 343 ::close(s);
duke@435 344 continue;
duke@435 345 }
duke@435 346 uid_t euid = geteuid();
duke@435 347 gid_t egid = getegid();
duke@435 348
duke@435 349 if (cred_info.uid != euid || cred_info.gid != egid) {
rdurbin@5264 350 ::close(s);
duke@435 351 continue;
duke@435 352 }
duke@435 353
duke@435 354 // peer credential look okay so we read the request
duke@435 355 LinuxAttachOperation* op = read_request(s);
duke@435 356 if (op == NULL) {
rdurbin@5264 357 ::close(s);
duke@435 358 continue;
duke@435 359 } else {
duke@435 360 return op;
duke@435 361 }
duke@435 362 }
duke@435 363 }
duke@435 364
duke@435 365 // write the given buffer to the socket
duke@435 366 int LinuxAttachListener::write_fully(int s, char* buf, int len) {
duke@435 367 do {
duke@435 368 int n = ::write(s, buf, len);
duke@435 369 if (n == -1) {
duke@435 370 if (errno != EINTR) return -1;
duke@435 371 } else {
duke@435 372 buf += n;
duke@435 373 len -= n;
duke@435 374 }
duke@435 375 }
duke@435 376 while (len > 0);
duke@435 377 return 0;
duke@435 378 }
duke@435 379
duke@435 380 // Complete an operation by sending the operation result and any result
duke@435 381 // output to the client. At this time the socket is in blocking mode so
duke@435 382 // potentially we can block if there is a lot of data and the client is
duke@435 383 // non-responsive. For most operations this is a non-issue because the
duke@435 384 // default send buffer is sufficient to buffer everything. In the future
duke@435 385 // if there are operations that involves a very big reply then it the
duke@435 386 // socket could be made non-blocking and a timeout could be used.
duke@435 387
duke@435 388 void LinuxAttachOperation::complete(jint result, bufferedStream* st) {
duke@435 389 JavaThread* thread = JavaThread::current();
duke@435 390 ThreadBlockInVM tbivm(thread);
duke@435 391
duke@435 392 thread->set_suspend_equivalent();
duke@435 393 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 394 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 395
duke@435 396 // write operation result
duke@435 397 char msg[32];
duke@435 398 sprintf(msg, "%d\n", result);
duke@435 399 int rc = LinuxAttachListener::write_fully(this->socket(), msg, strlen(msg));
duke@435 400
duke@435 401 // write any result data
duke@435 402 if (rc == 0) {
duke@435 403 LinuxAttachListener::write_fully(this->socket(), (char*) st->base(), st->size());
duke@435 404 ::shutdown(this->socket(), 2);
duke@435 405 }
duke@435 406
duke@435 407 // done
rdurbin@5264 408 ::close(this->socket());
duke@435 409
duke@435 410 // were we externally suspended while we were waiting?
duke@435 411 thread->check_and_wait_while_suspended();
duke@435 412
duke@435 413 delete this;
duke@435 414 }
duke@435 415
duke@435 416
duke@435 417 // AttachListener functions
duke@435 418
duke@435 419 AttachOperation* AttachListener::dequeue() {
duke@435 420 JavaThread* thread = JavaThread::current();
duke@435 421 ThreadBlockInVM tbivm(thread);
duke@435 422
duke@435 423 thread->set_suspend_equivalent();
duke@435 424 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 425 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 426
duke@435 427 AttachOperation* op = LinuxAttachListener::dequeue();
duke@435 428
duke@435 429 // were we externally suspended while we were waiting?
duke@435 430 thread->check_and_wait_while_suspended();
duke@435 431
duke@435 432 return op;
duke@435 433 }
duke@435 434
allwin@5412 435
allwin@5412 436 // Performs initialization at vm startup
allwin@5412 437 // For Linux we remove any stale .java_pid file which could cause
allwin@5412 438 // an attaching process to think we are ready to receive on the
allwin@5412 439 // domain socket before we are properly initialized
allwin@5412 440
allwin@5412 441 void AttachListener::vm_start() {
allwin@5412 442 char fn[UNIX_PATH_MAX];
allwin@5412 443 struct stat64 st;
allwin@5412 444 int ret;
allwin@5412 445
allwin@5412 446 int n = snprintf(fn, UNIX_PATH_MAX, "%s/.java_pid%d",
allwin@5412 447 os::get_temp_directory(), os::current_process_id());
allwin@5412 448 assert(n < (int)UNIX_PATH_MAX, "java_pid file name buffer overflow");
allwin@5412 449
allwin@5412 450 RESTARTABLE(::stat64(fn, &st), ret);
allwin@5412 451 if (ret == 0) {
allwin@5412 452 ret = ::unlink(fn);
allwin@5412 453 if (ret == -1) {
allwin@5412 454 debug_only(warning("failed to remove stale attach pid file at %s", fn));
allwin@5412 455 }
allwin@5412 456 }
allwin@5412 457 }
allwin@5412 458
duke@435 459 int AttachListener::pd_init() {
duke@435 460 JavaThread* thread = JavaThread::current();
duke@435 461 ThreadBlockInVM tbivm(thread);
duke@435 462
duke@435 463 thread->set_suspend_equivalent();
duke@435 464 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 465 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 466
duke@435 467 int ret_code = LinuxAttachListener::init();
duke@435 468
duke@435 469 // were we externally suspended while we were waiting?
duke@435 470 thread->check_and_wait_while_suspended();
duke@435 471
duke@435 472 return ret_code;
duke@435 473 }
duke@435 474
duke@435 475 // Attach Listener is started lazily except in the case when
duke@435 476 // +ReduseSignalUsage is used
duke@435 477 bool AttachListener::init_at_startup() {
duke@435 478 if (ReduceSignalUsage) {
duke@435 479 return true;
duke@435 480 } else {
duke@435 481 return false;
duke@435 482 }
duke@435 483 }
duke@435 484
duke@435 485 // If the file .attach_pid<pid> exists in the working directory
duke@435 486 // or /tmp then this is the trigger to start the attach mechanism
duke@435 487 bool AttachListener::is_init_trigger() {
duke@435 488 if (init_at_startup() || is_initialized()) {
duke@435 489 return false; // initialized at startup or already initialized
duke@435 490 }
coleenp@1852 491 char fn[PATH_MAX+1];
duke@435 492 sprintf(fn, ".attach_pid%d", os::current_process_id());
duke@435 493 int ret;
duke@435 494 struct stat64 st;
duke@435 495 RESTARTABLE(::stat64(fn, &st), ret);
duke@435 496 if (ret == -1) {
coleenp@1788 497 snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
coleenp@1788 498 os::get_temp_directory(), os::current_process_id());
duke@435 499 RESTARTABLE(::stat64(fn, &st), ret);
duke@435 500 }
duke@435 501 if (ret == 0) {
duke@435 502 // simple check to avoid starting the attach mechanism when
duke@435 503 // a bogus user creates the file
duke@435 504 if (st.st_uid == geteuid()) {
duke@435 505 init();
duke@435 506 return true;
duke@435 507 }
duke@435 508 }
duke@435 509 return false;
duke@435 510 }
duke@435 511
duke@435 512 // if VM aborts then remove listener
duke@435 513 void AttachListener::abort() {
duke@435 514 listener_cleanup();
duke@435 515 }
duke@435 516
duke@435 517 void AttachListener::pd_data_dump() {
duke@435 518 os::signal_notify(SIGQUIT);
duke@435 519 }
duke@435 520
duke@435 521 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
duke@435 522 return NULL;
duke@435 523 }
duke@435 524
duke@435 525 jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
duke@435 526 out->print_cr("flag '%s' cannot be changed", op->arg(0));
duke@435 527 return JNI_ERR;
duke@435 528 }
duke@435 529
duke@435 530 void AttachListener::pd_detachall() {
duke@435 531 // do nothing for now
duke@435 532 }

mercurial