1 /* 2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 # include "incls/_precompiled.incl" 26 # include "incls/_attachListener_solaris.cpp.incl" 27 28 #include <door.h> 29 #include <string.h> 30 #include <signal.h> 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <sys/stat.h> 34 35 // stropts.h uses STR in stream ioctl defines 36 #undef STR 37 #include <stropts.h> 38 #undef STR 39 #define STR(a) #a 40 41 // The attach mechanism on Solaris is implemented using the Doors IPC 42 // mechanism. The first tool to attempt to attach causes the attach 43 // listener thread to startup. This thread creats a door that is 44 // associated with a function that enqueues an operation to the attach 45 // listener. The door is attached to a file in the file system so that 46 // client (tools) can locate it. To enqueue an operation to the VM the 47 // client calls through the door which invokes the enqueue function in 48 // this process. The credentials of the client are checked and if the 49 // effective uid matches this process then the operation is enqueued. 50 // When an operation completes the attach listener is required to send the 51 // operation result and any result data to the client. In this implementation 52 // the result is returned via a UNIX domain socket. A pair of connected 53 // sockets (socketpair) is created in the enqueue function and the file 54 // descriptor for one of the sockets is returned to the client as the 55 // return from the door call. The other end is retained in this process. 56 // When the operation completes the result is sent to the client and 57 // the socket is closed. 58 59 // forward reference 60 class SolarisAttachOperation; 61 62 class SolarisAttachListener: AllStatic { 63 private: 64 65 // the path to which we attach the door file descriptor 66 static char _door_path[PATH_MAX+1]; 67 static volatile bool _has_door_path; 68 69 // door descriptor returned by door_create 70 static int _door_descriptor; 71 72 static void set_door_path(char* path) { 73 if (path == NULL) { 74 _has_door_path = false; 75 } else { 76 strncpy(_door_path, path, PATH_MAX); 77 _door_path[PATH_MAX] = '\0'; // ensure it's nul terminated 78 _has_door_path = true; 79 } 80 } 81 82 static void set_door_descriptor(int dd) { _door_descriptor = dd; } 83 84 // mutex to protect operation list 85 static mutex_t _mutex; 86 87 // semaphore to wakeup listener thread 88 static sema_t _wakeup; 89 90 static mutex_t* mutex() { return &_mutex; } 91 static sema_t* wakeup() { return &_wakeup; } 92 93 // enqueued operation list 94 static SolarisAttachOperation* _head; 95 static SolarisAttachOperation* _tail; 96 97 static SolarisAttachOperation* head() { return _head; } 98 static void set_head(SolarisAttachOperation* head) { _head = head; } 99 100 static SolarisAttachOperation* tail() { return _tail; } 101 static void set_tail(SolarisAttachOperation* tail) { _tail = tail; } 102 103 // create the door 104 static int create_door(); 105 106 public: 107 enum { 108 ATTACH_PROTOCOL_VER = 1 // protocol version 109 }; 110 enum { 111 ATTACH_ERROR_BADREQUEST = 100, // error code returned by 112 ATTACH_ERROR_BADVERSION = 101, // the door call 113 ATTACH_ERROR_RESOURCE = 102, 114 ATTACH_ERROR_INTERNAL = 103, 115 ATTACH_ERROR_DENIED = 104 116 }; 117 118 // initialize the listener 119 static int init(); 120 121 static bool has_door_path() { return _has_door_path; } 122 static char* door_path() { return _door_path; } 123 static int door_descriptor() { return _door_descriptor; } 124 125 // enqueue an operation 126 static void enqueue(SolarisAttachOperation* op); 127 128 // dequeue an operation 129 static SolarisAttachOperation* dequeue(); 130 }; 131 132 133 // SolarisAttachOperation is an AttachOperation that additionally encapsulates 134 // a socket connection to the requesting client/tool. SolarisAttachOperation 135 // can additionally be held in a linked list. 136 137 class SolarisAttachOperation: public AttachOperation { 138 private: 139 friend class SolarisAttachListener; 140 141 // connection to client 142 int _socket; 143 144 // linked list support 145 SolarisAttachOperation* _next; 146 147 SolarisAttachOperation* next() { return _next; } 148 void set_next(SolarisAttachOperation* next) { _next = next; } 149 150 public: 151 void complete(jint res, bufferedStream* st); 152 153 int socket() const { return _socket; } 154 void set_socket(int s) { _socket = s; } 155 156 SolarisAttachOperation(char* name) : AttachOperation(name) { 157 set_socket(-1); 158 set_next(NULL); 159 } 160 }; 161 162 // statics 163 char SolarisAttachListener::_door_path[PATH_MAX+1]; 164 volatile bool SolarisAttachListener::_has_door_path; 165 int SolarisAttachListener::_door_descriptor = -1; 166 mutex_t SolarisAttachListener::_mutex; 167 sema_t SolarisAttachListener::_wakeup; 168 SolarisAttachOperation* SolarisAttachListener::_head = NULL; 169 SolarisAttachOperation* SolarisAttachListener::_tail = NULL; 170 171 // Supporting class to help split a buffer into individual components 172 class ArgumentIterator : public StackObj { 173 private: 174 char* _pos; 175 char* _end; 176 public: 177 ArgumentIterator(char* arg_buffer, size_t arg_size) { 178 _pos = arg_buffer; 179 _end = _pos + arg_size - 1; 180 } 181 char* next() { 182 if (*_pos == '\0') { 183 return NULL; 184 } 185 char* res = _pos; 186 char* next_pos = strchr(_pos, '\0'); 187 if (next_pos < _end) { 188 next_pos++; 189 } 190 _pos = next_pos; 191 return res; 192 } 193 }; 194 195 // Calls from the door function to check that the client credentials 196 // match this process. Returns 0 if credentials okay, otherwise -1. 197 static int check_credentials() { 198 door_cred_t cred_info; 199 200 // get client credentials 201 if (door_cred(&cred_info) == -1) { 202 return -1; // unable to get them 203 } 204 205 // get our euid/eguid (probably could cache these) 206 uid_t euid = geteuid(); 207 gid_t egid = getegid(); 208 209 // check that the effective uid/gid matches - discuss this with Jeff. 210 if (cred_info.dc_euid == euid && cred_info.dc_egid == egid) { 211 return 0; // okay 212 } else { 213 return -1; // denied 214 } 215 } 216 217 218 // Parses the argument buffer to create an AttachOperation that we should 219 // enqueue to the attach listener. 220 // The buffer is expected to be formatted as follows: 221 // <ver>0<cmd>0<arg>0<arg>0<arg>0 222 // where <ver> is the version number (must be "1"), <cmd> is the command 223 // name ("load, "datadump", ...) and <arg> is an argument. 224 // 225 static SolarisAttachOperation* create_operation(char* argp, size_t arg_size, int* err) { 226 // assume bad request until parsed 227 *err = SolarisAttachListener::ATTACH_ERROR_BADREQUEST; 228 229 if (arg_size < 2 || argp[arg_size-1] != '\0') { 230 return NULL; // no ver or not null terminated 231 } 232 233 // Use supporting class to iterate over the buffer 234 ArgumentIterator args(argp, arg_size); 235 236 // First check the protocol version 237 char* ver = args.next(); 238 if (ver == NULL) { 239 return NULL; 240 } 241 if (atoi(ver) != SolarisAttachListener::ATTACH_PROTOCOL_VER) { 242 *err = SolarisAttachListener::ATTACH_ERROR_BADVERSION; 243 return NULL; 244 } 245 246 // Get command name and create the operation 247 char* name = args.next(); 248 if (name == NULL || strlen(name) > AttachOperation::name_length_max) { 249 return NULL; 250 } 251 SolarisAttachOperation* op = new SolarisAttachOperation(name); 252 253 // Iterate over the arguments 254 for (int i=0; i<AttachOperation::arg_count_max; i++) { 255 char* arg = args.next(); 256 if (arg == NULL) { 257 op->set_arg(i, NULL); 258 } else { 259 if (strlen(arg) > AttachOperation::arg_length_max) { 260 delete op; 261 return NULL; 262 } 263 op->set_arg(i, arg); 264 } 265 } 266 267 // return operation 268 *err = 0; 269 return op; 270 } 271 272 // create special operation to indicate all clients have detached 273 static SolarisAttachOperation* create_detachall_operation() { 274 return new SolarisAttachOperation(AttachOperation::detachall_operation_name()); 275 } 276 277 // This is door function which the client executes via a door_call. 278 extern "C" { 279 static void enqueue_proc(void* cookie, char* argp, size_t arg_size, 280 door_desc_t* dt, uint_t n_desc) 281 { 282 int return_fd = -1; 283 SolarisAttachOperation* op = NULL; 284 285 // no listener 286 jint res = 0; 287 if (!AttachListener::is_initialized()) { 288 // how did we get here? 289 debug_only(warning("door_call when not enabled")); 290 res = (jint)SolarisAttachListener::ATTACH_ERROR_INTERNAL; 291 } 292 293 // check client credentials 294 if (res == 0) { 295 if (check_credentials() != 0) { 296 res = (jint)SolarisAttachListener::ATTACH_ERROR_DENIED; 297 } 298 } 299 300 // if we are stopped at ShowMessageBoxOnError then maybe we can 301 // load a diagnostic library 302 if (res == 0 && is_error_reported()) { 303 if (ShowMessageBoxOnError) { 304 // TBD - support loading of diagnostic library here 305 } 306 307 // can't enqueue operation after fatal error 308 res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE; 309 } 310 311 // create the operation 312 if (res == 0) { 313 int err; 314 op = create_operation(argp, arg_size, &err); 315 res = (op == NULL) ? (jint)err : 0; 316 } 317 318 // create a pair of connected sockets. Store the file descriptor 319 // for one end in the operation and enqueue the operation. The 320 // file descriptor for the other end wil be returned to the client. 321 if (res == 0) { 322 int s[2]; 323 if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) < 0) { 324 delete op; 325 res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE; 326 } else { 327 op->set_socket(s[0]); 328 return_fd = s[1]; 329 SolarisAttachListener::enqueue(op); 330 } 331 } 332 333 // Return 0 (success) + file descriptor, or non-0 (error) 334 if (res == 0) { 335 door_desc_t desc; 336 desc.d_attributes = DOOR_DESCRIPTOR; 337 desc.d_data.d_desc.d_descriptor = return_fd; 338 door_return((char*)&res, sizeof(res), &desc, 1); 339 } else { 340 door_return((char*)&res, sizeof(res), NULL, 0); 341 } 342 } 343 } 344 345 // atexit hook to detach the door and remove the file 346 extern "C" { 347 static void listener_cleanup() { 348 static int cleanup_done; 349 if (!cleanup_done) { 350 cleanup_done = 1; 351 int dd = SolarisAttachListener::door_descriptor(); 352 if (dd >= 0) { 353 ::close(dd); 354 } 355 if (SolarisAttachListener::has_door_path()) { 356 char* path = SolarisAttachListener::door_path(); 357 ::fdetach(path); 358 ::unlink(path); 359 } 360 } 361 } 362 } 363 364 // Create the door 365 int SolarisAttachListener::create_door() { 366 char door_path[PATH_MAX+1]; 367 char initial_path[PATH_MAX+1]; 368 int fd, res; 369 370 // register exit function 371 ::atexit(listener_cleanup); 372 373 // create the door descriptor 374 int dd = ::door_create(enqueue_proc, NULL, 0); 375 if (dd < 0) { 376 return -1; 377 } 378 379 // create initial file to attach door descriptor 380 snprintf(door_path, sizeof(door_path), "%s/.java_pid%d", 381 os::get_temp_directory(), os::current_process_id()); 382 snprintf(initial_path, sizeof(initial_path), "%s.tmp", door_path); 383 RESTARTABLE(::creat(initial_path, S_IRUSR | S_IWUSR), fd); 384 if (fd == -1) { 385 debug_only(warning("attempt to create %s failed", initial_path)); 386 ::door_revoke(dd); 387 return -1; 388 } 389 assert(fd >= 0, "bad file descriptor"); 390 RESTARTABLE(::close(fd), res); 391 392 // attach the door descriptor to the file 393 if ((res = ::fattach(dd, initial_path)) == -1) { 394 // if busy then detach and try again 395 if (errno == EBUSY) { 396 ::fdetach(initial_path); 397 res = ::fattach(dd, initial_path); 398 } 399 if (res == -1) { 400 ::door_revoke(dd); 401 dd = -1; 402 } 403 } 404 405 // rename file so that clients can attach 406 if (dd >= 0) { 407 if (::rename(initial_path, door_path) == -1) { 408 RESTARTABLE(::close(dd), res); 409 ::fdetach(initial_path); 410 dd = -1; 411 } 412 } 413 if (dd >= 0) { 414 set_door_descriptor(dd); 415 set_door_path(door_path); 416 } else { 417 // unable to create door, attach it to file, or rename file into place 418 ::unlink(initial_path); 419 return -1; 420 } 421 422 return 0; 423 } 424 425 // Initialization - create the door, locks, and other initialization 426 int SolarisAttachListener::init() { 427 if (create_door()) { 428 return -1; 429 } 430 431 int status = os::Solaris::mutex_init(&_mutex); 432 assert_status(status==0, status, "mutex_init"); 433 434 status = ::sema_init(&_wakeup, 0, NULL, NULL); 435 assert_status(status==0, status, "sema_init"); 436 437 set_head(NULL); 438 set_tail(NULL); 439 440 return 0; 441 } 442 443 // Dequeue an operation 444 SolarisAttachOperation* SolarisAttachListener::dequeue() { 445 for (;;) { 446 int res; 447 448 // wait for somebody to enqueue something 449 while ((res = ::sema_wait(wakeup())) == EINTR) 450 ; 451 if (res) { 452 warning("sema_wait failed: %s", strerror(res)); 453 return NULL; 454 } 455 456 // lock the list 457 res = os::Solaris::mutex_lock(mutex()); 458 assert(res == 0, "mutex_lock failed"); 459 460 // remove the head of the list 461 SolarisAttachOperation* op = head(); 462 if (op != NULL) { 463 set_head(op->next()); 464 if (head() == NULL) { 465 set_tail(NULL); 466 } 467 } 468 469 // unlock 470 os::Solaris::mutex_unlock(mutex()); 471 472 // if we got an operation when return it. 473 if (op != NULL) { 474 return op; 475 } 476 } 477 } 478 479 // Enqueue an operation 480 void SolarisAttachListener::enqueue(SolarisAttachOperation* op) { 481 // lock list 482 int res = os::Solaris::mutex_lock(mutex()); 483 assert(res == 0, "mutex_lock failed"); 484 485 // enqueue at tail 486 op->set_next(NULL); 487 if (head() == NULL) { 488 set_head(op); 489 } else { 490 tail()->set_next(op); 491 } 492 set_tail(op); 493 494 // wakeup the attach listener 495 RESTARTABLE(::sema_post(wakeup()), res); 496 assert(res == 0, "sema_post failed"); 497 498 // unlock 499 os::Solaris::mutex_unlock(mutex()); 500 } 501 502 503 // support function - writes the (entire) buffer to a socket 504 static int write_fully(int s, char* buf, int len) { 505 do { 506 int n = ::write(s, buf, len); 507 if (n == -1) { 508 if (errno != EINTR) return -1; 509 } else { 510 buf += n; 511 len -= n; 512 } 513 } 514 while (len > 0); 515 return 0; 516 } 517 518 // Complete an operation by sending the operation result and any result 519 // output to the client. At this time the socket is in blocking mode so 520 // potentially we can block if there is a lot of data and the client is 521 // non-responsive. For most operations this is a non-issue because the 522 // default send buffer is sufficient to buffer everything. In the future 523 // if there are operations that involves a very big reply then it the 524 // socket could be made non-blocking and a timeout could be used. 525 526 void SolarisAttachOperation::complete(jint res, bufferedStream* st) { 527 if (this->socket() >= 0) { 528 JavaThread* thread = JavaThread::current(); 529 ThreadBlockInVM tbivm(thread); 530 531 thread->set_suspend_equivalent(); 532 // cleared by handle_special_suspend_equivalent_condition() or 533 // java_suspend_self() via check_and_wait_while_suspended() 534 535 // write operation result 536 char msg[32]; 537 sprintf(msg, "%d\n", res); 538 int rc = write_fully(this->socket(), msg, strlen(msg)); 539 540 // write any result data 541 if (rc == 0) { 542 write_fully(this->socket(), (char*) st->base(), st->size()); 543 ::shutdown(this->socket(), 2); 544 } 545 546 // close socket and we're done 547 RESTARTABLE(::close(this->socket()), rc); 548 549 // were we externally suspended while we were waiting? 550 thread->check_and_wait_while_suspended(); 551 } 552 delete this; 553 } 554 555 556 // AttachListener functions 557 558 AttachOperation* AttachListener::dequeue() { 559 JavaThread* thread = JavaThread::current(); 560 ThreadBlockInVM tbivm(thread); 561 562 thread->set_suspend_equivalent(); 563 // cleared by handle_special_suspend_equivalent_condition() or 564 // java_suspend_self() via check_and_wait_while_suspended() 565 566 AttachOperation* op = SolarisAttachListener::dequeue(); 567 568 // were we externally suspended while we were waiting? 569 thread->check_and_wait_while_suspended(); 570 571 return op; 572 } 573 574 int AttachListener::pd_init() { 575 JavaThread* thread = JavaThread::current(); 576 ThreadBlockInVM tbivm(thread); 577 578 thread->set_suspend_equivalent(); 579 // cleared by handle_special_suspend_equivalent_condition() or 580 // java_suspend_self() 581 582 int ret_code = SolarisAttachListener::init(); 583 584 // were we externally suspended while we were waiting? 585 thread->check_and_wait_while_suspended(); 586 587 return ret_code; 588 } 589 590 // Attach Listener is started lazily except in the case when 591 // +ReduseSignalUsage is used 592 bool AttachListener::init_at_startup() { 593 if (ReduceSignalUsage) { 594 return true; 595 } else { 596 return false; 597 } 598 } 599 600 // If the file .attach_pid<pid> exists in the working directory 601 // or /tmp then this is the trigger to start the attach mechanism 602 bool AttachListener::is_init_trigger() { 603 if (init_at_startup() || is_initialized()) { 604 return false; // initialized at startup or already initialized 605 } 606 char fn[PATH_MAX+1]; 607 sprintf(fn, ".attach_pid%d", os::current_process_id()); 608 int ret; 609 struct stat64 st; 610 RESTARTABLE(::stat64(fn, &st), ret); 611 if (ret == -1) { 612 snprintf(fn, sizeof(fn), "%s/.attach_pid%d", 613 os::get_temp_directory(), os::current_process_id()); 614 RESTARTABLE(::stat64(fn, &st), ret); 615 } 616 if (ret == 0) { 617 // simple check to avoid starting the attach mechanism when 618 // a bogus user creates the file 619 if (st.st_uid == geteuid()) { 620 init(); 621 return true; 622 } 623 } 624 return false; 625 } 626 627 // if VM aborts then detach/cleanup 628 void AttachListener::abort() { 629 listener_cleanup(); 630 } 631 632 void AttachListener::pd_data_dump() { 633 os::signal_notify(SIGQUIT); 634 } 635 636 static jint enable_dprobes(AttachOperation* op, outputStream* out) { 637 const char* probe = op->arg(0); 638 if (probe == NULL || probe[0] == '\0') { 639 out->print_cr("No probe specified"); 640 return JNI_ERR; 641 } else { 642 int probe_typess = atoi(probe); 643 if (errno) { 644 out->print_cr("invalid probe type"); 645 return JNI_ERR; 646 } else { 647 DTrace::enable_dprobes(probe_typess); 648 return JNI_OK; 649 } 650 } 651 } 652 653 // platform specific operations table 654 static AttachOperationFunctionInfo funcs[] = { 655 { "enabledprobes", enable_dprobes }, 656 { NULL, NULL } 657 }; 658 659 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* name) { 660 int i; 661 for (i = 0; funcs[i].name != NULL; i++) { 662 if (strcmp(funcs[i].name, name) == 0) { 663 return &funcs[i]; 664 } 665 } 666 return NULL; 667 } 668 669 // Solaris specific global flag set. Currently, we support only 670 // changing ExtendedDTraceProbes flag. 671 jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) { 672 const char* name = op->arg(0); 673 assert(name != NULL, "flag name should not be null"); 674 bool flag = true; 675 const char* arg1; 676 if ((arg1 = op->arg(1)) != NULL) { 677 flag = (atoi(arg1) != 0); 678 if (errno) { 679 out->print_cr("flag value has to be an integer"); 680 return JNI_ERR; 681 } 682 } 683 684 if (strcmp(name, "ExtendedDTraceProbes") == 0) { 685 DTrace::set_extended_dprobes(flag); 686 return JNI_OK; 687 } 688 689 if (strcmp(name, "DTraceMonitorProbes") == 0) { 690 DTrace::set_monitor_dprobes(flag); 691 return JNI_OK; 692 } 693 694 out->print_cr("flag '%s' cannot be changed", name); 695 return JNI_ERR; 696 } 697 698 void AttachListener::pd_detachall() { 699 DTrace::detach_all_clients(); 700 }