/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "classfile/vmSymbols.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "oops/oop.inline.hpp"
#include "os_bsd.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/perfMemory.hpp"
#include "services/memTracker.hpp"
#include "utilities/exceptions.hpp"
// put OS-includes here
# include <errno.h>
# include <stdio.h>
# include <unistd.h>
# include <signal.h>
# include <pwd.h>
// file, if successfully created.
// Standard Memory Implementation Details
// create the PerfData memory region in standard memory.
//
// allocate an aligned chuck of memory
if (mapAddress == NULL) {
return NULL;
}
// commit memory
if (PrintMiscellaneous && Verbose) {
warning("Could not commit PerfData memory\n");
}
return NULL;
}
return mapAddress;
}
// delete the PerfData memory region
//
// there are no persistent external resources to cleanup for standard
// memory. since DestroyJavaVM does not support unloading of the JVM,
// cleanup of the memory resource is not performed. The memory will be
// reclaimed by the OS upon termination of the process.
//
return;
}
// save the specified memory region to the given file
//
// Note: this function might be called from signal handler (by os::abort()),
// don't allocate heap memory.
//
int result;
result);;
if (PrintMiscellaneous && Verbose) {
warning("Could not create Perfdata save file: %s: %s\n",
}
} else {
if (PrintMiscellaneous && Verbose) {
warning("Could not write Perfdata save file: %s: %s\n",
}
break;
}
}
if (PrintMiscellaneous && Verbose) {
}
}
}
}
// Shared Memory Implementation Details
// Note: the solaris and bsd shared memory implementation uses the mmap
// interface with a backing store file to implement named shared memory.
// Using the file system as the name space for shared memory allows a
// common name space to be supported across a variety of platforms. It
// also provides a name space that Java applications can deal with through
// simple file apis.
//
// The solaris and bsd implementations store the backing store file in
// a user specific temporary directory located in the /tmp file system,
// which is always a local file system and is sometimes a RAM based file
// system.
// return the user specific temporary directory name.
//
// the caller is expected to free the allocated memory.
//
// construct the path name to user specific tmp directory
return dirname;
}
// convert the given file name into a process id. if the file
// does not meet the file naming constraints, return 0.
//
// a filename that doesn't begin with a digit is not a
// candidate for conversion.
//
return 0;
}
// check if file name can be converted to an integer without
// any leftover characters.
//
errno = 0;
if (errno != 0) {
return 0;
}
// check for left over characters. If any, then the filename is
// not a candidate for conversion.
//
return 0;
}
// successful conversion, return the pid
return pid;
}
// check if the given path is considered a secure directory for
// the backing store files. Returns true if the directory exists
// and is considered a secure location. Returns false if the path
// is a symbolic link or if an error occurred.
//
int result = 0;
return false;
}
// the path exists, now check it's mode
// the path represents a link or some non-directory file type,
// which is not what we expected. declare it insecure.
//
return false;
}
else {
// we have an existing directory, check if the permissions are safe.
//
// the directory is open for writing and could be subjected
// to a symlnk attack. declare it insecure.
//
return false;
}
}
return true;
}
// return the user name for the given user id
//
// the caller is expected to free the allocated memory.
//
// determine the max pwbuf size from sysconf, and hardcode
// a default if this not available through sysconf.
//
if (bufsize == -1)
bufsize = 1024;
// POSIX interface to getpwuid_r is used on LINUX
struct passwd* p;
if (PrintMiscellaneous && Verbose) {
if (result != 0) {
warning("Could not retrieve passwd entry: %s\n",
}
else if (p == NULL) {
// this check is added to protect against an observed problem
// with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
// indicating success, but has p == NULL. This was observed when
// inserting a file descriptor exhaustion fault prior to the call
// getpwuid_r() call. In this case, error is set to the appropriate
// error condition, but this is undocumented behavior. This check
// is safe under any condition, but the use of errno in the output
// message may result in an erroneous message.
// Bug Id 89052 was opened with RedHat.
//
warning("Could not retrieve passwd entry: %s\n",
}
else {
warning("Could not determine user name: %s\n",
"pw_name zero length");
}
}
return NULL;
}
return user_name;
}
// return the name of the user that owns the process identified by vmid.
//
// This method uses a slow directory search algorithm to find the backing
// store file for the specified vmid and returns the user name, as determined
// by the user name suffix of the hsperfdata_<username> directory name.
//
// the caller is expected to free the allocated memory.
//
// short circuit the directory search if the process doesn't even exist.
"Process not found");
}
else /* EPERM */ {
}
}
// directory search
return NULL;
}
// for each entry in the directory that matches the pattern hsperfdata_*,
// open the directory and check if the file for the given vmid exists.
// The file with the expected name and the latest creation date is used
// to determine the user name for the process id.
//
errno = 0;
// check if the directory entry is a hsperfdata file
continue;
}
continue;
}
// Since we don't create the backing store files in directories
// pointed to by symbolic links, we also don't follow them when
// looking for the files. We check for a symbolic link after the
// call to opendir in order to eliminate a small window where the
// symlink can be exploited.
//
if (!is_directory_secure(usrdir_name)) {
continue;
}
errno = 0;
int result;
// don't follow symbolic links for the file
continue;
}
// skip over files that are not regular files.
continue;
}
// compare and save filename with latest creation time
}
}
}
}
}
return(oldest_user);
}
// return the name of the user that owns the JVM indicated by the given vmid.
//
}
// return the file name of the backing store file for the named
// shared memory region for the given user name and vmid.
//
// the caller is expected to free the allocated memory.
//
// add 2 for the file separator and a null terminator.
return name;
}
// remove file
//
// this method removes the file specified by the given path
//
int result;
// if the file is a directory, the following unlink will fail. since
// we don't expect to find directories in the user temp directory, we
// won't try to handle this situation. even if accidentially or
// maliciously planted, the directory's presence won't hurt anything.
//
warning("Could not unlink shared memory backing"
}
}
}
// remove file
//
// this method removes the file with the given file name in the
// named directory.
//
}
// cleanup stale shared memory resources
//
// This method attempts to remove all stale shared memory files in
// the named user temporary directory. It scans the named directory
// for files matching the pattern ^$[0-9]*$. For each file found, the
// process id is extracted from the file name and a test is run to
// determine if the process is alive. If the process is not alive,
// any stale file resources are removed.
//
// open the user temp directory
// directory doesn't exist, so there is nothing to cleanup
return;
}
if (!is_directory_secure(dirname)) {
// the directory is not a secure directory
return;
}
// for each entry in the directory that matches the expected file
// name pattern, determine if the file resources are stale and if
// so, remove the file resources. Note, instrumented HotSpot processes
// remove or create new files in this directory. The behavior of this
// loop under these conditions is dependent upon the implementation of
//
errno = 0;
if (pid == 0) {
// attempt to remove all unexpected files, except "." and ".."
}
errno = 0;
continue;
}
// we now have a file name that converts to a valid integer
// that could represent a process id . if this process id
// matches the current process id or the process is not running,
// then remove the stale file resources.
//
// process liveness is detected by sending signal number 0 to
// the process id (see kill(2)). if kill determines that the
// process does not exist, then the file resources are removed.
// if kill determines that that we don't have permission to
// signal the process, then the file resources are assumed to
// be stale and are removed because the resources for such a
// process should be in a different user specific directory.
//
}
errno = 0;
}
}
// make the user specific temporary directory. Returns true if
// the directory exists and is secure upon return. Returns false
// if the directory exists but is either a symlink, is otherwise
// insecure, or if an error occurred.
//
// create the directory with 0755 permissions. note that the directory
// will be owned by euid::egid, which may not be the same as uid::gid.
//
// The directory already exists and was probably created by another
// JVM instance. However, this could also be the result of a
// deliberate symlink. Verify that the existing directory is safe.
//
if (!is_directory_secure(dirname)) {
// directory is not secure
if (PrintMiscellaneous && Verbose) {
}
return false;
}
}
else {
// we encountered some other failure while attempting
// to create the directory
//
if (PrintMiscellaneous && Verbose) {
warning("could not create directory %s: %s\n",
}
return false;
}
}
return true;
}
// create the shared memory file resources
//
// This method creates the shared memory file with the given size
// This method also creates the user specific temporary directory, if
// it does not yet exist.
//
// make the user temporary directory
if (!make_user_tmp_dir(dirname)) {
// was not secure
return -1;
}
int result;
if (PrintMiscellaneous && Verbose) {
}
return -1;
}
// save the file descriptor
// set the file size
if (PrintMiscellaneous && Verbose) {
}
return -1;
}
// Verify that we have enough disk space for this file.
// We'll get random SIGBUS crashes on memory accesses if
// we don't.
int zero_int = 0;
if (result == -1 ) break;
if (result != 1) {
warning("Insufficient space for shared memory file:\n %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename);
}
break;
}
}
if (result != -1) {
return fd;
} else {
return -1;
}
}
// open the shared memory file for the given user and vmid. returns
// the file descriptor for the open file or -1 if the file could not
// be opened.
//
// open the file
int result;
"Process not found");
}
"Permission denied");
}
else {
}
}
return result;
}
// create a named shared memory region. returns the address of the
// memory region on success or NULL on failure. A return value of
// NULL will ultimately disable the shared memory feature.
//
// On Solaris and Bsd, the name space for shared memory objects
// is the file system name space.
//
// A monitoring application attaching to a JVM does not need to know
// the file system name of the shared memory object. However, it may
// be convenient for applications to discover the existence of newly
// created and terminating JVMs by watching the file system name space
// for files being created or removed.
//
int result;
int fd;
char* mapAddress;
return NULL;
// cleanup any stale shared memory files
"unexpected PerfMemory region size");
if (fd == -1) {
return NULL;
}
// attempt to close the file - restart it if it was interrupted,
// but ignore other failures
if (mapAddress == MAP_FAILED) {
if (PrintMiscellaneous && Verbose) {
}
return NULL;
}
// save the file name for use in delete_shared_memory()
// clear the shared memory region
// it does not go through os api, the operation has to record from here
return mapAddress;
}
// release a named shared memory region
//
}
// create the PerfData memory region in shared memory.
//
// create the shared memory region.
return mmap_create_shared(size);
}
// delete the shared PerfData memory region
//
// cleanup the persistent shared memory resources. since DestroyJavaVM does
// not support unloading of the JVM, unmapping of the memory resource is
// not performed. The memory will be reclaimed by the OS upon termination of
// the process. The backing store file is deleted from the file system.
if (backing_store_file_name != NULL) {
// Don't.. Free heap memory could deadlock os::abort() if it is called
// from signal handler. OS will reclaim the heap memory.
// FREE_C_HEAP_ARRAY(char, backing_store_file_name);
}
}
// return the size of the file for the given file descriptor
// or 0 if it is not a valid size for a shared memory file
//
int result;
if (PrintMiscellaneous && Verbose) {
}
"Could not determine PerfMemory size");
}
"Invalid PerfMemory size");
}
}
// attach to a named shared memory region.
//
static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
char* mapAddress;
int result;
int fd;
int mmap_prot;
int file_flags;
// map the high level access mode to the appropriate permission
// constructs for the file and the shared memory mapping.
}
#ifdef LATER
file_flags = O_RDWR;
#else
"Unsupported access mode");
#endif
}
else {
"Illegal access mode");
}
}
else {
}
"Could not map vmid to user Name");
}
// since we don't follow symbolic links when creating the backing
// store file, we don't follow them when attaching either.
//
if (!is_directory_secure(dirname)) {
"Process not found");
}
// copy heap memory to resource memory. the open_sharedmem_file
// method below need to use the filename, but could throw an
// exception. using a resource array prevents the leak that
// would otherwise occur.
// free the c heap resources that are no longer needed
// open the shared memory file for the give vmid
if (*sizep == 0) {
}
// attempt to close the file - restart if it gets interrupted,
// but ignore other failures
if (mapAddress == MAP_FAILED) {
if (PrintMiscellaneous && Verbose) {
}
"Could not map PerfMemory");
}
// it does not go through os api, the operation has to record from here
*addr = mapAddress;
if (PerfTraceMemOps) {
}
}
// create the PerfData memory region
//
// This method creates the memory region used to store performance
// data for the JVM. The memory may be created in standard or
// shared memory.
//
if (PerfDisableSharedMem) {
// do not share the memory for the performance data.
}
else {
// creation of the shared memory region failed, attempt
// to create a contiguous, non-shared memory region instead.
//
if (PrintMiscellaneous && Verbose) {
warning("Reverting to non-shared PerfMemory region.\n");
}
PerfDisableSharedMem = true;
}
}
}
// delete the PerfData memory region
//
// This method deletes the memory region used to store performance
// data for the JVM. The memory region indicated by the <address, size>
// tuple will be inaccessible after a call to this method.
//
// If user specifies PerfDataSaveFile, it will save the performance data
// to the specified file name no matter whether PerfDataSaveToFile is specified
// or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
// -XX:+PerfDataSaveToFile.
}
if (PerfDisableSharedMem) {
}
else {
}
}
// attach to the PerfData memory region for another JVM
//
// This method returns an <address, size> tuple that points to
// a memory buffer that is kept reasonably synchronized with
// the PerfData memory region for the indicated JVM. This
// buffer may be kept in synchronization via shared memory
// or some other mechanism that keeps the buffer updated.
//
// If the JVM chooses not to support the attachability feature,
// this method should throw an UnsupportedOperation exception.
//
// This implementation utilizes named shared memory to map
// the indicated process's PerfData memory region into this JVMs
// address space.
//
void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
return;
}
}
// detach from the PerfData memory region of another JVM
//
// This method detaches the PerfData memory region of another
// JVM, specified as an <address, size> tuple of a buffer
// in this process's address space. This method may perform
// arbitrary actions to accomplish the detachment. The memory
// region specified by <address, size> will be inaccessible after
// a call to this method.
//
// If the JVM chooses not to support the attachability feature,
// this method should throw an UnsupportedOperation exception.
//
// This implementation utilizes named shared memory to detach
// the indicated process's PerfData memory region from this
// process's address space.
//
// prevent accidental detachment of this process's PerfMemory region
return;
}
}
return backing_store_file_name;
}