annotate src/os/linux/vm/attachListener_linux.cpp @ 196:d1605aabd0a1

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