duke@435: /* dcubed@6414: * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "os_linux.inline.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/perfMemory.hpp" zgu@4193: #include "services/memTracker.hpp" stefank@2314: #include "utilities/exceptions.hpp" duke@435: duke@435: // put OS-includes here duke@435: # include duke@435: # include duke@435: # include duke@435: # include duke@435: # include duke@435: # include duke@435: # include duke@435: # include duke@435: duke@435: static char* backing_store_file_name = NULL; // name of the backing store duke@435: // file, if successfully created. duke@435: duke@435: // Standard Memory Implementation Details duke@435: duke@435: // create the PerfData memory region in standard memory. duke@435: // duke@435: static char* create_standard_memory(size_t size) { duke@435: duke@435: // allocate an aligned chuck of memory duke@435: char* mapAddress = os::reserve_memory(size); duke@435: duke@435: if (mapAddress == NULL) { duke@435: return NULL; duke@435: } duke@435: duke@435: // commit memory dcubed@5255: if (!os::commit_memory(mapAddress, size, !ExecMem)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("Could not commit PerfData memory\n"); duke@435: } duke@435: os::release_memory(mapAddress, size); duke@435: return NULL; duke@435: } duke@435: duke@435: return mapAddress; duke@435: } duke@435: duke@435: // delete the PerfData memory region duke@435: // duke@435: static void delete_standard_memory(char* addr, size_t size) { duke@435: duke@435: // there are no persistent external resources to cleanup for standard duke@435: // memory. since DestroyJavaVM does not support unloading of the JVM, duke@435: // cleanup of the memory resource is not performed. The memory will be duke@435: // reclaimed by the OS upon termination of the process. duke@435: // duke@435: return; duke@435: } duke@435: duke@435: // save the specified memory region to the given file duke@435: // duke@435: // Note: this function might be called from signal handler (by os::abort()), duke@435: // don't allocate heap memory. duke@435: // duke@435: static void save_memory_to_file(char* addr, size_t size) { duke@435: duke@435: const char* destfile = PerfMemory::get_perfdata_file_path(); duke@435: assert(destfile[0] != '\0', "invalid PerfData file path"); duke@435: duke@435: int result; duke@435: duke@435: RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), duke@435: result);; duke@435: if (result == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("Could not create Perfdata save file: %s: %s\n", duke@435: destfile, strerror(errno)); duke@435: } duke@435: } else { duke@435: int fd = result; duke@435: duke@435: for (size_t remaining = size; remaining > 0;) { duke@435: duke@435: RESTARTABLE(::write(fd, addr, remaining), result); duke@435: if (result == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("Could not write Perfdata save file: %s: %s\n", duke@435: destfile, strerror(errno)); duke@435: } duke@435: break; duke@435: } duke@435: duke@435: remaining -= (size_t)result; duke@435: addr += result; duke@435: } duke@435: rdurbin@5264: result = ::close(fd); duke@435: if (PrintMiscellaneous && Verbose) { duke@435: if (result == OS_ERR) { duke@435: warning("Could not close %s: %s\n", destfile, strerror(errno)); duke@435: } duke@435: } duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, destfile, mtInternal); duke@435: } duke@435: duke@435: duke@435: // Shared Memory Implementation Details duke@435: duke@435: // Note: the solaris and linux shared memory implementation uses the mmap duke@435: // interface with a backing store file to implement named shared memory. duke@435: // Using the file system as the name space for shared memory allows a duke@435: // common name space to be supported across a variety of platforms. It duke@435: // also provides a name space that Java applications can deal with through duke@435: // simple file apis. duke@435: // duke@435: // The solaris and linux implementations store the backing store file in duke@435: // a user specific temporary directory located in the /tmp file system, duke@435: // which is always a local file system and is sometimes a RAM based file duke@435: // system. duke@435: duke@435: // return the user specific temporary directory name. duke@435: // duke@435: // the caller is expected to free the allocated memory. duke@435: // duke@435: static char* get_user_tmp_dir(const char* user) { duke@435: duke@435: const char* tmpdir = os::get_temp_directory(); duke@435: const char* perfdir = PERFDATA_NAME; coleenp@1788: size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; zgu@3900: char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); duke@435: duke@435: // construct the path name to user specific tmp directory coleenp@1788: snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user); duke@435: duke@435: return dirname; duke@435: } duke@435: duke@435: // convert the given file name into a process id. if the file duke@435: // does not meet the file naming constraints, return 0. duke@435: // duke@435: static pid_t filename_to_pid(const char* filename) { duke@435: duke@435: // a filename that doesn't begin with a digit is not a duke@435: // candidate for conversion. duke@435: // duke@435: if (!isdigit(*filename)) { duke@435: return 0; duke@435: } duke@435: duke@435: // check if file name can be converted to an integer without duke@435: // any leftover characters. duke@435: // duke@435: char* remainder = NULL; duke@435: errno = 0; duke@435: pid_t pid = (pid_t)strtol(filename, &remainder, 10); duke@435: duke@435: if (errno != 0) { duke@435: return 0; duke@435: } duke@435: duke@435: // check for left over characters. If any, then the filename is duke@435: // not a candidate for conversion. duke@435: // duke@435: if (remainder != NULL && *remainder != '\0') { duke@435: return 0; duke@435: } duke@435: duke@435: // successful conversion, return the pid duke@435: return pid; duke@435: } duke@435: duke@435: gthornbr@7493: // Check if the given statbuf is considered a secure directory for gthornbr@7493: // the backing store files. Returns true if the directory is considered gthornbr@7493: // a secure location. Returns false if the statbuf is a symbolic link or gthornbr@7493: // if an error occurred. gthornbr@7493: // gthornbr@7493: static bool is_statbuf_secure(struct stat *statp) { gthornbr@7493: if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) { gthornbr@7493: // The path represents a link or some non-directory file type, gthornbr@7493: // which is not what we expected. Declare it insecure. gthornbr@7493: // gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: // We have an existing directory, check if the permissions are safe. gthornbr@7493: // gthornbr@7493: if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) { gthornbr@7493: // The directory is open for writing and could be subjected gthornbr@7493: // to a symlink or a hard link attack. Declare it insecure. gthornbr@7493: // gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: // See if the uid of the directory matches the effective uid of the process. gthornbr@7493: // gthornbr@7493: if (statp->st_uid != geteuid()) { gthornbr@7493: // The directory was not created by this user, declare it insecure. gthornbr@7493: // gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: return true; gthornbr@7493: } gthornbr@7493: gthornbr@7493: gthornbr@7493: // Check if the given path is considered a secure directory for duke@435: // the backing store files. Returns true if the directory exists duke@435: // and is considered a secure location. Returns false if the path twisti@1040: // is a symbolic link or if an error occurred. duke@435: // duke@435: static bool is_directory_secure(const char* path) { duke@435: struct stat statbuf; duke@435: int result = 0; duke@435: duke@435: RESTARTABLE(::lstat(path, &statbuf), result); duke@435: if (result == OS_ERR) { duke@435: return false; duke@435: } duke@435: gthornbr@7493: // The path exists, see if it is secure. gthornbr@7493: return is_statbuf_secure(&statbuf); gthornbr@7493: } gthornbr@7493: gthornbr@7493: gthornbr@7493: // Check if the given directory file descriptor is considered a secure gthornbr@7493: // directory for the backing store files. Returns true if the directory gthornbr@7493: // exists and is considered a secure location. Returns false if the path gthornbr@7493: // is a symbolic link or if an error occurred. gthornbr@7493: // gthornbr@7493: static bool is_dirfd_secure(int dir_fd) { gthornbr@7493: struct stat statbuf; gthornbr@7493: int result = 0; gthornbr@7493: gthornbr@7493: RESTARTABLE(::fstat(dir_fd, &statbuf), result); gthornbr@7493: if (result == OS_ERR) { duke@435: return false; duke@435: } gthornbr@7493: gthornbr@7493: // The path exists, now check its mode. gthornbr@7493: return is_statbuf_secure(&statbuf); gthornbr@7493: } gthornbr@7493: gthornbr@7493: gthornbr@7493: // Check to make sure fd1 and fd2 are referencing the same file system object. gthornbr@7493: // gthornbr@7493: static bool is_same_fsobject(int fd1, int fd2) { gthornbr@7493: struct stat statbuf1; gthornbr@7493: struct stat statbuf2; gthornbr@7493: int result = 0; gthornbr@7493: gthornbr@7493: RESTARTABLE(::fstat(fd1, &statbuf1), result); gthornbr@7493: if (result == OS_ERR) { gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: RESTARTABLE(::fstat(fd2, &statbuf2), result); gthornbr@7493: if (result == OS_ERR) { gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: gthornbr@7493: if ((statbuf1.st_ino == statbuf2.st_ino) && gthornbr@7493: (statbuf1.st_dev == statbuf2.st_dev)) { gthornbr@7493: return true; gthornbr@7493: } else { gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: } gthornbr@7493: gthornbr@7493: gthornbr@7493: // Open the directory of the given path and validate it. gthornbr@7493: // Return a DIR * of the open directory. gthornbr@7493: // gthornbr@7493: static DIR *open_directory_secure(const char* dirname) { gthornbr@7493: // Open the directory using open() so that it can be verified gthornbr@7493: // to be secure by calling is_dirfd_secure(), opendir() and then check gthornbr@7493: // to see if they are the same file system object. This method does not gthornbr@7493: // introduce a window of opportunity for the directory to be attacked that gthornbr@7493: // calling opendir() and is_directory_secure() does. gthornbr@7493: int result; gthornbr@7493: DIR *dirp = NULL; gthornbr@7493: RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result); gthornbr@7493: if (result == OS_ERR) { gthornbr@7493: if (PrintMiscellaneous && Verbose) { gthornbr@7493: if (errno == ELOOP) { gthornbr@7493: warning("directory %s is a symlink and is not secure\n", dirname); gthornbr@7493: } else { gthornbr@7493: warning("could not open directory %s: %s\n", dirname, strerror(errno)); gthornbr@7493: } duke@435: } gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: int fd = result; gthornbr@7493: gthornbr@7493: // Determine if the open directory is secure. gthornbr@7493: if (!is_dirfd_secure(fd)) { gthornbr@7493: // The directory is not a secure directory. gthornbr@7493: os::close(fd); gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Open the directory. gthornbr@7493: dirp = ::opendir(dirname); gthornbr@7493: if (dirp == NULL) { gthornbr@7493: // The directory doesn't exist, close fd and return. gthornbr@7493: os::close(fd); gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Check to make sure fd and dirp are referencing the same file system object. gthornbr@7493: if (!is_same_fsobject(fd, dirfd(dirp))) { gthornbr@7493: // The directory is not secure. gthornbr@7493: os::close(fd); gthornbr@7493: os::closedir(dirp); gthornbr@7493: dirp = NULL; gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Close initial open now that we know directory is secure gthornbr@7493: os::close(fd); gthornbr@7493: gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // NOTE: The code below uses fchdir(), open() and unlink() because gthornbr@7493: // fdopendir(), openat() and unlinkat() are not supported on all gthornbr@7493: // versions. Once the support for fdopendir(), openat() and unlinkat() gthornbr@7493: // is available on all supported versions the code can be changed gthornbr@7493: // to use these functions. gthornbr@7493: gthornbr@7493: // Open the directory of the given path, validate it and set the gthornbr@7493: // current working directory to it. gthornbr@7493: // Return a DIR * of the open directory and the saved cwd fd. gthornbr@7493: // gthornbr@7493: static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) { gthornbr@7493: gthornbr@7493: // Open the directory. gthornbr@7493: DIR* dirp = open_directory_secure(dirname); gthornbr@7493: if (dirp == NULL) { gthornbr@7493: // Directory doesn't exist or is insecure, so there is nothing to cleanup. gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: int fd = dirfd(dirp); gthornbr@7493: gthornbr@7493: // Open a fd to the cwd and save it off. gthornbr@7493: int result; gthornbr@7493: RESTARTABLE(::open(".", O_RDONLY), result); gthornbr@7493: if (result == OS_ERR) { gthornbr@7493: *saved_cwd_fd = -1; gthornbr@7493: } else { gthornbr@7493: *saved_cwd_fd = result; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Set the current directory to dirname by using the fd of the directory. gthornbr@7493: result = fchdir(fd); gthornbr@7493: gthornbr@7493: return dirp; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Close the directory and restore the current working directory. gthornbr@7493: // gthornbr@7493: static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) { gthornbr@7493: gthornbr@7493: int result; gthornbr@7493: // If we have a saved cwd change back to it and close the fd. gthornbr@7493: if (saved_cwd_fd != -1) { gthornbr@7493: result = fchdir(saved_cwd_fd); gthornbr@7493: ::close(saved_cwd_fd); gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Close the directory. gthornbr@7493: os::closedir(dirp); gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Check if the given file descriptor is considered a secure. gthornbr@7493: // gthornbr@7493: static bool is_file_secure(int fd, const char *filename) { gthornbr@7493: gthornbr@7493: int result; gthornbr@7493: struct stat statbuf; gthornbr@7493: gthornbr@7493: // Determine if the file is secure. gthornbr@7493: RESTARTABLE(::fstat(fd, &statbuf), result); gthornbr@7493: if (result == OS_ERR) { gthornbr@7493: if (PrintMiscellaneous && Verbose) { gthornbr@7493: warning("fstat failed on %s: %s\n", filename, strerror(errno)); gthornbr@7493: } gthornbr@7493: return false; gthornbr@7493: } gthornbr@7493: if (statbuf.st_nlink > 1) { gthornbr@7493: // A file with multiple links is not expected. gthornbr@7493: if (PrintMiscellaneous && Verbose) { gthornbr@7493: warning("file %s has multiple links\n", filename); gthornbr@7493: } gthornbr@7493: return false; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: duke@435: // return the user name for the given user id duke@435: // duke@435: // the caller is expected to free the allocated memory. duke@435: // duke@435: static char* get_user_name(uid_t uid) { duke@435: duke@435: struct passwd pwent; duke@435: duke@435: // determine the max pwbuf size from sysconf, and hardcode duke@435: // a default if this not available through sysconf. duke@435: // duke@435: long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); duke@435: if (bufsize == -1) duke@435: bufsize = 1024; duke@435: zgu@3900: char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); duke@435: duke@435: // POSIX interface to getpwuid_r is used on LINUX duke@435: struct passwd* p; duke@435: int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p); duke@435: duke@435: if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: if (result != 0) { duke@435: warning("Could not retrieve passwd entry: %s\n", duke@435: strerror(result)); duke@435: } duke@435: else if (p == NULL) { duke@435: // this check is added to protect against an observed problem duke@435: // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0, duke@435: // indicating success, but has p == NULL. This was observed when duke@435: // inserting a file descriptor exhaustion fault prior to the call duke@435: // getpwuid_r() call. In this case, error is set to the appropriate duke@435: // error condition, but this is undocumented behavior. This check duke@435: // is safe under any condition, but the use of errno in the output duke@435: // message may result in an erroneous message. duke@435: // Bug Id 89052 was opened with RedHat. duke@435: // duke@435: warning("Could not retrieve passwd entry: %s\n", duke@435: strerror(errno)); duke@435: } duke@435: else { duke@435: warning("Could not determine user name: %s\n", duke@435: p->pw_name == NULL ? "pw_name = NULL" : duke@435: "pw_name zero length"); duke@435: } duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); duke@435: return NULL; duke@435: } duke@435: zgu@3900: char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal); duke@435: strcpy(user_name, p->pw_name); duke@435: zgu@3900: FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); duke@435: return user_name; duke@435: } duke@435: duke@435: // return the name of the user that owns the process identified by vmid. duke@435: // duke@435: // This method uses a slow directory search algorithm to find the backing duke@435: // store file for the specified vmid and returns the user name, as determined duke@435: // by the user name suffix of the hsperfdata_ directory name. duke@435: // duke@435: // the caller is expected to free the allocated memory. duke@435: // duke@435: static char* get_user_name_slow(int vmid, TRAPS) { duke@435: duke@435: // short circuit the directory search if the process doesn't even exist. duke@435: if (kill(vmid, 0) == OS_ERR) { duke@435: if (errno == ESRCH) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), duke@435: "Process not found"); duke@435: } duke@435: else /* EPERM */ { duke@435: THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); duke@435: } duke@435: } duke@435: duke@435: // directory search duke@435: char* oldest_user = NULL; duke@435: time_t oldest_ctime = 0; duke@435: duke@435: const char* tmpdirname = os::get_temp_directory(); duke@435: gthornbr@7493: // open the temp directory duke@435: DIR* tmpdirp = os::opendir(tmpdirname); duke@435: duke@435: if (tmpdirp == NULL) { gthornbr@7493: // Cannot open the directory to get the user name, return. duke@435: return NULL; duke@435: } duke@435: duke@435: // for each entry in the directory that matches the pattern hsperfdata_*, duke@435: // open the directory and check if the file for the given vmid exists. duke@435: // The file with the expected name and the latest creation date is used duke@435: // to determine the user name for the process id. duke@435: // duke@435: struct dirent* dentry; zgu@3900: char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal); duke@435: errno = 0; duke@435: while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) { duke@435: duke@435: // check if the directory entry is a hsperfdata file duke@435: if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) { duke@435: continue; duke@435: } duke@435: duke@435: char* usrdir_name = NEW_C_HEAP_ARRAY(char, zgu@3900: strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal); duke@435: strcpy(usrdir_name, tmpdirname); coleenp@1788: strcat(usrdir_name, "/"); duke@435: strcat(usrdir_name, dentry->d_name); duke@435: gthornbr@7493: // open the user directory gthornbr@7493: DIR* subdirp = open_directory_secure(usrdir_name); duke@435: duke@435: if (subdirp == NULL) { zgu@3900: FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); duke@435: continue; duke@435: } duke@435: duke@435: // Since we don't create the backing store files in directories duke@435: // pointed to by symbolic links, we also don't follow them when duke@435: // looking for the files. We check for a symbolic link after the duke@435: // call to opendir in order to eliminate a small window where the duke@435: // symlink can be exploited. duke@435: // duke@435: if (!is_directory_secure(usrdir_name)) { zgu@3900: FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); duke@435: os::closedir(subdirp); duke@435: continue; duke@435: } duke@435: duke@435: struct dirent* udentry; zgu@3900: char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal); duke@435: errno = 0; duke@435: while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) { duke@435: duke@435: if (filename_to_pid(udentry->d_name) == vmid) { duke@435: struct stat statbuf; duke@435: int result; duke@435: duke@435: char* filename = NEW_C_HEAP_ARRAY(char, zgu@3900: strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal); duke@435: duke@435: strcpy(filename, usrdir_name); duke@435: strcat(filename, "/"); duke@435: strcat(filename, udentry->d_name); duke@435: duke@435: // don't follow symbolic links for the file duke@435: RESTARTABLE(::lstat(filename, &statbuf), result); duke@435: if (result == OS_ERR) { zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: continue; duke@435: } duke@435: duke@435: // skip over files that are not regular files. duke@435: if (!S_ISREG(statbuf.st_mode)) { zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: continue; duke@435: } duke@435: duke@435: // compare and save filename with latest creation time duke@435: if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) { duke@435: duke@435: if (statbuf.st_ctime > oldest_ctime) { duke@435: char* user = strchr(dentry->d_name, '_') + 1; duke@435: zgu@3900: if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal); zgu@3900: oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal); duke@435: duke@435: strcpy(oldest_user, user); duke@435: oldest_ctime = statbuf.st_ctime; duke@435: } duke@435: } duke@435: zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: } duke@435: } duke@435: os::closedir(subdirp); zgu@3900: FREE_C_HEAP_ARRAY(char, udbuf, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); duke@435: } duke@435: os::closedir(tmpdirp); zgu@3900: FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal); duke@435: duke@435: return(oldest_user); duke@435: } duke@435: duke@435: // return the name of the user that owns the JVM indicated by the given vmid. duke@435: // duke@435: static char* get_user_name(int vmid, TRAPS) { duke@435: return get_user_name_slow(vmid, CHECK_NULL); duke@435: } duke@435: duke@435: // return the file name of the backing store file for the named duke@435: // shared memory region for the given user name and vmid. duke@435: // duke@435: // the caller is expected to free the allocated memory. duke@435: // duke@435: static char* get_sharedmem_filename(const char* dirname, int vmid) { duke@435: duke@435: // add 2 for the file separator and a null terminator. duke@435: size_t nbytes = strlen(dirname) + UINT_CHARS + 2; duke@435: zgu@3900: char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); duke@435: snprintf(name, nbytes, "%s/%d", dirname, vmid); duke@435: duke@435: return name; duke@435: } duke@435: duke@435: duke@435: // remove file duke@435: // duke@435: // this method removes the file specified by the given path duke@435: // duke@435: static void remove_file(const char* path) { duke@435: duke@435: int result; duke@435: duke@435: // if the file is a directory, the following unlink will fail. since duke@435: // we don't expect to find directories in the user temp directory, we duke@435: // won't try to handle this situation. even if accidentially or duke@435: // maliciously planted, the directory's presence won't hurt anything. duke@435: // duke@435: RESTARTABLE(::unlink(path), result); duke@435: if (PrintMiscellaneous && Verbose && result == OS_ERR) { duke@435: if (errno != ENOENT) { duke@435: warning("Could not unlink shared memory backing" duke@435: " store file %s : %s\n", path, strerror(errno)); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // cleanup stale shared memory resources duke@435: // duke@435: // This method attempts to remove all stale shared memory files in duke@435: // the named user temporary directory. It scans the named directory duke@435: // for files matching the pattern ^$[0-9]*$. For each file found, the duke@435: // process id is extracted from the file name and a test is run to duke@435: // determine if the process is alive. If the process is not alive, duke@435: // any stale file resources are removed. duke@435: // duke@435: static void cleanup_sharedmem_resources(const char* dirname) { duke@435: gthornbr@7493: int saved_cwd_fd; gthornbr@7493: // open the directory gthornbr@7493: DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd); duke@435: if (dirp == NULL) { gthornbr@7493: // directory doesn't exist or is insecure, so there is nothing to cleanup duke@435: return; duke@435: } duke@435: duke@435: // for each entry in the directory that matches the expected file duke@435: // name pattern, determine if the file resources are stale and if duke@435: // so, remove the file resources. Note, instrumented HotSpot processes duke@435: // for this user may start and/or terminate during this search and duke@435: // remove or create new files in this directory. The behavior of this duke@435: // loop under these conditions is dependent upon the implementation of duke@435: // opendir/readdir. duke@435: // duke@435: struct dirent* entry; zgu@3900: char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal); gthornbr@7493: duke@435: errno = 0; duke@435: while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) { duke@435: duke@435: pid_t pid = filename_to_pid(entry->d_name); duke@435: duke@435: if (pid == 0) { duke@435: duke@435: if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { duke@435: // attempt to remove all unexpected files, except "." and ".." gthornbr@7493: unlink(entry->d_name); duke@435: } duke@435: duke@435: errno = 0; duke@435: continue; duke@435: } duke@435: duke@435: // we now have a file name that converts to a valid integer duke@435: // that could represent a process id . if this process id duke@435: // matches the current process id or the process is not running, duke@435: // then remove the stale file resources. duke@435: // duke@435: // process liveness is detected by sending signal number 0 to duke@435: // the process id (see kill(2)). if kill determines that the duke@435: // process does not exist, then the file resources are removed. duke@435: // if kill determines that that we don't have permission to duke@435: // signal the process, then the file resources are assumed to duke@435: // be stale and are removed because the resources for such a duke@435: // process should be in a different user specific directory. duke@435: // duke@435: if ((pid == os::current_process_id()) || duke@435: (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) { gthornbr@7493: unlink(entry->d_name); duke@435: } duke@435: errno = 0; duke@435: } gthornbr@7493: gthornbr@7493: // close the directory and reset the current working directory gthornbr@7493: close_directory_secure_cwd(dirp, saved_cwd_fd); gthornbr@7493: zgu@3900: FREE_C_HEAP_ARRAY(char, dbuf, mtInternal); duke@435: } duke@435: duke@435: // make the user specific temporary directory. Returns true if duke@435: // the directory exists and is secure upon return. Returns false duke@435: // if the directory exists but is either a symlink, is otherwise duke@435: // insecure, or if an error occurred. duke@435: // duke@435: static bool make_user_tmp_dir(const char* dirname) { duke@435: duke@435: // create the directory with 0755 permissions. note that the directory duke@435: // will be owned by euid::egid, which may not be the same as uid::gid. duke@435: // duke@435: if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) { duke@435: if (errno == EEXIST) { duke@435: // The directory already exists and was probably created by another duke@435: // JVM instance. However, this could also be the result of a duke@435: // deliberate symlink. Verify that the existing directory is safe. duke@435: // duke@435: if (!is_directory_secure(dirname)) { duke@435: // directory is not secure duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("%s directory is insecure\n", dirname); duke@435: } duke@435: return false; duke@435: } duke@435: } duke@435: else { duke@435: // we encountered some other failure while attempting duke@435: // to create the directory duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("could not create directory %s: %s\n", duke@435: dirname, strerror(errno)); duke@435: } duke@435: return false; duke@435: } duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: // create the shared memory file resources duke@435: // duke@435: // This method creates the shared memory file with the given size duke@435: // This method also creates the user specific temporary directory, if duke@435: // it does not yet exist. duke@435: // duke@435: static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) { duke@435: duke@435: // make the user temporary directory duke@435: if (!make_user_tmp_dir(dirname)) { duke@435: // could not make/find the directory or the found directory duke@435: // was not secure duke@435: return -1; duke@435: } duke@435: gthornbr@7493: int saved_cwd_fd; gthornbr@7493: // open the directory and set the current working directory to it gthornbr@7493: DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd); gthornbr@7493: if (dirp == NULL) { gthornbr@7493: // Directory doesn't exist or is insecure, so cannot create shared gthornbr@7493: // memory file. gthornbr@7493: return -1; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // Open the filename in the current directory. gthornbr@7493: // Cannot use O_TRUNC here; truncation of an existing file has to happen gthornbr@7493: // after the is_file_secure() check below. duke@435: int result; gthornbr@7493: RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result); duke@435: if (result == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { gthornbr@7493: if (errno == ELOOP) { gthornbr@7493: warning("file %s is a symlink and is not secure\n", filename); gthornbr@7493: } else { gthornbr@7493: warning("could not create file %s: %s\n", filename, strerror(errno)); gthornbr@7493: } duke@435: } gthornbr@7493: // close the directory and reset the current working directory gthornbr@7493: close_directory_secure_cwd(dirp, saved_cwd_fd); gthornbr@7493: duke@435: return -1; duke@435: } gthornbr@7493: // close the directory and reset the current working directory gthornbr@7493: close_directory_secure_cwd(dirp, saved_cwd_fd); duke@435: duke@435: // save the file descriptor duke@435: int fd = result; duke@435: gthornbr@7493: // check to see if the file is secure gthornbr@7493: if (!is_file_secure(fd, filename)) { gthornbr@7493: ::close(fd); gthornbr@7493: return -1; gthornbr@7493: } gthornbr@7493: gthornbr@7493: // truncate the file to get rid of any existing data gthornbr@7493: RESTARTABLE(::ftruncate(fd, (off_t)0), result); gthornbr@7493: if (result == OS_ERR) { gthornbr@7493: if (PrintMiscellaneous && Verbose) { gthornbr@7493: warning("could not truncate shared memory file: %s\n", strerror(errno)); gthornbr@7493: } gthornbr@7493: ::close(fd); gthornbr@7493: return -1; gthornbr@7493: } duke@435: // set the file size duke@435: RESTARTABLE(::ftruncate(fd, (off_t)size), result); duke@435: if (result == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("could not set shared memory file size: %s\n", strerror(errno)); duke@435: } rdurbin@5264: ::close(fd); duke@435: return -1; duke@435: } duke@435: bobv@2389: // Verify that we have enough disk space for this file. bobv@2389: // We'll get random SIGBUS crashes on memory accesses if bobv@2389: // we don't. bobv@2389: bobv@2389: for (size_t seekpos = 0; seekpos < size; seekpos += os::vm_page_size()) { bobv@2389: int zero_int = 0; bobv@2389: result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos)); bobv@2389: if (result == -1 ) break; bobv@2389: RESTARTABLE(::write(fd, &zero_int, 1), result); bobv@2389: if (result != 1) { bobv@2389: if (errno == ENOSPC) { bobv@2389: warning("Insufficient space for shared memory file:\n %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename); bobv@2389: } bobv@2389: break; bobv@2389: } bobv@2389: } bobv@2389: bobv@2389: if (result != -1) { bobv@2389: return fd; bobv@2389: } else { rdurbin@5264: ::close(fd); bobv@2389: return -1; bobv@2389: } duke@435: } duke@435: duke@435: // open the shared memory file for the given user and vmid. returns duke@435: // the file descriptor for the open file or -1 if the file could not duke@435: // be opened. duke@435: // duke@435: static int open_sharedmem_file(const char* filename, int oflags, TRAPS) { duke@435: duke@435: // open the file duke@435: int result; duke@435: RESTARTABLE(::open(filename, oflags), result); duke@435: if (result == OS_ERR) { duke@435: if (errno == ENOENT) { ccheung@4893: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), ccheung@4893: "Process not found", OS_ERR); duke@435: } duke@435: else if (errno == EACCES) { ccheung@4893: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), ccheung@4893: "Permission denied", OS_ERR); duke@435: } duke@435: else { ccheung@4893: THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR); duke@435: } duke@435: } gthornbr@7493: int fd = result; duke@435: gthornbr@7493: // check to see if the file is secure gthornbr@7493: if (!is_file_secure(fd, filename)) { gthornbr@7493: ::close(fd); gthornbr@7493: return -1; gthornbr@7493: } gthornbr@7493: gthornbr@7493: return fd; duke@435: } duke@435: duke@435: // create a named shared memory region. returns the address of the duke@435: // memory region on success or NULL on failure. A return value of duke@435: // NULL will ultimately disable the shared memory feature. duke@435: // duke@435: // On Solaris and Linux, the name space for shared memory objects duke@435: // is the file system name space. duke@435: // duke@435: // A monitoring application attaching to a JVM does not need to know duke@435: // the file system name of the shared memory object. However, it may duke@435: // be convenient for applications to discover the existence of newly duke@435: // created and terminating JVMs by watching the file system name space duke@435: // for files being created or removed. duke@435: // duke@435: static char* mmap_create_shared(size_t size) { duke@435: duke@435: int result; duke@435: int fd; duke@435: char* mapAddress; duke@435: duke@435: int vmid = os::current_process_id(); duke@435: duke@435: char* user_name = get_user_name(geteuid()); duke@435: duke@435: if (user_name == NULL) duke@435: return NULL; duke@435: duke@435: char* dirname = get_user_tmp_dir(user_name); duke@435: char* filename = get_sharedmem_filename(dirname, vmid); gthornbr@7493: // get the short filename gthornbr@7493: char* short_filename = strrchr(filename, '/'); gthornbr@7493: if (short_filename == NULL) { gthornbr@7493: short_filename = filename; gthornbr@7493: } else { gthornbr@7493: short_filename++; gthornbr@7493: } duke@435: duke@435: // cleanup any stale shared memory files duke@435: cleanup_sharedmem_resources(dirname); duke@435: duke@435: assert(((size > 0) && (size % os::vm_page_size() == 0)), duke@435: "unexpected PerfMemory region size"); duke@435: gthornbr@7493: fd = create_sharedmem_resources(dirname, short_filename, size); duke@435: zgu@3900: FREE_C_HEAP_ARRAY(char, user_name, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); duke@435: duke@435: if (fd == -1) { zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: return NULL; duke@435: } duke@435: duke@435: mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); duke@435: rdurbin@5264: result = ::close(fd); duke@435: assert(result != OS_ERR, "could not close file"); duke@435: duke@435: if (mapAddress == MAP_FAILED) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("mmap failed - %s\n", strerror(errno)); duke@435: } duke@435: remove_file(filename); zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: return NULL; duke@435: } duke@435: duke@435: // save the file name for use in delete_shared_memory() duke@435: backing_store_file_name = filename; duke@435: duke@435: // clear the shared memory region duke@435: (void)::memset((void*) mapAddress, 0, size); duke@435: zgu@4193: // it does not go through os api, the operation has to record from here zgu@7074: MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal); zgu@4193: duke@435: return mapAddress; duke@435: } duke@435: duke@435: // release a named shared memory region duke@435: // duke@435: static void unmap_shared(char* addr, size_t bytes) { duke@435: os::release_memory(addr, bytes); duke@435: } duke@435: duke@435: // create the PerfData memory region in shared memory. duke@435: // duke@435: static char* create_shared_memory(size_t size) { duke@435: duke@435: // create the shared memory region. duke@435: return mmap_create_shared(size); duke@435: } duke@435: duke@435: // delete the shared PerfData memory region duke@435: // duke@435: static void delete_shared_memory(char* addr, size_t size) { duke@435: duke@435: // cleanup the persistent shared memory resources. since DestroyJavaVM does duke@435: // not support unloading of the JVM, unmapping of the memory resource is duke@435: // not performed. The memory will be reclaimed by the OS upon termination of duke@435: // the process. The backing store file is deleted from the file system. duke@435: duke@435: assert(!PerfDisableSharedMem, "shouldn't be here"); duke@435: duke@435: if (backing_store_file_name != NULL) { duke@435: remove_file(backing_store_file_name); duke@435: // Don't.. Free heap memory could deadlock os::abort() if it is called duke@435: // from signal handler. OS will reclaim the heap memory. duke@435: // FREE_C_HEAP_ARRAY(char, backing_store_file_name); duke@435: backing_store_file_name = NULL; duke@435: } duke@435: } duke@435: duke@435: // return the size of the file for the given file descriptor duke@435: // or 0 if it is not a valid size for a shared memory file duke@435: // duke@435: static size_t sharedmem_filesize(int fd, TRAPS) { duke@435: duke@435: struct stat statbuf; duke@435: int result; duke@435: duke@435: RESTARTABLE(::fstat(fd, &statbuf), result); duke@435: if (result == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("fstat failed: %s\n", strerror(errno)); duke@435: } duke@435: THROW_MSG_0(vmSymbols::java_io_IOException(), duke@435: "Could not determine PerfMemory size"); duke@435: } duke@435: duke@435: if ((statbuf.st_size == 0) || duke@435: ((size_t)statbuf.st_size % os::vm_page_size() != 0)) { duke@435: THROW_MSG_0(vmSymbols::java_lang_Exception(), duke@435: "Invalid PerfMemory size"); duke@435: } duke@435: duke@435: return (size_t)statbuf.st_size; duke@435: } duke@435: duke@435: // attach to a named shared memory region. duke@435: // duke@435: static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) { duke@435: duke@435: char* mapAddress; duke@435: int result; duke@435: int fd; ccheung@4893: size_t size = 0; duke@435: const char* luser = NULL; duke@435: duke@435: int mmap_prot; duke@435: int file_flags; duke@435: duke@435: ResourceMark rm; duke@435: duke@435: // map the high level access mode to the appropriate permission duke@435: // constructs for the file and the shared memory mapping. duke@435: if (mode == PerfMemory::PERF_MODE_RO) { duke@435: mmap_prot = PROT_READ; gthornbr@7493: file_flags = O_RDONLY | O_NOFOLLOW; duke@435: } duke@435: else if (mode == PerfMemory::PERF_MODE_RW) { duke@435: #ifdef LATER duke@435: mmap_prot = PROT_READ | PROT_WRITE; gthornbr@7493: file_flags = O_RDWR | O_NOFOLLOW; duke@435: #else duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), duke@435: "Unsupported access mode"); duke@435: #endif duke@435: } duke@435: else { duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), duke@435: "Illegal access mode"); duke@435: } duke@435: duke@435: if (user == NULL || strlen(user) == 0) { duke@435: luser = get_user_name(vmid, CHECK); duke@435: } duke@435: else { duke@435: luser = user; duke@435: } duke@435: duke@435: if (luser == NULL) { duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), duke@435: "Could not map vmid to user Name"); duke@435: } duke@435: duke@435: char* dirname = get_user_tmp_dir(luser); duke@435: duke@435: // since we don't follow symbolic links when creating the backing duke@435: // store file, we don't follow them when attaching either. duke@435: // duke@435: if (!is_directory_secure(dirname)) { zgu@3900: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), duke@435: "Process not found"); duke@435: } duke@435: duke@435: char* filename = get_sharedmem_filename(dirname, vmid); duke@435: duke@435: // copy heap memory to resource memory. the open_sharedmem_file duke@435: // method below need to use the filename, but could throw an duke@435: // exception. using a resource array prevents the leak that duke@435: // would otherwise occur. duke@435: char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); duke@435: strcpy(rfilename, filename); duke@435: duke@435: // free the c heap resources that are no longer needed zgu@3900: if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: duke@435: // open the shared memory file for the give vmid dcubed@6414: fd = open_sharedmem_file(rfilename, file_flags, THREAD); dcubed@6414: dcubed@6414: if (fd == OS_ERR) { dcubed@6414: return; dcubed@6414: } dcubed@6414: dcubed@6414: if (HAS_PENDING_EXCEPTION) { dcubed@6414: ::close(fd); dcubed@6414: return; dcubed@6414: } duke@435: duke@435: if (*sizep == 0) { duke@435: size = sharedmem_filesize(fd, CHECK); ccheung@4893: } else { ccheung@4893: size = *sizep; duke@435: } duke@435: ccheung@4893: assert(size > 0, "unexpected size <= 0"); ccheung@4893: duke@435: mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0); duke@435: rdurbin@5264: result = ::close(fd); duke@435: assert(result != OS_ERR, "could not close file"); duke@435: duke@435: if (mapAddress == MAP_FAILED) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("mmap failed: %s\n", strerror(errno)); duke@435: } duke@435: THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), duke@435: "Could not map PerfMemory"); duke@435: } duke@435: zgu@4193: // it does not go through os api, the operation has to record from here zgu@7074: MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal); zgu@4193: duke@435: *addr = mapAddress; duke@435: *sizep = size; duke@435: duke@435: if (PerfTraceMemOps) { duke@435: tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " drchase@6680: INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress)); duke@435: } duke@435: } duke@435: duke@435: duke@435: duke@435: duke@435: // create the PerfData memory region duke@435: // duke@435: // This method creates the memory region used to store performance duke@435: // data for the JVM. The memory may be created in standard or duke@435: // shared memory. duke@435: // duke@435: void PerfMemory::create_memory_region(size_t size) { duke@435: duke@435: if (PerfDisableSharedMem) { duke@435: // do not share the memory for the performance data. duke@435: _start = create_standard_memory(size); duke@435: } duke@435: else { duke@435: _start = create_shared_memory(size); duke@435: if (_start == NULL) { duke@435: duke@435: // creation of the shared memory region failed, attempt duke@435: // to create a contiguous, non-shared memory region instead. duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("Reverting to non-shared PerfMemory region.\n"); duke@435: } duke@435: PerfDisableSharedMem = true; duke@435: _start = create_standard_memory(size); duke@435: } duke@435: } duke@435: duke@435: if (_start != NULL) _capacity = size; duke@435: duke@435: } duke@435: duke@435: // delete the PerfData memory region duke@435: // duke@435: // This method deletes the memory region used to store performance duke@435: // data for the JVM. The memory region indicated by the duke@435: // tuple will be inaccessible after a call to this method. duke@435: // duke@435: void PerfMemory::delete_memory_region() { duke@435: duke@435: assert((start() != NULL && capacity() > 0), "verify proper state"); duke@435: duke@435: // If user specifies PerfDataSaveFile, it will save the performance data duke@435: // to the specified file name no matter whether PerfDataSaveToFile is specified duke@435: // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag duke@435: // -XX:+PerfDataSaveToFile. duke@435: if (PerfDataSaveToFile || PerfDataSaveFile != NULL) { duke@435: save_memory_to_file(start(), capacity()); duke@435: } duke@435: duke@435: if (PerfDisableSharedMem) { duke@435: delete_standard_memory(start(), capacity()); duke@435: } duke@435: else { duke@435: delete_shared_memory(start(), capacity()); duke@435: } duke@435: } duke@435: duke@435: // attach to the PerfData memory region for another JVM duke@435: // duke@435: // This method returns an tuple that points to duke@435: // a memory buffer that is kept reasonably synchronized with duke@435: // the PerfData memory region for the indicated JVM. This duke@435: // buffer may be kept in synchronization via shared memory duke@435: // or some other mechanism that keeps the buffer updated. duke@435: // duke@435: // If the JVM chooses not to support the attachability feature, duke@435: // this method should throw an UnsupportedOperation exception. duke@435: // duke@435: // This implementation utilizes named shared memory to map duke@435: // the indicated process's PerfData memory region into this JVMs duke@435: // address space. duke@435: // duke@435: void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) { duke@435: duke@435: if (vmid == 0 || vmid == os::current_process_id()) { duke@435: *addrp = start(); duke@435: *sizep = capacity(); duke@435: return; duke@435: } duke@435: duke@435: mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK); duke@435: } duke@435: duke@435: // detach from the PerfData memory region of another JVM duke@435: // duke@435: // This method detaches the PerfData memory region of another duke@435: // JVM, specified as an tuple of a buffer duke@435: // in this process's address space. This method may perform duke@435: // arbitrary actions to accomplish the detachment. The memory duke@435: // region specified by will be inaccessible after duke@435: // a call to this method. duke@435: // duke@435: // If the JVM chooses not to support the attachability feature, duke@435: // this method should throw an UnsupportedOperation exception. duke@435: // duke@435: // This implementation utilizes named shared memory to detach duke@435: // the indicated process's PerfData memory region from this duke@435: // process's address space. duke@435: // duke@435: void PerfMemory::detach(char* addr, size_t bytes, TRAPS) { duke@435: duke@435: assert(addr != 0, "address sanity check"); duke@435: assert(bytes > 0, "capacity sanity check"); duke@435: duke@435: if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) { duke@435: // prevent accidental detachment of this process's PerfMemory region duke@435: return; duke@435: } duke@435: duke@435: unmap_shared(addr, bytes); duke@435: } duke@435: duke@435: char* PerfMemory::backing_store_filename() { duke@435: return backing_store_file_name; duke@435: }