src/os/solaris/vm/perfMemory_solaris.cpp

Fri, 31 Oct 2014 17:09:14 -0700

author
asaha
date
Fri, 31 Oct 2014 17:09:14 -0700
changeset 7709
5ca2ea5eeff0
parent 7074
833b0f92429a
parent 7707
60a992c821f8
child 7711
fb677d6aebea
permissions
-rw-r--r--

Merge

duke@435 1 /*
dcubed@6349 2 * Copyright (c) 2001, 2014, 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
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/vmSymbols.hpp"
stefank@2314 27 #include "memory/allocation.inline.hpp"
stefank@2314 28 #include "memory/resourceArea.hpp"
stefank@2314 29 #include "oops/oop.inline.hpp"
stefank@2314 30 #include "os_solaris.inline.hpp"
stefank@2314 31 #include "runtime/handles.inline.hpp"
stefank@2314 32 #include "runtime/perfMemory.hpp"
zgu@4193 33 #include "services/memTracker.hpp"
stefank@2314 34 #include "utilities/exceptions.hpp"
duke@435 35
duke@435 36 // put OS-includes here
duke@435 37 # include <sys/types.h>
duke@435 38 # include <sys/mman.h>
duke@435 39 # include <errno.h>
duke@435 40 # include <stdio.h>
duke@435 41 # include <unistd.h>
duke@435 42 # include <sys/stat.h>
duke@435 43 # include <signal.h>
duke@435 44 # include <pwd.h>
duke@435 45 # include <procfs.h>
duke@435 46
duke@435 47
duke@435 48 static char* backing_store_file_name = NULL; // name of the backing store
duke@435 49 // file, if successfully created.
duke@435 50
duke@435 51 // Standard Memory Implementation Details
duke@435 52
duke@435 53 // create the PerfData memory region in standard memory.
duke@435 54 //
duke@435 55 static char* create_standard_memory(size_t size) {
duke@435 56
duke@435 57 // allocate an aligned chuck of memory
duke@435 58 char* mapAddress = os::reserve_memory(size);
duke@435 59
duke@435 60 if (mapAddress == NULL) {
duke@435 61 return NULL;
duke@435 62 }
duke@435 63
duke@435 64 // commit memory
dcubed@5255 65 if (!os::commit_memory(mapAddress, size, !ExecMem)) {
duke@435 66 if (PrintMiscellaneous && Verbose) {
duke@435 67 warning("Could not commit PerfData memory\n");
duke@435 68 }
duke@435 69 os::release_memory(mapAddress, size);
duke@435 70 return NULL;
duke@435 71 }
duke@435 72
duke@435 73 return mapAddress;
duke@435 74 }
duke@435 75
duke@435 76 // delete the PerfData memory region
duke@435 77 //
duke@435 78 static void delete_standard_memory(char* addr, size_t size) {
duke@435 79
duke@435 80 // there are no persistent external resources to cleanup for standard
duke@435 81 // memory. since DestroyJavaVM does not support unloading of the JVM,
duke@435 82 // cleanup of the memory resource is not performed. The memory will be
duke@435 83 // reclaimed by the OS upon termination of the process.
duke@435 84 //
duke@435 85 return;
duke@435 86 }
duke@435 87
duke@435 88 // save the specified memory region to the given file
duke@435 89 //
duke@435 90 // Note: this function might be called from signal handler (by os::abort()),
duke@435 91 // don't allocate heap memory.
duke@435 92 //
duke@435 93 static void save_memory_to_file(char* addr, size_t size) {
duke@435 94
duke@435 95 const char* destfile = PerfMemory::get_perfdata_file_path();
duke@435 96 assert(destfile[0] != '\0', "invalid PerfData file path");
duke@435 97
duke@435 98 int result;
duke@435 99
duke@435 100 RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
duke@435 101 result);;
duke@435 102 if (result == OS_ERR) {
duke@435 103 if (PrintMiscellaneous && Verbose) {
duke@435 104 warning("Could not create Perfdata save file: %s: %s\n",
duke@435 105 destfile, strerror(errno));
duke@435 106 }
duke@435 107 } else {
duke@435 108
duke@435 109 int fd = result;
duke@435 110
duke@435 111 for (size_t remaining = size; remaining > 0;) {
duke@435 112
duke@435 113 RESTARTABLE(::write(fd, addr, remaining), result);
duke@435 114 if (result == OS_ERR) {
duke@435 115 if (PrintMiscellaneous && Verbose) {
duke@435 116 warning("Could not write Perfdata save file: %s: %s\n",
duke@435 117 destfile, strerror(errno));
duke@435 118 }
duke@435 119 break;
duke@435 120 }
duke@435 121 remaining -= (size_t)result;
duke@435 122 addr += result;
duke@435 123 }
duke@435 124
rdurbin@5264 125 result = ::close(fd);
duke@435 126 if (PrintMiscellaneous && Verbose) {
duke@435 127 if (result == OS_ERR) {
duke@435 128 warning("Could not close %s: %s\n", destfile, strerror(errno));
duke@435 129 }
duke@435 130 }
duke@435 131 }
zgu@3900 132 FREE_C_HEAP_ARRAY(char, destfile, mtInternal);
duke@435 133 }
duke@435 134
duke@435 135
duke@435 136 // Shared Memory Implementation Details
duke@435 137
duke@435 138 // Note: the solaris and linux shared memory implementation uses the mmap
duke@435 139 // interface with a backing store file to implement named shared memory.
duke@435 140 // Using the file system as the name space for shared memory allows a
duke@435 141 // common name space to be supported across a variety of platforms. It
duke@435 142 // also provides a name space that Java applications can deal with through
duke@435 143 // simple file apis.
duke@435 144 //
duke@435 145 // The solaris and linux implementations store the backing store file in
duke@435 146 // a user specific temporary directory located in the /tmp file system,
duke@435 147 // which is always a local file system and is sometimes a RAM based file
duke@435 148 // system.
duke@435 149
duke@435 150 // return the user specific temporary directory name.
duke@435 151 //
duke@435 152 // the caller is expected to free the allocated memory.
duke@435 153 //
duke@435 154 static char* get_user_tmp_dir(const char* user) {
duke@435 155
duke@435 156 const char* tmpdir = os::get_temp_directory();
duke@435 157 const char* perfdir = PERFDATA_NAME;
coleenp@1788 158 size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
zgu@3900 159 char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
duke@435 160
duke@435 161 // construct the path name to user specific tmp directory
coleenp@1788 162 snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
duke@435 163
duke@435 164 return dirname;
duke@435 165 }
duke@435 166
duke@435 167 // convert the given file name into a process id. if the file
duke@435 168 // does not meet the file naming constraints, return 0.
duke@435 169 //
duke@435 170 static pid_t filename_to_pid(const char* filename) {
duke@435 171
duke@435 172 // a filename that doesn't begin with a digit is not a
duke@435 173 // candidate for conversion.
duke@435 174 //
duke@435 175 if (!isdigit(*filename)) {
duke@435 176 return 0;
duke@435 177 }
duke@435 178
duke@435 179 // check if file name can be converted to an integer without
duke@435 180 // any leftover characters.
duke@435 181 //
duke@435 182 char* remainder = NULL;
duke@435 183 errno = 0;
duke@435 184 pid_t pid = (pid_t)strtol(filename, &remainder, 10);
duke@435 185
duke@435 186 if (errno != 0) {
duke@435 187 return 0;
duke@435 188 }
duke@435 189
duke@435 190 // check for left over characters. If any, then the filename is
duke@435 191 // not a candidate for conversion.
duke@435 192 //
duke@435 193 if (remainder != NULL && *remainder != '\0') {
duke@435 194 return 0;
duke@435 195 }
duke@435 196
duke@435 197 // successful conversion, return the pid
duke@435 198 return pid;
duke@435 199 }
duke@435 200
duke@435 201
hseigel@7707 202 // Check if the given statbuf is considered a secure directory for
hseigel@7707 203 // the backing store files. Returns true if the directory is considered
hseigel@7707 204 // a secure location. Returns false if the statbuf is a symbolic link or
hseigel@7707 205 // if an error occurred.
hseigel@7707 206 //
hseigel@7707 207 static bool is_statbuf_secure(struct stat *statp) {
hseigel@7707 208 if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) {
hseigel@7707 209 // The path represents a link or some non-directory file type,
hseigel@7707 210 // which is not what we expected. Declare it insecure.
hseigel@7707 211 //
hseigel@7707 212 return false;
hseigel@7707 213 }
hseigel@7707 214 // We have an existing directory, check if the permissions are safe.
hseigel@7707 215 //
hseigel@7707 216 if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) {
hseigel@7707 217 // The directory is open for writing and could be subjected
hseigel@7707 218 // to a symlink or a hard link attack. Declare it insecure.
hseigel@7707 219 //
hseigel@7707 220 return false;
hseigel@7707 221 }
hseigel@7707 222 // See if the uid of the directory matches the effective uid of the process.
hseigel@7707 223 //
hseigel@7707 224 if (statp->st_uid != geteuid()) {
hseigel@7707 225 // The directory was not created by this user, declare it insecure.
hseigel@7707 226 //
hseigel@7707 227 return false;
hseigel@7707 228 }
hseigel@7707 229 return true;
hseigel@7707 230 }
hseigel@7707 231
hseigel@7707 232
hseigel@7707 233 // Check if the given path is considered a secure directory for
duke@435 234 // the backing store files. Returns true if the directory exists
duke@435 235 // and is considered a secure location. Returns false if the path
twisti@1040 236 // is a symbolic link or if an error occurred.
duke@435 237 //
duke@435 238 static bool is_directory_secure(const char* path) {
duke@435 239 struct stat statbuf;
duke@435 240 int result = 0;
duke@435 241
duke@435 242 RESTARTABLE(::lstat(path, &statbuf), result);
duke@435 243 if (result == OS_ERR) {
duke@435 244 return false;
duke@435 245 }
duke@435 246
hseigel@7707 247 // The path exists, see if it is secure.
hseigel@7707 248 return is_statbuf_secure(&statbuf);
hseigel@7707 249 }
hseigel@7707 250
hseigel@7707 251
hseigel@7707 252 // Check if the given directory file descriptor is considered a secure
hseigel@7707 253 // directory for the backing store files. Returns true if the directory
hseigel@7707 254 // exists and is considered a secure location. Returns false if the path
hseigel@7707 255 // is a symbolic link or if an error occurred.
hseigel@7707 256 //
hseigel@7707 257 static bool is_dirfd_secure(int dir_fd) {
hseigel@7707 258 struct stat statbuf;
hseigel@7707 259 int result = 0;
hseigel@7707 260
hseigel@7707 261 RESTARTABLE(::fstat(dir_fd, &statbuf), result);
hseigel@7707 262 if (result == OS_ERR) {
duke@435 263 return false;
duke@435 264 }
hseigel@7707 265
hseigel@7707 266 // The path exists, now check its mode.
hseigel@7707 267 return is_statbuf_secure(&statbuf);
hseigel@7707 268 }
hseigel@7707 269
hseigel@7707 270
hseigel@7707 271 // Check to make sure fd1 and fd2 are referencing the same file system object.
hseigel@7707 272 //
hseigel@7707 273 static bool is_same_fsobject(int fd1, int fd2) {
hseigel@7707 274 struct stat statbuf1;
hseigel@7707 275 struct stat statbuf2;
hseigel@7707 276 int result = 0;
hseigel@7707 277
hseigel@7707 278 RESTARTABLE(::fstat(fd1, &statbuf1), result);
hseigel@7707 279 if (result == OS_ERR) {
hseigel@7707 280 return false;
hseigel@7707 281 }
hseigel@7707 282 RESTARTABLE(::fstat(fd2, &statbuf2), result);
hseigel@7707 283 if (result == OS_ERR) {
hseigel@7707 284 return false;
hseigel@7707 285 }
hseigel@7707 286
hseigel@7707 287 if ((statbuf1.st_ino == statbuf2.st_ino) &&
hseigel@7707 288 (statbuf1.st_dev == statbuf2.st_dev)) {
hseigel@7707 289 return true;
hseigel@7707 290 } else {
hseigel@7707 291 return false;
hseigel@7707 292 }
hseigel@7707 293 }
hseigel@7707 294
hseigel@7707 295
hseigel@7707 296 // Open the directory of the given path and validate it.
hseigel@7707 297 // Return a DIR * of the open directory.
hseigel@7707 298 //
hseigel@7707 299 static DIR *open_directory_secure(const char* dirname) {
hseigel@7707 300 // Open the directory using open() so that it can be verified
hseigel@7707 301 // to be secure by calling is_dirfd_secure(), opendir() and then check
hseigel@7707 302 // to see if they are the same file system object. This method does not
hseigel@7707 303 // introduce a window of opportunity for the directory to be attacked that
hseigel@7707 304 // calling opendir() and is_directory_secure() does.
hseigel@7707 305 int result;
hseigel@7707 306 DIR *dirp = NULL;
hseigel@7707 307 RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
hseigel@7707 308 if (result == OS_ERR) {
hseigel@7707 309 // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
hseigel@7707 310 if (PrintMiscellaneous && Verbose) {
hseigel@7707 311 if (errno == ELOOP) {
hseigel@7707 312 warning("directory %s is a symlink and is not secure\n", dirname);
hseigel@7707 313 } else {
hseigel@7707 314 warning("could not open directory %s: %s\n", dirname, strerror(errno));
hseigel@7707 315 }
duke@435 316 }
hseigel@7707 317 return dirp;
hseigel@7707 318 }
hseigel@7707 319 int fd = result;
hseigel@7707 320
hseigel@7707 321 // Determine if the open directory is secure.
hseigel@7707 322 if (!is_dirfd_secure(fd)) {
hseigel@7707 323 // The directory is not a secure directory.
hseigel@7707 324 os::close(fd);
hseigel@7707 325 return dirp;
hseigel@7707 326 }
hseigel@7707 327
hseigel@7707 328 // Open the directory.
hseigel@7707 329 dirp = ::opendir(dirname);
hseigel@7707 330 if (dirp == NULL) {
hseigel@7707 331 // The directory doesn't exist, close fd and return.
hseigel@7707 332 os::close(fd);
hseigel@7707 333 return dirp;
hseigel@7707 334 }
hseigel@7707 335
hseigel@7707 336 // Check to make sure fd and dirp are referencing the same file system object.
hseigel@7707 337 if (!is_same_fsobject(fd, dirp->dd_fd)) {
hseigel@7707 338 // The directory is not secure.
hseigel@7707 339 os::close(fd);
hseigel@7707 340 os::closedir(dirp);
hseigel@7707 341 dirp = NULL;
hseigel@7707 342 return dirp;
hseigel@7707 343 }
hseigel@7707 344
hseigel@7707 345 // Close initial open now that we know directory is secure
hseigel@7707 346 os::close(fd);
hseigel@7707 347
hseigel@7707 348 return dirp;
hseigel@7707 349 }
hseigel@7707 350
hseigel@7707 351 // NOTE: The code below uses fchdir(), open() and unlink() because
hseigel@7707 352 // fdopendir(), openat() and unlinkat() are not supported on all
hseigel@7707 353 // versions. Once the support for fdopendir(), openat() and unlinkat()
hseigel@7707 354 // is available on all supported versions the code can be changed
hseigel@7707 355 // to use these functions.
hseigel@7707 356
hseigel@7707 357 // Open the directory of the given path, validate it and set the
hseigel@7707 358 // current working directory to it.
hseigel@7707 359 // Return a DIR * of the open directory and the saved cwd fd.
hseigel@7707 360 //
hseigel@7707 361 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
hseigel@7707 362
hseigel@7707 363 // Open the directory.
hseigel@7707 364 DIR* dirp = open_directory_secure(dirname);
hseigel@7707 365 if (dirp == NULL) {
hseigel@7707 366 // Directory doesn't exist or is insecure, so there is nothing to cleanup.
hseigel@7707 367 return dirp;
hseigel@7707 368 }
hseigel@7707 369 int fd = dirp->dd_fd;
hseigel@7707 370
hseigel@7707 371 // Open a fd to the cwd and save it off.
hseigel@7707 372 int result;
hseigel@7707 373 RESTARTABLE(::open(".", O_RDONLY), result);
hseigel@7707 374 if (result == OS_ERR) {
hseigel@7707 375 *saved_cwd_fd = -1;
hseigel@7707 376 } else {
hseigel@7707 377 *saved_cwd_fd = result;
hseigel@7707 378 }
hseigel@7707 379
hseigel@7707 380 // Set the current directory to dirname by using the fd of the directory.
hseigel@7707 381 result = fchdir(fd);
hseigel@7707 382
hseigel@7707 383 return dirp;
hseigel@7707 384 }
hseigel@7707 385
hseigel@7707 386 // Close the directory and restore the current working directory.
hseigel@7707 387 //
hseigel@7707 388 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
hseigel@7707 389
hseigel@7707 390 int result;
hseigel@7707 391 // If we have a saved cwd change back to it and close the fd.
hseigel@7707 392 if (saved_cwd_fd != -1) {
hseigel@7707 393 result = fchdir(saved_cwd_fd);
hseigel@7707 394 ::close(saved_cwd_fd);
hseigel@7707 395 }
hseigel@7707 396
hseigel@7707 397 // Close the directory.
hseigel@7707 398 os::closedir(dirp);
hseigel@7707 399 }
hseigel@7707 400
hseigel@7707 401 // Check if the given file descriptor is considered a secure.
hseigel@7707 402 //
hseigel@7707 403 static bool is_file_secure(int fd, const char *filename) {
hseigel@7707 404
hseigel@7707 405 int result;
hseigel@7707 406 struct stat statbuf;
hseigel@7707 407
hseigel@7707 408 // Determine if the file is secure.
hseigel@7707 409 RESTARTABLE(::fstat(fd, &statbuf), result);
hseigel@7707 410 if (result == OS_ERR) {
hseigel@7707 411 if (PrintMiscellaneous && Verbose) {
hseigel@7707 412 warning("fstat failed on %s: %s\n", filename, strerror(errno));
hseigel@7707 413 }
hseigel@7707 414 return false;
hseigel@7707 415 }
hseigel@7707 416 if (statbuf.st_nlink > 1) {
hseigel@7707 417 // A file with multiple links is not expected.
hseigel@7707 418 if (PrintMiscellaneous && Verbose) {
hseigel@7707 419 warning("file %s has multiple links\n", filename);
hseigel@7707 420 }
hseigel@7707 421 return false;
duke@435 422 }
duke@435 423 return true;
duke@435 424 }
duke@435 425
duke@435 426 // return the user name for the given user id
duke@435 427 //
duke@435 428 // the caller is expected to free the allocated memory.
duke@435 429 //
duke@435 430 static char* get_user_name(uid_t uid) {
duke@435 431
duke@435 432 struct passwd pwent;
duke@435 433
duke@435 434 // determine the max pwbuf size from sysconf, and hardcode
duke@435 435 // a default if this not available through sysconf.
duke@435 436 //
duke@435 437 long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
duke@435 438 if (bufsize == -1)
duke@435 439 bufsize = 1024;
duke@435 440
zgu@3900 441 char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
duke@435 442
duke@435 443 #ifdef _GNU_SOURCE
duke@435 444 struct passwd* p = NULL;
duke@435 445 int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
duke@435 446 #else // _GNU_SOURCE
duke@435 447 struct passwd* p = getpwuid_r(uid, &pwent, pwbuf, (int)bufsize);
duke@435 448 #endif // _GNU_SOURCE
duke@435 449
duke@435 450 if (p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
duke@435 451 if (PrintMiscellaneous && Verbose) {
duke@435 452 if (p == NULL) {
duke@435 453 warning("Could not retrieve passwd entry: %s\n",
duke@435 454 strerror(errno));
duke@435 455 }
duke@435 456 else {
duke@435 457 warning("Could not determine user name: %s\n",
duke@435 458 p->pw_name == NULL ? "pw_name = NULL" :
duke@435 459 "pw_name zero length");
duke@435 460 }
duke@435 461 }
zgu@3900 462 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
duke@435 463 return NULL;
duke@435 464 }
duke@435 465
zgu@3900 466 char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
duke@435 467 strcpy(user_name, p->pw_name);
duke@435 468
zgu@3900 469 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
duke@435 470 return user_name;
duke@435 471 }
duke@435 472
duke@435 473 // return the name of the user that owns the process identified by vmid.
duke@435 474 //
duke@435 475 // This method uses a slow directory search algorithm to find the backing
duke@435 476 // store file for the specified vmid and returns the user name, as determined
duke@435 477 // by the user name suffix of the hsperfdata_<username> directory name.
duke@435 478 //
duke@435 479 // the caller is expected to free the allocated memory.
duke@435 480 //
duke@435 481 static char* get_user_name_slow(int vmid, TRAPS) {
duke@435 482
duke@435 483 // short circuit the directory search if the process doesn't even exist.
duke@435 484 if (kill(vmid, 0) == OS_ERR) {
duke@435 485 if (errno == ESRCH) {
duke@435 486 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 487 "Process not found");
duke@435 488 }
duke@435 489 else /* EPERM */ {
duke@435 490 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
duke@435 491 }
duke@435 492 }
duke@435 493
duke@435 494 // directory search
duke@435 495 char* oldest_user = NULL;
duke@435 496 time_t oldest_ctime = 0;
duke@435 497
duke@435 498 const char* tmpdirname = os::get_temp_directory();
duke@435 499
hseigel@7707 500 // open the temp directory
hseigel@7707 501 DIR* tmpdirp = open_directory_secure(tmpdirname);
duke@435 502 if (tmpdirp == NULL) {
hseigel@7707 503 // Cannot open the directory to get the user name, return.
duke@435 504 return NULL;
duke@435 505 }
duke@435 506
duke@435 507 // for each entry in the directory that matches the pattern hsperfdata_*,
duke@435 508 // open the directory and check if the file for the given vmid exists.
duke@435 509 // The file with the expected name and the latest creation date is used
duke@435 510 // to determine the user name for the process id.
duke@435 511 //
duke@435 512 struct dirent* dentry;
zgu@3900 513 char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
duke@435 514 errno = 0;
duke@435 515 while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
duke@435 516
duke@435 517 // check if the directory entry is a hsperfdata file
duke@435 518 if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
duke@435 519 continue;
duke@435 520 }
duke@435 521
duke@435 522 char* usrdir_name = NEW_C_HEAP_ARRAY(char,
zgu@3900 523 strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
duke@435 524 strcpy(usrdir_name, tmpdirname);
coleenp@1788 525 strcat(usrdir_name, "/");
duke@435 526 strcat(usrdir_name, dentry->d_name);
duke@435 527
hseigel@7707 528 // open the user directory
hseigel@7707 529 DIR* subdirp = open_directory_secure(usrdir_name);
duke@435 530
duke@435 531 if (subdirp == NULL) {
zgu@3900 532 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
duke@435 533 continue;
duke@435 534 }
duke@435 535
duke@435 536 // Since we don't create the backing store files in directories
duke@435 537 // pointed to by symbolic links, we also don't follow them when
duke@435 538 // looking for the files. We check for a symbolic link after the
duke@435 539 // call to opendir in order to eliminate a small window where the
duke@435 540 // symlink can be exploited.
duke@435 541 //
duke@435 542 if (!is_directory_secure(usrdir_name)) {
zgu@3900 543 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
duke@435 544 os::closedir(subdirp);
duke@435 545 continue;
duke@435 546 }
duke@435 547
duke@435 548 struct dirent* udentry;
zgu@3900 549 char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
duke@435 550 errno = 0;
duke@435 551 while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
duke@435 552
duke@435 553 if (filename_to_pid(udentry->d_name) == vmid) {
duke@435 554 struct stat statbuf;
duke@435 555 int result;
duke@435 556
duke@435 557 char* filename = NEW_C_HEAP_ARRAY(char,
zgu@3900 558 strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
duke@435 559
duke@435 560 strcpy(filename, usrdir_name);
duke@435 561 strcat(filename, "/");
duke@435 562 strcat(filename, udentry->d_name);
duke@435 563
duke@435 564 // don't follow symbolic links for the file
duke@435 565 RESTARTABLE(::lstat(filename, &statbuf), result);
duke@435 566 if (result == OS_ERR) {
zgu@3900 567 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
duke@435 568 continue;
duke@435 569 }
duke@435 570
duke@435 571 // skip over files that are not regular files.
duke@435 572 if (!S_ISREG(statbuf.st_mode)) {
zgu@3900 573 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
duke@435 574 continue;
duke@435 575 }
duke@435 576
duke@435 577 // compare and save filename with latest creation time
duke@435 578 if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
duke@435 579
duke@435 580 if (statbuf.st_ctime > oldest_ctime) {
duke@435 581 char* user = strchr(dentry->d_name, '_') + 1;
duke@435 582
zgu@3900 583 if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
zgu@3900 584 oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
duke@435 585
duke@435 586 strcpy(oldest_user, user);
duke@435 587 oldest_ctime = statbuf.st_ctime;
duke@435 588 }
duke@435 589 }
duke@435 590
zgu@3900 591 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
duke@435 592 }
duke@435 593 }
duke@435 594 os::closedir(subdirp);
zgu@3900 595 FREE_C_HEAP_ARRAY(char, udbuf, mtInternal);
zgu@3900 596 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
duke@435 597 }
duke@435 598 os::closedir(tmpdirp);
zgu@3900 599 FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal);
duke@435 600
duke@435 601 return(oldest_user);
duke@435 602 }
duke@435 603
duke@435 604 // return the name of the user that owns the JVM indicated by the given vmid.
duke@435 605 //
duke@435 606 static char* get_user_name(int vmid, TRAPS) {
duke@435 607
duke@435 608 char psinfo_name[PATH_MAX];
duke@435 609 int result;
duke@435 610
duke@435 611 snprintf(psinfo_name, PATH_MAX, "/proc/%d/psinfo", vmid);
duke@435 612
duke@435 613 RESTARTABLE(::open(psinfo_name, O_RDONLY), result);
duke@435 614
duke@435 615 if (result != OS_ERR) {
duke@435 616 int fd = result;
duke@435 617
duke@435 618 psinfo_t psinfo;
duke@435 619 char* addr = (char*)&psinfo;
duke@435 620
duke@435 621 for (size_t remaining = sizeof(psinfo_t); remaining > 0;) {
duke@435 622
duke@435 623 RESTARTABLE(::read(fd, addr, remaining), result);
duke@435 624 if (result == OS_ERR) {
dcubed@6349 625 ::close(fd);
duke@435 626 THROW_MSG_0(vmSymbols::java_io_IOException(), "Read error");
dcubed@6349 627 } else {
dcubed@6349 628 remaining-=result;
dcubed@6349 629 addr+=result;
duke@435 630 }
duke@435 631 }
duke@435 632
rdurbin@5264 633 ::close(fd);
duke@435 634
duke@435 635 // get the user name for the effective user id of the process
duke@435 636 char* user_name = get_user_name(psinfo.pr_euid);
duke@435 637
duke@435 638 return user_name;
duke@435 639 }
duke@435 640
duke@435 641 if (result == OS_ERR && errno == EACCES) {
duke@435 642
duke@435 643 // In this case, the psinfo file for the process id existed,
duke@435 644 // but we didn't have permission to access it.
duke@435 645 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 646 strerror(errno));
duke@435 647 }
duke@435 648
duke@435 649 // at this point, we don't know if the process id itself doesn't
duke@435 650 // exist or if the psinfo file doesn't exit. If the psinfo file
duke@435 651 // doesn't exist, then we are running on Solaris 2.5.1 or earlier.
duke@435 652 // since the structured procfs and old procfs interfaces can't be
duke@435 653 // mixed, we attempt to find the file through a directory search.
duke@435 654
duke@435 655 return get_user_name_slow(vmid, CHECK_NULL);
duke@435 656 }
duke@435 657
duke@435 658 // return the file name of the backing store file for the named
duke@435 659 // shared memory region for the given user name and vmid.
duke@435 660 //
duke@435 661 // the caller is expected to free the allocated memory.
duke@435 662 //
duke@435 663 static char* get_sharedmem_filename(const char* dirname, int vmid) {
duke@435 664
duke@435 665 // add 2 for the file separator and a NULL terminator.
duke@435 666 size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
duke@435 667
zgu@3900 668 char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
duke@435 669 snprintf(name, nbytes, "%s/%d", dirname, vmid);
duke@435 670
duke@435 671 return name;
duke@435 672 }
duke@435 673
duke@435 674
duke@435 675 // remove file
duke@435 676 //
duke@435 677 // this method removes the file specified by the given path
duke@435 678 //
duke@435 679 static void remove_file(const char* path) {
duke@435 680
duke@435 681 int result;
duke@435 682
duke@435 683 // if the file is a directory, the following unlink will fail. since
duke@435 684 // we don't expect to find directories in the user temp directory, we
duke@435 685 // won't try to handle this situation. even if accidentially or
duke@435 686 // maliciously planted, the directory's presence won't hurt anything.
duke@435 687 //
duke@435 688 RESTARTABLE(::unlink(path), result);
duke@435 689 if (PrintMiscellaneous && Verbose && result == OS_ERR) {
duke@435 690 if (errno != ENOENT) {
duke@435 691 warning("Could not unlink shared memory backing"
duke@435 692 " store file %s : %s\n", path, strerror(errno));
duke@435 693 }
duke@435 694 }
duke@435 695 }
duke@435 696
duke@435 697
duke@435 698 // cleanup stale shared memory resources
duke@435 699 //
duke@435 700 // This method attempts to remove all stale shared memory files in
duke@435 701 // the named user temporary directory. It scans the named directory
duke@435 702 // for files matching the pattern ^$[0-9]*$. For each file found, the
duke@435 703 // process id is extracted from the file name and a test is run to
duke@435 704 // determine if the process is alive. If the process is not alive,
duke@435 705 // any stale file resources are removed.
duke@435 706 //
duke@435 707 static void cleanup_sharedmem_resources(const char* dirname) {
duke@435 708
hseigel@7707 709 int saved_cwd_fd;
hseigel@7707 710 // open the directory
hseigel@7707 711 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
duke@435 712 if (dirp == NULL) {
hseigel@7707 713 // directory doesn't exist or is insecure, so there is nothing to cleanup
duke@435 714 return;
duke@435 715 }
duke@435 716
duke@435 717 // for each entry in the directory that matches the expected file
duke@435 718 // name pattern, determine if the file resources are stale and if
duke@435 719 // so, remove the file resources. Note, instrumented HotSpot processes
duke@435 720 // for this user may start and/or terminate during this search and
duke@435 721 // remove or create new files in this directory. The behavior of this
duke@435 722 // loop under these conditions is dependent upon the implementation of
duke@435 723 // opendir/readdir.
duke@435 724 //
duke@435 725 struct dirent* entry;
zgu@3900 726 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
hseigel@7707 727
duke@435 728 errno = 0;
duke@435 729 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
duke@435 730
duke@435 731 pid_t pid = filename_to_pid(entry->d_name);
duke@435 732
duke@435 733 if (pid == 0) {
duke@435 734
duke@435 735 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
duke@435 736
duke@435 737 // attempt to remove all unexpected files, except "." and ".."
hseigel@7707 738 unlink(entry->d_name);
duke@435 739 }
duke@435 740
duke@435 741 errno = 0;
duke@435 742 continue;
duke@435 743 }
duke@435 744
duke@435 745 // we now have a file name that converts to a valid integer
duke@435 746 // that could represent a process id . if this process id
duke@435 747 // matches the current process id or the process is not running,
duke@435 748 // then remove the stale file resources.
duke@435 749 //
duke@435 750 // process liveness is detected by sending signal number 0 to
duke@435 751 // the process id (see kill(2)). if kill determines that the
duke@435 752 // process does not exist, then the file resources are removed.
duke@435 753 // if kill determines that that we don't have permission to
duke@435 754 // signal the process, then the file resources are assumed to
duke@435 755 // be stale and are removed because the resources for such a
duke@435 756 // process should be in a different user specific directory.
duke@435 757 //
duke@435 758 if ((pid == os::current_process_id()) ||
duke@435 759 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
duke@435 760
hseigel@7707 761 unlink(entry->d_name);
duke@435 762 }
duke@435 763 errno = 0;
duke@435 764 }
hseigel@7707 765
hseigel@7707 766 // close the directory and reset the current working directory
hseigel@7707 767 close_directory_secure_cwd(dirp, saved_cwd_fd);
hseigel@7707 768
zgu@3900 769 FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
duke@435 770 }
duke@435 771
duke@435 772 // make the user specific temporary directory. Returns true if
duke@435 773 // the directory exists and is secure upon return. Returns false
duke@435 774 // if the directory exists but is either a symlink, is otherwise
duke@435 775 // insecure, or if an error occurred.
duke@435 776 //
duke@435 777 static bool make_user_tmp_dir(const char* dirname) {
duke@435 778
duke@435 779 // create the directory with 0755 permissions. note that the directory
duke@435 780 // will be owned by euid::egid, which may not be the same as uid::gid.
duke@435 781 //
duke@435 782 if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
duke@435 783 if (errno == EEXIST) {
duke@435 784 // The directory already exists and was probably created by another
duke@435 785 // JVM instance. However, this could also be the result of a
duke@435 786 // deliberate symlink. Verify that the existing directory is safe.
duke@435 787 //
duke@435 788 if (!is_directory_secure(dirname)) {
duke@435 789 // directory is not secure
duke@435 790 if (PrintMiscellaneous && Verbose) {
duke@435 791 warning("%s directory is insecure\n", dirname);
duke@435 792 }
duke@435 793 return false;
duke@435 794 }
duke@435 795 }
duke@435 796 else {
duke@435 797 // we encountered some other failure while attempting
duke@435 798 // to create the directory
duke@435 799 //
duke@435 800 if (PrintMiscellaneous && Verbose) {
duke@435 801 warning("could not create directory %s: %s\n",
duke@435 802 dirname, strerror(errno));
duke@435 803 }
duke@435 804 return false;
duke@435 805 }
duke@435 806 }
duke@435 807 return true;
duke@435 808 }
duke@435 809
duke@435 810 // create the shared memory file resources
duke@435 811 //
duke@435 812 // This method creates the shared memory file with the given size
duke@435 813 // This method also creates the user specific temporary directory, if
duke@435 814 // it does not yet exist.
duke@435 815 //
duke@435 816 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
duke@435 817
duke@435 818 // make the user temporary directory
duke@435 819 if (!make_user_tmp_dir(dirname)) {
duke@435 820 // could not make/find the directory or the found directory
duke@435 821 // was not secure
duke@435 822 return -1;
duke@435 823 }
duke@435 824
hseigel@7707 825 int saved_cwd_fd;
hseigel@7707 826 // open the directory and set the current working directory to it
hseigel@7707 827 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
hseigel@7707 828 if (dirp == NULL) {
hseigel@7707 829 // Directory doesn't exist or is insecure, so cannot create shared
hseigel@7707 830 // memory file.
hseigel@7707 831 return -1;
hseigel@7707 832 }
hseigel@7707 833
hseigel@7707 834 // Open the filename in the current directory.
hseigel@7707 835 // Cannot use O_TRUNC here; truncation of an existing file has to happen
hseigel@7707 836 // after the is_file_secure() check below.
duke@435 837 int result;
hseigel@7707 838 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
duke@435 839 if (result == OS_ERR) {
duke@435 840 if (PrintMiscellaneous && Verbose) {
hseigel@7707 841 if (errno == ELOOP) {
hseigel@7707 842 warning("file %s is a symlink and is not secure\n", filename);
hseigel@7707 843 } else {
hseigel@7707 844 warning("could not create file %s: %s\n", filename, strerror(errno));
hseigel@7707 845 }
duke@435 846 }
hseigel@7707 847 // close the directory and reset the current working directory
hseigel@7707 848 close_directory_secure_cwd(dirp, saved_cwd_fd);
hseigel@7707 849
duke@435 850 return -1;
duke@435 851 }
hseigel@7707 852 // close the directory and reset the current working directory
hseigel@7707 853 close_directory_secure_cwd(dirp, saved_cwd_fd);
duke@435 854
duke@435 855 // save the file descriptor
duke@435 856 int fd = result;
duke@435 857
hseigel@7707 858 // check to see if the file is secure
hseigel@7707 859 if (!is_file_secure(fd, filename)) {
hseigel@7707 860 ::close(fd);
hseigel@7707 861 return -1;
hseigel@7707 862 }
hseigel@7707 863
hseigel@7707 864 // truncate the file to get rid of any existing data
hseigel@7707 865 RESTARTABLE(::ftruncate(fd, (off_t)0), result);
hseigel@7707 866 if (result == OS_ERR) {
hseigel@7707 867 if (PrintMiscellaneous && Verbose) {
hseigel@7707 868 warning("could not truncate shared memory file: %s\n", strerror(errno));
hseigel@7707 869 }
hseigel@7707 870 ::close(fd);
hseigel@7707 871 return -1;
hseigel@7707 872 }
duke@435 873 // set the file size
duke@435 874 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
duke@435 875 if (result == OS_ERR) {
duke@435 876 if (PrintMiscellaneous && Verbose) {
duke@435 877 warning("could not set shared memory file size: %s\n", strerror(errno));
duke@435 878 }
rdurbin@5264 879 ::close(fd);
duke@435 880 return -1;
duke@435 881 }
duke@435 882
duke@435 883 return fd;
duke@435 884 }
duke@435 885
duke@435 886 // open the shared memory file for the given user and vmid. returns
duke@435 887 // the file descriptor for the open file or -1 if the file could not
duke@435 888 // be opened.
duke@435 889 //
duke@435 890 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
duke@435 891
duke@435 892 // open the file
duke@435 893 int result;
duke@435 894 RESTARTABLE(::open(filename, oflags), result);
duke@435 895 if (result == OS_ERR) {
duke@435 896 if (errno == ENOENT) {
ccheung@4893 897 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
ccheung@4893 898 "Process not found", OS_ERR);
duke@435 899 }
duke@435 900 else if (errno == EACCES) {
ccheung@4893 901 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
ccheung@4893 902 "Permission denied", OS_ERR);
duke@435 903 }
duke@435 904 else {
ccheung@4893 905 THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR);
duke@435 906 }
duke@435 907 }
hseigel@7707 908 int fd = result;
duke@435 909
hseigel@7707 910 // check to see if the file is secure
hseigel@7707 911 if (!is_file_secure(fd, filename)) {
hseigel@7707 912 ::close(fd);
hseigel@7707 913 return -1;
hseigel@7707 914 }
hseigel@7707 915
hseigel@7707 916 return fd;
duke@435 917 }
duke@435 918
duke@435 919 // create a named shared memory region. returns the address of the
duke@435 920 // memory region on success or NULL on failure. A return value of
duke@435 921 // NULL will ultimately disable the shared memory feature.
duke@435 922 //
duke@435 923 // On Solaris and Linux, the name space for shared memory objects
duke@435 924 // is the file system name space.
duke@435 925 //
duke@435 926 // A monitoring application attaching to a JVM does not need to know
duke@435 927 // the file system name of the shared memory object. However, it may
duke@435 928 // be convenient for applications to discover the existence of newly
duke@435 929 // created and terminating JVMs by watching the file system name space
duke@435 930 // for files being created or removed.
duke@435 931 //
duke@435 932 static char* mmap_create_shared(size_t size) {
duke@435 933
duke@435 934 int result;
duke@435 935 int fd;
duke@435 936 char* mapAddress;
duke@435 937
duke@435 938 int vmid = os::current_process_id();
duke@435 939
duke@435 940 char* user_name = get_user_name(geteuid());
duke@435 941
duke@435 942 if (user_name == NULL)
duke@435 943 return NULL;
duke@435 944
duke@435 945 char* dirname = get_user_tmp_dir(user_name);
duke@435 946 char* filename = get_sharedmem_filename(dirname, vmid);
duke@435 947
hseigel@7707 948 // get the short filename
hseigel@7707 949 char* short_filename = strrchr(filename, '/');
hseigel@7707 950 if (short_filename == NULL) {
hseigel@7707 951 short_filename = filename;
hseigel@7707 952 } else {
hseigel@7707 953 short_filename++;
hseigel@7707 954 }
hseigel@7707 955
duke@435 956 // cleanup any stale shared memory files
duke@435 957 cleanup_sharedmem_resources(dirname);
duke@435 958
duke@435 959 assert(((size > 0) && (size % os::vm_page_size() == 0)),
duke@435 960 "unexpected PerfMemory region size");
duke@435 961
hseigel@7707 962 fd = create_sharedmem_resources(dirname, short_filename, size);
duke@435 963
zgu@3900 964 FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
zgu@3900 965 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
duke@435 966
duke@435 967 if (fd == -1) {
zgu@3900 968 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
duke@435 969 return NULL;
duke@435 970 }
duke@435 971
duke@435 972 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
duke@435 973
rdurbin@5264 974 result = ::close(fd);
duke@435 975 assert(result != OS_ERR, "could not close file");
duke@435 976
duke@435 977 if (mapAddress == MAP_FAILED) {
duke@435 978 if (PrintMiscellaneous && Verbose) {
duke@435 979 warning("mmap failed - %s\n", strerror(errno));
duke@435 980 }
duke@435 981 remove_file(filename);
zgu@3900 982 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
duke@435 983 return NULL;
duke@435 984 }
duke@435 985
duke@435 986 // save the file name for use in delete_shared_memory()
duke@435 987 backing_store_file_name = filename;
duke@435 988
duke@435 989 // clear the shared memory region
duke@435 990 (void)::memset((void*) mapAddress, 0, size);
duke@435 991
zgu@4193 992 // it does not go through os api, the operation has to record from here
zgu@7074 993 MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress,
zgu@7074 994 size, CURRENT_PC, mtInternal);
zgu@4193 995
duke@435 996 return mapAddress;
duke@435 997 }
duke@435 998
duke@435 999 // release a named shared memory region
duke@435 1000 //
duke@435 1001 static void unmap_shared(char* addr, size_t bytes) {
duke@435 1002 os::release_memory(addr, bytes);
duke@435 1003 }
duke@435 1004
duke@435 1005 // create the PerfData memory region in shared memory.
duke@435 1006 //
duke@435 1007 static char* create_shared_memory(size_t size) {
duke@435 1008
duke@435 1009 // create the shared memory region.
duke@435 1010 return mmap_create_shared(size);
duke@435 1011 }
duke@435 1012
duke@435 1013 // delete the shared PerfData memory region
duke@435 1014 //
duke@435 1015 static void delete_shared_memory(char* addr, size_t size) {
duke@435 1016
duke@435 1017 // cleanup the persistent shared memory resources. since DestroyJavaVM does
duke@435 1018 // not support unloading of the JVM, unmapping of the memory resource is
duke@435 1019 // not performed. The memory will be reclaimed by the OS upon termination of
duke@435 1020 // the process. The backing store file is deleted from the file system.
duke@435 1021
duke@435 1022 assert(!PerfDisableSharedMem, "shouldn't be here");
duke@435 1023
duke@435 1024 if (backing_store_file_name != NULL) {
duke@435 1025 remove_file(backing_store_file_name);
duke@435 1026 // Don't.. Free heap memory could deadlock os::abort() if it is called
duke@435 1027 // from signal handler. OS will reclaim the heap memory.
duke@435 1028 // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
duke@435 1029 backing_store_file_name = NULL;
duke@435 1030 }
duke@435 1031 }
duke@435 1032
duke@435 1033 // return the size of the file for the given file descriptor
duke@435 1034 // or 0 if it is not a valid size for a shared memory file
duke@435 1035 //
duke@435 1036 static size_t sharedmem_filesize(int fd, TRAPS) {
duke@435 1037
duke@435 1038 struct stat statbuf;
duke@435 1039 int result;
duke@435 1040
duke@435 1041 RESTARTABLE(::fstat(fd, &statbuf), result);
duke@435 1042 if (result == OS_ERR) {
duke@435 1043 if (PrintMiscellaneous && Verbose) {
duke@435 1044 warning("fstat failed: %s\n", strerror(errno));
duke@435 1045 }
duke@435 1046 THROW_MSG_0(vmSymbols::java_io_IOException(),
duke@435 1047 "Could not determine PerfMemory size");
duke@435 1048 }
duke@435 1049
duke@435 1050 if ((statbuf.st_size == 0) ||
duke@435 1051 ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
duke@435 1052 THROW_MSG_0(vmSymbols::java_lang_Exception(),
duke@435 1053 "Invalid PerfMemory size");
duke@435 1054 }
duke@435 1055
duke@435 1056 return (size_t)statbuf.st_size;
duke@435 1057 }
duke@435 1058
duke@435 1059 // attach to a named shared memory region.
duke@435 1060 //
duke@435 1061 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
duke@435 1062
duke@435 1063 char* mapAddress;
duke@435 1064 int result;
duke@435 1065 int fd;
ccheung@4893 1066 size_t size = 0;
duke@435 1067 const char* luser = NULL;
duke@435 1068
duke@435 1069 int mmap_prot;
duke@435 1070 int file_flags;
duke@435 1071
duke@435 1072 ResourceMark rm;
duke@435 1073
duke@435 1074 // map the high level access mode to the appropriate permission
duke@435 1075 // constructs for the file and the shared memory mapping.
duke@435 1076 if (mode == PerfMemory::PERF_MODE_RO) {
duke@435 1077 mmap_prot = PROT_READ;
hseigel@7707 1078 file_flags = O_RDONLY | O_NOFOLLOW;
duke@435 1079 }
duke@435 1080 else if (mode == PerfMemory::PERF_MODE_RW) {
duke@435 1081 #ifdef LATER
duke@435 1082 mmap_prot = PROT_READ | PROT_WRITE;
hseigel@7707 1083 file_flags = O_RDWR | O_NOFOLLOW;
duke@435 1084 #else
duke@435 1085 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1086 "Unsupported access mode");
duke@435 1087 #endif
duke@435 1088 }
duke@435 1089 else {
duke@435 1090 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1091 "Illegal access mode");
duke@435 1092 }
duke@435 1093
duke@435 1094 if (user == NULL || strlen(user) == 0) {
duke@435 1095 luser = get_user_name(vmid, CHECK);
duke@435 1096 }
duke@435 1097 else {
duke@435 1098 luser = user;
duke@435 1099 }
duke@435 1100
duke@435 1101 if (luser == NULL) {
duke@435 1102 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1103 "Could not map vmid to user Name");
duke@435 1104 }
duke@435 1105
duke@435 1106 char* dirname = get_user_tmp_dir(luser);
duke@435 1107
duke@435 1108 // since we don't follow symbolic links when creating the backing
duke@435 1109 // store file, we don't follow them when attaching either.
duke@435 1110 //
duke@435 1111 if (!is_directory_secure(dirname)) {
zgu@3900 1112 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
duke@435 1113 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1114 "Process not found");
duke@435 1115 }
duke@435 1116
duke@435 1117 char* filename = get_sharedmem_filename(dirname, vmid);
duke@435 1118
duke@435 1119 // copy heap memory to resource memory. the open_sharedmem_file
duke@435 1120 // method below need to use the filename, but could throw an
duke@435 1121 // exception. using a resource array prevents the leak that
duke@435 1122 // would otherwise occur.
duke@435 1123 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
duke@435 1124 strcpy(rfilename, filename);
duke@435 1125
duke@435 1126 // free the c heap resources that are no longer needed
zgu@3900 1127 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
zgu@3900 1128 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
zgu@3900 1129 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
duke@435 1130
duke@435 1131 // open the shared memory file for the give vmid
dcubed@6349 1132 fd = open_sharedmem_file(rfilename, file_flags, THREAD);
dcubed@6349 1133
dcubed@6349 1134 if (fd == OS_ERR) {
dcubed@6349 1135 return;
dcubed@6349 1136 }
dcubed@6349 1137
dcubed@6349 1138 if (HAS_PENDING_EXCEPTION) {
dcubed@6349 1139 ::close(fd);
dcubed@6349 1140 return;
dcubed@6349 1141 }
duke@435 1142
duke@435 1143 if (*sizep == 0) {
duke@435 1144 size = sharedmem_filesize(fd, CHECK);
ccheung@4893 1145 } else {
ccheung@4893 1146 size = *sizep;
duke@435 1147 }
duke@435 1148
ccheung@4893 1149 assert(size > 0, "unexpected size <= 0");
ccheung@4893 1150
duke@435 1151 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
duke@435 1152
rdurbin@5264 1153 result = ::close(fd);
duke@435 1154 assert(result != OS_ERR, "could not close file");
duke@435 1155
duke@435 1156 if (mapAddress == MAP_FAILED) {
duke@435 1157 if (PrintMiscellaneous && Verbose) {
duke@435 1158 warning("mmap failed: %s\n", strerror(errno));
duke@435 1159 }
duke@435 1160 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
duke@435 1161 "Could not map PerfMemory");
duke@435 1162 }
duke@435 1163
zgu@4193 1164 // it does not go through os api, the operation has to record from here
zgu@7074 1165 MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress,
zgu@7074 1166 size, CURRENT_PC, mtInternal);
zgu@4193 1167
duke@435 1168 *addr = mapAddress;
duke@435 1169 *sizep = size;
duke@435 1170
duke@435 1171 if (PerfTraceMemOps) {
duke@435 1172 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
duke@435 1173 INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress);
duke@435 1174 }
duke@435 1175 }
duke@435 1176
duke@435 1177
duke@435 1178
duke@435 1179
duke@435 1180 // create the PerfData memory region
duke@435 1181 //
duke@435 1182 // This method creates the memory region used to store performance
duke@435 1183 // data for the JVM. The memory may be created in standard or
duke@435 1184 // shared memory.
duke@435 1185 //
duke@435 1186 void PerfMemory::create_memory_region(size_t size) {
duke@435 1187
duke@435 1188 if (PerfDisableSharedMem) {
duke@435 1189 // do not share the memory for the performance data.
duke@435 1190 _start = create_standard_memory(size);
duke@435 1191 }
duke@435 1192 else {
duke@435 1193 _start = create_shared_memory(size);
duke@435 1194 if (_start == NULL) {
duke@435 1195
duke@435 1196 // creation of the shared memory region failed, attempt
duke@435 1197 // to create a contiguous, non-shared memory region instead.
duke@435 1198 //
duke@435 1199 if (PrintMiscellaneous && Verbose) {
duke@435 1200 warning("Reverting to non-shared PerfMemory region.\n");
duke@435 1201 }
duke@435 1202 PerfDisableSharedMem = true;
duke@435 1203 _start = create_standard_memory(size);
duke@435 1204 }
duke@435 1205 }
duke@435 1206
duke@435 1207 if (_start != NULL) _capacity = size;
duke@435 1208
duke@435 1209 }
duke@435 1210
duke@435 1211 // delete the PerfData memory region
duke@435 1212 //
duke@435 1213 // This method deletes the memory region used to store performance
duke@435 1214 // data for the JVM. The memory region indicated by the <address, size>
duke@435 1215 // tuple will be inaccessible after a call to this method.
duke@435 1216 //
duke@435 1217 void PerfMemory::delete_memory_region() {
duke@435 1218
duke@435 1219 assert((start() != NULL && capacity() > 0), "verify proper state");
duke@435 1220
duke@435 1221 // If user specifies PerfDataSaveFile, it will save the performance data
duke@435 1222 // to the specified file name no matter whether PerfDataSaveToFile is specified
duke@435 1223 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
duke@435 1224 // -XX:+PerfDataSaveToFile.
duke@435 1225 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
duke@435 1226 save_memory_to_file(start(), capacity());
duke@435 1227 }
duke@435 1228
duke@435 1229 if (PerfDisableSharedMem) {
duke@435 1230 delete_standard_memory(start(), capacity());
duke@435 1231 }
duke@435 1232 else {
duke@435 1233 delete_shared_memory(start(), capacity());
duke@435 1234 }
duke@435 1235 }
duke@435 1236
duke@435 1237 // attach to the PerfData memory region for another JVM
duke@435 1238 //
duke@435 1239 // This method returns an <address, size> tuple that points to
duke@435 1240 // a memory buffer that is kept reasonably synchronized with
duke@435 1241 // the PerfData memory region for the indicated JVM. This
duke@435 1242 // buffer may be kept in synchronization via shared memory
duke@435 1243 // or some other mechanism that keeps the buffer updated.
duke@435 1244 //
duke@435 1245 // If the JVM chooses not to support the attachability feature,
duke@435 1246 // this method should throw an UnsupportedOperation exception.
duke@435 1247 //
duke@435 1248 // This implementation utilizes named shared memory to map
duke@435 1249 // the indicated process's PerfData memory region into this JVMs
duke@435 1250 // address space.
duke@435 1251 //
duke@435 1252 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
duke@435 1253
duke@435 1254 if (vmid == 0 || vmid == os::current_process_id()) {
duke@435 1255 *addrp = start();
duke@435 1256 *sizep = capacity();
duke@435 1257 return;
duke@435 1258 }
duke@435 1259
duke@435 1260 mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
duke@435 1261 }
duke@435 1262
duke@435 1263 // detach from the PerfData memory region of another JVM
duke@435 1264 //
duke@435 1265 // This method detaches the PerfData memory region of another
duke@435 1266 // JVM, specified as an <address, size> tuple of a buffer
duke@435 1267 // in this process's address space. This method may perform
duke@435 1268 // arbitrary actions to accomplish the detachment. The memory
duke@435 1269 // region specified by <address, size> will be inaccessible after
duke@435 1270 // a call to this method.
duke@435 1271 //
duke@435 1272 // If the JVM chooses not to support the attachability feature,
duke@435 1273 // this method should throw an UnsupportedOperation exception.
duke@435 1274 //
duke@435 1275 // This implementation utilizes named shared memory to detach
duke@435 1276 // the indicated process's PerfData memory region from this
duke@435 1277 // process's address space.
duke@435 1278 //
duke@435 1279 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
duke@435 1280
duke@435 1281 assert(addr != 0, "address sanity check");
duke@435 1282 assert(bytes > 0, "capacity sanity check");
duke@435 1283
duke@435 1284 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
duke@435 1285 // prevent accidental detachment of this process's PerfMemory region
duke@435 1286 return;
duke@435 1287 }
duke@435 1288
duke@435 1289 unmap_shared(addr, bytes);
duke@435 1290 }
duke@435 1291
duke@435 1292 char* PerfMemory::backing_store_filename() {
duke@435 1293 return backing_store_file_name;
duke@435 1294 }

mercurial