src/os/bsd/vm/perfMemory_bsd.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7495
42f27b59c550
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial