src/os/linux/vm/attachListener_linux.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 485
485d403e94e1
permissions
-rw-r--r--

Initial load

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

mercurial