1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os/solaris/vm/perfMemory_solaris.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,1048 @@ 1.4 +/* 1.5 + * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_perfMemory_solaris.cpp.incl" 1.30 + 1.31 +// put OS-includes here 1.32 +# include <sys/types.h> 1.33 +# include <sys/mman.h> 1.34 +# include <errno.h> 1.35 +# include <stdio.h> 1.36 +# include <unistd.h> 1.37 +# include <sys/stat.h> 1.38 +# include <signal.h> 1.39 +# include <pwd.h> 1.40 +# include <procfs.h> 1.41 + 1.42 + 1.43 +static char* backing_store_file_name = NULL; // name of the backing store 1.44 + // file, if successfully created. 1.45 + 1.46 +// Standard Memory Implementation Details 1.47 + 1.48 +// create the PerfData memory region in standard memory. 1.49 +// 1.50 +static char* create_standard_memory(size_t size) { 1.51 + 1.52 + // allocate an aligned chuck of memory 1.53 + char* mapAddress = os::reserve_memory(size); 1.54 + 1.55 + if (mapAddress == NULL) { 1.56 + return NULL; 1.57 + } 1.58 + 1.59 + // commit memory 1.60 + if (!os::commit_memory(mapAddress, size)) { 1.61 + if (PrintMiscellaneous && Verbose) { 1.62 + warning("Could not commit PerfData memory\n"); 1.63 + } 1.64 + os::release_memory(mapAddress, size); 1.65 + return NULL; 1.66 + } 1.67 + 1.68 + return mapAddress; 1.69 +} 1.70 + 1.71 +// delete the PerfData memory region 1.72 +// 1.73 +static void delete_standard_memory(char* addr, size_t size) { 1.74 + 1.75 + // there are no persistent external resources to cleanup for standard 1.76 + // memory. since DestroyJavaVM does not support unloading of the JVM, 1.77 + // cleanup of the memory resource is not performed. The memory will be 1.78 + // reclaimed by the OS upon termination of the process. 1.79 + // 1.80 + return; 1.81 +} 1.82 + 1.83 +// save the specified memory region to the given file 1.84 +// 1.85 +// Note: this function might be called from signal handler (by os::abort()), 1.86 +// don't allocate heap memory. 1.87 +// 1.88 +static void save_memory_to_file(char* addr, size_t size) { 1.89 + 1.90 + const char* destfile = PerfMemory::get_perfdata_file_path(); 1.91 + assert(destfile[0] != '\0', "invalid PerfData file path"); 1.92 + 1.93 + int result; 1.94 + 1.95 + RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), 1.96 + result);; 1.97 + if (result == OS_ERR) { 1.98 + if (PrintMiscellaneous && Verbose) { 1.99 + warning("Could not create Perfdata save file: %s: %s\n", 1.100 + destfile, strerror(errno)); 1.101 + } 1.102 + } else { 1.103 + 1.104 + int fd = result; 1.105 + 1.106 + for (size_t remaining = size; remaining > 0;) { 1.107 + 1.108 + RESTARTABLE(::write(fd, addr, remaining), result); 1.109 + if (result == OS_ERR) { 1.110 + if (PrintMiscellaneous && Verbose) { 1.111 + warning("Could not write Perfdata save file: %s: %s\n", 1.112 + destfile, strerror(errno)); 1.113 + } 1.114 + break; 1.115 + } 1.116 + remaining -= (size_t)result; 1.117 + addr += result; 1.118 + } 1.119 + 1.120 + RESTARTABLE(::close(fd), result); 1.121 + if (PrintMiscellaneous && Verbose) { 1.122 + if (result == OS_ERR) { 1.123 + warning("Could not close %s: %s\n", destfile, strerror(errno)); 1.124 + } 1.125 + } 1.126 + } 1.127 + FREE_C_HEAP_ARRAY(char, destfile); 1.128 +} 1.129 + 1.130 + 1.131 +// Shared Memory Implementation Details 1.132 + 1.133 +// Note: the solaris and linux shared memory implementation uses the mmap 1.134 +// interface with a backing store file to implement named shared memory. 1.135 +// Using the file system as the name space for shared memory allows a 1.136 +// common name space to be supported across a variety of platforms. It 1.137 +// also provides a name space that Java applications can deal with through 1.138 +// simple file apis. 1.139 +// 1.140 +// The solaris and linux implementations store the backing store file in 1.141 +// a user specific temporary directory located in the /tmp file system, 1.142 +// which is always a local file system and is sometimes a RAM based file 1.143 +// system. 1.144 + 1.145 +// return the user specific temporary directory name. 1.146 +// 1.147 +// the caller is expected to free the allocated memory. 1.148 +// 1.149 +static char* get_user_tmp_dir(const char* user) { 1.150 + 1.151 + const char* tmpdir = os::get_temp_directory(); 1.152 + const char* perfdir = PERFDATA_NAME; 1.153 + size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 2; 1.154 + char* dirname = NEW_C_HEAP_ARRAY(char, nbytes); 1.155 + 1.156 + // construct the path name to user specific tmp directory 1.157 + snprintf(dirname, nbytes, "%s%s_%s", tmpdir, perfdir, user); 1.158 + 1.159 + return dirname; 1.160 +} 1.161 + 1.162 +// convert the given file name into a process id. if the file 1.163 +// does not meet the file naming constraints, return 0. 1.164 +// 1.165 +static pid_t filename_to_pid(const char* filename) { 1.166 + 1.167 + // a filename that doesn't begin with a digit is not a 1.168 + // candidate for conversion. 1.169 + // 1.170 + if (!isdigit(*filename)) { 1.171 + return 0; 1.172 + } 1.173 + 1.174 + // check if file name can be converted to an integer without 1.175 + // any leftover characters. 1.176 + // 1.177 + char* remainder = NULL; 1.178 + errno = 0; 1.179 + pid_t pid = (pid_t)strtol(filename, &remainder, 10); 1.180 + 1.181 + if (errno != 0) { 1.182 + return 0; 1.183 + } 1.184 + 1.185 + // check for left over characters. If any, then the filename is 1.186 + // not a candidate for conversion. 1.187 + // 1.188 + if (remainder != NULL && *remainder != '\0') { 1.189 + return 0; 1.190 + } 1.191 + 1.192 + // successful conversion, return the pid 1.193 + return pid; 1.194 +} 1.195 + 1.196 + 1.197 +// check if the given path is considered a secure directory for 1.198 +// the backing store files. Returns true if the directory exists 1.199 +// and is considered a secure location. Returns false if the path 1.200 +// is a symbolic link or if an error occured. 1.201 +// 1.202 +static bool is_directory_secure(const char* path) { 1.203 + struct stat statbuf; 1.204 + int result = 0; 1.205 + 1.206 + RESTARTABLE(::lstat(path, &statbuf), result); 1.207 + if (result == OS_ERR) { 1.208 + return false; 1.209 + } 1.210 + 1.211 + // the path exists, now check it's mode 1.212 + if (S_ISLNK(statbuf.st_mode) || !S_ISDIR(statbuf.st_mode)) { 1.213 + // the path represents a link or some non-directory file type, 1.214 + // which is not what we expected. declare it insecure. 1.215 + // 1.216 + return false; 1.217 + } 1.218 + else { 1.219 + // we have an existing directory, check if the permissions are safe. 1.220 + // 1.221 + if ((statbuf.st_mode & (S_IWGRP|S_IWOTH)) != 0) { 1.222 + // the directory is open for writing and could be subjected 1.223 + // to a symlnk attack. declare it insecure. 1.224 + // 1.225 + return false; 1.226 + } 1.227 + } 1.228 + return true; 1.229 +} 1.230 + 1.231 + 1.232 +// return the user name for the given user id 1.233 +// 1.234 +// the caller is expected to free the allocated memory. 1.235 +// 1.236 +static char* get_user_name(uid_t uid) { 1.237 + 1.238 + struct passwd pwent; 1.239 + 1.240 + // determine the max pwbuf size from sysconf, and hardcode 1.241 + // a default if this not available through sysconf. 1.242 + // 1.243 + long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); 1.244 + if (bufsize == -1) 1.245 + bufsize = 1024; 1.246 + 1.247 + char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize); 1.248 + 1.249 +#ifdef _GNU_SOURCE 1.250 + struct passwd* p = NULL; 1.251 + int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p); 1.252 +#else // _GNU_SOURCE 1.253 + struct passwd* p = getpwuid_r(uid, &pwent, pwbuf, (int)bufsize); 1.254 +#endif // _GNU_SOURCE 1.255 + 1.256 + if (p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { 1.257 + if (PrintMiscellaneous && Verbose) { 1.258 + if (p == NULL) { 1.259 + warning("Could not retrieve passwd entry: %s\n", 1.260 + strerror(errno)); 1.261 + } 1.262 + else { 1.263 + warning("Could not determine user name: %s\n", 1.264 + p->pw_name == NULL ? "pw_name = NULL" : 1.265 + "pw_name zero length"); 1.266 + } 1.267 + } 1.268 + FREE_C_HEAP_ARRAY(char, pwbuf); 1.269 + return NULL; 1.270 + } 1.271 + 1.272 + char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1); 1.273 + strcpy(user_name, p->pw_name); 1.274 + 1.275 + FREE_C_HEAP_ARRAY(char, pwbuf); 1.276 + return user_name; 1.277 +} 1.278 + 1.279 +// return the name of the user that owns the process identified by vmid. 1.280 +// 1.281 +// This method uses a slow directory search algorithm to find the backing 1.282 +// store file for the specified vmid and returns the user name, as determined 1.283 +// by the user name suffix of the hsperfdata_<username> directory name. 1.284 +// 1.285 +// the caller is expected to free the allocated memory. 1.286 +// 1.287 +static char* get_user_name_slow(int vmid, TRAPS) { 1.288 + 1.289 + // short circuit the directory search if the process doesn't even exist. 1.290 + if (kill(vmid, 0) == OS_ERR) { 1.291 + if (errno == ESRCH) { 1.292 + THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 1.293 + "Process not found"); 1.294 + } 1.295 + else /* EPERM */ { 1.296 + THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); 1.297 + } 1.298 + } 1.299 + 1.300 + // directory search 1.301 + char* oldest_user = NULL; 1.302 + time_t oldest_ctime = 0; 1.303 + 1.304 + const char* tmpdirname = os::get_temp_directory(); 1.305 + 1.306 + DIR* tmpdirp = os::opendir(tmpdirname); 1.307 + 1.308 + if (tmpdirp == NULL) { 1.309 + return NULL; 1.310 + } 1.311 + 1.312 + // for each entry in the directory that matches the pattern hsperfdata_*, 1.313 + // open the directory and check if the file for the given vmid exists. 1.314 + // The file with the expected name and the latest creation date is used 1.315 + // to determine the user name for the process id. 1.316 + // 1.317 + struct dirent* dentry; 1.318 + char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname)); 1.319 + errno = 0; 1.320 + while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) { 1.321 + 1.322 + // check if the directory entry is a hsperfdata file 1.323 + if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) { 1.324 + continue; 1.325 + } 1.326 + 1.327 + char* usrdir_name = NEW_C_HEAP_ARRAY(char, 1.328 + strlen(tmpdirname) + strlen(dentry->d_name) + 1); 1.329 + strcpy(usrdir_name, tmpdirname); 1.330 + strcat(usrdir_name, dentry->d_name); 1.331 + 1.332 + DIR* subdirp = os::opendir(usrdir_name); 1.333 + 1.334 + if (subdirp == NULL) { 1.335 + FREE_C_HEAP_ARRAY(char, usrdir_name); 1.336 + continue; 1.337 + } 1.338 + 1.339 + // Since we don't create the backing store files in directories 1.340 + // pointed to by symbolic links, we also don't follow them when 1.341 + // looking for the files. We check for a symbolic link after the 1.342 + // call to opendir in order to eliminate a small window where the 1.343 + // symlink can be exploited. 1.344 + // 1.345 + if (!is_directory_secure(usrdir_name)) { 1.346 + FREE_C_HEAP_ARRAY(char, usrdir_name); 1.347 + os::closedir(subdirp); 1.348 + continue; 1.349 + } 1.350 + 1.351 + struct dirent* udentry; 1.352 + char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name)); 1.353 + errno = 0; 1.354 + while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) { 1.355 + 1.356 + if (filename_to_pid(udentry->d_name) == vmid) { 1.357 + struct stat statbuf; 1.358 + int result; 1.359 + 1.360 + char* filename = NEW_C_HEAP_ARRAY(char, 1.361 + strlen(usrdir_name) + strlen(udentry->d_name) + 2); 1.362 + 1.363 + strcpy(filename, usrdir_name); 1.364 + strcat(filename, "/"); 1.365 + strcat(filename, udentry->d_name); 1.366 + 1.367 + // don't follow symbolic links for the file 1.368 + RESTARTABLE(::lstat(filename, &statbuf), result); 1.369 + if (result == OS_ERR) { 1.370 + FREE_C_HEAP_ARRAY(char, filename); 1.371 + continue; 1.372 + } 1.373 + 1.374 + // skip over files that are not regular files. 1.375 + if (!S_ISREG(statbuf.st_mode)) { 1.376 + FREE_C_HEAP_ARRAY(char, filename); 1.377 + continue; 1.378 + } 1.379 + 1.380 + // compare and save filename with latest creation time 1.381 + if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) { 1.382 + 1.383 + if (statbuf.st_ctime > oldest_ctime) { 1.384 + char* user = strchr(dentry->d_name, '_') + 1; 1.385 + 1.386 + if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user); 1.387 + oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1); 1.388 + 1.389 + strcpy(oldest_user, user); 1.390 + oldest_ctime = statbuf.st_ctime; 1.391 + } 1.392 + } 1.393 + 1.394 + FREE_C_HEAP_ARRAY(char, filename); 1.395 + } 1.396 + } 1.397 + os::closedir(subdirp); 1.398 + FREE_C_HEAP_ARRAY(char, udbuf); 1.399 + FREE_C_HEAP_ARRAY(char, usrdir_name); 1.400 + } 1.401 + os::closedir(tmpdirp); 1.402 + FREE_C_HEAP_ARRAY(char, tdbuf); 1.403 + 1.404 + return(oldest_user); 1.405 +} 1.406 + 1.407 +// return the name of the user that owns the JVM indicated by the given vmid. 1.408 +// 1.409 +static char* get_user_name(int vmid, TRAPS) { 1.410 + 1.411 + char psinfo_name[PATH_MAX]; 1.412 + int result; 1.413 + 1.414 + snprintf(psinfo_name, PATH_MAX, "/proc/%d/psinfo", vmid); 1.415 + 1.416 + RESTARTABLE(::open(psinfo_name, O_RDONLY), result); 1.417 + 1.418 + if (result != OS_ERR) { 1.419 + int fd = result; 1.420 + 1.421 + psinfo_t psinfo; 1.422 + char* addr = (char*)&psinfo; 1.423 + 1.424 + for (size_t remaining = sizeof(psinfo_t); remaining > 0;) { 1.425 + 1.426 + RESTARTABLE(::read(fd, addr, remaining), result); 1.427 + if (result == OS_ERR) { 1.428 + THROW_MSG_0(vmSymbols::java_io_IOException(), "Read error"); 1.429 + } 1.430 + remaining-=result; 1.431 + addr+=result; 1.432 + } 1.433 + 1.434 + RESTARTABLE(::close(fd), result); 1.435 + 1.436 + // get the user name for the effective user id of the process 1.437 + char* user_name = get_user_name(psinfo.pr_euid); 1.438 + 1.439 + return user_name; 1.440 + } 1.441 + 1.442 + if (result == OS_ERR && errno == EACCES) { 1.443 + 1.444 + // In this case, the psinfo file for the process id existed, 1.445 + // but we didn't have permission to access it. 1.446 + THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 1.447 + strerror(errno)); 1.448 + } 1.449 + 1.450 + // at this point, we don't know if the process id itself doesn't 1.451 + // exist or if the psinfo file doesn't exit. If the psinfo file 1.452 + // doesn't exist, then we are running on Solaris 2.5.1 or earlier. 1.453 + // since the structured procfs and old procfs interfaces can't be 1.454 + // mixed, we attempt to find the file through a directory search. 1.455 + 1.456 + return get_user_name_slow(vmid, CHECK_NULL); 1.457 +} 1.458 + 1.459 +// return the file name of the backing store file for the named 1.460 +// shared memory region for the given user name and vmid. 1.461 +// 1.462 +// the caller is expected to free the allocated memory. 1.463 +// 1.464 +static char* get_sharedmem_filename(const char* dirname, int vmid) { 1.465 + 1.466 + // add 2 for the file separator and a NULL terminator. 1.467 + size_t nbytes = strlen(dirname) + UINT_CHARS + 2; 1.468 + 1.469 + char* name = NEW_C_HEAP_ARRAY(char, nbytes); 1.470 + snprintf(name, nbytes, "%s/%d", dirname, vmid); 1.471 + 1.472 + return name; 1.473 +} 1.474 + 1.475 + 1.476 +// remove file 1.477 +// 1.478 +// this method removes the file specified by the given path 1.479 +// 1.480 +static void remove_file(const char* path) { 1.481 + 1.482 + int result; 1.483 + 1.484 + // if the file is a directory, the following unlink will fail. since 1.485 + // we don't expect to find directories in the user temp directory, we 1.486 + // won't try to handle this situation. even if accidentially or 1.487 + // maliciously planted, the directory's presence won't hurt anything. 1.488 + // 1.489 + RESTARTABLE(::unlink(path), result); 1.490 + if (PrintMiscellaneous && Verbose && result == OS_ERR) { 1.491 + if (errno != ENOENT) { 1.492 + warning("Could not unlink shared memory backing" 1.493 + " store file %s : %s\n", path, strerror(errno)); 1.494 + } 1.495 + } 1.496 +} 1.497 + 1.498 + 1.499 +// remove file 1.500 +// 1.501 +// this method removes the file with the given file name in the 1.502 +// named directory. 1.503 +// 1.504 +static void remove_file(const char* dirname, const char* filename) { 1.505 + 1.506 + size_t nbytes = strlen(dirname) + strlen(filename) + 2; 1.507 + char* path = NEW_C_HEAP_ARRAY(char, nbytes); 1.508 + 1.509 + strcpy(path, dirname); 1.510 + strcat(path, "/"); 1.511 + strcat(path, filename); 1.512 + 1.513 + remove_file(path); 1.514 + 1.515 + FREE_C_HEAP_ARRAY(char, path); 1.516 +} 1.517 + 1.518 + 1.519 +// cleanup stale shared memory resources 1.520 +// 1.521 +// This method attempts to remove all stale shared memory files in 1.522 +// the named user temporary directory. It scans the named directory 1.523 +// for files matching the pattern ^$[0-9]*$. For each file found, the 1.524 +// process id is extracted from the file name and a test is run to 1.525 +// determine if the process is alive. If the process is not alive, 1.526 +// any stale file resources are removed. 1.527 +// 1.528 +static void cleanup_sharedmem_resources(const char* dirname) { 1.529 + 1.530 + // open the user temp directory 1.531 + DIR* dirp = os::opendir(dirname); 1.532 + 1.533 + if (dirp == NULL) { 1.534 + // directory doesn't exist, so there is nothing to cleanup 1.535 + return; 1.536 + } 1.537 + 1.538 + if (!is_directory_secure(dirname)) { 1.539 + // the directory is not a secure directory 1.540 + return; 1.541 + } 1.542 + 1.543 + // for each entry in the directory that matches the expected file 1.544 + // name pattern, determine if the file resources are stale and if 1.545 + // so, remove the file resources. Note, instrumented HotSpot processes 1.546 + // for this user may start and/or terminate during this search and 1.547 + // remove or create new files in this directory. The behavior of this 1.548 + // loop under these conditions is dependent upon the implementation of 1.549 + // opendir/readdir. 1.550 + // 1.551 + struct dirent* entry; 1.552 + char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname)); 1.553 + errno = 0; 1.554 + while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) { 1.555 + 1.556 + pid_t pid = filename_to_pid(entry->d_name); 1.557 + 1.558 + if (pid == 0) { 1.559 + 1.560 + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 1.561 + 1.562 + // attempt to remove all unexpected files, except "." and ".." 1.563 + remove_file(dirname, entry->d_name); 1.564 + } 1.565 + 1.566 + errno = 0; 1.567 + continue; 1.568 + } 1.569 + 1.570 + // we now have a file name that converts to a valid integer 1.571 + // that could represent a process id . if this process id 1.572 + // matches the current process id or the process is not running, 1.573 + // then remove the stale file resources. 1.574 + // 1.575 + // process liveness is detected by sending signal number 0 to 1.576 + // the process id (see kill(2)). if kill determines that the 1.577 + // process does not exist, then the file resources are removed. 1.578 + // if kill determines that that we don't have permission to 1.579 + // signal the process, then the file resources are assumed to 1.580 + // be stale and are removed because the resources for such a 1.581 + // process should be in a different user specific directory. 1.582 + // 1.583 + if ((pid == os::current_process_id()) || 1.584 + (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) { 1.585 + 1.586 + remove_file(dirname, entry->d_name); 1.587 + } 1.588 + errno = 0; 1.589 + } 1.590 + os::closedir(dirp); 1.591 + FREE_C_HEAP_ARRAY(char, dbuf); 1.592 +} 1.593 + 1.594 +// make the user specific temporary directory. Returns true if 1.595 +// the directory exists and is secure upon return. Returns false 1.596 +// if the directory exists but is either a symlink, is otherwise 1.597 +// insecure, or if an error occurred. 1.598 +// 1.599 +static bool make_user_tmp_dir(const char* dirname) { 1.600 + 1.601 + // create the directory with 0755 permissions. note that the directory 1.602 + // will be owned by euid::egid, which may not be the same as uid::gid. 1.603 + // 1.604 + if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) { 1.605 + if (errno == EEXIST) { 1.606 + // The directory already exists and was probably created by another 1.607 + // JVM instance. However, this could also be the result of a 1.608 + // deliberate symlink. Verify that the existing directory is safe. 1.609 + // 1.610 + if (!is_directory_secure(dirname)) { 1.611 + // directory is not secure 1.612 + if (PrintMiscellaneous && Verbose) { 1.613 + warning("%s directory is insecure\n", dirname); 1.614 + } 1.615 + return false; 1.616 + } 1.617 + } 1.618 + else { 1.619 + // we encountered some other failure while attempting 1.620 + // to create the directory 1.621 + // 1.622 + if (PrintMiscellaneous && Verbose) { 1.623 + warning("could not create directory %s: %s\n", 1.624 + dirname, strerror(errno)); 1.625 + } 1.626 + return false; 1.627 + } 1.628 + } 1.629 + return true; 1.630 +} 1.631 + 1.632 +// create the shared memory file resources 1.633 +// 1.634 +// This method creates the shared memory file with the given size 1.635 +// This method also creates the user specific temporary directory, if 1.636 +// it does not yet exist. 1.637 +// 1.638 +static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) { 1.639 + 1.640 + // make the user temporary directory 1.641 + if (!make_user_tmp_dir(dirname)) { 1.642 + // could not make/find the directory or the found directory 1.643 + // was not secure 1.644 + return -1; 1.645 + } 1.646 + 1.647 + int result; 1.648 + 1.649 + RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE), result); 1.650 + if (result == OS_ERR) { 1.651 + if (PrintMiscellaneous && Verbose) { 1.652 + warning("could not create file %s: %s\n", filename, strerror(errno)); 1.653 + } 1.654 + return -1; 1.655 + } 1.656 + 1.657 + // save the file descriptor 1.658 + int fd = result; 1.659 + 1.660 + // set the file size 1.661 + RESTARTABLE(::ftruncate(fd, (off_t)size), result); 1.662 + if (result == OS_ERR) { 1.663 + if (PrintMiscellaneous && Verbose) { 1.664 + warning("could not set shared memory file size: %s\n", strerror(errno)); 1.665 + } 1.666 + RESTARTABLE(::close(fd), result); 1.667 + return -1; 1.668 + } 1.669 + 1.670 + return fd; 1.671 +} 1.672 + 1.673 +// open the shared memory file for the given user and vmid. returns 1.674 +// the file descriptor for the open file or -1 if the file could not 1.675 +// be opened. 1.676 +// 1.677 +static int open_sharedmem_file(const char* filename, int oflags, TRAPS) { 1.678 + 1.679 + // open the file 1.680 + int result; 1.681 + RESTARTABLE(::open(filename, oflags), result); 1.682 + if (result == OS_ERR) { 1.683 + if (errno == ENOENT) { 1.684 + THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 1.685 + "Process not found"); 1.686 + } 1.687 + else if (errno == EACCES) { 1.688 + THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 1.689 + "Permission denied"); 1.690 + } 1.691 + else { 1.692 + THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); 1.693 + } 1.694 + } 1.695 + 1.696 + return result; 1.697 +} 1.698 + 1.699 +// create a named shared memory region. returns the address of the 1.700 +// memory region on success or NULL on failure. A return value of 1.701 +// NULL will ultimately disable the shared memory feature. 1.702 +// 1.703 +// On Solaris and Linux, the name space for shared memory objects 1.704 +// is the file system name space. 1.705 +// 1.706 +// A monitoring application attaching to a JVM does not need to know 1.707 +// the file system name of the shared memory object. However, it may 1.708 +// be convenient for applications to discover the existence of newly 1.709 +// created and terminating JVMs by watching the file system name space 1.710 +// for files being created or removed. 1.711 +// 1.712 +static char* mmap_create_shared(size_t size) { 1.713 + 1.714 + int result; 1.715 + int fd; 1.716 + char* mapAddress; 1.717 + 1.718 + int vmid = os::current_process_id(); 1.719 + 1.720 + char* user_name = get_user_name(geteuid()); 1.721 + 1.722 + if (user_name == NULL) 1.723 + return NULL; 1.724 + 1.725 + char* dirname = get_user_tmp_dir(user_name); 1.726 + char* filename = get_sharedmem_filename(dirname, vmid); 1.727 + 1.728 + // cleanup any stale shared memory files 1.729 + cleanup_sharedmem_resources(dirname); 1.730 + 1.731 + assert(((size > 0) && (size % os::vm_page_size() == 0)), 1.732 + "unexpected PerfMemory region size"); 1.733 + 1.734 + fd = create_sharedmem_resources(dirname, filename, size); 1.735 + 1.736 + FREE_C_HEAP_ARRAY(char, user_name); 1.737 + FREE_C_HEAP_ARRAY(char, dirname); 1.738 + 1.739 + if (fd == -1) { 1.740 + FREE_C_HEAP_ARRAY(char, filename); 1.741 + return NULL; 1.742 + } 1.743 + 1.744 + mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 1.745 + 1.746 + // attempt to close the file - restart it if it was interrupted, 1.747 + // but ignore other failures 1.748 + RESTARTABLE(::close(fd), result); 1.749 + assert(result != OS_ERR, "could not close file"); 1.750 + 1.751 + if (mapAddress == MAP_FAILED) { 1.752 + if (PrintMiscellaneous && Verbose) { 1.753 + warning("mmap failed - %s\n", strerror(errno)); 1.754 + } 1.755 + remove_file(filename); 1.756 + FREE_C_HEAP_ARRAY(char, filename); 1.757 + return NULL; 1.758 + } 1.759 + 1.760 + // save the file name for use in delete_shared_memory() 1.761 + backing_store_file_name = filename; 1.762 + 1.763 + // clear the shared memory region 1.764 + (void)::memset((void*) mapAddress, 0, size); 1.765 + 1.766 + return mapAddress; 1.767 +} 1.768 + 1.769 +// release a named shared memory region 1.770 +// 1.771 +static void unmap_shared(char* addr, size_t bytes) { 1.772 + os::release_memory(addr, bytes); 1.773 +} 1.774 + 1.775 +// create the PerfData memory region in shared memory. 1.776 +// 1.777 +static char* create_shared_memory(size_t size) { 1.778 + 1.779 + // create the shared memory region. 1.780 + return mmap_create_shared(size); 1.781 +} 1.782 + 1.783 +// delete the shared PerfData memory region 1.784 +// 1.785 +static void delete_shared_memory(char* addr, size_t size) { 1.786 + 1.787 + // cleanup the persistent shared memory resources. since DestroyJavaVM does 1.788 + // not support unloading of the JVM, unmapping of the memory resource is 1.789 + // not performed. The memory will be reclaimed by the OS upon termination of 1.790 + // the process. The backing store file is deleted from the file system. 1.791 + 1.792 + assert(!PerfDisableSharedMem, "shouldn't be here"); 1.793 + 1.794 + if (backing_store_file_name != NULL) { 1.795 + remove_file(backing_store_file_name); 1.796 + // Don't.. Free heap memory could deadlock os::abort() if it is called 1.797 + // from signal handler. OS will reclaim the heap memory. 1.798 + // FREE_C_HEAP_ARRAY(char, backing_store_file_name); 1.799 + backing_store_file_name = NULL; 1.800 + } 1.801 +} 1.802 + 1.803 +// return the size of the file for the given file descriptor 1.804 +// or 0 if it is not a valid size for a shared memory file 1.805 +// 1.806 +static size_t sharedmem_filesize(int fd, TRAPS) { 1.807 + 1.808 + struct stat statbuf; 1.809 + int result; 1.810 + 1.811 + RESTARTABLE(::fstat(fd, &statbuf), result); 1.812 + if (result == OS_ERR) { 1.813 + if (PrintMiscellaneous && Verbose) { 1.814 + warning("fstat failed: %s\n", strerror(errno)); 1.815 + } 1.816 + THROW_MSG_0(vmSymbols::java_io_IOException(), 1.817 + "Could not determine PerfMemory size"); 1.818 + } 1.819 + 1.820 + if ((statbuf.st_size == 0) || 1.821 + ((size_t)statbuf.st_size % os::vm_page_size() != 0)) { 1.822 + THROW_MSG_0(vmSymbols::java_lang_Exception(), 1.823 + "Invalid PerfMemory size"); 1.824 + } 1.825 + 1.826 + return (size_t)statbuf.st_size; 1.827 +} 1.828 + 1.829 +// attach to a named shared memory region. 1.830 +// 1.831 +static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) { 1.832 + 1.833 + char* mapAddress; 1.834 + int result; 1.835 + int fd; 1.836 + size_t size; 1.837 + const char* luser = NULL; 1.838 + 1.839 + int mmap_prot; 1.840 + int file_flags; 1.841 + 1.842 + ResourceMark rm; 1.843 + 1.844 + // map the high level access mode to the appropriate permission 1.845 + // constructs for the file and the shared memory mapping. 1.846 + if (mode == PerfMemory::PERF_MODE_RO) { 1.847 + mmap_prot = PROT_READ; 1.848 + file_flags = O_RDONLY; 1.849 + } 1.850 + else if (mode == PerfMemory::PERF_MODE_RW) { 1.851 +#ifdef LATER 1.852 + mmap_prot = PROT_READ | PROT_WRITE; 1.853 + file_flags = O_RDWR; 1.854 +#else 1.855 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.856 + "Unsupported access mode"); 1.857 +#endif 1.858 + } 1.859 + else { 1.860 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.861 + "Illegal access mode"); 1.862 + } 1.863 + 1.864 + if (user == NULL || strlen(user) == 0) { 1.865 + luser = get_user_name(vmid, CHECK); 1.866 + } 1.867 + else { 1.868 + luser = user; 1.869 + } 1.870 + 1.871 + if (luser == NULL) { 1.872 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.873 + "Could not map vmid to user Name"); 1.874 + } 1.875 + 1.876 + char* dirname = get_user_tmp_dir(luser); 1.877 + 1.878 + // since we don't follow symbolic links when creating the backing 1.879 + // store file, we don't follow them when attaching either. 1.880 + // 1.881 + if (!is_directory_secure(dirname)) { 1.882 + FREE_C_HEAP_ARRAY(char, dirname); 1.883 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.884 + "Process not found"); 1.885 + } 1.886 + 1.887 + char* filename = get_sharedmem_filename(dirname, vmid); 1.888 + 1.889 + // copy heap memory to resource memory. the open_sharedmem_file 1.890 + // method below need to use the filename, but could throw an 1.891 + // exception. using a resource array prevents the leak that 1.892 + // would otherwise occur. 1.893 + char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); 1.894 + strcpy(rfilename, filename); 1.895 + 1.896 + // free the c heap resources that are no longer needed 1.897 + if (luser != user) FREE_C_HEAP_ARRAY(char, luser); 1.898 + FREE_C_HEAP_ARRAY(char, dirname); 1.899 + FREE_C_HEAP_ARRAY(char, filename); 1.900 + 1.901 + // open the shared memory file for the give vmid 1.902 + fd = open_sharedmem_file(rfilename, file_flags, CHECK); 1.903 + assert(fd != OS_ERR, "unexpected value"); 1.904 + 1.905 + if (*sizep == 0) { 1.906 + size = sharedmem_filesize(fd, CHECK); 1.907 + assert(size != 0, "unexpected size"); 1.908 + } 1.909 + 1.910 + mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0); 1.911 + 1.912 + // attempt to close the file - restart if it gets interrupted, 1.913 + // but ignore other failures 1.914 + RESTARTABLE(::close(fd), result); 1.915 + assert(result != OS_ERR, "could not close file"); 1.916 + 1.917 + if (mapAddress == MAP_FAILED) { 1.918 + if (PrintMiscellaneous && Verbose) { 1.919 + warning("mmap failed: %s\n", strerror(errno)); 1.920 + } 1.921 + THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), 1.922 + "Could not map PerfMemory"); 1.923 + } 1.924 + 1.925 + *addr = mapAddress; 1.926 + *sizep = size; 1.927 + 1.928 + if (PerfTraceMemOps) { 1.929 + tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " 1.930 + INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress); 1.931 + } 1.932 +} 1.933 + 1.934 + 1.935 + 1.936 + 1.937 +// create the PerfData memory region 1.938 +// 1.939 +// This method creates the memory region used to store performance 1.940 +// data for the JVM. The memory may be created in standard or 1.941 +// shared memory. 1.942 +// 1.943 +void PerfMemory::create_memory_region(size_t size) { 1.944 + 1.945 + if (PerfDisableSharedMem) { 1.946 + // do not share the memory for the performance data. 1.947 + _start = create_standard_memory(size); 1.948 + } 1.949 + else { 1.950 + _start = create_shared_memory(size); 1.951 + if (_start == NULL) { 1.952 + 1.953 + // creation of the shared memory region failed, attempt 1.954 + // to create a contiguous, non-shared memory region instead. 1.955 + // 1.956 + if (PrintMiscellaneous && Verbose) { 1.957 + warning("Reverting to non-shared PerfMemory region.\n"); 1.958 + } 1.959 + PerfDisableSharedMem = true; 1.960 + _start = create_standard_memory(size); 1.961 + } 1.962 + } 1.963 + 1.964 + if (_start != NULL) _capacity = size; 1.965 + 1.966 +} 1.967 + 1.968 +// delete the PerfData memory region 1.969 +// 1.970 +// This method deletes the memory region used to store performance 1.971 +// data for the JVM. The memory region indicated by the <address, size> 1.972 +// tuple will be inaccessible after a call to this method. 1.973 +// 1.974 +void PerfMemory::delete_memory_region() { 1.975 + 1.976 + assert((start() != NULL && capacity() > 0), "verify proper state"); 1.977 + 1.978 + // If user specifies PerfDataSaveFile, it will save the performance data 1.979 + // to the specified file name no matter whether PerfDataSaveToFile is specified 1.980 + // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag 1.981 + // -XX:+PerfDataSaveToFile. 1.982 + if (PerfDataSaveToFile || PerfDataSaveFile != NULL) { 1.983 + save_memory_to_file(start(), capacity()); 1.984 + } 1.985 + 1.986 + if (PerfDisableSharedMem) { 1.987 + delete_standard_memory(start(), capacity()); 1.988 + } 1.989 + else { 1.990 + delete_shared_memory(start(), capacity()); 1.991 + } 1.992 +} 1.993 + 1.994 +// attach to the PerfData memory region for another JVM 1.995 +// 1.996 +// This method returns an <address, size> tuple that points to 1.997 +// a memory buffer that is kept reasonably synchronized with 1.998 +// the PerfData memory region for the indicated JVM. This 1.999 +// buffer may be kept in synchronization via shared memory 1.1000 +// or some other mechanism that keeps the buffer updated. 1.1001 +// 1.1002 +// If the JVM chooses not to support the attachability feature, 1.1003 +// this method should throw an UnsupportedOperation exception. 1.1004 +// 1.1005 +// This implementation utilizes named shared memory to map 1.1006 +// the indicated process's PerfData memory region into this JVMs 1.1007 +// address space. 1.1008 +// 1.1009 +void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) { 1.1010 + 1.1011 + if (vmid == 0 || vmid == os::current_process_id()) { 1.1012 + *addrp = start(); 1.1013 + *sizep = capacity(); 1.1014 + return; 1.1015 + } 1.1016 + 1.1017 + mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK); 1.1018 +} 1.1019 + 1.1020 +// detach from the PerfData memory region of another JVM 1.1021 +// 1.1022 +// This method detaches the PerfData memory region of another 1.1023 +// JVM, specified as an <address, size> tuple of a buffer 1.1024 +// in this process's address space. This method may perform 1.1025 +// arbitrary actions to accomplish the detachment. The memory 1.1026 +// region specified by <address, size> will be inaccessible after 1.1027 +// a call to this method. 1.1028 +// 1.1029 +// If the JVM chooses not to support the attachability feature, 1.1030 +// this method should throw an UnsupportedOperation exception. 1.1031 +// 1.1032 +// This implementation utilizes named shared memory to detach 1.1033 +// the indicated process's PerfData memory region from this 1.1034 +// process's address space. 1.1035 +// 1.1036 +void PerfMemory::detach(char* addr, size_t bytes, TRAPS) { 1.1037 + 1.1038 + assert(addr != 0, "address sanity check"); 1.1039 + assert(bytes > 0, "capacity sanity check"); 1.1040 + 1.1041 + if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) { 1.1042 + // prevent accidental detachment of this process's PerfMemory region 1.1043 + return; 1.1044 + } 1.1045 + 1.1046 + unmap_shared(addr, bytes); 1.1047 +} 1.1048 + 1.1049 +char* PerfMemory::backing_store_filename() { 1.1050 + return backing_store_file_name; 1.1051 +}