src/os/bsd/vm/perfMemory_bsd.cpp

Thu, 24 May 2018 17:06:56 +0800

author
aoqi
date
Thu, 24 May 2018 17:06:56 +0800
changeset 8604
04d83ba48607
parent 8210
2d23269a45a0
parent 7994
04ff2f6cd0eb
child 9572
624a0741915c
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
clanger@8177 2 * Copyright (c) 2001, 2015, 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 }
clanger@8210 220 // If user is not root then see if the uid of the directory matches the effective uid of the process.
clanger@8210 221 uid_t euid = geteuid();
clanger@8210 222 if ((euid != 0) && (statp->st_uid != euid)) {
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
clanger@8177 378 // Set the current directory to dirname by using the fd of the directory and
clanger@8177 379 // handle errors, otherwise shared memory files will be created in cwd.
gthornbr@7493 380 result = fchdir(fd);
clanger@8177 381 if (result == OS_ERR) {
clanger@8177 382 if (PrintMiscellaneous && Verbose) {
clanger@8177 383 warning("could not change to directory %s", dirname);
clanger@8177 384 }
clanger@8177 385 if (*saved_cwd_fd != -1) {
clanger@8177 386 ::close(*saved_cwd_fd);
clanger@8177 387 *saved_cwd_fd = -1;
clanger@8177 388 }
clanger@8177 389 // Close the directory.
clanger@8177 390 os::closedir(dirp);
clanger@8177 391 return NULL;
clanger@8177 392 } else {
clanger@8177 393 return dirp;
clanger@8177 394 }
gthornbr@7493 395 }
gthornbr@7493 396
gthornbr@7493 397 // Close the directory and restore the current working directory.
gthornbr@7493 398 //
gthornbr@7493 399 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
gthornbr@7493 400
gthornbr@7493 401 int result;
gthornbr@7493 402 // If we have a saved cwd change back to it and close the fd.
gthornbr@7493 403 if (saved_cwd_fd != -1) {
gthornbr@7493 404 result = fchdir(saved_cwd_fd);
gthornbr@7493 405 ::close(saved_cwd_fd);
gthornbr@7493 406 }
gthornbr@7493 407
gthornbr@7493 408 // Close the directory.
gthornbr@7493 409 os::closedir(dirp);
gthornbr@7493 410 }
gthornbr@7493 411
gthornbr@7493 412 // Check if the given file descriptor is considered a secure.
gthornbr@7493 413 //
gthornbr@7493 414 static bool is_file_secure(int fd, const char *filename) {
gthornbr@7493 415
gthornbr@7493 416 int result;
gthornbr@7493 417 struct stat statbuf;
gthornbr@7493 418
gthornbr@7493 419 // Determine if the file is secure.
gthornbr@7493 420 RESTARTABLE(::fstat(fd, &statbuf), result);
gthornbr@7493 421 if (result == OS_ERR) {
gthornbr@7493 422 if (PrintMiscellaneous && Verbose) {
gthornbr@7493 423 warning("fstat failed on %s: %s\n", filename, strerror(errno));
gthornbr@7493 424 }
gthornbr@7493 425 return false;
gthornbr@7493 426 }
gthornbr@7493 427 if (statbuf.st_nlink > 1) {
gthornbr@7493 428 // A file with multiple links is not expected.
gthornbr@7493 429 if (PrintMiscellaneous && Verbose) {
gthornbr@7493 430 warning("file %s has multiple links\n", filename);
gthornbr@7493 431 }
gthornbr@7493 432 return false;
aoqi@0 433 }
aoqi@0 434 return true;
aoqi@0 435 }
aoqi@0 436
aoqi@0 437 // return the user name for the given user id
aoqi@0 438 //
aoqi@0 439 // the caller is expected to free the allocated memory.
aoqi@0 440 //
aoqi@0 441 static char* get_user_name(uid_t uid) {
aoqi@0 442
aoqi@0 443 struct passwd pwent;
aoqi@0 444
aoqi@0 445 // determine the max pwbuf size from sysconf, and hardcode
aoqi@0 446 // a default if this not available through sysconf.
aoqi@0 447 //
aoqi@0 448 long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
aoqi@0 449 if (bufsize == -1)
aoqi@0 450 bufsize = 1024;
aoqi@0 451
aoqi@0 452 char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
aoqi@0 453
aoqi@0 454 // POSIX interface to getpwuid_r is used on LINUX
aoqi@0 455 struct passwd* p;
aoqi@0 456 int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
aoqi@0 457
aoqi@0 458 if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
aoqi@0 459 if (PrintMiscellaneous && Verbose) {
aoqi@0 460 if (result != 0) {
aoqi@0 461 warning("Could not retrieve passwd entry: %s\n",
aoqi@0 462 strerror(result));
aoqi@0 463 }
aoqi@0 464 else if (p == NULL) {
aoqi@0 465 // this check is added to protect against an observed problem
aoqi@0 466 // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
aoqi@0 467 // indicating success, but has p == NULL. This was observed when
aoqi@0 468 // inserting a file descriptor exhaustion fault prior to the call
aoqi@0 469 // getpwuid_r() call. In this case, error is set to the appropriate
aoqi@0 470 // error condition, but this is undocumented behavior. This check
aoqi@0 471 // is safe under any condition, but the use of errno in the output
aoqi@0 472 // message may result in an erroneous message.
aoqi@0 473 // Bug Id 89052 was opened with RedHat.
aoqi@0 474 //
aoqi@0 475 warning("Could not retrieve passwd entry: %s\n",
aoqi@0 476 strerror(errno));
aoqi@0 477 }
aoqi@0 478 else {
aoqi@0 479 warning("Could not determine user name: %s\n",
aoqi@0 480 p->pw_name == NULL ? "pw_name = NULL" :
aoqi@0 481 "pw_name zero length");
aoqi@0 482 }
aoqi@0 483 }
aoqi@0 484 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
aoqi@0 485 return NULL;
aoqi@0 486 }
aoqi@0 487
aoqi@0 488 char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
aoqi@0 489 strcpy(user_name, p->pw_name);
aoqi@0 490
aoqi@0 491 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
aoqi@0 492 return user_name;
aoqi@0 493 }
aoqi@0 494
aoqi@0 495 // return the name of the user that owns the process identified by vmid.
aoqi@0 496 //
aoqi@0 497 // This method uses a slow directory search algorithm to find the backing
aoqi@0 498 // store file for the specified vmid and returns the user name, as determined
aoqi@0 499 // by the user name suffix of the hsperfdata_<username> directory name.
aoqi@0 500 //
aoqi@0 501 // the caller is expected to free the allocated memory.
aoqi@0 502 //
aoqi@0 503 static char* get_user_name_slow(int vmid, TRAPS) {
aoqi@0 504
aoqi@0 505 // short circuit the directory search if the process doesn't even exist.
aoqi@0 506 if (kill(vmid, 0) == OS_ERR) {
aoqi@0 507 if (errno == ESRCH) {
aoqi@0 508 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 509 "Process not found");
aoqi@0 510 }
aoqi@0 511 else /* EPERM */ {
aoqi@0 512 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
aoqi@0 513 }
aoqi@0 514 }
aoqi@0 515
aoqi@0 516 // directory search
aoqi@0 517 char* oldest_user = NULL;
aoqi@0 518 time_t oldest_ctime = 0;
aoqi@0 519
aoqi@0 520 const char* tmpdirname = os::get_temp_directory();
aoqi@0 521
gthornbr@7493 522 // open the temp directory
aoqi@0 523 DIR* tmpdirp = os::opendir(tmpdirname);
aoqi@0 524
aoqi@0 525 if (tmpdirp == NULL) {
gthornbr@7493 526 // Cannot open the directory to get the user name, return.
aoqi@0 527 return NULL;
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 // for each entry in the directory that matches the pattern hsperfdata_*,
aoqi@0 531 // open the directory and check if the file for the given vmid exists.
aoqi@0 532 // The file with the expected name and the latest creation date is used
aoqi@0 533 // to determine the user name for the process id.
aoqi@0 534 //
aoqi@0 535 struct dirent* dentry;
aoqi@0 536 char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
aoqi@0 537 errno = 0;
aoqi@0 538 while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
aoqi@0 539
aoqi@0 540 // check if the directory entry is a hsperfdata file
aoqi@0 541 if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
aoqi@0 542 continue;
aoqi@0 543 }
aoqi@0 544
aoqi@0 545 char* usrdir_name = NEW_C_HEAP_ARRAY(char,
aoqi@0 546 strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
aoqi@0 547 strcpy(usrdir_name, tmpdirname);
aoqi@0 548 strcat(usrdir_name, "/");
aoqi@0 549 strcat(usrdir_name, dentry->d_name);
aoqi@0 550
gthornbr@7493 551 // open the user directory
gthornbr@7493 552 DIR* subdirp = open_directory_secure(usrdir_name);
aoqi@0 553
aoqi@0 554 if (subdirp == NULL) {
aoqi@0 555 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
aoqi@0 556 continue;
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 struct dirent* udentry;
aoqi@0 560 char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
aoqi@0 561 errno = 0;
aoqi@0 562 while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
aoqi@0 563
aoqi@0 564 if (filename_to_pid(udentry->d_name) == vmid) {
aoqi@0 565 struct stat statbuf;
aoqi@0 566 int result;
aoqi@0 567
aoqi@0 568 char* filename = NEW_C_HEAP_ARRAY(char,
aoqi@0 569 strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
aoqi@0 570
aoqi@0 571 strcpy(filename, usrdir_name);
aoqi@0 572 strcat(filename, "/");
aoqi@0 573 strcat(filename, udentry->d_name);
aoqi@0 574
aoqi@0 575 // don't follow symbolic links for the file
aoqi@0 576 RESTARTABLE(::lstat(filename, &statbuf), result);
aoqi@0 577 if (result == OS_ERR) {
aoqi@0 578 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 579 continue;
aoqi@0 580 }
aoqi@0 581
aoqi@0 582 // skip over files that are not regular files.
aoqi@0 583 if (!S_ISREG(statbuf.st_mode)) {
aoqi@0 584 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 585 continue;
aoqi@0 586 }
aoqi@0 587
aoqi@0 588 // compare and save filename with latest creation time
aoqi@0 589 if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
aoqi@0 590
aoqi@0 591 if (statbuf.st_ctime > oldest_ctime) {
aoqi@0 592 char* user = strchr(dentry->d_name, '_') + 1;
aoqi@0 593
aoqi@0 594 if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
aoqi@0 595 oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
aoqi@0 596
aoqi@0 597 strcpy(oldest_user, user);
aoqi@0 598 oldest_ctime = statbuf.st_ctime;
aoqi@0 599 }
aoqi@0 600 }
aoqi@0 601
aoqi@0 602 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 603 }
aoqi@0 604 }
aoqi@0 605 os::closedir(subdirp);
aoqi@0 606 FREE_C_HEAP_ARRAY(char, udbuf, mtInternal);
aoqi@0 607 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
aoqi@0 608 }
aoqi@0 609 os::closedir(tmpdirp);
aoqi@0 610 FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal);
aoqi@0 611
aoqi@0 612 return(oldest_user);
aoqi@0 613 }
aoqi@0 614
aoqi@0 615 // return the name of the user that owns the JVM indicated by the given vmid.
aoqi@0 616 //
aoqi@0 617 static char* get_user_name(int vmid, TRAPS) {
aoqi@0 618 return get_user_name_slow(vmid, CHECK_NULL);
aoqi@0 619 }
aoqi@0 620
aoqi@0 621 // return the file name of the backing store file for the named
aoqi@0 622 // shared memory region for the given user name and vmid.
aoqi@0 623 //
aoqi@0 624 // the caller is expected to free the allocated memory.
aoqi@0 625 //
aoqi@0 626 static char* get_sharedmem_filename(const char* dirname, int vmid) {
aoqi@0 627
aoqi@0 628 // add 2 for the file separator and a null terminator.
aoqi@0 629 size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
aoqi@0 630
aoqi@0 631 char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
aoqi@0 632 snprintf(name, nbytes, "%s/%d", dirname, vmid);
aoqi@0 633
aoqi@0 634 return name;
aoqi@0 635 }
aoqi@0 636
aoqi@0 637
aoqi@0 638 // remove file
aoqi@0 639 //
aoqi@0 640 // this method removes the file specified by the given path
aoqi@0 641 //
aoqi@0 642 static void remove_file(const char* path) {
aoqi@0 643
aoqi@0 644 int result;
aoqi@0 645
aoqi@0 646 // if the file is a directory, the following unlink will fail. since
aoqi@0 647 // we don't expect to find directories in the user temp directory, we
aoqi@0 648 // won't try to handle this situation. even if accidentially or
aoqi@0 649 // maliciously planted, the directory's presence won't hurt anything.
aoqi@0 650 //
aoqi@0 651 RESTARTABLE(::unlink(path), result);
aoqi@0 652 if (PrintMiscellaneous && Verbose && result == OS_ERR) {
aoqi@0 653 if (errno != ENOENT) {
aoqi@0 654 warning("Could not unlink shared memory backing"
aoqi@0 655 " store file %s : %s\n", path, strerror(errno));
aoqi@0 656 }
aoqi@0 657 }
aoqi@0 658 }
aoqi@0 659
aoqi@0 660
aoqi@0 661 // cleanup stale shared memory resources
aoqi@0 662 //
aoqi@0 663 // This method attempts to remove all stale shared memory files in
aoqi@0 664 // the named user temporary directory. It scans the named directory
aoqi@0 665 // for files matching the pattern ^$[0-9]*$. For each file found, the
aoqi@0 666 // process id is extracted from the file name and a test is run to
aoqi@0 667 // determine if the process is alive. If the process is not alive,
aoqi@0 668 // any stale file resources are removed.
aoqi@0 669 //
aoqi@0 670 static void cleanup_sharedmem_resources(const char* dirname) {
aoqi@0 671
gthornbr@7493 672 int saved_cwd_fd;
gthornbr@7493 673 // open the directory and set the current working directory to it
gthornbr@7493 674 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
aoqi@0 675 if (dirp == NULL) {
gthornbr@7493 676 // directory doesn't exist or is insecure, so there is nothing to cleanup
aoqi@0 677 return;
aoqi@0 678 }
aoqi@0 679
aoqi@0 680 // for each entry in the directory that matches the expected file
aoqi@0 681 // name pattern, determine if the file resources are stale and if
aoqi@0 682 // so, remove the file resources. Note, instrumented HotSpot processes
aoqi@0 683 // for this user may start and/or terminate during this search and
aoqi@0 684 // remove or create new files in this directory. The behavior of this
aoqi@0 685 // loop under these conditions is dependent upon the implementation of
aoqi@0 686 // opendir/readdir.
aoqi@0 687 //
aoqi@0 688 struct dirent* entry;
aoqi@0 689 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
gthornbr@7493 690
aoqi@0 691 errno = 0;
aoqi@0 692 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
aoqi@0 693
aoqi@0 694 pid_t pid = filename_to_pid(entry->d_name);
aoqi@0 695
aoqi@0 696 if (pid == 0) {
aoqi@0 697
aoqi@0 698 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
aoqi@0 699
aoqi@0 700 // attempt to remove all unexpected files, except "." and ".."
gthornbr@7493 701 unlink(entry->d_name);
aoqi@0 702 }
aoqi@0 703
aoqi@0 704 errno = 0;
aoqi@0 705 continue;
aoqi@0 706 }
aoqi@0 707
aoqi@0 708 // we now have a file name that converts to a valid integer
aoqi@0 709 // that could represent a process id . if this process id
aoqi@0 710 // matches the current process id or the process is not running,
aoqi@0 711 // then remove the stale file resources.
aoqi@0 712 //
aoqi@0 713 // process liveness is detected by sending signal number 0 to
aoqi@0 714 // the process id (see kill(2)). if kill determines that the
aoqi@0 715 // process does not exist, then the file resources are removed.
aoqi@0 716 // if kill determines that that we don't have permission to
aoqi@0 717 // signal the process, then the file resources are assumed to
aoqi@0 718 // be stale and are removed because the resources for such a
aoqi@0 719 // process should be in a different user specific directory.
aoqi@0 720 //
aoqi@0 721 if ((pid == os::current_process_id()) ||
aoqi@0 722 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
aoqi@0 723
gthornbr@7493 724 unlink(entry->d_name);
aoqi@0 725 }
aoqi@0 726 errno = 0;
aoqi@0 727 }
gthornbr@7493 728
gthornbr@7493 729 // close the directory and reset the current working directory
gthornbr@7493 730 close_directory_secure_cwd(dirp, saved_cwd_fd);
gthornbr@7493 731
aoqi@0 732 FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
aoqi@0 733 }
aoqi@0 734
aoqi@0 735 // make the user specific temporary directory. Returns true if
aoqi@0 736 // the directory exists and is secure upon return. Returns false
aoqi@0 737 // if the directory exists but is either a symlink, is otherwise
aoqi@0 738 // insecure, or if an error occurred.
aoqi@0 739 //
aoqi@0 740 static bool make_user_tmp_dir(const char* dirname) {
aoqi@0 741
aoqi@0 742 // create the directory with 0755 permissions. note that the directory
aoqi@0 743 // will be owned by euid::egid, which may not be the same as uid::gid.
aoqi@0 744 //
aoqi@0 745 if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
aoqi@0 746 if (errno == EEXIST) {
aoqi@0 747 // The directory already exists and was probably created by another
aoqi@0 748 // JVM instance. However, this could also be the result of a
aoqi@0 749 // deliberate symlink. Verify that the existing directory is safe.
aoqi@0 750 //
aoqi@0 751 if (!is_directory_secure(dirname)) {
aoqi@0 752 // directory is not secure
aoqi@0 753 if (PrintMiscellaneous && Verbose) {
aoqi@0 754 warning("%s directory is insecure\n", dirname);
aoqi@0 755 }
aoqi@0 756 return false;
aoqi@0 757 }
aoqi@0 758 }
aoqi@0 759 else {
aoqi@0 760 // we encountered some other failure while attempting
aoqi@0 761 // to create the directory
aoqi@0 762 //
aoqi@0 763 if (PrintMiscellaneous && Verbose) {
aoqi@0 764 warning("could not create directory %s: %s\n",
aoqi@0 765 dirname, strerror(errno));
aoqi@0 766 }
aoqi@0 767 return false;
aoqi@0 768 }
aoqi@0 769 }
aoqi@0 770 return true;
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 // create the shared memory file resources
aoqi@0 774 //
aoqi@0 775 // This method creates the shared memory file with the given size
aoqi@0 776 // This method also creates the user specific temporary directory, if
aoqi@0 777 // it does not yet exist.
aoqi@0 778 //
aoqi@0 779 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
aoqi@0 780
aoqi@0 781 // make the user temporary directory
aoqi@0 782 if (!make_user_tmp_dir(dirname)) {
aoqi@0 783 // could not make/find the directory or the found directory
aoqi@0 784 // was not secure
aoqi@0 785 return -1;
aoqi@0 786 }
aoqi@0 787
gthornbr@7493 788 int saved_cwd_fd;
gthornbr@7493 789 // open the directory and set the current working directory to it
gthornbr@7493 790 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
gthornbr@7493 791 if (dirp == NULL) {
gthornbr@7493 792 // Directory doesn't exist or is insecure, so cannot create shared
gthornbr@7493 793 // memory file.
gthornbr@7493 794 return -1;
gthornbr@7493 795 }
gthornbr@7493 796
gthornbr@7493 797 // Open the filename in the current directory.
gthornbr@7493 798 // Cannot use O_TRUNC here; truncation of an existing file has to happen
gthornbr@7493 799 // after the is_file_secure() check below.
aoqi@0 800 int result;
gthornbr@7493 801 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
aoqi@0 802 if (result == OS_ERR) {
aoqi@0 803 if (PrintMiscellaneous && Verbose) {
gthornbr@7493 804 if (errno == ELOOP) {
gthornbr@7493 805 warning("file %s is a symlink and is not secure\n", filename);
gthornbr@7493 806 } else {
gthornbr@7493 807 warning("could not create file %s: %s\n", filename, strerror(errno));
gthornbr@7493 808 }
aoqi@0 809 }
gthornbr@7493 810 // close the directory and reset the current working directory
gthornbr@7493 811 close_directory_secure_cwd(dirp, saved_cwd_fd);
gthornbr@7493 812
aoqi@0 813 return -1;
aoqi@0 814 }
gthornbr@7493 815 // close the directory and reset the current working directory
gthornbr@7493 816 close_directory_secure_cwd(dirp, saved_cwd_fd);
aoqi@0 817
aoqi@0 818 // save the file descriptor
aoqi@0 819 int fd = result;
aoqi@0 820
gthornbr@7493 821 // check to see if the file is secure
gthornbr@7493 822 if (!is_file_secure(fd, filename)) {
gthornbr@7493 823 ::close(fd);
gthornbr@7493 824 return -1;
gthornbr@7493 825 }
gthornbr@7493 826
gthornbr@7493 827 // truncate the file to get rid of any existing data
gthornbr@7493 828 RESTARTABLE(::ftruncate(fd, (off_t)0), result);
gthornbr@7493 829 if (result == OS_ERR) {
gthornbr@7493 830 if (PrintMiscellaneous && Verbose) {
gthornbr@7493 831 warning("could not truncate shared memory file: %s\n", strerror(errno));
gthornbr@7493 832 }
gthornbr@7493 833 ::close(fd);
gthornbr@7493 834 return -1;
gthornbr@7493 835 }
aoqi@0 836 // set the file size
aoqi@0 837 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
aoqi@0 838 if (result == OS_ERR) {
aoqi@0 839 if (PrintMiscellaneous && Verbose) {
aoqi@0 840 warning("could not set shared memory file size: %s\n", strerror(errno));
aoqi@0 841 }
aoqi@0 842 ::close(fd);
aoqi@0 843 return -1;
aoqi@0 844 }
aoqi@0 845
aoqi@0 846 // Verify that we have enough disk space for this file.
aoqi@0 847 // We'll get random SIGBUS crashes on memory accesses if
aoqi@0 848 // we don't.
aoqi@0 849
aoqi@0 850 for (size_t seekpos = 0; seekpos < size; seekpos += os::vm_page_size()) {
aoqi@0 851 int zero_int = 0;
aoqi@0 852 result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos));
aoqi@0 853 if (result == -1 ) break;
aoqi@0 854 RESTARTABLE(::write(fd, &zero_int, 1), result);
aoqi@0 855 if (result != 1) {
aoqi@0 856 if (errno == ENOSPC) {
aoqi@0 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);
aoqi@0 858 }
aoqi@0 859 break;
aoqi@0 860 }
aoqi@0 861 }
aoqi@0 862
aoqi@0 863 if (result != -1) {
aoqi@0 864 return fd;
aoqi@0 865 } else {
aoqi@0 866 ::close(fd);
aoqi@0 867 return -1;
aoqi@0 868 }
aoqi@0 869 }
aoqi@0 870
aoqi@0 871 // open the shared memory file for the given user and vmid. returns
aoqi@0 872 // the file descriptor for the open file or -1 if the file could not
aoqi@0 873 // be opened.
aoqi@0 874 //
aoqi@0 875 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
aoqi@0 876
aoqi@0 877 // open the file
aoqi@0 878 int result;
aoqi@0 879 RESTARTABLE(::open(filename, oflags), result);
aoqi@0 880 if (result == OS_ERR) {
aoqi@0 881 if (errno == ENOENT) {
aoqi@0 882 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 883 "Process not found", OS_ERR);
aoqi@0 884 }
aoqi@0 885 else if (errno == EACCES) {
aoqi@0 886 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 887 "Permission denied", OS_ERR);
aoqi@0 888 }
aoqi@0 889 else {
aoqi@0 890 THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR);
aoqi@0 891 }
aoqi@0 892 }
gthornbr@7493 893 int fd = result;
aoqi@0 894
gthornbr@7493 895 // check to see if the file is secure
gthornbr@7493 896 if (!is_file_secure(fd, filename)) {
gthornbr@7493 897 ::close(fd);
gthornbr@7493 898 return -1;
gthornbr@7493 899 }
gthornbr@7493 900
gthornbr@7493 901 return fd;
aoqi@0 902 }
aoqi@0 903
aoqi@0 904 // create a named shared memory region. returns the address of the
aoqi@0 905 // memory region on success or NULL on failure. A return value of
aoqi@0 906 // NULL will ultimately disable the shared memory feature.
aoqi@0 907 //
aoqi@0 908 // On Solaris and Bsd, the name space for shared memory objects
aoqi@0 909 // is the file system name space.
aoqi@0 910 //
aoqi@0 911 // A monitoring application attaching to a JVM does not need to know
aoqi@0 912 // the file system name of the shared memory object. However, it may
aoqi@0 913 // be convenient for applications to discover the existence of newly
aoqi@0 914 // created and terminating JVMs by watching the file system name space
aoqi@0 915 // for files being created or removed.
aoqi@0 916 //
aoqi@0 917 static char* mmap_create_shared(size_t size) {
aoqi@0 918
aoqi@0 919 int result;
aoqi@0 920 int fd;
aoqi@0 921 char* mapAddress;
aoqi@0 922
aoqi@0 923 int vmid = os::current_process_id();
aoqi@0 924
aoqi@0 925 char* user_name = get_user_name(geteuid());
aoqi@0 926
aoqi@0 927 if (user_name == NULL)
aoqi@0 928 return NULL;
aoqi@0 929
aoqi@0 930 char* dirname = get_user_tmp_dir(user_name);
aoqi@0 931 char* filename = get_sharedmem_filename(dirname, vmid);
aoqi@0 932
gthornbr@7493 933 // get the short filename
gthornbr@7493 934 char* short_filename = strrchr(filename, '/');
gthornbr@7493 935 if (short_filename == NULL) {
gthornbr@7493 936 short_filename = filename;
gthornbr@7493 937 } else {
gthornbr@7493 938 short_filename++;
gthornbr@7493 939 }
gthornbr@7493 940
aoqi@0 941 // cleanup any stale shared memory files
aoqi@0 942 cleanup_sharedmem_resources(dirname);
aoqi@0 943
aoqi@0 944 assert(((size > 0) && (size % os::vm_page_size() == 0)),
aoqi@0 945 "unexpected PerfMemory region size");
aoqi@0 946
gthornbr@7493 947 fd = create_sharedmem_resources(dirname, short_filename, size);
aoqi@0 948
aoqi@0 949 FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
aoqi@0 950 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
aoqi@0 951
aoqi@0 952 if (fd == -1) {
aoqi@0 953 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 954 return NULL;
aoqi@0 955 }
aoqi@0 956
aoqi@0 957 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
aoqi@0 958
aoqi@0 959 result = ::close(fd);
aoqi@0 960 assert(result != OS_ERR, "could not close file");
aoqi@0 961
aoqi@0 962 if (mapAddress == MAP_FAILED) {
aoqi@0 963 if (PrintMiscellaneous && Verbose) {
aoqi@0 964 warning("mmap failed - %s\n", strerror(errno));
aoqi@0 965 }
aoqi@0 966 remove_file(filename);
aoqi@0 967 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 968 return NULL;
aoqi@0 969 }
aoqi@0 970
aoqi@0 971 // save the file name for use in delete_shared_memory()
aoqi@0 972 backing_store_file_name = filename;
aoqi@0 973
aoqi@0 974 // clear the shared memory region
aoqi@0 975 (void)::memset((void*) mapAddress, 0, size);
aoqi@0 976
aoqi@0 977 // it does not go through os api, the operation has to record from here
zgu@7074 978 MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal);
aoqi@0 979
aoqi@0 980 return mapAddress;
aoqi@0 981 }
aoqi@0 982
aoqi@0 983 // release a named shared memory region
aoqi@0 984 //
aoqi@0 985 static void unmap_shared(char* addr, size_t bytes) {
aoqi@0 986 os::release_memory(addr, bytes);
aoqi@0 987 }
aoqi@0 988
aoqi@0 989 // create the PerfData memory region in shared memory.
aoqi@0 990 //
aoqi@0 991 static char* create_shared_memory(size_t size) {
aoqi@0 992
aoqi@0 993 // create the shared memory region.
aoqi@0 994 return mmap_create_shared(size);
aoqi@0 995 }
aoqi@0 996
aoqi@0 997 // delete the shared PerfData memory region
aoqi@0 998 //
aoqi@0 999 static void delete_shared_memory(char* addr, size_t size) {
aoqi@0 1000
aoqi@0 1001 // cleanup the persistent shared memory resources. since DestroyJavaVM does
aoqi@0 1002 // not support unloading of the JVM, unmapping of the memory resource is
aoqi@0 1003 // not performed. The memory will be reclaimed by the OS upon termination of
aoqi@0 1004 // the process. The backing store file is deleted from the file system.
aoqi@0 1005
aoqi@0 1006 assert(!PerfDisableSharedMem, "shouldn't be here");
aoqi@0 1007
aoqi@0 1008 if (backing_store_file_name != NULL) {
aoqi@0 1009 remove_file(backing_store_file_name);
aoqi@0 1010 // Don't.. Free heap memory could deadlock os::abort() if it is called
aoqi@0 1011 // from signal handler. OS will reclaim the heap memory.
aoqi@0 1012 // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
aoqi@0 1013 backing_store_file_name = NULL;
aoqi@0 1014 }
aoqi@0 1015 }
aoqi@0 1016
aoqi@0 1017 // return the size of the file for the given file descriptor
aoqi@0 1018 // or 0 if it is not a valid size for a shared memory file
aoqi@0 1019 //
aoqi@0 1020 static size_t sharedmem_filesize(int fd, TRAPS) {
aoqi@0 1021
aoqi@0 1022 struct stat statbuf;
aoqi@0 1023 int result;
aoqi@0 1024
aoqi@0 1025 RESTARTABLE(::fstat(fd, &statbuf), result);
aoqi@0 1026 if (result == OS_ERR) {
aoqi@0 1027 if (PrintMiscellaneous && Verbose) {
aoqi@0 1028 warning("fstat failed: %s\n", strerror(errno));
aoqi@0 1029 }
aoqi@0 1030 THROW_MSG_0(vmSymbols::java_io_IOException(),
aoqi@0 1031 "Could not determine PerfMemory size");
aoqi@0 1032 }
aoqi@0 1033
aoqi@0 1034 if ((statbuf.st_size == 0) ||
aoqi@0 1035 ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
aoqi@0 1036 THROW_MSG_0(vmSymbols::java_lang_Exception(),
aoqi@0 1037 "Invalid PerfMemory size");
aoqi@0 1038 }
aoqi@0 1039
aoqi@0 1040 return (size_t)statbuf.st_size;
aoqi@0 1041 }
aoqi@0 1042
aoqi@0 1043 // attach to a named shared memory region.
aoqi@0 1044 //
aoqi@0 1045 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
aoqi@0 1046
aoqi@0 1047 char* mapAddress;
aoqi@0 1048 int result;
aoqi@0 1049 int fd;
aoqi@0 1050 size_t size = 0;
aoqi@0 1051 const char* luser = NULL;
aoqi@0 1052
aoqi@0 1053 int mmap_prot;
aoqi@0 1054 int file_flags;
aoqi@0 1055
aoqi@0 1056 ResourceMark rm;
aoqi@0 1057
aoqi@0 1058 // map the high level access mode to the appropriate permission
aoqi@0 1059 // constructs for the file and the shared memory mapping.
aoqi@0 1060 if (mode == PerfMemory::PERF_MODE_RO) {
aoqi@0 1061 mmap_prot = PROT_READ;
gthornbr@7493 1062 file_flags = O_RDONLY | O_NOFOLLOW;
aoqi@0 1063 }
aoqi@0 1064 else if (mode == PerfMemory::PERF_MODE_RW) {
aoqi@0 1065 #ifdef LATER
aoqi@0 1066 mmap_prot = PROT_READ | PROT_WRITE;
gthornbr@7493 1067 file_flags = O_RDWR | O_NOFOLLOW;
aoqi@0 1068 #else
aoqi@0 1069 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1070 "Unsupported access mode");
aoqi@0 1071 #endif
aoqi@0 1072 }
aoqi@0 1073 else {
aoqi@0 1074 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1075 "Illegal access mode");
aoqi@0 1076 }
aoqi@0 1077
aoqi@0 1078 if (user == NULL || strlen(user) == 0) {
aoqi@0 1079 luser = get_user_name(vmid, CHECK);
aoqi@0 1080 }
aoqi@0 1081 else {
aoqi@0 1082 luser = user;
aoqi@0 1083 }
aoqi@0 1084
aoqi@0 1085 if (luser == NULL) {
aoqi@0 1086 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1087 "Could not map vmid to user Name");
aoqi@0 1088 }
aoqi@0 1089
aoqi@0 1090 char* dirname = get_user_tmp_dir(luser);
aoqi@0 1091
aoqi@0 1092 // since we don't follow symbolic links when creating the backing
aoqi@0 1093 // store file, we don't follow them when attaching either.
aoqi@0 1094 //
aoqi@0 1095 if (!is_directory_secure(dirname)) {
aoqi@0 1096 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
aoqi@0 1097 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1098 "Process not found");
aoqi@0 1099 }
aoqi@0 1100
aoqi@0 1101 char* filename = get_sharedmem_filename(dirname, vmid);
aoqi@0 1102
aoqi@0 1103 // copy heap memory to resource memory. the open_sharedmem_file
aoqi@0 1104 // method below need to use the filename, but could throw an
aoqi@0 1105 // exception. using a resource array prevents the leak that
aoqi@0 1106 // would otherwise occur.
aoqi@0 1107 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
aoqi@0 1108 strcpy(rfilename, filename);
aoqi@0 1109
aoqi@0 1110 // free the c heap resources that are no longer needed
aoqi@0 1111 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
aoqi@0 1112 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
aoqi@0 1113 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 1114
aoqi@0 1115 // open the shared memory file for the give vmid
aoqi@0 1116 fd = open_sharedmem_file(rfilename, file_flags, CHECK);
aoqi@0 1117 assert(fd != OS_ERR, "unexpected value");
aoqi@0 1118
aoqi@0 1119 if (*sizep == 0) {
aoqi@0 1120 size = sharedmem_filesize(fd, CHECK);
aoqi@0 1121 } else {
aoqi@0 1122 size = *sizep;
aoqi@0 1123 }
aoqi@0 1124
aoqi@0 1125 assert(size > 0, "unexpected size <= 0");
aoqi@0 1126
aoqi@0 1127 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
aoqi@0 1128
aoqi@0 1129 // attempt to close the file - restart if it gets interrupted,
aoqi@0 1130 // but ignore other failures
aoqi@0 1131 result = ::close(fd);
aoqi@0 1132 assert(result != OS_ERR, "could not close file");
aoqi@0 1133
aoqi@0 1134 if (mapAddress == MAP_FAILED) {
aoqi@0 1135 if (PrintMiscellaneous && Verbose) {
aoqi@0 1136 warning("mmap failed: %s\n", strerror(errno));
aoqi@0 1137 }
aoqi@0 1138 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
aoqi@0 1139 "Could not map PerfMemory");
aoqi@0 1140 }
aoqi@0 1141
aoqi@0 1142 // it does not go through os api, the operation has to record from here
zgu@7074 1143 MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal);
aoqi@0 1144
aoqi@0 1145 *addr = mapAddress;
aoqi@0 1146 *sizep = size;
aoqi@0 1147
aoqi@0 1148 if (PerfTraceMemOps) {
aoqi@0 1149 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
aoqi@0 1150 INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress));
aoqi@0 1151 }
aoqi@0 1152 }
aoqi@0 1153
aoqi@0 1154
aoqi@0 1155
aoqi@0 1156
aoqi@0 1157 // create the PerfData memory region
aoqi@0 1158 //
aoqi@0 1159 // This method creates the memory region used to store performance
aoqi@0 1160 // data for the JVM. The memory may be created in standard or
aoqi@0 1161 // shared memory.
aoqi@0 1162 //
aoqi@0 1163 void PerfMemory::create_memory_region(size_t size) {
aoqi@0 1164
aoqi@0 1165 if (PerfDisableSharedMem) {
aoqi@0 1166 // do not share the memory for the performance data.
aoqi@0 1167 _start = create_standard_memory(size);
aoqi@0 1168 }
aoqi@0 1169 else {
aoqi@0 1170 _start = create_shared_memory(size);
aoqi@0 1171 if (_start == NULL) {
aoqi@0 1172
aoqi@0 1173 // creation of the shared memory region failed, attempt
aoqi@0 1174 // to create a contiguous, non-shared memory region instead.
aoqi@0 1175 //
aoqi@0 1176 if (PrintMiscellaneous && Verbose) {
aoqi@0 1177 warning("Reverting to non-shared PerfMemory region.\n");
aoqi@0 1178 }
aoqi@0 1179 PerfDisableSharedMem = true;
aoqi@0 1180 _start = create_standard_memory(size);
aoqi@0 1181 }
aoqi@0 1182 }
aoqi@0 1183
aoqi@0 1184 if (_start != NULL) _capacity = size;
aoqi@0 1185
aoqi@0 1186 }
aoqi@0 1187
aoqi@0 1188 // delete the PerfData memory region
aoqi@0 1189 //
aoqi@0 1190 // This method deletes the memory region used to store performance
aoqi@0 1191 // data for the JVM. The memory region indicated by the <address, size>
aoqi@0 1192 // tuple will be inaccessible after a call to this method.
aoqi@0 1193 //
aoqi@0 1194 void PerfMemory::delete_memory_region() {
aoqi@0 1195
aoqi@0 1196 assert((start() != NULL && capacity() > 0), "verify proper state");
aoqi@0 1197
aoqi@0 1198 // If user specifies PerfDataSaveFile, it will save the performance data
aoqi@0 1199 // to the specified file name no matter whether PerfDataSaveToFile is specified
aoqi@0 1200 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
aoqi@0 1201 // -XX:+PerfDataSaveToFile.
aoqi@0 1202 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
aoqi@0 1203 save_memory_to_file(start(), capacity());
aoqi@0 1204 }
aoqi@0 1205
aoqi@0 1206 if (PerfDisableSharedMem) {
aoqi@0 1207 delete_standard_memory(start(), capacity());
aoqi@0 1208 }
aoqi@0 1209 else {
aoqi@0 1210 delete_shared_memory(start(), capacity());
aoqi@0 1211 }
aoqi@0 1212 }
aoqi@0 1213
aoqi@0 1214 // attach to the PerfData memory region for another JVM
aoqi@0 1215 //
aoqi@0 1216 // This method returns an <address, size> tuple that points to
aoqi@0 1217 // a memory buffer that is kept reasonably synchronized with
aoqi@0 1218 // the PerfData memory region for the indicated JVM. This
aoqi@0 1219 // buffer may be kept in synchronization via shared memory
aoqi@0 1220 // or some other mechanism that keeps the buffer updated.
aoqi@0 1221 //
aoqi@0 1222 // If the JVM chooses not to support the attachability feature,
aoqi@0 1223 // this method should throw an UnsupportedOperation exception.
aoqi@0 1224 //
aoqi@0 1225 // This implementation utilizes named shared memory to map
aoqi@0 1226 // the indicated process's PerfData memory region into this JVMs
aoqi@0 1227 // address space.
aoqi@0 1228 //
aoqi@0 1229 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
aoqi@0 1230
aoqi@0 1231 if (vmid == 0 || vmid == os::current_process_id()) {
aoqi@0 1232 *addrp = start();
aoqi@0 1233 *sizep = capacity();
aoqi@0 1234 return;
aoqi@0 1235 }
aoqi@0 1236
aoqi@0 1237 mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
aoqi@0 1238 }
aoqi@0 1239
aoqi@0 1240 // detach from the PerfData memory region of another JVM
aoqi@0 1241 //
aoqi@0 1242 // This method detaches the PerfData memory region of another
aoqi@0 1243 // JVM, specified as an <address, size> tuple of a buffer
aoqi@0 1244 // in this process's address space. This method may perform
aoqi@0 1245 // arbitrary actions to accomplish the detachment. The memory
aoqi@0 1246 // region specified by <address, size> will be inaccessible after
aoqi@0 1247 // a call to this method.
aoqi@0 1248 //
aoqi@0 1249 // If the JVM chooses not to support the attachability feature,
aoqi@0 1250 // this method should throw an UnsupportedOperation exception.
aoqi@0 1251 //
aoqi@0 1252 // This implementation utilizes named shared memory to detach
aoqi@0 1253 // the indicated process's PerfData memory region from this
aoqi@0 1254 // process's address space.
aoqi@0 1255 //
aoqi@0 1256 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
aoqi@0 1257
aoqi@0 1258 assert(addr != 0, "address sanity check");
aoqi@0 1259 assert(bytes > 0, "capacity sanity check");
aoqi@0 1260
aoqi@0 1261 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
aoqi@0 1262 // prevent accidental detachment of this process's PerfMemory region
aoqi@0 1263 return;
aoqi@0 1264 }
aoqi@0 1265
aoqi@0 1266 unmap_shared(addr, bytes);
aoqi@0 1267 }
aoqi@0 1268
aoqi@0 1269 char* PerfMemory::backing_store_filename() {
aoqi@0 1270 return backing_store_file_name;
aoqi@0 1271 }

mercurial