src/os/solaris/vm/attachListener_solaris.cpp

Mon, 19 Apr 2010 18:58:31 -0400

author
coleenp
date
Mon, 19 Apr 2010 18:58:31 -0400
changeset 1852
96d554193f72
parent 1788
a2ea687fdc7c
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6944822: Fix for 6938627 exposes problem with hard-coded buffer sizes
Summary: Make tmpdir buffer sizes MAX_PATH+1
Reviewed-by: dholmes, coleenp
Contributed-by: andreas.kohn@fredhopper.com

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

mercurial