src/os/linux/vm/attachListener_linux.cpp

Fri, 16 Jul 2010 13:14:03 +0100

author
alanb
date
Fri, 16 Jul 2010 13:14:03 +0100
changeset 2030
a81afd9c293c
parent 1907
c18cbe5936b8
child 2223
3dc12ef8735e
permissions
-rw-r--r--

6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
Reviewed-by: dcubed, dholmes

duke@435 1 /*
trims@1907 2 * Copyright (c) 2005, 2008, 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
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_attachListener_linux.cpp.incl"
duke@435 27
duke@435 28 #include <unistd.h>
duke@435 29 #include <signal.h>
duke@435 30 #include <sys/types.h>
duke@435 31 #include <sys/socket.h>
duke@435 32 #include <sys/un.h>
duke@435 33 #include <sys/stat.h>
duke@435 34
alanb@2030 35 #ifndef UNIX_PATH_MAX
alanb@2030 36 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
alanb@2030 37 #endif
alanb@2030 38
duke@435 39 // The attach mechanism on Linux uses a UNIX domain socket. An attach listener
duke@435 40 // thread is created at startup or is created on-demand via a signal from
duke@435 41 // the client tool. The attach listener creates a socket and binds it to a file
duke@435 42 // in the filesystem. The attach listener then acts as a simple (single-
alanb@2030 43 // threaded) server - it waits for a client to connect, reads the request,
duke@435 44 // executes it, and returns the response to the client via the socket
duke@435 45 // connection.
duke@435 46 //
duke@435 47 // As the socket is a UNIX domain socket it means that only clients on the
duke@435 48 // local machine can connect. In addition there are two other aspects to
duke@435 49 // the security:
duke@435 50 // 1. The well known file that the socket is bound to has permission 400
duke@435 51 // 2. When a client connect, the SO_PEERCRED socket option is used to
duke@435 52 // obtain the credentials of client. We check that the effective uid
duke@435 53 // of the client matches this process.
duke@435 54
duke@435 55 // forward reference
duke@435 56 class LinuxAttachOperation;
duke@435 57
duke@435 58 class LinuxAttachListener: AllStatic {
duke@435 59 private:
duke@435 60 // the path to which we bind the UNIX domain socket
alanb@2030 61 static char _path[UNIX_PATH_MAX];
duke@435 62 static bool _has_path;
duke@435 63
duke@435 64 // the file descriptor for the listening socket
duke@435 65 static int _listener;
duke@435 66
duke@435 67 static void set_path(char* path) {
duke@435 68 if (path == NULL) {
duke@435 69 _has_path = false;
duke@435 70 } else {
alanb@2030 71 strncpy(_path, path, UNIX_PATH_MAX);
alanb@2030 72 _path[UNIX_PATH_MAX-1] = '\0';
duke@435 73 _has_path = true;
duke@435 74 }
duke@435 75 }
duke@435 76
duke@435 77 static void set_listener(int s) { _listener = s; }
duke@435 78
duke@435 79 // reads a request from the given connected socket
duke@435 80 static LinuxAttachOperation* read_request(int s);
duke@435 81
duke@435 82 public:
duke@435 83 enum {
duke@435 84 ATTACH_PROTOCOL_VER = 1 // protocol version
duke@435 85 };
duke@435 86 enum {
duke@435 87 ATTACH_ERROR_BADVERSION = 101 // error codes
duke@435 88 };
duke@435 89
duke@435 90 // initialize the listener, returns 0 if okay
duke@435 91 static int init();
duke@435 92
duke@435 93 static char* path() { return _path; }
duke@435 94 static bool has_path() { return _has_path; }
duke@435 95 static int listener() { return _listener; }
duke@435 96
duke@435 97 // write the given buffer to a socket
duke@435 98 static int write_fully(int s, char* buf, int len);
duke@435 99
duke@435 100 static LinuxAttachOperation* dequeue();
duke@435 101 };
duke@435 102
duke@435 103 class LinuxAttachOperation: public AttachOperation {
duke@435 104 private:
duke@435 105 // the connection to the client
duke@435 106 int _socket;
duke@435 107
duke@435 108 public:
duke@435 109 void complete(jint res, bufferedStream* st);
duke@435 110
duke@435 111 void set_socket(int s) { _socket = s; }
duke@435 112 int socket() const { return _socket; }
duke@435 113
duke@435 114 LinuxAttachOperation(char* name) : AttachOperation(name) {
duke@435 115 set_socket(-1);
duke@435 116 }
duke@435 117 };
duke@435 118
duke@435 119 // statics
alanb@2030 120 char LinuxAttachListener::_path[UNIX_PATH_MAX];
duke@435 121 bool LinuxAttachListener::_has_path;
duke@435 122 int LinuxAttachListener::_listener = -1;
duke@435 123
duke@435 124 // Supporting class to help split a buffer into individual components
duke@435 125 class ArgumentIterator : public StackObj {
duke@435 126 private:
duke@435 127 char* _pos;
duke@435 128 char* _end;
duke@435 129 public:
duke@435 130 ArgumentIterator(char* arg_buffer, size_t arg_size) {
duke@435 131 _pos = arg_buffer;
duke@435 132 _end = _pos + arg_size - 1;
duke@435 133 }
duke@435 134 char* next() {
duke@435 135 if (*_pos == '\0') {
duke@435 136 return NULL;
duke@435 137 }
duke@435 138 char* res = _pos;
duke@435 139 char* next_pos = strchr(_pos, '\0');
duke@435 140 if (next_pos < _end) {
duke@435 141 next_pos++;
duke@435 142 }
duke@435 143 _pos = next_pos;
duke@435 144 return res;
duke@435 145 }
duke@435 146 };
duke@435 147
duke@435 148
duke@435 149 // atexit hook to stop listener and unlink the file that it is
duke@435 150 // bound too.
duke@435 151 extern "C" {
duke@435 152 static void listener_cleanup() {
duke@435 153 static int cleanup_done;
duke@435 154 if (!cleanup_done) {
duke@435 155 cleanup_done = 1;
duke@435 156 int s = LinuxAttachListener::listener();
duke@435 157 if (s != -1) {
duke@435 158 ::close(s);
duke@435 159 }
duke@435 160 if (LinuxAttachListener::has_path()) {
duke@435 161 ::unlink(LinuxAttachListener::path());
duke@435 162 }
duke@435 163 }
duke@435 164 }
duke@435 165 }
duke@435 166
duke@435 167 // Initialization - create a listener socket and bind it to a file
duke@435 168
duke@435 169 int LinuxAttachListener::init() {
alanb@2030 170 char path[UNIX_PATH_MAX]; // socket file
alanb@2030 171 char initial_path[UNIX_PATH_MAX]; // socket file during setup
alanb@2030 172 int listener; // listener socket (file descriptor)
duke@435 173
duke@435 174 // register function to cleanup
duke@435 175 ::atexit(listener_cleanup);
duke@435 176
alanb@2030 177 int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
alanb@2030 178 os::get_temp_directory(), os::current_process_id());
alanb@2030 179 if (n <= (int)UNIX_PATH_MAX) {
alanb@2030 180 n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
alanb@2030 181 }
alanb@2030 182 if (n > (int)UNIX_PATH_MAX) {
alanb@2030 183 return -1;
alanb@2030 184 }
alanb@2030 185
duke@435 186 // create the listener socket
duke@435 187 listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
duke@435 188 if (listener == -1) {
duke@435 189 return -1;
duke@435 190 }
duke@435 191
alanb@2030 192 // bind socket
duke@435 193 struct sockaddr_un addr;
duke@435 194 addr.sun_family = AF_UNIX;
alanb@2030 195 strcpy(addr.sun_path, initial_path);
alanb@2030 196 ::unlink(initial_path);
alanb@2030 197 int res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr));
duke@435 198 if (res == -1) {
duke@435 199 RESTARTABLE(::close(listener), res);
duke@435 200 return -1;
duke@435 201 }
duke@435 202
alanb@2030 203 // put in listen mode, set permissions, and rename into place
alanb@2030 204 res = ::listen(listener, 5);
alanb@2030 205 if (res == 0) {
alanb@2030 206 RESTARTABLE(::chmod(initial_path, S_IREAD|S_IWRITE), res);
alanb@2030 207 if (res == 0) {
alanb@2030 208 res = ::rename(initial_path, path);
alanb@2030 209 }
alanb@2030 210 }
alanb@2030 211 if (res == -1) {
duke@435 212 RESTARTABLE(::close(listener), res);
alanb@2030 213 ::unlink(initial_path);
duke@435 214 return -1;
duke@435 215 }
alanb@2030 216 set_path(path);
duke@435 217 set_listener(listener);
duke@435 218
duke@435 219 return 0;
duke@435 220 }
duke@435 221
duke@435 222 // Given a socket that is connected to a peer we read the request and
duke@435 223 // create an AttachOperation. As the socket is blocking there is potential
duke@435 224 // for a denial-of-service if the peer does not response. However this happens
duke@435 225 // after the peer credentials have been checked and in the worst case it just
duke@435 226 // means that the attach listener thread is blocked.
duke@435 227 //
duke@435 228 LinuxAttachOperation* LinuxAttachListener::read_request(int s) {
duke@435 229 char ver_str[8];
duke@435 230 sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
duke@435 231
duke@435 232 // The request is a sequence of strings so we first figure out the
duke@435 233 // expected count and the maximum possible length of the request.
duke@435 234 // The request is:
duke@435 235 // <ver>0<cmd>0<arg>0<arg>0<arg>0
duke@435 236 // where <ver> is the protocol version (1), <cmd> is the command
duke@435 237 // name ("load", "datadump", ...), and <arg> is an argument
duke@435 238 int expected_str_count = 2 + AttachOperation::arg_count_max;
dcubed@485 239 const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) +
duke@435 240 AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1);
duke@435 241
duke@435 242 char buf[max_len];
duke@435 243 int str_count = 0;
duke@435 244
duke@435 245 // Read until all (expected) strings have been read, the buffer is
duke@435 246 // full, or EOF.
duke@435 247
duke@435 248 int off = 0;
duke@435 249 int left = max_len;
duke@435 250
duke@435 251 do {
duke@435 252 int n;
duke@435 253 RESTARTABLE(read(s, buf+off, left), n);
duke@435 254 if (n == -1) {
duke@435 255 return NULL; // reset by peer or other error
duke@435 256 }
duke@435 257 if (n == 0) {
duke@435 258 break;
duke@435 259 }
duke@435 260 for (int i=0; i<n; i++) {
duke@435 261 if (buf[off+i] == 0) {
duke@435 262 // EOS found
duke@435 263 str_count++;
duke@435 264
duke@435 265 // The first string is <ver> so check it now to
duke@435 266 // check for protocol mis-match
duke@435 267 if (str_count == 1) {
duke@435 268 if ((strlen(buf) != strlen(ver_str)) ||
duke@435 269 (atoi(buf) != ATTACH_PROTOCOL_VER)) {
duke@435 270 char msg[32];
duke@435 271 sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
duke@435 272 write_fully(s, msg, strlen(msg));
duke@435 273 return NULL;
duke@435 274 }
duke@435 275 }
duke@435 276 }
duke@435 277 }
duke@435 278 off += n;
duke@435 279 left -= n;
duke@435 280 } while (left > 0 && str_count < expected_str_count);
duke@435 281
duke@435 282 if (str_count != expected_str_count) {
duke@435 283 return NULL; // incomplete request
duke@435 284 }
duke@435 285
duke@435 286 // parse request
duke@435 287
duke@435 288 ArgumentIterator args(buf, (max_len)-left);
duke@435 289
duke@435 290 // version already checked
duke@435 291 char* v = args.next();
duke@435 292
duke@435 293 char* name = args.next();
duke@435 294 if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
duke@435 295 return NULL;
duke@435 296 }
duke@435 297
duke@435 298 LinuxAttachOperation* op = new LinuxAttachOperation(name);
duke@435 299
duke@435 300 for (int i=0; i<AttachOperation::arg_count_max; i++) {
duke@435 301 char* arg = args.next();
duke@435 302 if (arg == NULL) {
duke@435 303 op->set_arg(i, NULL);
duke@435 304 } else {
duke@435 305 if (strlen(arg) > AttachOperation::arg_length_max) {
duke@435 306 delete op;
duke@435 307 return NULL;
duke@435 308 }
duke@435 309 op->set_arg(i, arg);
duke@435 310 }
duke@435 311 }
duke@435 312
duke@435 313 op->set_socket(s);
duke@435 314 return op;
duke@435 315 }
duke@435 316
duke@435 317
duke@435 318 // Dequeue an operation
duke@435 319 //
duke@435 320 // In the Linux implementation there is only a single operation and clients
duke@435 321 // cannot queue commands (except at the socket level).
duke@435 322 //
duke@435 323 LinuxAttachOperation* LinuxAttachListener::dequeue() {
duke@435 324 for (;;) {
duke@435 325 int s;
duke@435 326
duke@435 327 // wait for client to connect
duke@435 328 struct sockaddr addr;
duke@435 329 socklen_t len = sizeof(addr);
duke@435 330 RESTARTABLE(::accept(listener(), &addr, &len), s);
duke@435 331 if (s == -1) {
duke@435 332 return NULL; // log a warning?
duke@435 333 }
duke@435 334
duke@435 335 // get the credentials of the peer and check the effective uid/guid
duke@435 336 // - check with jeff on this.
duke@435 337 struct ucred cred_info;
duke@435 338 socklen_t optlen = sizeof(cred_info);
duke@435 339 if (::getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void*)&cred_info, &optlen) == -1) {
duke@435 340 int res;
duke@435 341 RESTARTABLE(::close(s), res);
duke@435 342 continue;
duke@435 343 }
duke@435 344 uid_t euid = geteuid();
duke@435 345 gid_t egid = getegid();
duke@435 346
duke@435 347 if (cred_info.uid != euid || cred_info.gid != egid) {
duke@435 348 int res;
duke@435 349 RESTARTABLE(::close(s), res);
duke@435 350 continue;
duke@435 351 }
duke@435 352
duke@435 353 // peer credential look okay so we read the request
duke@435 354 LinuxAttachOperation* op = read_request(s);
duke@435 355 if (op == NULL) {
duke@435 356 int res;
duke@435 357 RESTARTABLE(::close(s), res);
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
duke@435 408 RESTARTABLE(::close(this->socket()), rc);
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
duke@435 435 int AttachListener::pd_init() {
duke@435 436 JavaThread* thread = JavaThread::current();
duke@435 437 ThreadBlockInVM tbivm(thread);
duke@435 438
duke@435 439 thread->set_suspend_equivalent();
duke@435 440 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 441 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 442
duke@435 443 int ret_code = LinuxAttachListener::init();
duke@435 444
duke@435 445 // were we externally suspended while we were waiting?
duke@435 446 thread->check_and_wait_while_suspended();
duke@435 447
duke@435 448 return ret_code;
duke@435 449 }
duke@435 450
duke@435 451 // Attach Listener is started lazily except in the case when
duke@435 452 // +ReduseSignalUsage is used
duke@435 453 bool AttachListener::init_at_startup() {
duke@435 454 if (ReduceSignalUsage) {
duke@435 455 return true;
duke@435 456 } else {
duke@435 457 return false;
duke@435 458 }
duke@435 459 }
duke@435 460
duke@435 461 // If the file .attach_pid<pid> exists in the working directory
duke@435 462 // or /tmp then this is the trigger to start the attach mechanism
duke@435 463 bool AttachListener::is_init_trigger() {
duke@435 464 if (init_at_startup() || is_initialized()) {
duke@435 465 return false; // initialized at startup or already initialized
duke@435 466 }
coleenp@1852 467 char fn[PATH_MAX+1];
duke@435 468 sprintf(fn, ".attach_pid%d", os::current_process_id());
duke@435 469 int ret;
duke@435 470 struct stat64 st;
duke@435 471 RESTARTABLE(::stat64(fn, &st), ret);
duke@435 472 if (ret == -1) {
coleenp@1788 473 snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
coleenp@1788 474 os::get_temp_directory(), os::current_process_id());
duke@435 475 RESTARTABLE(::stat64(fn, &st), ret);
duke@435 476 }
duke@435 477 if (ret == 0) {
duke@435 478 // simple check to avoid starting the attach mechanism when
duke@435 479 // a bogus user creates the file
duke@435 480 if (st.st_uid == geteuid()) {
duke@435 481 init();
duke@435 482 return true;
duke@435 483 }
duke@435 484 }
duke@435 485 return false;
duke@435 486 }
duke@435 487
duke@435 488 // if VM aborts then remove listener
duke@435 489 void AttachListener::abort() {
duke@435 490 listener_cleanup();
duke@435 491 }
duke@435 492
duke@435 493 void AttachListener::pd_data_dump() {
duke@435 494 os::signal_notify(SIGQUIT);
duke@435 495 }
duke@435 496
duke@435 497 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
duke@435 498 return NULL;
duke@435 499 }
duke@435 500
duke@435 501 jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
duke@435 502 out->print_cr("flag '%s' cannot be changed", op->arg(0));
duke@435 503 return JNI_ERR;
duke@435 504 }
duke@435 505
duke@435 506 void AttachListener::pd_detachall() {
duke@435 507 // do nothing for now
duke@435 508 }

mercurial