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