duke@435: /* mikael@4153: * Copyright (c) 2001, 2012, 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_windows.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: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: duke@435: typedef BOOL (WINAPI *SetSecurityDescriptorControlFnPtr)( duke@435: IN PSECURITY_DESCRIPTOR pSecurityDescriptor, duke@435: IN SECURITY_DESCRIPTOR_CONTROL ControlBitsOfInterest, duke@435: IN SECURITY_DESCRIPTOR_CONTROL ControlBitsToSet); 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 duke@435: if (!os::commit_memory(mapAddress, size)) { 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: duke@435: // save the specified memory region to the given file 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 fd = ::_open(destfile, _O_BINARY|_O_CREAT|_O_WRONLY|_O_TRUNC, duke@435: _S_IREAD|_S_IWRITE); duke@435: duke@435: if (fd == 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: for (size_t remaining = size; remaining > 0;) { duke@435: duke@435: int nbytes = ::_write(fd, addr, (unsigned int)remaining); duke@435: if (nbytes == 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)nbytes; duke@435: addr += nbytes; duke@435: } duke@435: duke@435: int 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: } duke@435: zgu@3900: FREE_C_HEAP_ARRAY(char, destfile, mtInternal); duke@435: } duke@435: duke@435: // Shared Memory Implementation Details duke@435: duke@435: // Note: the win32 shared memory implementation uses two objects to represent duke@435: // the shared memory: a windows kernel based file mapping object and a backing duke@435: // store file. On windows, the name space for shared memory is a kernel duke@435: // based name space that is disjoint from other win32 name spaces. Since Java duke@435: // is unaware of this name space, a parallel file system based name space is duke@435: // maintained, which provides a common file system based shared memory name duke@435: // space across the supported platforms and one that Java apps can deal with duke@435: // through simple file apis. duke@435: // duke@435: // For performance and resource cleanup reasons, it is recommended that the duke@435: // user specific directory and the backing store file be stored in either a duke@435: // RAM based file system or a local disk based file system. Network based duke@435: // file systems are not recommended for performance reasons. In addition, duke@435: // use of SMB network based file systems may result in unsuccesful cleanup duke@435: // of the disk based resource on exit of the VM. The Windows TMP and TEMP duke@435: // environement variables, as used by the GetTempPath() Win32 API (see duke@435: // os::get_temp_directory() in os_win32.cpp), control the location of the duke@435: // user specific directory and the shared memory backing store file. duke@435: duke@435: static HANDLE sharedmem_fileMapHandle = NULL; duke@435: static HANDLE sharedmem_fileHandle = INVALID_HANDLE_VALUE; duke@435: static char* sharedmem_fileName = NULL; 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 int 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: int pid = (int)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: // 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: duke@435: DWORD fa; duke@435: duke@435: fa = GetFileAttributes(path); duke@435: if (fa == 0xFFFFFFFF) { duke@435: DWORD lasterror = GetLastError(); duke@435: if (lasterror == ERROR_FILE_NOT_FOUND) { duke@435: return false; duke@435: } duke@435: else { duke@435: // unexpected error, declare the path insecure duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("could not get attributes for file %s: ", duke@435: " lasterror = %d\n", path, lasterror); duke@435: } duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: if (fa & FILE_ATTRIBUTE_REPARSE_POINT) { duke@435: // we don't accept any redirection for the user specific directory duke@435: // so declare the path insecure. This may be too conservative, duke@435: // as some types of reparse points might be acceptable, but it duke@435: // is probably more secure to avoid these conditions. duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("%s is a reparse point\n", path); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: if (fa & FILE_ATTRIBUTE_DIRECTORY) { duke@435: // this is the expected case. Since windows supports symbolic duke@435: // links to directories only, not to files, there is no need duke@435: // to check for open write permissions on the directory. If the duke@435: // directory has open write permissions, any files deposited that duke@435: // are not expected will be removed by the cleanup code. duke@435: // duke@435: return true; duke@435: } duke@435: else { duke@435: // this is either a regular file or some other type of file, duke@435: // any of which are unexpected and therefore insecure. duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("%s is not a directory, file attributes = " duke@435: INTPTR_FORMAT "\n", path, fa); duke@435: } duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: // return the user name for the owner of this process duke@435: // duke@435: // the caller is expected to free the allocated memory. duke@435: // duke@435: static char* get_user_name() { duke@435: duke@435: /* get the user name. This code is adapted from code found in duke@435: * the jdk in src/windows/native/java/lang/java_props_md.c duke@435: * java_props_md.c 1.29 02/02/06. According to the original duke@435: * source, the call to GetUserName is avoided because of a resulting duke@435: * increase in footprint of 100K. duke@435: */ duke@435: char* user = getenv("USERNAME"); duke@435: char buf[UNLEN+1]; duke@435: DWORD buflen = sizeof(buf); duke@435: if (user == NULL || strlen(user) == 0) { duke@435: if (GetUserName(buf, &buflen)) { duke@435: user = buf; duke@435: } duke@435: else { duke@435: return NULL; duke@435: } duke@435: } duke@435: zgu@3900: char* user_name = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal); duke@435: strcpy(user_name, user); duke@435: 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) { duke@435: duke@435: // directory search dcubed@2543: char* latest_user = NULL; dcubed@2543: time_t latest_ctime = 0; duke@435: duke@435: const char* tmpdirname = os::get_temp_directory(); duke@435: duke@435: DIR* tmpdirp = os::opendir(tmpdirname); duke@435: duke@435: if (tmpdirp == NULL) { 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: duke@435: DIR* subdirp = os::opendir(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: 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: if (::stat(filename, &statbuf) == 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 ((statbuf.st_mode & S_IFMT) != S_IFREG) { zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); duke@435: continue; duke@435: } duke@435: dcubed@2543: // If we found a matching file with a newer creation time, then dcubed@2543: // save the user name. The newer creation time indicates that dcubed@2543: // we found a newer incarnation of the process associated with dcubed@2543: // vmid. Due to the way that Windows recycles pids and the fact dcubed@2543: // that we can't delete the file from the file system namespace dcubed@2543: // until last close, it is possible for there to be more than dcubed@2543: // one hsperfdata file with a name matching vmid (diff users). dcubed@2543: // dcubed@2543: // We no longer ignore hsperfdata files where (st_size == 0). dcubed@2543: // In this function, all we're trying to do is determine the dcubed@2543: // name of the user that owns the process associated with vmid dcubed@2543: // so the size doesn't matter. Very rarely, we have observed dcubed@2543: // hsperfdata files where (st_size == 0) and the st_size field dcubed@2543: // later becomes the expected value. dcubed@2543: // dcubed@2543: if (statbuf.st_ctime > latest_ctime) { dcubed@2543: char* user = strchr(dentry->d_name, '_') + 1; duke@435: zgu@3900: if (latest_user != NULL) FREE_C_HEAP_ARRAY(char, latest_user, mtInternal); zgu@3900: latest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal); duke@435: dcubed@2543: strcpy(latest_user, user); dcubed@2543: latest_ctime = statbuf.st_ctime; 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: dcubed@2543: return(latest_user); duke@435: } duke@435: duke@435: // return the name of the user that owns the process identified by vmid. duke@435: // duke@435: // note: this method should only be used via the Perf native methods. duke@435: // There are various costs to this method and limiting its use to the duke@435: // Perf native methods limits the impact to monitoring applications only. duke@435: // duke@435: static char* get_user_name(int vmid) { duke@435: duke@435: // A fast implementation is not provided at this time. It's possible duke@435: // to provide a fast process id to user name mapping function using duke@435: // the win32 apis, but the default ACL for the process object only duke@435: // allows processes with the same owner SID to acquire the process duke@435: // handle (via OpenProcess(PROCESS_QUERY_INFORMATION)). It's possible duke@435: // to have the JVM change the ACL for the process object to allow arbitrary duke@435: // users to access the process handle and the process security token. duke@435: // The security ramifications need to be studied before providing this duke@435: // mechanism. duke@435: // duke@435: return get_user_name_slow(vmid); duke@435: } duke@435: duke@435: // return the name of the shared memory file mapping object for the duke@435: // named shared memory region for the given user name and vmid. duke@435: // duke@435: // The file mapping object's name is not the file name. It is a name duke@435: // in a separate name space. duke@435: // duke@435: // the caller is expected to free the allocated memory. duke@435: // duke@435: static char *get_sharedmem_objectname(const char* user, int vmid) { duke@435: duke@435: // construct file mapping object's name, add 3 for two '_' and a duke@435: // null terminator. duke@435: int nbytes = (int)strlen(PERFDATA_NAME) + (int)strlen(user) + 3; duke@435: duke@435: // the id is converted to an unsigned value here because win32 allows duke@435: // negative process ids. However, OpenFileMapping API complains duke@435: // about a name containing a '-' characters. duke@435: // duke@435: nbytes += UINT_CHARS; zgu@3900: char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); duke@435: _snprintf(name, nbytes, "%s_%s_%u", PERFDATA_NAME, user, vmid); duke@435: duke@435: return name; 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: // remove file duke@435: // duke@435: // this method removes the file with the given file name. duke@435: // duke@435: // Note: if the indicated file is on an SMB network file system, this duke@435: // method may be unsuccessful in removing the file. duke@435: // duke@435: static void remove_file(const char* dirname, const char* filename) { duke@435: duke@435: size_t nbytes = strlen(dirname) + strlen(filename) + 2; zgu@3900: char* path = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); duke@435: duke@435: strcpy(path, dirname); duke@435: strcat(path, "\\"); duke@435: strcat(path, filename); duke@435: duke@435: if (::unlink(path) == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { 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: zgu@3900: FREE_C_HEAP_ARRAY(char, path, mtInternal); duke@435: } duke@435: duke@435: // returns true if the process represented by pid is alive, otherwise duke@435: // returns false. the validity of the result is only accurate if the duke@435: // target process is owned by the same principal that owns this process. duke@435: // this method should not be used if to test the status of an otherwise duke@435: // arbitrary process unless it is know that this process has the appropriate duke@435: // privileges to guarantee a result valid. duke@435: // duke@435: static bool is_alive(int pid) { duke@435: duke@435: HANDLE ph = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); duke@435: if (ph == NULL) { duke@435: // the process does not exist. duke@435: if (PrintMiscellaneous && Verbose) { duke@435: DWORD lastError = GetLastError(); duke@435: if (lastError != ERROR_INVALID_PARAMETER) { duke@435: warning("OpenProcess failed: %d\n", GetLastError()); duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: DWORD exit_status; duke@435: if (!GetExitCodeProcess(ph, &exit_status)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("GetExitCodeProcess failed: %d\n", GetLastError()); duke@435: } duke@435: CloseHandle(ph); duke@435: return false; duke@435: } duke@435: duke@435: CloseHandle(ph); duke@435: return (exit_status == STILL_ACTIVE) ? true : false; duke@435: } duke@435: duke@435: // check if the file system is considered secure for the backing store files duke@435: // duke@435: static bool is_filesystem_secure(const char* path) { duke@435: duke@435: char root_path[MAX_PATH]; duke@435: char fs_type[MAX_PATH]; duke@435: duke@435: if (PerfBypassFileSystemCheck) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("bypassing file system criteria checks for %s\n", path); duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: char* first_colon = strchr((char *)path, ':'); duke@435: if (first_colon == NULL) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("expected device specifier in path: %s\n", path); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: size_t len = (size_t)(first_colon - path); duke@435: assert(len + 2 <= MAX_PATH, "unexpected device specifier length"); duke@435: strncpy(root_path, path, len + 1); duke@435: root_path[len + 1] = '\\'; duke@435: root_path[len + 2] = '\0'; duke@435: duke@435: // check that we have something like "C:\" or "AA:\" duke@435: assert(strlen(root_path) >= 3, "device specifier too short"); duke@435: assert(strchr(root_path, ':') != NULL, "bad device specifier format"); duke@435: assert(strchr(root_path, '\\') != NULL, "bad device specifier format"); duke@435: duke@435: DWORD maxpath; duke@435: DWORD flags; duke@435: duke@435: if (!GetVolumeInformation(root_path, NULL, 0, NULL, &maxpath, duke@435: &flags, fs_type, MAX_PATH)) { duke@435: // we can't get information about the volume, so assume unsafe. duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("could not get device information for %s: " duke@435: " path = %s: lasterror = %d\n", duke@435: root_path, path, GetLastError()); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: if ((flags & FS_PERSISTENT_ACLS) == 0) { duke@435: // file system doesn't support ACLs, declare file system unsafe duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("file system type %s on device %s does not support" duke@435: " ACLs\n", fs_type, root_path); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: if ((flags & FS_VOL_IS_COMPRESSED) != 0) { duke@435: // file system is compressed, declare file system unsafe duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("file system type %s on device %s is compressed\n", duke@435: fs_type, root_path); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: return true; 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: duke@435: // open the user temp directory duke@435: DIR* dirp = os::opendir(dirname); duke@435: duke@435: if (dirp == NULL) { duke@435: // directory doesn't exist, so there is nothing to cleanup duke@435: return; duke@435: } duke@435: duke@435: if (!is_directory_secure(dirname)) { duke@435: // the directory is not secure, don't attempt any 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); duke@435: errno = 0; duke@435: while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) { duke@435: duke@435: int 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: duke@435: // attempt to remove all unexpected files, except "." and ".." duke@435: remove_file(dirname, 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 checking the exit status duke@435: // of the process. if the process id is valid and the exit status duke@435: // indicates that it is still running, the file file resources duke@435: // are not removed. If the process id is invalid, or if we don't duke@435: // have permissions to check the process status, or if the process duke@435: // id is valid and the process has terminated, the the file resources duke@435: // are assumed to be stale and are removed. duke@435: // duke@435: if (pid == os::current_process_id() || !is_alive(pid)) { duke@435: duke@435: // we can only remove the file resources. Any mapped views duke@435: // of the file can only be unmapped by the processes that duke@435: // opened those views and the file mapping object will not duke@435: // get removed until all views are unmapped. duke@435: // duke@435: remove_file(dirname, entry->d_name); duke@435: } duke@435: errno = 0; duke@435: } duke@435: os::closedir(dirp); zgu@3900: FREE_C_HEAP_ARRAY(char, dbuf, mtInternal); duke@435: } duke@435: duke@435: // create a file mapping object with the requested name, and size duke@435: // from the file represented by the given Handle object duke@435: // duke@435: static HANDLE create_file_mapping(const char* name, HANDLE fh, LPSECURITY_ATTRIBUTES fsa, size_t size) { duke@435: duke@435: DWORD lowSize = (DWORD)size; duke@435: DWORD highSize = 0; duke@435: HANDLE fmh = NULL; duke@435: duke@435: // Create a file mapping object with the given name. This function duke@435: // will grow the file to the specified size. duke@435: // duke@435: fmh = CreateFileMapping( duke@435: fh, /* HANDLE file handle for backing store */ duke@435: fsa, /* LPSECURITY_ATTRIBUTES Not inheritable */ duke@435: PAGE_READWRITE, /* DWORD protections */ duke@435: highSize, /* DWORD High word of max size */ duke@435: lowSize, /* DWORD Low word of max size */ duke@435: name); /* LPCTSTR name for object */ duke@435: duke@435: if (fmh == NULL) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("CreateFileMapping failed, lasterror = %d\n", GetLastError()); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: if (GetLastError() == ERROR_ALREADY_EXISTS) { duke@435: duke@435: // a stale file mapping object was encountered. This object may be duke@435: // owned by this or some other user and cannot be removed until duke@435: // the other processes either exit or close their mapping objects duke@435: // and/or mapped views of this mapping object. duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("file mapping already exists, lasterror = %d\n", GetLastError()); duke@435: } duke@435: duke@435: CloseHandle(fmh); duke@435: return NULL; duke@435: } duke@435: duke@435: return fmh; duke@435: } duke@435: duke@435: duke@435: // method to free the given security descriptor and the contained duke@435: // access control list. duke@435: // duke@435: static void free_security_desc(PSECURITY_DESCRIPTOR pSD) { duke@435: duke@435: BOOL success, exists, isdefault; duke@435: PACL pACL; duke@435: duke@435: if (pSD != NULL) { duke@435: duke@435: // get the access control list from the security descriptor duke@435: success = GetSecurityDescriptorDacl(pSD, &exists, &pACL, &isdefault); duke@435: duke@435: // if an ACL existed and it was not a default acl, then it must duke@435: // be an ACL we enlisted. free the resources. duke@435: // duke@435: if (success && exists && pACL != NULL && !isdefault) { zgu@3900: FREE_C_HEAP_ARRAY(char, pACL, mtInternal); duke@435: } duke@435: duke@435: // free the security descriptor zgu@3900: FREE_C_HEAP_ARRAY(char, pSD, mtInternal); duke@435: } duke@435: } duke@435: duke@435: // method to free up a security attributes structure and any duke@435: // contained security descriptors and ACL duke@435: // duke@435: static void free_security_attr(LPSECURITY_ATTRIBUTES lpSA) { duke@435: duke@435: if (lpSA != NULL) { duke@435: // free the contained security descriptor and the ACL duke@435: free_security_desc(lpSA->lpSecurityDescriptor); duke@435: lpSA->lpSecurityDescriptor = NULL; duke@435: duke@435: // free the security attributes structure zgu@3900: FREE_C_HEAP_ARRAY(char, lpSA, mtInternal); duke@435: } duke@435: } duke@435: duke@435: // get the user SID for the process indicated by the process handle duke@435: // duke@435: static PSID get_user_sid(HANDLE hProcess) { duke@435: duke@435: HANDLE hAccessToken; duke@435: PTOKEN_USER token_buf = NULL; duke@435: DWORD rsize = 0; duke@435: duke@435: if (hProcess == NULL) { duke@435: return NULL; duke@435: } duke@435: duke@435: // get the process token duke@435: if (!OpenProcessToken(hProcess, TOKEN_READ, &hAccessToken)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("OpenProcessToken failure: lasterror = %d \n", GetLastError()); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // determine the size of the token structured needed to retrieve duke@435: // the user token information from the access token. duke@435: // duke@435: if (!GetTokenInformation(hAccessToken, TokenUser, NULL, rsize, &rsize)) { duke@435: DWORD lasterror = GetLastError(); duke@435: if (lasterror != ERROR_INSUFFICIENT_BUFFER) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("GetTokenInformation failure: lasterror = %d," duke@435: " rsize = %d\n", lasterror, rsize); duke@435: } duke@435: CloseHandle(hAccessToken); duke@435: return NULL; duke@435: } duke@435: } duke@435: zgu@3900: token_buf = (PTOKEN_USER) NEW_C_HEAP_ARRAY(char, rsize, mtInternal); duke@435: duke@435: // get the user token information duke@435: if (!GetTokenInformation(hAccessToken, TokenUser, token_buf, rsize, &rsize)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("GetTokenInformation failure: lasterror = %d," duke@435: " rsize = %d\n", GetLastError(), rsize); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, token_buf, mtInternal); duke@435: CloseHandle(hAccessToken); duke@435: return NULL; duke@435: } duke@435: duke@435: DWORD nbytes = GetLengthSid(token_buf->User.Sid); zgu@3900: PSID pSID = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); duke@435: duke@435: if (!CopySid(nbytes, pSID, token_buf->User.Sid)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("GetTokenInformation failure: lasterror = %d," duke@435: " rsize = %d\n", GetLastError(), rsize); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, token_buf, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, pSID, mtInternal); duke@435: CloseHandle(hAccessToken); duke@435: return NULL; duke@435: } duke@435: duke@435: // close the access token. duke@435: CloseHandle(hAccessToken); zgu@3900: FREE_C_HEAP_ARRAY(char, token_buf, mtInternal); duke@435: duke@435: return pSID; duke@435: } duke@435: duke@435: // structure used to consolidate access control entry information duke@435: // duke@435: typedef struct ace_data { duke@435: PSID pSid; // SID of the ACE duke@435: DWORD mask; // mask for the ACE duke@435: } ace_data_t; duke@435: duke@435: duke@435: // method to add an allow access control entry with the access rights duke@435: // indicated in mask for the principal indicated in SID to the given duke@435: // security descriptor. Much of the DACL handling was adapted from duke@435: // the example provided here: duke@435: // http://support.microsoft.com/kb/102102/EN-US/ duke@435: // duke@435: duke@435: static bool add_allow_aces(PSECURITY_DESCRIPTOR pSD, duke@435: ace_data_t aces[], int ace_count) { duke@435: PACL newACL = NULL; duke@435: PACL oldACL = NULL; duke@435: duke@435: if (pSD == NULL) { duke@435: return false; duke@435: } duke@435: duke@435: BOOL exists, isdefault; duke@435: duke@435: // retrieve any existing access control list. duke@435: if (!GetSecurityDescriptorDacl(pSD, &exists, &oldACL, &isdefault)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("GetSecurityDescriptor failure: lasterror = %d \n", duke@435: GetLastError()); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: // get the size of the DACL duke@435: ACL_SIZE_INFORMATION aclinfo; duke@435: duke@435: // GetSecurityDescriptorDacl may return true value for exists (lpbDaclPresent) duke@435: // while oldACL is NULL for some case. duke@435: if (oldACL == NULL) { duke@435: exists = FALSE; duke@435: } duke@435: duke@435: if (exists) { duke@435: if (!GetAclInformation(oldACL, &aclinfo, duke@435: sizeof(ACL_SIZE_INFORMATION), duke@435: AclSizeInformation)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("GetAclInformation failure: lasterror = %d \n", GetLastError()); duke@435: return false; duke@435: } duke@435: } duke@435: } else { duke@435: aclinfo.AceCount = 0; // assume NULL DACL duke@435: aclinfo.AclBytesFree = 0; duke@435: aclinfo.AclBytesInUse = sizeof(ACL); duke@435: } duke@435: duke@435: // compute the size needed for the new ACL duke@435: // initial size of ACL is sum of the following: duke@435: // * size of ACL structure. duke@435: // * size of each ACE structure that ACL is to contain minus the sid duke@435: // sidStart member (DWORD) of the ACE. duke@435: // * length of the SID that each ACE is to contain. duke@435: DWORD newACLsize = aclinfo.AclBytesInUse + duke@435: (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) * ace_count; duke@435: for (int i = 0; i < ace_count; i++) { poonam@2310: assert(aces[i].pSid != 0, "pSid should not be 0"); duke@435: newACLsize += GetLengthSid(aces[i].pSid); duke@435: } duke@435: duke@435: // create the new ACL zgu@3900: newACL = (PACL) NEW_C_HEAP_ARRAY(char, newACLsize, mtInternal); duke@435: duke@435: if (!InitializeAcl(newACL, newACLsize, ACL_REVISION)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("InitializeAcl failure: lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: duke@435: unsigned int ace_index = 0; duke@435: // copy any existing ACEs from the old ACL (if any) to the new ACL. duke@435: if (aclinfo.AceCount != 0) { duke@435: while (ace_index < aclinfo.AceCount) { duke@435: LPVOID ace; duke@435: if (!GetAce(oldACL, ace_index, &ace)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("InitializeAcl failure: lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: if (((ACCESS_ALLOWED_ACE *)ace)->Header.AceFlags && INHERITED_ACE) { duke@435: // this is an inherited, allowed ACE; break from loop so we can duke@435: // add the new access allowed, non-inherited ACE in the correct duke@435: // position, immediately following all non-inherited ACEs. duke@435: break; duke@435: } duke@435: duke@435: // determine if the SID of this ACE matches any of the SIDs duke@435: // for which we plan to set ACEs. duke@435: int matches = 0; duke@435: for (int i = 0; i < ace_count; i++) { duke@435: if (EqualSid(aces[i].pSid, &(((ACCESS_ALLOWED_ACE *)ace)->SidStart))) { duke@435: matches++; duke@435: break; duke@435: } duke@435: } duke@435: duke@435: // if there are no SID matches, then add this existing ACE to the new ACL duke@435: if (matches == 0) { duke@435: if (!AddAce(newACL, ACL_REVISION, MAXDWORD, ace, duke@435: ((PACE_HEADER)ace)->AceSize)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("AddAce failure: lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: } duke@435: ace_index++; duke@435: } duke@435: } duke@435: duke@435: // add the passed-in access control entries to the new ACL duke@435: for (int i = 0; i < ace_count; i++) { duke@435: if (!AddAccessAllowedAce(newACL, ACL_REVISION, duke@435: aces[i].mask, aces[i].pSid)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("AddAccessAllowedAce failure: lasterror = %d \n", duke@435: GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: // now copy the rest of the inherited ACEs from the old ACL duke@435: if (aclinfo.AceCount != 0) { duke@435: // picking up at ace_index, where we left off in the duke@435: // previous ace_index loop duke@435: while (ace_index < aclinfo.AceCount) { duke@435: LPVOID ace; duke@435: if (!GetAce(oldACL, ace_index, &ace)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("InitializeAcl failure: lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: if (!AddAce(newACL, ACL_REVISION, MAXDWORD, ace, duke@435: ((PACE_HEADER)ace)->AceSize)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("AddAce failure: lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: ace_index++; duke@435: } duke@435: } duke@435: duke@435: // add the new ACL to the security descriptor. duke@435: if (!SetSecurityDescriptorDacl(pSD, TRUE, newACL, FALSE)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("SetSecurityDescriptorDacl failure:" duke@435: " lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: twisti@1040: // if running on windows 2000 or later, set the automatic inheritance duke@435: // control flags. duke@435: SetSecurityDescriptorControlFnPtr _SetSecurityDescriptorControl; duke@435: _SetSecurityDescriptorControl = (SetSecurityDescriptorControlFnPtr) duke@435: GetProcAddress(GetModuleHandle(TEXT("advapi32.dll")), duke@435: "SetSecurityDescriptorControl"); duke@435: duke@435: if (_SetSecurityDescriptorControl != NULL) { twisti@1040: // We do not want to further propagate inherited DACLs, so making them duke@435: // protected prevents that. duke@435: if (!_SetSecurityDescriptorControl(pSD, SE_DACL_PROTECTED, duke@435: SE_DACL_PROTECTED)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("SetSecurityDescriptorControl failure:" duke@435: " lasterror = %d \n", GetLastError()); duke@435: } zgu@3900: FREE_C_HEAP_ARRAY(char, newACL, mtInternal); duke@435: return false; duke@435: } duke@435: } duke@435: // Note, the security descriptor maintains a reference to the newACL, not duke@435: // a copy of it. Therefore, the newACL is not freed here. It is freed when duke@435: // the security descriptor containing its reference is freed. duke@435: // duke@435: return true; duke@435: } duke@435: duke@435: // method to create a security attributes structure, which contains a duke@435: // security descriptor and an access control list comprised of 0 or more duke@435: // access control entries. The method take an array of ace_data structures duke@435: // that indicate the ACE to be added to the security descriptor. duke@435: // duke@435: // the caller must free the resources associated with the security duke@435: // attributes structure created by this method by calling the duke@435: // free_security_attr() method. duke@435: // duke@435: static LPSECURITY_ATTRIBUTES make_security_attr(ace_data_t aces[], int count) { duke@435: duke@435: // allocate space for a security descriptor duke@435: PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR) zgu@3900: NEW_C_HEAP_ARRAY(char, SECURITY_DESCRIPTOR_MIN_LENGTH, mtInternal); duke@435: duke@435: // initialize the security descriptor duke@435: if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("InitializeSecurityDescriptor failure: " duke@435: "lasterror = %d \n", GetLastError()); duke@435: } duke@435: free_security_desc(pSD); duke@435: return NULL; duke@435: } duke@435: duke@435: // add the access control entries duke@435: if (!add_allow_aces(pSD, aces, count)) { duke@435: free_security_desc(pSD); duke@435: return NULL; duke@435: } duke@435: duke@435: // allocate and initialize the security attributes structure and duke@435: // return it to the caller. duke@435: // duke@435: LPSECURITY_ATTRIBUTES lpSA = (LPSECURITY_ATTRIBUTES) zgu@3900: NEW_C_HEAP_ARRAY(char, sizeof(SECURITY_ATTRIBUTES), mtInternal); duke@435: lpSA->nLength = sizeof(SECURITY_ATTRIBUTES); duke@435: lpSA->lpSecurityDescriptor = pSD; duke@435: lpSA->bInheritHandle = FALSE; duke@435: duke@435: return(lpSA); duke@435: } duke@435: duke@435: // method to create a security attributes structure with a restrictive duke@435: // access control list that creates a set access rights for the user/owner duke@435: // of the securable object and a separate set access rights for everyone else. duke@435: // also provides for full access rights for the administrator group. duke@435: // duke@435: // the caller must free the resources associated with the security duke@435: // attributes structure created by this method by calling the duke@435: // free_security_attr() method. duke@435: // duke@435: duke@435: static LPSECURITY_ATTRIBUTES make_user_everybody_admin_security_attr( duke@435: DWORD umask, DWORD emask, DWORD amask) { duke@435: duke@435: ace_data_t aces[3]; duke@435: duke@435: // initialize the user ace data duke@435: aces[0].pSid = get_user_sid(GetCurrentProcess()); duke@435: aces[0].mask = umask; duke@435: poonam@2310: if (aces[0].pSid == 0) poonam@2310: return NULL; poonam@2310: duke@435: // get the well known SID for BUILTIN\Administrators duke@435: PSID administratorsSid = NULL; duke@435: SID_IDENTIFIER_AUTHORITY SIDAuthAdministrators = SECURITY_NT_AUTHORITY; duke@435: duke@435: if (!AllocateAndInitializeSid( &SIDAuthAdministrators, 2, duke@435: SECURITY_BUILTIN_DOMAIN_RID, duke@435: DOMAIN_ALIAS_RID_ADMINS, duke@435: 0, 0, 0, 0, 0, 0, &administratorsSid)) { duke@435: duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("AllocateAndInitializeSid failure: " duke@435: "lasterror = %d \n", GetLastError()); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // initialize the ace data for administrator group duke@435: aces[1].pSid = administratorsSid; duke@435: aces[1].mask = amask; duke@435: duke@435: // get the well known SID for the universal Everybody duke@435: PSID everybodySid = NULL; duke@435: SID_IDENTIFIER_AUTHORITY SIDAuthEverybody = SECURITY_WORLD_SID_AUTHORITY; duke@435: duke@435: if (!AllocateAndInitializeSid( &SIDAuthEverybody, 1, SECURITY_WORLD_RID, duke@435: 0, 0, 0, 0, 0, 0, 0, &everybodySid)) { duke@435: duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("AllocateAndInitializeSid failure: " duke@435: "lasterror = %d \n", GetLastError()); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // initialize the ace data for everybody else. duke@435: aces[2].pSid = everybodySid; duke@435: aces[2].mask = emask; duke@435: duke@435: // create a security attributes structure with access control duke@435: // entries as initialized above. duke@435: LPSECURITY_ATTRIBUTES lpSA = make_security_attr(aces, 3); zgu@3900: FREE_C_HEAP_ARRAY(char, aces[0].pSid, mtInternal); duke@435: FreeSid(everybodySid); duke@435: FreeSid(administratorsSid); duke@435: return(lpSA); duke@435: } duke@435: duke@435: duke@435: // method to create the security attributes structure for restricting duke@435: // access to the user temporary directory. duke@435: // duke@435: // the caller must free the resources associated with the security duke@435: // attributes structure created by this method by calling the duke@435: // free_security_attr() method. duke@435: // duke@435: static LPSECURITY_ATTRIBUTES make_tmpdir_security_attr() { duke@435: duke@435: // create full access rights for the user/owner of the directory duke@435: // and read-only access rights for everybody else. This is duke@435: // effectively equivalent to UNIX 755 permissions on a directory. duke@435: // duke@435: DWORD umask = STANDARD_RIGHTS_REQUIRED | FILE_ALL_ACCESS; duke@435: DWORD emask = GENERIC_READ | FILE_LIST_DIRECTORY | FILE_TRAVERSE; duke@435: DWORD amask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS; duke@435: duke@435: return make_user_everybody_admin_security_attr(umask, emask, amask); duke@435: } duke@435: duke@435: // method to create the security attributes structure for restricting duke@435: // access to the shared memory backing store file. duke@435: // duke@435: // the caller must free the resources associated with the security duke@435: // attributes structure created by this method by calling the duke@435: // free_security_attr() method. duke@435: // duke@435: static LPSECURITY_ATTRIBUTES make_file_security_attr() { duke@435: duke@435: // create extensive access rights for the user/owner of the file duke@435: // and attribute read-only access rights for everybody else. This duke@435: // is effectively equivalent to UNIX 600 permissions on a file. duke@435: // duke@435: DWORD umask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS; duke@435: DWORD emask = STANDARD_RIGHTS_READ | FILE_READ_ATTRIBUTES | duke@435: FILE_READ_EA | FILE_LIST_DIRECTORY | FILE_TRAVERSE; duke@435: DWORD amask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS; duke@435: duke@435: return make_user_everybody_admin_security_attr(umask, emask, amask); duke@435: } duke@435: duke@435: // method to create the security attributes structure for restricting duke@435: // access to the name shared memory file mapping object. duke@435: // duke@435: // the caller must free the resources associated with the security duke@435: // attributes structure created by this method by calling the duke@435: // free_security_attr() method. duke@435: // duke@435: static LPSECURITY_ATTRIBUTES make_smo_security_attr() { duke@435: duke@435: // create extensive access rights for the user/owner of the shared duke@435: // memory object and attribute read-only access rights for everybody duke@435: // else. This is effectively equivalent to UNIX 600 permissions on duke@435: // on the shared memory object. duke@435: // duke@435: DWORD umask = STANDARD_RIGHTS_REQUIRED | FILE_MAP_ALL_ACCESS; duke@435: DWORD emask = STANDARD_RIGHTS_READ; // attributes only duke@435: DWORD amask = STANDARD_RIGHTS_ALL | FILE_MAP_ALL_ACCESS; duke@435: duke@435: return make_user_everybody_admin_security_attr(umask, emask, amask); duke@435: } duke@435: duke@435: // make the user specific temporary directory duke@435: // duke@435: static bool make_user_tmp_dir(const char* dirname) { duke@435: duke@435: duke@435: LPSECURITY_ATTRIBUTES pDirSA = make_tmpdir_security_attr(); duke@435: if (pDirSA == NULL) { duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // create the directory with the given security attributes duke@435: if (!CreateDirectory(dirname, pDirSA)) { duke@435: DWORD lasterror = GetLastError(); duke@435: if (lasterror == ERROR_ALREADY_EXISTS) { 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: // The administrator should be able to delete this directory. duke@435: // But the directory created by previous version of JVM may not duke@435: // have permission for administrators to delete this directory. duke@435: // So add full permission to the administrator. Also setting new duke@435: // DACLs might fix the corrupted the DACLs. duke@435: SECURITY_INFORMATION secInfo = DACL_SECURITY_INFORMATION; duke@435: if (!SetFileSecurity(dirname, secInfo, pDirSA->lpSecurityDescriptor)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: lasterror = GetLastError(); duke@435: warning("SetFileSecurity failed for %s directory. lasterror %d \n", duke@435: dirname, lasterror); duke@435: } duke@435: } duke@435: } duke@435: else { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("CreateDirectory failed: %d\n", GetLastError()); duke@435: } duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: // free the security attributes structure duke@435: free_security_attr(pDirSA); duke@435: duke@435: return true; duke@435: } duke@435: duke@435: // create the shared memory resources duke@435: // duke@435: // This function creates the shared memory resources. This includes duke@435: // the backing store file and the file mapping shared memory object. duke@435: // duke@435: static HANDLE create_sharedmem_resources(const char* dirname, const char* filename, const char* objectname, size_t size) { duke@435: duke@435: HANDLE fh = INVALID_HANDLE_VALUE; duke@435: HANDLE fmh = NULL; duke@435: duke@435: duke@435: // create the security attributes for the backing store file duke@435: LPSECURITY_ATTRIBUTES lpFileSA = make_file_security_attr(); duke@435: if (lpFileSA == NULL) { duke@435: return NULL; duke@435: } duke@435: duke@435: // create the security attributes for the shared memory object duke@435: LPSECURITY_ATTRIBUTES lpSmoSA = make_smo_security_attr(); duke@435: if (lpSmoSA == NULL) { duke@435: free_security_attr(lpFileSA); duke@435: return NULL; duke@435: } duke@435: duke@435: // create 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 NULL; duke@435: } duke@435: duke@435: // Create the file - the FILE_FLAG_DELETE_ON_CLOSE flag allows the duke@435: // file to be deleted by the last process that closes its handle to duke@435: // the file. This is important as the apis do not allow a terminating duke@435: // JVM being monitored by another process to remove the file name. duke@435: // duke@435: // the FILE_SHARE_DELETE share mode is valid only in winnt duke@435: // duke@435: fh = CreateFile( duke@435: filename, /* LPCTSTR file name */ duke@435: duke@435: GENERIC_READ|GENERIC_WRITE, /* DWORD desired access */ duke@435: duke@435: (os::win32::is_nt() ? FILE_SHARE_DELETE : 0)| duke@435: FILE_SHARE_READ, /* DWORD share mode, future READONLY duke@435: * open operations allowed duke@435: */ duke@435: lpFileSA, /* LPSECURITY security attributes */ duke@435: CREATE_ALWAYS, /* DWORD creation disposition duke@435: * create file, if it already duke@435: * exists, overwrite it. duke@435: */ duke@435: FILE_FLAG_DELETE_ON_CLOSE, /* DWORD flags and attributes */ duke@435: duke@435: NULL); /* HANDLE template file access */ duke@435: duke@435: free_security_attr(lpFileSA); duke@435: duke@435: if (fh == INVALID_HANDLE_VALUE) { duke@435: DWORD lasterror = GetLastError(); duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("could not create file %s: %d\n", filename, lasterror); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // try to create the file mapping duke@435: fmh = create_file_mapping(objectname, fh, lpSmoSA, size); duke@435: duke@435: free_security_attr(lpSmoSA); duke@435: duke@435: if (fmh == NULL) { duke@435: // closing the file handle here will decrement the reference count duke@435: // on the file. When all processes accessing the file close their duke@435: // handle to it, the reference count will decrement to 0 and the duke@435: // OS will delete the file. These semantics are requested by the duke@435: // FILE_FLAG_DELETE_ON_CLOSE flag in CreateFile call above. duke@435: CloseHandle(fh); duke@435: fh = NULL; duke@435: return NULL; dcubed@2543: } else { dcubed@2543: // We created the file mapping, but rarely the size of the dcubed@2543: // backing store file is reported as zero (0) which can cause dcubed@2543: // failures when trying to use the hsperfdata file. dcubed@2543: struct stat statbuf; dcubed@2543: int ret_code = ::stat(filename, &statbuf); dcubed@2543: if (ret_code == OS_ERR) { dcubed@2543: if (PrintMiscellaneous && Verbose) { dcubed@2543: warning("Could not get status information from file %s: %s\n", dcubed@2543: filename, strerror(errno)); dcubed@2543: } dcubed@2543: CloseHandle(fmh); dcubed@2543: CloseHandle(fh); dcubed@2543: fh = NULL; dcubed@2543: fmh = NULL; dcubed@2543: return NULL; dcubed@2543: } dcubed@2543: dcubed@2543: // We could always call FlushFileBuffers() but the Microsoft dcubed@2543: // docs indicate that it is considered expensive so we only dcubed@2543: // call it when we observe the size as zero (0). dcubed@2543: if (statbuf.st_size == 0 && FlushFileBuffers(fh) != TRUE) { dcubed@2543: DWORD lasterror = GetLastError(); dcubed@2543: if (PrintMiscellaneous && Verbose) { dcubed@2543: warning("could not flush file %s: %d\n", filename, lasterror); dcubed@2543: } dcubed@2543: CloseHandle(fmh); dcubed@2543: CloseHandle(fh); dcubed@2543: fh = NULL; dcubed@2543: fmh = NULL; dcubed@2543: return NULL; dcubed@2543: } duke@435: } duke@435: duke@435: // the file has been successfully created and the file mapping duke@435: // object has been created. duke@435: sharedmem_fileHandle = fh; duke@435: sharedmem_fileName = strdup(filename); duke@435: duke@435: return fmh; duke@435: } duke@435: duke@435: // open the shared memory object for the given vmid. duke@435: // duke@435: static HANDLE open_sharedmem_object(const char* objectname, DWORD ofm_access, TRAPS) { duke@435: duke@435: HANDLE fmh; duke@435: duke@435: // open the file mapping with the requested mode duke@435: fmh = OpenFileMapping( duke@435: ofm_access, /* DWORD access mode */ duke@435: FALSE, /* BOOL inherit flag - Do not allow inherit */ duke@435: objectname); /* name for object */ duke@435: duke@435: if (fmh == NULL) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("OpenFileMapping failed for shared memory object %s:" duke@435: " lasterror = %d\n", objectname, GetLastError()); duke@435: } duke@435: THROW_MSG_(vmSymbols::java_lang_Exception(), duke@435: "Could not open PerfMemory", INVALID_HANDLE_VALUE); duke@435: } duke@435: duke@435: return fmh;; duke@435: } duke@435: duke@435: // create a named shared memory region duke@435: // duke@435: // On Win32, a named shared memory object has a name space that duke@435: // is independent of the file system name space. Shared memory object, duke@435: // or more precisely, file mapping objects, provide no mechanism to duke@435: // inquire the size of the memory region. There is also no api to duke@435: // enumerate the memory regions for various processes. duke@435: // duke@435: // This implementation utilizes the shared memory name space in parallel duke@435: // with the file system name space. This allows us to determine the duke@435: // size of the shared memory region from the size of the file and it duke@435: // allows us to provide a common, file system based name space for duke@435: // shared memory across platforms. duke@435: // duke@435: static char* mapping_create_shared(size_t size) { duke@435: duke@435: void *mapAddress; duke@435: int vmid = os::current_process_id(); duke@435: duke@435: // get the name of the user associated with this process duke@435: char* user = get_user_name(); duke@435: duke@435: if (user == NULL) { duke@435: return NULL; duke@435: } duke@435: duke@435: // construct the name of the user specific temporary directory duke@435: char* dirname = get_user_tmp_dir(user); duke@435: duke@435: // check that the file system is secure - i.e. it supports ACLs. duke@435: if (!is_filesystem_secure(dirname)) { duke@435: return NULL; duke@435: } duke@435: duke@435: // create the names of the backing store files and for the duke@435: // share memory object. duke@435: // duke@435: char* filename = get_sharedmem_filename(dirname, vmid); duke@435: char* objectname = get_sharedmem_objectname(user, vmid); duke@435: duke@435: // cleanup any stale shared memory resources duke@435: cleanup_sharedmem_resources(dirname); duke@435: duke@435: assert(((size != 0) && (size % os::vm_page_size() == 0)), duke@435: "unexpected PerfMemry region size"); duke@435: zgu@3900: FREE_C_HEAP_ARRAY(char, user, mtInternal); duke@435: duke@435: // create the shared memory resources duke@435: sharedmem_fileMapHandle = duke@435: create_sharedmem_resources(dirname, filename, objectname, size); duke@435: zgu@3900: FREE_C_HEAP_ARRAY(char, filename, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, objectname, mtInternal); zgu@3900: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); duke@435: duke@435: if (sharedmem_fileMapHandle == NULL) { duke@435: return NULL; duke@435: } duke@435: duke@435: // map the file into the address space duke@435: mapAddress = MapViewOfFile( duke@435: sharedmem_fileMapHandle, /* HANDLE = file mapping object */ duke@435: FILE_MAP_ALL_ACCESS, /* DWORD access flags */ duke@435: 0, /* DWORD High word of offset */ duke@435: 0, /* DWORD Low word of offset */ duke@435: (DWORD)size); /* DWORD Number of bytes to map */ duke@435: duke@435: if (mapAddress == NULL) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("MapViewOfFile failed, lasterror = %d\n", GetLastError()); duke@435: } duke@435: CloseHandle(sharedmem_fileMapHandle); duke@435: sharedmem_fileMapHandle = NULL; duke@435: return NULL; duke@435: } duke@435: duke@435: // clear the shared memory region duke@435: (void)memset(mapAddress, '\0', size); duke@435: zgu@4193: // it does not go through os api, the operation has to record from here zgu@4193: MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC); zgu@4193: MemTracker::record_virtual_memory_type((address)mapAddress, mtInternal); zgu@4193: duke@435: return (char*) mapAddress; duke@435: } duke@435: duke@435: // this method deletes the file mapping object. duke@435: // duke@435: static void delete_file_mapping(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 not duke@435: // performed. The memory will be reclaimed by the OS upon termination of all duke@435: // processes mapping the resource. The file mapping handle and the file duke@435: // handle are closed here to expedite the remove of the file by the OS. The duke@435: // file is not removed directly because it was created with duke@435: // FILE_FLAG_DELETE_ON_CLOSE semantics and any attempt to remove it would duke@435: // be unsuccessful. duke@435: duke@435: // close the fileMapHandle. the file mapping will still be retained duke@435: // by the OS as long as any other JVM processes has an open file mapping duke@435: // handle or a mapped view of the file. duke@435: // duke@435: if (sharedmem_fileMapHandle != NULL) { duke@435: CloseHandle(sharedmem_fileMapHandle); duke@435: sharedmem_fileMapHandle = NULL; duke@435: } duke@435: duke@435: // close the file handle. This will decrement the reference count on the duke@435: // backing store file. When the reference count decrements to 0, the OS duke@435: // will delete the file. These semantics apply because the file was duke@435: // created with the FILE_FLAG_DELETE_ON_CLOSE flag. duke@435: // duke@435: if (sharedmem_fileHandle != INVALID_HANDLE_VALUE) { duke@435: CloseHandle(sharedmem_fileHandle); duke@435: sharedmem_fileHandle = INVALID_HANDLE_VALUE; duke@435: } duke@435: } duke@435: duke@435: // this method determines the size of the shared memory file duke@435: // duke@435: static size_t sharedmem_filesize(const char* filename, TRAPS) { duke@435: duke@435: struct stat statbuf; duke@435: duke@435: // get the file size duke@435: // duke@435: // on win95/98/me, _stat returns a file size of 0 bytes, but on duke@435: // winnt/2k the appropriate file size is returned. support for duke@435: // the sharable aspects of performance counters was abandonded duke@435: // on the non-nt win32 platforms due to this and other api duke@435: // inconsistencies duke@435: // duke@435: if (::stat(filename, &statbuf) == OS_ERR) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("stat %s failed: %s\n", filename, 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) || (statbuf.st_size % os::vm_page_size() != 0)) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("unexpected file size: size = " SIZE_FORMAT "\n", duke@435: statbuf.st_size); duke@435: } duke@435: THROW_MSG_0(vmSymbols::java_lang_Exception(), duke@435: "Invalid PerfMemory size"); duke@435: } duke@435: duke@435: return statbuf.st_size; duke@435: } duke@435: duke@435: // this method opens a file mapping object and maps the object duke@435: // into the address space of the process duke@435: // duke@435: static void open_file_mapping(const char* user, int vmid, duke@435: PerfMemory::PerfMemoryMode mode, duke@435: char** addrp, size_t* sizep, TRAPS) { duke@435: duke@435: ResourceMark rm; duke@435: duke@435: void *mapAddress = 0; duke@435: size_t size; duke@435: HANDLE fmh; duke@435: DWORD ofm_access; duke@435: DWORD mv_access; duke@435: const char* luser = NULL; duke@435: duke@435: if (mode == PerfMemory::PERF_MODE_RO) { duke@435: ofm_access = FILE_MAP_READ; duke@435: mv_access = FILE_MAP_READ; duke@435: } duke@435: else if (mode == PerfMemory::PERF_MODE_RW) { duke@435: #ifdef LATER duke@435: ofm_access = FILE_MAP_READ | FILE_MAP_WRITE; duke@435: mv_access = FILE_MAP_READ | FILE_MAP_WRITE; 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 a user name wasn't specified, then find the user name for duke@435: // the owner of the target vm. duke@435: if (user == NULL || strlen(user) == 0) { duke@435: luser = get_user_name(vmid); 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: // get the names for the resources for the target vm 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 also don't following them when attaching 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: char* objectname = get_sharedmem_objectname(luser, vmid); duke@435: duke@435: // copy heap memory to resource memory. the objectname and duke@435: // filename are passed to methods that may throw exceptions. duke@435: // using resource arrays for these names prevents the leaks duke@435: // that would otherwise occur. duke@435: // duke@435: char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); duke@435: char* robjectname = NEW_RESOURCE_ARRAY(char, strlen(objectname) + 1); duke@435: strcpy(rfilename, filename); duke@435: strcpy(robjectname, objectname); 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); zgu@3900: FREE_C_HEAP_ARRAY(char, objectname, mtInternal); duke@435: duke@435: if (*sizep == 0) { duke@435: size = sharedmem_filesize(rfilename, CHECK); duke@435: assert(size != 0, "unexpected size"); duke@435: } duke@435: duke@435: // Open the file mapping object with the given name duke@435: fmh = open_sharedmem_object(robjectname, ofm_access, CHECK); duke@435: duke@435: assert(fmh != INVALID_HANDLE_VALUE, "unexpected handle value"); duke@435: duke@435: // map the entire file into the address space duke@435: mapAddress = MapViewOfFile( duke@435: fmh, /* HANDLE Handle of file mapping object */ duke@435: mv_access, /* DWORD access flags */ duke@435: 0, /* DWORD High word of offset */ duke@435: 0, /* DWORD Low word of offset */ duke@435: size); /* DWORD Number of bytes to map */ duke@435: duke@435: if (mapAddress == NULL) { duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("MapViewOfFile failed, lasterror = %d\n", GetLastError()); duke@435: } duke@435: CloseHandle(fmh); 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@4193: MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC); zgu@4193: MemTracker::record_virtual_memory_type((address)mapAddress, mtInternal); zgu@4193: zgu@4193: duke@435: *addrp = (char*)mapAddress; duke@435: *sizep = size; duke@435: duke@435: // File mapping object can be closed at this time without duke@435: // invalidating the mapped view of the file duke@435: CloseHandle(fmh); duke@435: duke@435: if (PerfTraceMemOps) { duke@435: tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " duke@435: INTPTR_FORMAT "\n", size, vmid, mapAddress); duke@435: } duke@435: } duke@435: duke@435: // this method unmaps the the mapped view of the the duke@435: // file mapping object. duke@435: // duke@435: static void remove_file_mapping(char* addr) { duke@435: duke@435: // the file mapping object was closed in open_file_mapping() duke@435: // after the file map view was created. We only need to duke@435: // unmap the file view here. duke@435: UnmapViewOfFile(addr); duke@435: } duke@435: duke@435: // create the PerfData memory region in shared memory. duke@435: static char* create_shared_memory(size_t size) { duke@435: duke@435: return mapping_create_shared(size); duke@435: } duke@435: duke@435: // release a named, shared memory region duke@435: // duke@435: void delete_shared_memory(char* addr, size_t size) { duke@435: duke@435: delete_file_mapping(addr, size); 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 || !os::win32::is_nt()) { duke@435: // do not share the memory for the performance data. duke@435: PerfDisableSharedMem = true; 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, duke@435: 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: open_file_mapping(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: remove_file_mapping(addr); zgu@4193: // it does not go through os api, the operation has to record from here zgu@4193: MemTracker::record_virtual_memory_release((address)addr, bytes); duke@435: } duke@435: duke@435: char* PerfMemory::backing_store_filename() { duke@435: return sharedmem_fileName; duke@435: }