src/os/linux/vm/perfMemory_linux.cpp

Tue, 24 Feb 2015 15:04:52 -0500

author
dlong
date
Tue, 24 Feb 2015 15:04:52 -0500
changeset 7598
ddce0b7cee93
parent 7495
42f27b59c550
child 7535
7ae4e26cb1e0
child 7715
f3ffb37f88a6
permissions
-rw-r--r--

8072383: resolve conflicts between open and closed ports
Summary: refactor close to remove references to closed ports
Reviewed-by: kvn, simonis, sgehwolf, dholmes

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

mercurial