annotate src/os/bsd/vm/perfMemory_bsd.cpp @ 24608:068ddd707f15

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