src/os/linux/vm/attachListener_linux.cpp

Wed, 31 Mar 2010 16:51:18 -0700

author
coleenp
date
Wed, 31 Mar 2010 16:51:18 -0700
changeset 1788
a2ea687fdc7c
parent 631
d1605aabd0a1
child 1852
96d554193f72
permissions
-rw-r--r--

6938627: Make temporary directory use property java.io.tmpdir when specified
Summary: Get java.io.tmpdir property in os::get_temp_directory() and call this instead of harcoding "/tmp". Don't assume trailing file_separator either.
Reviewed-by: dholmes, kamg

duke@435 1 /*
xdono@631 2 * Copyright 2005-2008 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) {
coleenp@1788 195 snprintf(path, PATH_MAX+1, "%s/.java_pid%d",
coleenp@1788 196 os::get_temp_directory(), os::current_process_id());
duke@435 197 strcpy(addr.sun_path, path);
duke@435 198 ::unlink(path);
duke@435 199 res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr));
duke@435 200 }
duke@435 201 if (res == -1) {
duke@435 202 RESTARTABLE(::close(listener), res);
duke@435 203 return -1;
duke@435 204 }
duke@435 205 set_path(path);
duke@435 206
duke@435 207 // put in listen mode and set permission
duke@435 208 if ((::listen(listener, 5) == -1) || (::chmod(path, S_IREAD|S_IWRITE) == -1)) {
duke@435 209 RESTARTABLE(::close(listener), res);
duke@435 210 ::unlink(path);
duke@435 211 set_path(NULL);
duke@435 212 return -1;
duke@435 213 }
duke@435 214 set_listener(listener);
duke@435 215
duke@435 216 return 0;
duke@435 217 }
duke@435 218
duke@435 219 // Given a socket that is connected to a peer we read the request and
duke@435 220 // create an AttachOperation. As the socket is blocking there is potential
duke@435 221 // for a denial-of-service if the peer does not response. However this happens
duke@435 222 // after the peer credentials have been checked and in the worst case it just
duke@435 223 // means that the attach listener thread is blocked.
duke@435 224 //
duke@435 225 LinuxAttachOperation* LinuxAttachListener::read_request(int s) {
duke@435 226 char ver_str[8];
duke@435 227 sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
duke@435 228
duke@435 229 // The request is a sequence of strings so we first figure out the
duke@435 230 // expected count and the maximum possible length of the request.
duke@435 231 // The request is:
duke@435 232 // <ver>0<cmd>0<arg>0<arg>0<arg>0
duke@435 233 // where <ver> is the protocol version (1), <cmd> is the command
duke@435 234 // name ("load", "datadump", ...), and <arg> is an argument
duke@435 235 int expected_str_count = 2 + AttachOperation::arg_count_max;
dcubed@485 236 const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) +
duke@435 237 AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1);
duke@435 238
duke@435 239 char buf[max_len];
duke@435 240 int str_count = 0;
duke@435 241
duke@435 242 // Read until all (expected) strings have been read, the buffer is
duke@435 243 // full, or EOF.
duke@435 244
duke@435 245 int off = 0;
duke@435 246 int left = max_len;
duke@435 247
duke@435 248 do {
duke@435 249 int n;
duke@435 250 RESTARTABLE(read(s, buf+off, left), n);
duke@435 251 if (n == -1) {
duke@435 252 return NULL; // reset by peer or other error
duke@435 253 }
duke@435 254 if (n == 0) {
duke@435 255 break;
duke@435 256 }
duke@435 257 for (int i=0; i<n; i++) {
duke@435 258 if (buf[off+i] == 0) {
duke@435 259 // EOS found
duke@435 260 str_count++;
duke@435 261
duke@435 262 // The first string is <ver> so check it now to
duke@435 263 // check for protocol mis-match
duke@435 264 if (str_count == 1) {
duke@435 265 if ((strlen(buf) != strlen(ver_str)) ||
duke@435 266 (atoi(buf) != ATTACH_PROTOCOL_VER)) {
duke@435 267 char msg[32];
duke@435 268 sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
duke@435 269 write_fully(s, msg, strlen(msg));
duke@435 270 return NULL;
duke@435 271 }
duke@435 272 }
duke@435 273 }
duke@435 274 }
duke@435 275 off += n;
duke@435 276 left -= n;
duke@435 277 } while (left > 0 && str_count < expected_str_count);
duke@435 278
duke@435 279 if (str_count != expected_str_count) {
duke@435 280 return NULL; // incomplete request
duke@435 281 }
duke@435 282
duke@435 283 // parse request
duke@435 284
duke@435 285 ArgumentIterator args(buf, (max_len)-left);
duke@435 286
duke@435 287 // version already checked
duke@435 288 char* v = args.next();
duke@435 289
duke@435 290 char* name = args.next();
duke@435 291 if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
duke@435 292 return NULL;
duke@435 293 }
duke@435 294
duke@435 295 LinuxAttachOperation* op = new LinuxAttachOperation(name);
duke@435 296
duke@435 297 for (int i=0; i<AttachOperation::arg_count_max; i++) {
duke@435 298 char* arg = args.next();
duke@435 299 if (arg == NULL) {
duke@435 300 op->set_arg(i, NULL);
duke@435 301 } else {
duke@435 302 if (strlen(arg) > AttachOperation::arg_length_max) {
duke@435 303 delete op;
duke@435 304 return NULL;
duke@435 305 }
duke@435 306 op->set_arg(i, arg);
duke@435 307 }
duke@435 308 }
duke@435 309
duke@435 310 op->set_socket(s);
duke@435 311 return op;
duke@435 312 }
duke@435 313
duke@435 314
duke@435 315 // Dequeue an operation
duke@435 316 //
duke@435 317 // In the Linux implementation there is only a single operation and clients
duke@435 318 // cannot queue commands (except at the socket level).
duke@435 319 //
duke@435 320 LinuxAttachOperation* LinuxAttachListener::dequeue() {
duke@435 321 for (;;) {
duke@435 322 int s;
duke@435 323
duke@435 324 // wait for client to connect
duke@435 325 struct sockaddr addr;
duke@435 326 socklen_t len = sizeof(addr);
duke@435 327 RESTARTABLE(::accept(listener(), &addr, &len), s);
duke@435 328 if (s == -1) {
duke@435 329 return NULL; // log a warning?
duke@435 330 }
duke@435 331
duke@435 332 // get the credentials of the peer and check the effective uid/guid
duke@435 333 // - check with jeff on this.
duke@435 334 struct ucred cred_info;
duke@435 335 socklen_t optlen = sizeof(cred_info);
duke@435 336 if (::getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void*)&cred_info, &optlen) == -1) {
duke@435 337 int res;
duke@435 338 RESTARTABLE(::close(s), res);
duke@435 339 continue;
duke@435 340 }
duke@435 341 uid_t euid = geteuid();
duke@435 342 gid_t egid = getegid();
duke@435 343
duke@435 344 if (cred_info.uid != euid || cred_info.gid != egid) {
duke@435 345 int res;
duke@435 346 RESTARTABLE(::close(s), res);
duke@435 347 continue;
duke@435 348 }
duke@435 349
duke@435 350 // peer credential look okay so we read the request
duke@435 351 LinuxAttachOperation* op = read_request(s);
duke@435 352 if (op == NULL) {
duke@435 353 int res;
duke@435 354 RESTARTABLE(::close(s), res);
duke@435 355 continue;
duke@435 356 } else {
duke@435 357 return op;
duke@435 358 }
duke@435 359 }
duke@435 360 }
duke@435 361
duke@435 362 // write the given buffer to the socket
duke@435 363 int LinuxAttachListener::write_fully(int s, char* buf, int len) {
duke@435 364 do {
duke@435 365 int n = ::write(s, buf, len);
duke@435 366 if (n == -1) {
duke@435 367 if (errno != EINTR) return -1;
duke@435 368 } else {
duke@435 369 buf += n;
duke@435 370 len -= n;
duke@435 371 }
duke@435 372 }
duke@435 373 while (len > 0);
duke@435 374 return 0;
duke@435 375 }
duke@435 376
duke@435 377 // Complete an operation by sending the operation result and any result
duke@435 378 // output to the client. At this time the socket is in blocking mode so
duke@435 379 // potentially we can block if there is a lot of data and the client is
duke@435 380 // non-responsive. For most operations this is a non-issue because the
duke@435 381 // default send buffer is sufficient to buffer everything. In the future
duke@435 382 // if there are operations that involves a very big reply then it the
duke@435 383 // socket could be made non-blocking and a timeout could be used.
duke@435 384
duke@435 385 void LinuxAttachOperation::complete(jint result, bufferedStream* st) {
duke@435 386 JavaThread* thread = JavaThread::current();
duke@435 387 ThreadBlockInVM tbivm(thread);
duke@435 388
duke@435 389 thread->set_suspend_equivalent();
duke@435 390 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 391 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 392
duke@435 393 // write operation result
duke@435 394 char msg[32];
duke@435 395 sprintf(msg, "%d\n", result);
duke@435 396 int rc = LinuxAttachListener::write_fully(this->socket(), msg, strlen(msg));
duke@435 397
duke@435 398 // write any result data
duke@435 399 if (rc == 0) {
duke@435 400 LinuxAttachListener::write_fully(this->socket(), (char*) st->base(), st->size());
duke@435 401 ::shutdown(this->socket(), 2);
duke@435 402 }
duke@435 403
duke@435 404 // done
duke@435 405 RESTARTABLE(::close(this->socket()), rc);
duke@435 406
duke@435 407 // were we externally suspended while we were waiting?
duke@435 408 thread->check_and_wait_while_suspended();
duke@435 409
duke@435 410 delete this;
duke@435 411 }
duke@435 412
duke@435 413
duke@435 414 // AttachListener functions
duke@435 415
duke@435 416 AttachOperation* AttachListener::dequeue() {
duke@435 417 JavaThread* thread = JavaThread::current();
duke@435 418 ThreadBlockInVM tbivm(thread);
duke@435 419
duke@435 420 thread->set_suspend_equivalent();
duke@435 421 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 422 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 423
duke@435 424 AttachOperation* op = LinuxAttachListener::dequeue();
duke@435 425
duke@435 426 // were we externally suspended while we were waiting?
duke@435 427 thread->check_and_wait_while_suspended();
duke@435 428
duke@435 429 return op;
duke@435 430 }
duke@435 431
duke@435 432 int AttachListener::pd_init() {
duke@435 433 JavaThread* thread = JavaThread::current();
duke@435 434 ThreadBlockInVM tbivm(thread);
duke@435 435
duke@435 436 thread->set_suspend_equivalent();
duke@435 437 // cleared by handle_special_suspend_equivalent_condition() or
duke@435 438 // java_suspend_self() via check_and_wait_while_suspended()
duke@435 439
duke@435 440 int ret_code = LinuxAttachListener::init();
duke@435 441
duke@435 442 // were we externally suspended while we were waiting?
duke@435 443 thread->check_and_wait_while_suspended();
duke@435 444
duke@435 445 return ret_code;
duke@435 446 }
duke@435 447
duke@435 448 // Attach Listener is started lazily except in the case when
duke@435 449 // +ReduseSignalUsage is used
duke@435 450 bool AttachListener::init_at_startup() {
duke@435 451 if (ReduceSignalUsage) {
duke@435 452 return true;
duke@435 453 } else {
duke@435 454 return false;
duke@435 455 }
duke@435 456 }
duke@435 457
duke@435 458 // If the file .attach_pid<pid> exists in the working directory
duke@435 459 // or /tmp then this is the trigger to start the attach mechanism
duke@435 460 bool AttachListener::is_init_trigger() {
duke@435 461 if (init_at_startup() || is_initialized()) {
duke@435 462 return false; // initialized at startup or already initialized
duke@435 463 }
coleenp@1788 464 char fn[128];
duke@435 465 sprintf(fn, ".attach_pid%d", os::current_process_id());
duke@435 466 int ret;
duke@435 467 struct stat64 st;
duke@435 468 RESTARTABLE(::stat64(fn, &st), ret);
duke@435 469 if (ret == -1) {
coleenp@1788 470 snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
coleenp@1788 471 os::get_temp_directory(), os::current_process_id());
duke@435 472 RESTARTABLE(::stat64(fn, &st), ret);
duke@435 473 }
duke@435 474 if (ret == 0) {
duke@435 475 // simple check to avoid starting the attach mechanism when
duke@435 476 // a bogus user creates the file
duke@435 477 if (st.st_uid == geteuid()) {
duke@435 478 init();
duke@435 479 return true;
duke@435 480 }
duke@435 481 }
duke@435 482 return false;
duke@435 483 }
duke@435 484
duke@435 485 // if VM aborts then remove listener
duke@435 486 void AttachListener::abort() {
duke@435 487 listener_cleanup();
duke@435 488 }
duke@435 489
duke@435 490 void AttachListener::pd_data_dump() {
duke@435 491 os::signal_notify(SIGQUIT);
duke@435 492 }
duke@435 493
duke@435 494 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
duke@435 495 return NULL;
duke@435 496 }
duke@435 497
duke@435 498 jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
duke@435 499 out->print_cr("flag '%s' cannot be changed", op->arg(0));
duke@435 500 return JNI_ERR;
duke@435 501 }
duke@435 502
duke@435 503 void AttachListener::pd_detachall() {
duke@435 504 // do nothing for now
duke@435 505 }

mercurial