src/os/aix/vm/perfMemory_aix.cpp

Tue, 13 Jan 2015 16:09:52 +0100

author
goetz
date
Tue, 13 Jan 2015 16:09:52 +0100
changeset 7515
b9c06f87e476
parent 0
f90c822e73f8
child 8177
9f8038f83a6e
permissions
-rw-r--r--

8069590: AIX port of "8050807: Better performing performance data handling"
Reviewed-by: simonis, goetz
Contributed-by: matthias.baesken@sap.com, martin.doerr@sap.com

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * Copyright 2012, 2013 SAP AG. All rights reserved.
aoqi@0 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 5 *
aoqi@0 6 * This code is free software; you can redistribute it and/or modify it
aoqi@0 7 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 8 * published by the Free Software Foundation.
aoqi@0 9 *
aoqi@0 10 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 13 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 14 * accompanied this code).
aoqi@0 15 *
aoqi@0 16 * You should have received a copy of the GNU General Public License version
aoqi@0 17 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 19 *
aoqi@0 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 21 * or visit www.oracle.com if you need additional information or have any
aoqi@0 22 * questions.
aoqi@0 23 *
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 #include "precompiled.hpp"
aoqi@0 27 #include "classfile/vmSymbols.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "memory/resourceArea.hpp"
aoqi@0 30 #include "oops/oop.inline.hpp"
aoqi@0 31 #include "os_aix.inline.hpp"
aoqi@0 32 #include "runtime/handles.inline.hpp"
aoqi@0 33 #include "runtime/perfMemory.hpp"
goetz@7515 34 #include "services/memTracker.hpp"
aoqi@0 35 #include "utilities/exceptions.hpp"
aoqi@0 36
aoqi@0 37 // put OS-includes here
aoqi@0 38 # include <sys/types.h>
aoqi@0 39 # include <sys/mman.h>
aoqi@0 40 # include <errno.h>
aoqi@0 41 # include <stdio.h>
aoqi@0 42 # include <unistd.h>
aoqi@0 43 # include <sys/stat.h>
aoqi@0 44 # include <signal.h>
aoqi@0 45 # include <pwd.h>
aoqi@0 46
aoqi@0 47 static char* backing_store_file_name = NULL; // name of the backing store
aoqi@0 48 // file, if successfully created.
aoqi@0 49
aoqi@0 50 // Standard Memory Implementation Details
aoqi@0 51
aoqi@0 52 // create the PerfData memory region in standard memory.
aoqi@0 53 //
aoqi@0 54 static char* create_standard_memory(size_t size) {
aoqi@0 55
aoqi@0 56 // allocate an aligned chuck of memory
aoqi@0 57 char* mapAddress = os::reserve_memory(size);
aoqi@0 58
aoqi@0 59 if (mapAddress == NULL) {
aoqi@0 60 return NULL;
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 // commit memory
aoqi@0 64 if (!os::commit_memory(mapAddress, size, !ExecMem)) {
aoqi@0 65 if (PrintMiscellaneous && Verbose) {
aoqi@0 66 warning("Could not commit PerfData memory\n");
aoqi@0 67 }
aoqi@0 68 os::release_memory(mapAddress, size);
aoqi@0 69 return NULL;
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 return mapAddress;
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 // delete the PerfData memory region
aoqi@0 76 //
aoqi@0 77 static void delete_standard_memory(char* addr, size_t size) {
aoqi@0 78
aoqi@0 79 // there are no persistent external resources to cleanup for standard
aoqi@0 80 // memory. since DestroyJavaVM does not support unloading of the JVM,
aoqi@0 81 // cleanup of the memory resource is not performed. The memory will be
aoqi@0 82 // reclaimed by the OS upon termination of the process.
aoqi@0 83 //
aoqi@0 84 return;
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 // save the specified memory region to the given file
aoqi@0 88 //
aoqi@0 89 // Note: this function might be called from signal handler (by os::abort()),
aoqi@0 90 // don't allocate heap memory.
aoqi@0 91 //
aoqi@0 92 static void save_memory_to_file(char* addr, size_t size) {
aoqi@0 93
aoqi@0 94 const char* destfile = PerfMemory::get_perfdata_file_path();
aoqi@0 95 assert(destfile[0] != '\0', "invalid PerfData file path");
aoqi@0 96
aoqi@0 97 int result;
aoqi@0 98
aoqi@0 99 RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
aoqi@0 100 result);;
aoqi@0 101 if (result == OS_ERR) {
aoqi@0 102 if (PrintMiscellaneous && Verbose) {
aoqi@0 103 warning("Could not create Perfdata save file: %s: %s\n",
aoqi@0 104 destfile, strerror(errno));
aoqi@0 105 }
aoqi@0 106 } else {
aoqi@0 107 int fd = result;
aoqi@0 108
aoqi@0 109 for (size_t remaining = size; remaining > 0;) {
aoqi@0 110
aoqi@0 111 RESTARTABLE(::write(fd, addr, remaining), result);
aoqi@0 112 if (result == OS_ERR) {
aoqi@0 113 if (PrintMiscellaneous && Verbose) {
aoqi@0 114 warning("Could not write Perfdata save file: %s: %s\n",
aoqi@0 115 destfile, strerror(errno));
aoqi@0 116 }
aoqi@0 117 break;
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 remaining -= (size_t)result;
aoqi@0 121 addr += result;
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 RESTARTABLE(::close(fd), result);
aoqi@0 125 if (PrintMiscellaneous && Verbose) {
aoqi@0 126 if (result == OS_ERR) {
aoqi@0 127 warning("Could not close %s: %s\n", destfile, strerror(errno));
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131 FREE_C_HEAP_ARRAY(char, destfile, mtInternal);
aoqi@0 132 }
aoqi@0 133
aoqi@0 134
aoqi@0 135 // Shared Memory Implementation Details
aoqi@0 136
aoqi@0 137 // Note: the solaris and linux shared memory implementation uses the mmap
aoqi@0 138 // interface with a backing store file to implement named shared memory.
aoqi@0 139 // Using the file system as the name space for shared memory allows a
aoqi@0 140 // common name space to be supported across a variety of platforms. It
aoqi@0 141 // also provides a name space that Java applications can deal with through
aoqi@0 142 // simple file apis.
aoqi@0 143 //
aoqi@0 144 // The solaris and linux implementations store the backing store file in
aoqi@0 145 // a user specific temporary directory located in the /tmp file system,
aoqi@0 146 // which is always a local file system and is sometimes a RAM based file
aoqi@0 147 // system.
aoqi@0 148
aoqi@0 149 // return the user specific temporary directory name.
aoqi@0 150 //
aoqi@0 151 // the caller is expected to free the allocated memory.
aoqi@0 152 //
aoqi@0 153 static char* get_user_tmp_dir(const char* user) {
aoqi@0 154
aoqi@0 155 const char* tmpdir = os::get_temp_directory();
aoqi@0 156 const char* perfdir = PERFDATA_NAME;
aoqi@0 157 size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
aoqi@0 158 char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
aoqi@0 159
aoqi@0 160 // construct the path name to user specific tmp directory
aoqi@0 161 snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
aoqi@0 162
aoqi@0 163 return dirname;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 // convert the given file name into a process id. if the file
aoqi@0 167 // does not meet the file naming constraints, return 0.
aoqi@0 168 //
aoqi@0 169 static pid_t filename_to_pid(const char* filename) {
aoqi@0 170
aoqi@0 171 // a filename that doesn't begin with a digit is not a
aoqi@0 172 // candidate for conversion.
aoqi@0 173 //
aoqi@0 174 if (!isdigit(*filename)) {
aoqi@0 175 return 0;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 // check if file name can be converted to an integer without
aoqi@0 179 // any leftover characters.
aoqi@0 180 //
aoqi@0 181 char* remainder = NULL;
aoqi@0 182 errno = 0;
aoqi@0 183 pid_t pid = (pid_t)strtol(filename, &remainder, 10);
aoqi@0 184
aoqi@0 185 if (errno != 0) {
aoqi@0 186 return 0;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 // check for left over characters. If any, then the filename is
aoqi@0 190 // not a candidate for conversion.
aoqi@0 191 //
aoqi@0 192 if (remainder != NULL && *remainder != '\0') {
aoqi@0 193 return 0;
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 // successful conversion, return the pid
aoqi@0 197 return pid;
aoqi@0 198 }
aoqi@0 199
goetz@7515 200 // Check if the given statbuf is considered a secure directory for
goetz@7515 201 // the backing store files. Returns true if the directory is considered
goetz@7515 202 // a secure location. Returns false if the statbuf is a symbolic link or
goetz@7515 203 // if an error occurred.
goetz@7515 204 static bool is_statbuf_secure(struct stat *statp) {
goetz@7515 205 if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) {
goetz@7515 206 // The path represents a link or some non-directory file type,
goetz@7515 207 // which is not what we expected. Declare it insecure.
goetz@7515 208 //
goetz@7515 209 return false;
goetz@7515 210 }
goetz@7515 211 // We have an existing directory, check if the permissions are safe.
goetz@7515 212 if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) {
goetz@7515 213 // The directory is open for writing and could be subjected
goetz@7515 214 // to a symlink or a hard link attack. Declare it insecure.
goetz@7515 215 return false;
goetz@7515 216 }
goetz@7515 217 // See if the uid of the directory matches the effective uid of the process.
goetz@7515 218 //
goetz@7515 219 if (statp->st_uid != geteuid()) {
goetz@7515 220 // The directory was not created by this user, declare it insecure.
goetz@7515 221 return false;
goetz@7515 222 }
goetz@7515 223 return true;
goetz@7515 224 }
aoqi@0 225
goetz@7515 226
goetz@7515 227 // Check if the given path is considered a secure directory for
aoqi@0 228 // the backing store files. Returns true if the directory exists
aoqi@0 229 // and is considered a secure location. Returns false if the path
aoqi@0 230 // is a symbolic link or if an error occurred.
aoqi@0 231 static bool is_directory_secure(const char* path) {
aoqi@0 232 struct stat statbuf;
aoqi@0 233 int result = 0;
aoqi@0 234
aoqi@0 235 RESTARTABLE(::lstat(path, &statbuf), result);
aoqi@0 236 if (result == OS_ERR) {
aoqi@0 237 return false;
aoqi@0 238 }
aoqi@0 239
goetz@7515 240 // The path exists, see if it is secure.
goetz@7515 241 return is_statbuf_secure(&statbuf);
goetz@7515 242 }
goetz@7515 243
goetz@7515 244 // (Taken over from Solaris to support the O_NOFOLLOW case on AIX.)
goetz@7515 245 // Check if the given directory file descriptor is considered a secure
goetz@7515 246 // directory for the backing store files. Returns true if the directory
goetz@7515 247 // exists and is considered a secure location. Returns false if the path
goetz@7515 248 // is a symbolic link or if an error occurred.
goetz@7515 249 static bool is_dirfd_secure(int dir_fd) {
goetz@7515 250 struct stat statbuf;
goetz@7515 251 int result = 0;
goetz@7515 252
goetz@7515 253 RESTARTABLE(::fstat(dir_fd, &statbuf), result);
goetz@7515 254 if (result == OS_ERR) {
aoqi@0 255 return false;
aoqi@0 256 }
goetz@7515 257
goetz@7515 258 // The path exists, now check its mode.
goetz@7515 259 return is_statbuf_secure(&statbuf);
goetz@7515 260 }
goetz@7515 261
goetz@7515 262
goetz@7515 263 // Check to make sure fd1 and fd2 are referencing the same file system object.
goetz@7515 264 static bool is_same_fsobject(int fd1, int fd2) {
goetz@7515 265 struct stat statbuf1;
goetz@7515 266 struct stat statbuf2;
goetz@7515 267 int result = 0;
goetz@7515 268
goetz@7515 269 RESTARTABLE(::fstat(fd1, &statbuf1), result);
goetz@7515 270 if (result == OS_ERR) {
goetz@7515 271 return false;
goetz@7515 272 }
goetz@7515 273 RESTARTABLE(::fstat(fd2, &statbuf2), result);
goetz@7515 274 if (result == OS_ERR) {
goetz@7515 275 return false;
goetz@7515 276 }
goetz@7515 277
goetz@7515 278 if ((statbuf1.st_ino == statbuf2.st_ino) &&
goetz@7515 279 (statbuf1.st_dev == statbuf2.st_dev)) {
goetz@7515 280 return true;
goetz@7515 281 } else {
goetz@7515 282 return false;
goetz@7515 283 }
goetz@7515 284 }
goetz@7515 285
goetz@7515 286 // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1.
goetz@7515 287 // We use the jdk6 implementation here.
goetz@7515 288 #ifndef O_NOFOLLOW
goetz@7515 289 // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour
goetz@7515 290 // was done in jdk 5/6 hotspot by Oracle this way
goetz@7515 291 static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) {
goetz@7515 292 struct stat orig_st;
goetz@7515 293 struct stat new_st;
goetz@7515 294 bool create;
goetz@7515 295 int error;
goetz@7515 296 int fd;
goetz@7515 297
goetz@7515 298 create = false;
goetz@7515 299
goetz@7515 300 if (lstat(path, &orig_st) != 0) {
goetz@7515 301 if (errno == ENOENT && (oflag & O_CREAT) != 0) {
goetz@7515 302 // File doesn't exist, but_we want to create it, add O_EXCL flag
goetz@7515 303 // to make sure no-one creates it (or a symlink) before us
goetz@7515 304 // This works as we expect with symlinks, from posix man page:
goetz@7515 305 // 'If O_EXCL and O_CREAT are set, and path names a symbolic
goetz@7515 306 // link, open() shall fail and set errno to [EEXIST]'.
goetz@7515 307 oflag |= O_EXCL;
goetz@7515 308 create = true;
goetz@7515 309 } else {
goetz@7515 310 // File doesn't exist, and we are not creating it.
goetz@7515 311 return OS_ERR;
aoqi@0 312 }
goetz@7515 313 } else {
goetz@7515 314 // Lstat success, check if existing file is a link.
goetz@7515 315 if ((orig_st.st_mode & S_IFMT) == S_IFLNK) {
goetz@7515 316 // File is a symlink.
goetz@7515 317 errno = ELOOP;
goetz@7515 318 return OS_ERR;
goetz@7515 319 }
goetz@7515 320 }
goetz@7515 321
goetz@7515 322 if (use_mode == true) {
goetz@7515 323 fd = open(path, oflag, mode);
goetz@7515 324 } else {
goetz@7515 325 fd = open(path, oflag);
goetz@7515 326 }
goetz@7515 327
goetz@7515 328 if (fd == OS_ERR) {
goetz@7515 329 return fd;
goetz@7515 330 }
goetz@7515 331
goetz@7515 332 // Can't do inode checks on before/after if we created the file.
goetz@7515 333 if (create == false) {
goetz@7515 334 if (fstat(fd, &new_st) != 0) {
goetz@7515 335 // Keep errno from fstat, in case close also fails.
goetz@7515 336 error = errno;
goetz@7515 337 ::close(fd);
goetz@7515 338 errno = error;
goetz@7515 339 return OS_ERR;
goetz@7515 340 }
goetz@7515 341
goetz@7515 342 if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) {
goetz@7515 343 // File was tampered with during race window.
goetz@7515 344 ::close(fd);
goetz@7515 345 errno = EEXIST;
goetz@7515 346 if (PrintMiscellaneous && Verbose) {
goetz@7515 347 warning("possible file tampering attempt detected when opening %s", path);
goetz@7515 348 }
goetz@7515 349 return OS_ERR;
goetz@7515 350 }
goetz@7515 351 }
goetz@7515 352
goetz@7515 353 return fd;
goetz@7515 354 }
goetz@7515 355
goetz@7515 356 static int open_o_nofollow(const char* path, int oflag, mode_t mode) {
goetz@7515 357 return open_o_nofollow_impl(path, oflag, mode, true);
goetz@7515 358 }
goetz@7515 359
goetz@7515 360 static int open_o_nofollow(const char* path, int oflag) {
goetz@7515 361 return open_o_nofollow_impl(path, oflag, 0, false);
goetz@7515 362 }
goetz@7515 363 #endif
goetz@7515 364
goetz@7515 365 // Open the directory of the given path and validate it.
goetz@7515 366 // Return a DIR * of the open directory.
goetz@7515 367 static DIR *open_directory_secure(const char* dirname) {
goetz@7515 368 // Open the directory using open() so that it can be verified
goetz@7515 369 // to be secure by calling is_dirfd_secure(), opendir() and then check
goetz@7515 370 // to see if they are the same file system object. This method does not
goetz@7515 371 // introduce a window of opportunity for the directory to be attacked that
goetz@7515 372 // calling opendir() and is_directory_secure() does.
goetz@7515 373 int result;
goetz@7515 374 DIR *dirp = NULL;
goetz@7515 375
goetz@7515 376 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
goetz@7515 377 // so provide a workaround in this case.
goetz@7515 378 #ifdef O_NOFOLLOW
goetz@7515 379 RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
goetz@7515 380 #else
goetz@7515 381 // workaround (jdk6 coding)
goetz@7515 382 RESTARTABLE(::open_o_nofollow(dirname, O_RDONLY), result);
goetz@7515 383 #endif
goetz@7515 384
goetz@7515 385 if (result == OS_ERR) {
goetz@7515 386 // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
goetz@7515 387 if (PrintMiscellaneous && Verbose) {
goetz@7515 388 if (errno == ELOOP) {
goetz@7515 389 warning("directory %s is a symlink and is not secure\n", dirname);
goetz@7515 390 } else {
goetz@7515 391 warning("could not open directory %s: %s\n", dirname, strerror(errno));
goetz@7515 392 }
goetz@7515 393 }
goetz@7515 394 return dirp;
goetz@7515 395 }
goetz@7515 396 int fd = result;
goetz@7515 397
goetz@7515 398 // Determine if the open directory is secure.
goetz@7515 399 if (!is_dirfd_secure(fd)) {
goetz@7515 400 // The directory is not a secure directory.
goetz@7515 401 os::close(fd);
goetz@7515 402 return dirp;
goetz@7515 403 }
goetz@7515 404
goetz@7515 405 // Open the directory.
goetz@7515 406 dirp = ::opendir(dirname);
goetz@7515 407 if (dirp == NULL) {
goetz@7515 408 // The directory doesn't exist, close fd and return.
goetz@7515 409 os::close(fd);
goetz@7515 410 return dirp;
goetz@7515 411 }
goetz@7515 412
goetz@7515 413 // Check to make sure fd and dirp are referencing the same file system object.
goetz@7515 414 if (!is_same_fsobject(fd, dirp->dd_fd)) {
goetz@7515 415 // The directory is not secure.
goetz@7515 416 os::close(fd);
goetz@7515 417 os::closedir(dirp);
goetz@7515 418 dirp = NULL;
goetz@7515 419 return dirp;
goetz@7515 420 }
goetz@7515 421
goetz@7515 422 // Close initial open now that we know directory is secure
goetz@7515 423 os::close(fd);
goetz@7515 424
goetz@7515 425 return dirp;
goetz@7515 426 }
goetz@7515 427
goetz@7515 428 // NOTE: The code below uses fchdir(), open() and unlink() because
goetz@7515 429 // fdopendir(), openat() and unlinkat() are not supported on all
goetz@7515 430 // versions. Once the support for fdopendir(), openat() and unlinkat()
goetz@7515 431 // is available on all supported versions the code can be changed
goetz@7515 432 // to use these functions.
goetz@7515 433
goetz@7515 434 // Open the directory of the given path, validate it and set the
goetz@7515 435 // current working directory to it.
goetz@7515 436 // Return a DIR * of the open directory and the saved cwd fd.
goetz@7515 437 //
goetz@7515 438 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
goetz@7515 439
goetz@7515 440 // Open the directory.
goetz@7515 441 DIR* dirp = open_directory_secure(dirname);
goetz@7515 442 if (dirp == NULL) {
goetz@7515 443 // Directory doesn't exist or is insecure, so there is nothing to cleanup.
goetz@7515 444 return dirp;
goetz@7515 445 }
goetz@7515 446 int fd = dirp->dd_fd;
goetz@7515 447
goetz@7515 448 // Open a fd to the cwd and save it off.
goetz@7515 449 int result;
goetz@7515 450 RESTARTABLE(::open(".", O_RDONLY), result);
goetz@7515 451 if (result == OS_ERR) {
goetz@7515 452 *saved_cwd_fd = -1;
goetz@7515 453 } else {
goetz@7515 454 *saved_cwd_fd = result;
goetz@7515 455 }
goetz@7515 456
goetz@7515 457 // Set the current directory to dirname by using the fd of the directory.
goetz@7515 458 result = fchdir(fd);
goetz@7515 459
goetz@7515 460 return dirp;
goetz@7515 461 }
goetz@7515 462
goetz@7515 463 // Close the directory and restore the current working directory.
goetz@7515 464 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
goetz@7515 465
goetz@7515 466 int result;
goetz@7515 467 // If we have a saved cwd change back to it and close the fd.
goetz@7515 468 if (saved_cwd_fd != -1) {
goetz@7515 469 result = fchdir(saved_cwd_fd);
goetz@7515 470 ::close(saved_cwd_fd);
goetz@7515 471 }
goetz@7515 472
goetz@7515 473 // Close the directory.
goetz@7515 474 os::closedir(dirp);
goetz@7515 475 }
goetz@7515 476
goetz@7515 477 // Check if the given file descriptor is considered a secure.
goetz@7515 478 static bool is_file_secure(int fd, const char *filename) {
goetz@7515 479
goetz@7515 480 int result;
goetz@7515 481 struct stat statbuf;
goetz@7515 482
goetz@7515 483 // Determine if the file is secure.
goetz@7515 484 RESTARTABLE(::fstat(fd, &statbuf), result);
goetz@7515 485 if (result == OS_ERR) {
goetz@7515 486 if (PrintMiscellaneous && Verbose) {
goetz@7515 487 warning("fstat failed on %s: %s\n", filename, strerror(errno));
goetz@7515 488 }
goetz@7515 489 return false;
goetz@7515 490 }
goetz@7515 491 if (statbuf.st_nlink > 1) {
goetz@7515 492 // A file with multiple links is not expected.
goetz@7515 493 if (PrintMiscellaneous && Verbose) {
goetz@7515 494 warning("file %s has multiple links\n", filename);
goetz@7515 495 }
goetz@7515 496 return false;
aoqi@0 497 }
aoqi@0 498 return true;
aoqi@0 499 }
aoqi@0 500
goetz@7515 501 // Return the user name for the given user id.
aoqi@0 502 //
goetz@7515 503 // The caller is expected to free the allocated memory.
aoqi@0 504 static char* get_user_name(uid_t uid) {
aoqi@0 505
aoqi@0 506 struct passwd pwent;
aoqi@0 507
goetz@7515 508 // Determine the max pwbuf size from sysconf, and hardcode
aoqi@0 509 // a default if this not available through sysconf.
aoqi@0 510 long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
aoqi@0 511 if (bufsize == -1)
aoqi@0 512 bufsize = 1024;
aoqi@0 513
aoqi@0 514 char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
aoqi@0 515
aoqi@0 516 // POSIX interface to getpwuid_r is used on LINUX
aoqi@0 517 struct passwd* p;
aoqi@0 518 int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
aoqi@0 519
aoqi@0 520 if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
aoqi@0 521 if (PrintMiscellaneous && Verbose) {
aoqi@0 522 if (result != 0) {
aoqi@0 523 warning("Could not retrieve passwd entry: %s\n",
aoqi@0 524 strerror(result));
aoqi@0 525 }
aoqi@0 526 else if (p == NULL) {
aoqi@0 527 // this check is added to protect against an observed problem
aoqi@0 528 // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
aoqi@0 529 // indicating success, but has p == NULL. This was observed when
aoqi@0 530 // inserting a file descriptor exhaustion fault prior to the call
aoqi@0 531 // getpwuid_r() call. In this case, error is set to the appropriate
aoqi@0 532 // error condition, but this is undocumented behavior. This check
aoqi@0 533 // is safe under any condition, but the use of errno in the output
aoqi@0 534 // message may result in an erroneous message.
aoqi@0 535 // Bug Id 89052 was opened with RedHat.
aoqi@0 536 //
aoqi@0 537 warning("Could not retrieve passwd entry: %s\n",
aoqi@0 538 strerror(errno));
aoqi@0 539 }
aoqi@0 540 else {
aoqi@0 541 warning("Could not determine user name: %s\n",
aoqi@0 542 p->pw_name == NULL ? "pw_name = NULL" :
aoqi@0 543 "pw_name zero length");
aoqi@0 544 }
aoqi@0 545 }
aoqi@0 546 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
aoqi@0 547 return NULL;
aoqi@0 548 }
aoqi@0 549
aoqi@0 550 char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
aoqi@0 551 strcpy(user_name, p->pw_name);
aoqi@0 552
aoqi@0 553 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
aoqi@0 554 return user_name;
aoqi@0 555 }
aoqi@0 556
aoqi@0 557 // return the name of the user that owns the process identified by vmid.
aoqi@0 558 //
aoqi@0 559 // This method uses a slow directory search algorithm to find the backing
aoqi@0 560 // store file for the specified vmid and returns the user name, as determined
aoqi@0 561 // by the user name suffix of the hsperfdata_<username> directory name.
aoqi@0 562 //
aoqi@0 563 // the caller is expected to free the allocated memory.
aoqi@0 564 //
aoqi@0 565 static char* get_user_name_slow(int vmid, TRAPS) {
aoqi@0 566
aoqi@0 567 // short circuit the directory search if the process doesn't even exist.
aoqi@0 568 if (kill(vmid, 0) == OS_ERR) {
aoqi@0 569 if (errno == ESRCH) {
aoqi@0 570 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 571 "Process not found");
aoqi@0 572 }
aoqi@0 573 else /* EPERM */ {
aoqi@0 574 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
aoqi@0 575 }
aoqi@0 576 }
aoqi@0 577
aoqi@0 578 // directory search
aoqi@0 579 char* oldest_user = NULL;
aoqi@0 580 time_t oldest_ctime = 0;
aoqi@0 581
aoqi@0 582 const char* tmpdirname = os::get_temp_directory();
aoqi@0 583
aoqi@0 584 DIR* tmpdirp = os::opendir(tmpdirname);
aoqi@0 585
aoqi@0 586 if (tmpdirp == NULL) {
aoqi@0 587 return NULL;
aoqi@0 588 }
aoqi@0 589
aoqi@0 590 // for each entry in the directory that matches the pattern hsperfdata_*,
aoqi@0 591 // open the directory and check if the file for the given vmid exists.
aoqi@0 592 // The file with the expected name and the latest creation date is used
aoqi@0 593 // to determine the user name for the process id.
aoqi@0 594 //
aoqi@0 595 struct dirent* dentry;
aoqi@0 596 char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
aoqi@0 597 errno = 0;
aoqi@0 598 while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
aoqi@0 599
aoqi@0 600 // check if the directory entry is a hsperfdata file
aoqi@0 601 if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
aoqi@0 602 continue;
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 char* usrdir_name = NEW_C_HEAP_ARRAY(char,
aoqi@0 606 strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
aoqi@0 607 strcpy(usrdir_name, tmpdirname);
aoqi@0 608 strcat(usrdir_name, "/");
aoqi@0 609 strcat(usrdir_name, dentry->d_name);
aoqi@0 610
goetz@7515 611 // Open the user directory.
goetz@7515 612 DIR* subdirp = open_directory_secure(usrdir_name);
aoqi@0 613
aoqi@0 614 if (subdirp == NULL) {
aoqi@0 615 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
aoqi@0 616 continue;
aoqi@0 617 }
aoqi@0 618
aoqi@0 619 // Since we don't create the backing store files in directories
aoqi@0 620 // pointed to by symbolic links, we also don't follow them when
aoqi@0 621 // looking for the files. We check for a symbolic link after the
aoqi@0 622 // call to opendir in order to eliminate a small window where the
aoqi@0 623 // symlink can be exploited.
aoqi@0 624 //
aoqi@0 625 if (!is_directory_secure(usrdir_name)) {
aoqi@0 626 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
aoqi@0 627 os::closedir(subdirp);
aoqi@0 628 continue;
aoqi@0 629 }
aoqi@0 630
aoqi@0 631 struct dirent* udentry;
aoqi@0 632 char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
aoqi@0 633 errno = 0;
aoqi@0 634 while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
aoqi@0 635
aoqi@0 636 if (filename_to_pid(udentry->d_name) == vmid) {
aoqi@0 637 struct stat statbuf;
aoqi@0 638 int result;
aoqi@0 639
aoqi@0 640 char* filename = NEW_C_HEAP_ARRAY(char,
aoqi@0 641 strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
aoqi@0 642
aoqi@0 643 strcpy(filename, usrdir_name);
aoqi@0 644 strcat(filename, "/");
aoqi@0 645 strcat(filename, udentry->d_name);
aoqi@0 646
aoqi@0 647 // don't follow symbolic links for the file
aoqi@0 648 RESTARTABLE(::lstat(filename, &statbuf), result);
aoqi@0 649 if (result == OS_ERR) {
aoqi@0 650 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 651 continue;
aoqi@0 652 }
aoqi@0 653
aoqi@0 654 // skip over files that are not regular files.
aoqi@0 655 if (!S_ISREG(statbuf.st_mode)) {
aoqi@0 656 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 657 continue;
aoqi@0 658 }
aoqi@0 659
aoqi@0 660 // compare and save filename with latest creation time
aoqi@0 661 if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
aoqi@0 662
aoqi@0 663 if (statbuf.st_ctime > oldest_ctime) {
aoqi@0 664 char* user = strchr(dentry->d_name, '_') + 1;
aoqi@0 665
aoqi@0 666 if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
aoqi@0 667 oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
aoqi@0 668
aoqi@0 669 strcpy(oldest_user, user);
aoqi@0 670 oldest_ctime = statbuf.st_ctime;
aoqi@0 671 }
aoqi@0 672 }
aoqi@0 673
aoqi@0 674 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 675 }
aoqi@0 676 }
aoqi@0 677 os::closedir(subdirp);
aoqi@0 678 FREE_C_HEAP_ARRAY(char, udbuf, mtInternal);
aoqi@0 679 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
aoqi@0 680 }
aoqi@0 681 os::closedir(tmpdirp);
aoqi@0 682 FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal);
aoqi@0 683
aoqi@0 684 return(oldest_user);
aoqi@0 685 }
aoqi@0 686
aoqi@0 687 // return the name of the user that owns the JVM indicated by the given vmid.
aoqi@0 688 //
aoqi@0 689 static char* get_user_name(int vmid, TRAPS) {
aoqi@0 690 return get_user_name_slow(vmid, CHECK_NULL);
aoqi@0 691 }
aoqi@0 692
aoqi@0 693 // return the file name of the backing store file for the named
aoqi@0 694 // shared memory region for the given user name and vmid.
aoqi@0 695 //
aoqi@0 696 // the caller is expected to free the allocated memory.
aoqi@0 697 //
aoqi@0 698 static char* get_sharedmem_filename(const char* dirname, int vmid) {
aoqi@0 699
aoqi@0 700 // add 2 for the file separator and a null terminator.
aoqi@0 701 size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
aoqi@0 702
aoqi@0 703 char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
aoqi@0 704 snprintf(name, nbytes, "%s/%d", dirname, vmid);
aoqi@0 705
aoqi@0 706 return name;
aoqi@0 707 }
aoqi@0 708
aoqi@0 709
aoqi@0 710 // remove file
aoqi@0 711 //
aoqi@0 712 // this method removes the file specified by the given path
aoqi@0 713 //
aoqi@0 714 static void remove_file(const char* path) {
aoqi@0 715
aoqi@0 716 int result;
aoqi@0 717
aoqi@0 718 // if the file is a directory, the following unlink will fail. since
aoqi@0 719 // we don't expect to find directories in the user temp directory, we
aoqi@0 720 // won't try to handle this situation. even if accidentially or
aoqi@0 721 // maliciously planted, the directory's presence won't hurt anything.
aoqi@0 722 //
aoqi@0 723 RESTARTABLE(::unlink(path), result);
aoqi@0 724 if (PrintMiscellaneous && Verbose && result == OS_ERR) {
aoqi@0 725 if (errno != ENOENT) {
aoqi@0 726 warning("Could not unlink shared memory backing"
aoqi@0 727 " store file %s : %s\n", path, strerror(errno));
aoqi@0 728 }
aoqi@0 729 }
aoqi@0 730 }
aoqi@0 731
goetz@7515 732 // Cleanup stale shared memory resources
aoqi@0 733 //
aoqi@0 734 // This method attempts to remove all stale shared memory files in
aoqi@0 735 // the named user temporary directory. It scans the named directory
aoqi@0 736 // for files matching the pattern ^$[0-9]*$. For each file found, the
aoqi@0 737 // process id is extracted from the file name and a test is run to
aoqi@0 738 // determine if the process is alive. If the process is not alive,
aoqi@0 739 // any stale file resources are removed.
aoqi@0 740 static void cleanup_sharedmem_resources(const char* dirname) {
aoqi@0 741
goetz@7515 742 int saved_cwd_fd;
goetz@7515 743 // Open the directory.
goetz@7515 744 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
aoqi@0 745 if (dirp == NULL) {
goetz@7515 746 // Directory doesn't exist or is insecure, so there is nothing to cleanup.
aoqi@0 747 return;
aoqi@0 748 }
aoqi@0 749
goetz@7515 750 // For each entry in the directory that matches the expected file
aoqi@0 751 // name pattern, determine if the file resources are stale and if
aoqi@0 752 // so, remove the file resources. Note, instrumented HotSpot processes
aoqi@0 753 // for this user may start and/or terminate during this search and
aoqi@0 754 // remove or create new files in this directory. The behavior of this
aoqi@0 755 // loop under these conditions is dependent upon the implementation of
aoqi@0 756 // opendir/readdir.
aoqi@0 757 struct dirent* entry;
aoqi@0 758 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
goetz@7515 759
aoqi@0 760 errno = 0;
aoqi@0 761 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
aoqi@0 762
aoqi@0 763 pid_t pid = filename_to_pid(entry->d_name);
aoqi@0 764
aoqi@0 765 if (pid == 0) {
aoqi@0 766
aoqi@0 767 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
aoqi@0 768
goetz@7515 769 // Attempt to remove all unexpected files, except "." and "..".
goetz@7515 770 unlink(entry->d_name);
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 errno = 0;
aoqi@0 774 continue;
aoqi@0 775 }
aoqi@0 776
goetz@7515 777 // We now have a file name that converts to a valid integer
aoqi@0 778 // that could represent a process id . if this process id
aoqi@0 779 // matches the current process id or the process is not running,
aoqi@0 780 // then remove the stale file resources.
aoqi@0 781 //
goetz@7515 782 // Process liveness is detected by sending signal number 0 to
aoqi@0 783 // the process id (see kill(2)). if kill determines that the
aoqi@0 784 // process does not exist, then the file resources are removed.
aoqi@0 785 // if kill determines that that we don't have permission to
aoqi@0 786 // signal the process, then the file resources are assumed to
aoqi@0 787 // be stale and are removed because the resources for such a
aoqi@0 788 // process should be in a different user specific directory.
aoqi@0 789 if ((pid == os::current_process_id()) ||
aoqi@0 790 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
aoqi@0 791
goetz@7515 792 unlink(entry->d_name);
aoqi@0 793 }
aoqi@0 794 errno = 0;
aoqi@0 795 }
goetz@7515 796
goetz@7515 797 // Close the directory and reset the current working directory.
goetz@7515 798 close_directory_secure_cwd(dirp, saved_cwd_fd);
goetz@7515 799
aoqi@0 800 FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
aoqi@0 801 }
aoqi@0 802
goetz@7515 803 // Make the user specific temporary directory. Returns true if
aoqi@0 804 // the directory exists and is secure upon return. Returns false
aoqi@0 805 // if the directory exists but is either a symlink, is otherwise
aoqi@0 806 // insecure, or if an error occurred.
aoqi@0 807 static bool make_user_tmp_dir(const char* dirname) {
aoqi@0 808
goetz@7515 809 // Create the directory with 0755 permissions. note that the directory
aoqi@0 810 // will be owned by euid::egid, which may not be the same as uid::gid.
aoqi@0 811 if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
aoqi@0 812 if (errno == EEXIST) {
aoqi@0 813 // The directory already exists and was probably created by another
aoqi@0 814 // JVM instance. However, this could also be the result of a
aoqi@0 815 // deliberate symlink. Verify that the existing directory is safe.
aoqi@0 816 if (!is_directory_secure(dirname)) {
goetz@7515 817 // Directory is not secure.
aoqi@0 818 if (PrintMiscellaneous && Verbose) {
aoqi@0 819 warning("%s directory is insecure\n", dirname);
aoqi@0 820 }
aoqi@0 821 return false;
aoqi@0 822 }
aoqi@0 823 }
aoqi@0 824 else {
aoqi@0 825 // we encountered some other failure while attempting
aoqi@0 826 // to create the directory
aoqi@0 827 //
aoqi@0 828 if (PrintMiscellaneous && Verbose) {
aoqi@0 829 warning("could not create directory %s: %s\n",
aoqi@0 830 dirname, strerror(errno));
aoqi@0 831 }
aoqi@0 832 return false;
aoqi@0 833 }
aoqi@0 834 }
aoqi@0 835 return true;
aoqi@0 836 }
aoqi@0 837
aoqi@0 838 // create the shared memory file resources
aoqi@0 839 //
aoqi@0 840 // This method creates the shared memory file with the given size
aoqi@0 841 // This method also creates the user specific temporary directory, if
aoqi@0 842 // it does not yet exist.
aoqi@0 843 //
aoqi@0 844 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
aoqi@0 845
aoqi@0 846 // make the user temporary directory
aoqi@0 847 if (!make_user_tmp_dir(dirname)) {
aoqi@0 848 // could not make/find the directory or the found directory
aoqi@0 849 // was not secure
aoqi@0 850 return -1;
aoqi@0 851 }
aoqi@0 852
goetz@7515 853 int saved_cwd_fd;
goetz@7515 854 // Open the directory and set the current working directory to it.
goetz@7515 855 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
goetz@7515 856 if (dirp == NULL) {
goetz@7515 857 // Directory doesn't exist or is insecure, so cannot create shared
goetz@7515 858 // memory file.
goetz@7515 859 return -1;
goetz@7515 860 }
goetz@7515 861
goetz@7515 862 // Open the filename in the current directory.
goetz@7515 863 // Cannot use O_TRUNC here; truncation of an existing file has to happen
goetz@7515 864 // after the is_file_secure() check below.
aoqi@0 865 int result;
aoqi@0 866
goetz@7515 867 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
goetz@7515 868 // so provide a workaround in this case.
goetz@7515 869 #ifdef O_NOFOLLOW
goetz@7515 870 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
goetz@7515 871 #else
goetz@7515 872 // workaround function (jdk6 code)
goetz@7515 873 RESTARTABLE(::open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE), result);
goetz@7515 874 #endif
goetz@7515 875
aoqi@0 876 if (result == OS_ERR) {
aoqi@0 877 if (PrintMiscellaneous && Verbose) {
goetz@7515 878 if (errno == ELOOP) {
goetz@7515 879 warning("file %s is a symlink and is not secure\n", filename);
goetz@7515 880 } else {
goetz@7515 881 warning("could not create file %s: %s\n", filename, strerror(errno));
goetz@7515 882 }
aoqi@0 883 }
goetz@7515 884 // Close the directory and reset the current working directory.
goetz@7515 885 close_directory_secure_cwd(dirp, saved_cwd_fd);
goetz@7515 886
aoqi@0 887 return -1;
aoqi@0 888 }
goetz@7515 889 // Close the directory and reset the current working directory.
goetz@7515 890 close_directory_secure_cwd(dirp, saved_cwd_fd);
aoqi@0 891
aoqi@0 892 // save the file descriptor
aoqi@0 893 int fd = result;
aoqi@0 894
goetz@7515 895 // Check to see if the file is secure.
goetz@7515 896 if (!is_file_secure(fd, filename)) {
goetz@7515 897 ::close(fd);
goetz@7515 898 return -1;
goetz@7515 899 }
goetz@7515 900
goetz@7515 901 // Truncate the file to get rid of any existing data.
goetz@7515 902 RESTARTABLE(::ftruncate(fd, (off_t)0), result);
goetz@7515 903 if (result == OS_ERR) {
goetz@7515 904 if (PrintMiscellaneous && Verbose) {
goetz@7515 905 warning("could not truncate shared memory file: %s\n", strerror(errno));
goetz@7515 906 }
goetz@7515 907 ::close(fd);
goetz@7515 908 return -1;
goetz@7515 909 }
aoqi@0 910 // set the file size
aoqi@0 911 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
aoqi@0 912 if (result == OS_ERR) {
aoqi@0 913 if (PrintMiscellaneous && Verbose) {
aoqi@0 914 warning("could not set shared memory file size: %s\n", strerror(errno));
aoqi@0 915 }
aoqi@0 916 RESTARTABLE(::close(fd), result);
aoqi@0 917 return -1;
aoqi@0 918 }
aoqi@0 919
aoqi@0 920 return fd;
aoqi@0 921 }
aoqi@0 922
aoqi@0 923 // open the shared memory file for the given user and vmid. returns
aoqi@0 924 // the file descriptor for the open file or -1 if the file could not
aoqi@0 925 // be opened.
aoqi@0 926 //
aoqi@0 927 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
aoqi@0 928
aoqi@0 929 // open the file
aoqi@0 930 int result;
goetz@7515 931 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
goetz@7515 932 // so provide a workaround in this case
goetz@7515 933 #ifdef O_NOFOLLOW
aoqi@0 934 RESTARTABLE(::open(filename, oflags), result);
goetz@7515 935 #else
goetz@7515 936 RESTARTABLE(::open_o_nofollow(filename, oflags), result);
goetz@7515 937 #endif
goetz@7515 938
aoqi@0 939 if (result == OS_ERR) {
aoqi@0 940 if (errno == ENOENT) {
aoqi@0 941 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 942 "Process not found");
aoqi@0 943 }
aoqi@0 944 else if (errno == EACCES) {
aoqi@0 945 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 946 "Permission denied");
aoqi@0 947 }
aoqi@0 948 else {
aoqi@0 949 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
aoqi@0 950 }
aoqi@0 951 }
goetz@7515 952 int fd = result;
aoqi@0 953
goetz@7515 954 // Check to see if the file is secure.
goetz@7515 955 if (!is_file_secure(fd, filename)) {
goetz@7515 956 ::close(fd);
goetz@7515 957 return -1;
goetz@7515 958 }
goetz@7515 959
goetz@7515 960 return fd;
aoqi@0 961 }
aoqi@0 962
aoqi@0 963 // create a named shared memory region. returns the address of the
aoqi@0 964 // memory region on success or NULL on failure. A return value of
aoqi@0 965 // NULL will ultimately disable the shared memory feature.
aoqi@0 966 //
aoqi@0 967 // On Solaris and Linux, the name space for shared memory objects
aoqi@0 968 // is the file system name space.
aoqi@0 969 //
aoqi@0 970 // A monitoring application attaching to a JVM does not need to know
aoqi@0 971 // the file system name of the shared memory object. However, it may
aoqi@0 972 // be convenient for applications to discover the existence of newly
aoqi@0 973 // created and terminating JVMs by watching the file system name space
aoqi@0 974 // for files being created or removed.
aoqi@0 975 //
aoqi@0 976 static char* mmap_create_shared(size_t size) {
aoqi@0 977
aoqi@0 978 int result;
aoqi@0 979 int fd;
aoqi@0 980 char* mapAddress;
aoqi@0 981
aoqi@0 982 int vmid = os::current_process_id();
aoqi@0 983
aoqi@0 984 char* user_name = get_user_name(geteuid());
aoqi@0 985
aoqi@0 986 if (user_name == NULL)
aoqi@0 987 return NULL;
aoqi@0 988
aoqi@0 989 char* dirname = get_user_tmp_dir(user_name);
aoqi@0 990 char* filename = get_sharedmem_filename(dirname, vmid);
aoqi@0 991
goetz@7515 992 // Get the short filename.
goetz@7515 993 char* short_filename = strrchr(filename, '/');
goetz@7515 994 if (short_filename == NULL) {
goetz@7515 995 short_filename = filename;
goetz@7515 996 } else {
goetz@7515 997 short_filename++;
goetz@7515 998 }
goetz@7515 999
aoqi@0 1000 // cleanup any stale shared memory files
aoqi@0 1001 cleanup_sharedmem_resources(dirname);
aoqi@0 1002
aoqi@0 1003 assert(((size > 0) && (size % os::vm_page_size() == 0)),
aoqi@0 1004 "unexpected PerfMemory region size");
aoqi@0 1005
goetz@7515 1006 fd = create_sharedmem_resources(dirname, short_filename, size);
aoqi@0 1007
aoqi@0 1008 FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
aoqi@0 1009 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
aoqi@0 1010
aoqi@0 1011 if (fd == -1) {
aoqi@0 1012 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 1013 return NULL;
aoqi@0 1014 }
aoqi@0 1015
aoqi@0 1016 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
aoqi@0 1017
aoqi@0 1018 // attempt to close the file - restart it if it was interrupted,
aoqi@0 1019 // but ignore other failures
aoqi@0 1020 RESTARTABLE(::close(fd), result);
aoqi@0 1021 assert(result != OS_ERR, "could not close file");
aoqi@0 1022
aoqi@0 1023 if (mapAddress == MAP_FAILED) {
aoqi@0 1024 if (PrintMiscellaneous && Verbose) {
aoqi@0 1025 warning("mmap failed - %s\n", strerror(errno));
aoqi@0 1026 }
aoqi@0 1027 remove_file(filename);
aoqi@0 1028 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 1029 return NULL;
aoqi@0 1030 }
aoqi@0 1031
aoqi@0 1032 // save the file name for use in delete_shared_memory()
aoqi@0 1033 backing_store_file_name = filename;
aoqi@0 1034
aoqi@0 1035 // clear the shared memory region
aoqi@0 1036 (void)::memset((void*) mapAddress, 0, size);
aoqi@0 1037
goetz@7515 1038 // It does not go through os api, the operation has to record from here.
goetz@7515 1039 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
goetz@7515 1040
aoqi@0 1041 return mapAddress;
aoqi@0 1042 }
aoqi@0 1043
aoqi@0 1044 // release a named shared memory region
aoqi@0 1045 //
aoqi@0 1046 static void unmap_shared(char* addr, size_t bytes) {
aoqi@0 1047 // Do not rely on os::reserve_memory/os::release_memory to use mmap.
aoqi@0 1048 // Use os::reserve_memory/os::release_memory for PerfDisableSharedMem=1, mmap/munmap for PerfDisableSharedMem=0
aoqi@0 1049 if (::munmap(addr, bytes) == -1) {
aoqi@0 1050 warning("perfmemory: munmap failed (%d)\n", errno);
aoqi@0 1051 }
aoqi@0 1052 }
aoqi@0 1053
aoqi@0 1054 // create the PerfData memory region in shared memory.
aoqi@0 1055 //
aoqi@0 1056 static char* create_shared_memory(size_t size) {
aoqi@0 1057
aoqi@0 1058 // create the shared memory region.
aoqi@0 1059 return mmap_create_shared(size);
aoqi@0 1060 }
aoqi@0 1061
aoqi@0 1062 // delete the shared PerfData memory region
aoqi@0 1063 //
aoqi@0 1064 static void delete_shared_memory(char* addr, size_t size) {
aoqi@0 1065
aoqi@0 1066 // cleanup the persistent shared memory resources. since DestroyJavaVM does
aoqi@0 1067 // not support unloading of the JVM, unmapping of the memory resource is
aoqi@0 1068 // not performed. The memory will be reclaimed by the OS upon termination of
aoqi@0 1069 // the process. The backing store file is deleted from the file system.
aoqi@0 1070
aoqi@0 1071 assert(!PerfDisableSharedMem, "shouldn't be here");
aoqi@0 1072
aoqi@0 1073 if (backing_store_file_name != NULL) {
aoqi@0 1074 remove_file(backing_store_file_name);
aoqi@0 1075 // Don't.. Free heap memory could deadlock os::abort() if it is called
aoqi@0 1076 // from signal handler. OS will reclaim the heap memory.
aoqi@0 1077 // FREE_C_HEAP_ARRAY(char, backing_store_file_name, mtInternal);
aoqi@0 1078 backing_store_file_name = NULL;
aoqi@0 1079 }
aoqi@0 1080 }
aoqi@0 1081
aoqi@0 1082 // return the size of the file for the given file descriptor
aoqi@0 1083 // or 0 if it is not a valid size for a shared memory file
aoqi@0 1084 //
aoqi@0 1085 static size_t sharedmem_filesize(int fd, TRAPS) {
aoqi@0 1086
aoqi@0 1087 struct stat statbuf;
aoqi@0 1088 int result;
aoqi@0 1089
aoqi@0 1090 RESTARTABLE(::fstat(fd, &statbuf), result);
aoqi@0 1091 if (result == OS_ERR) {
aoqi@0 1092 if (PrintMiscellaneous && Verbose) {
aoqi@0 1093 warning("fstat failed: %s\n", strerror(errno));
aoqi@0 1094 }
aoqi@0 1095 THROW_MSG_0(vmSymbols::java_io_IOException(),
aoqi@0 1096 "Could not determine PerfMemory size");
aoqi@0 1097 }
aoqi@0 1098
aoqi@0 1099 if ((statbuf.st_size == 0) ||
aoqi@0 1100 ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
aoqi@0 1101 THROW_MSG_0(vmSymbols::java_lang_Exception(),
aoqi@0 1102 "Invalid PerfMemory size");
aoqi@0 1103 }
aoqi@0 1104
aoqi@0 1105 return (size_t)statbuf.st_size;
aoqi@0 1106 }
aoqi@0 1107
aoqi@0 1108 // attach to a named shared memory region.
aoqi@0 1109 //
aoqi@0 1110 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
aoqi@0 1111
aoqi@0 1112 char* mapAddress;
aoqi@0 1113 int result;
aoqi@0 1114 int fd;
goetz@7515 1115 size_t size = 0;
aoqi@0 1116 const char* luser = NULL;
aoqi@0 1117
aoqi@0 1118 int mmap_prot;
aoqi@0 1119 int file_flags;
aoqi@0 1120
aoqi@0 1121 ResourceMark rm;
aoqi@0 1122
aoqi@0 1123 // map the high level access mode to the appropriate permission
aoqi@0 1124 // constructs for the file and the shared memory mapping.
aoqi@0 1125 if (mode == PerfMemory::PERF_MODE_RO) {
aoqi@0 1126 mmap_prot = PROT_READ;
goetz@7515 1127
goetz@7515 1128 // No O_NOFOLLOW defined at buildtime, and it is not documented for open.
goetz@7515 1129 #ifdef O_NOFOLLOW
goetz@7515 1130 file_flags = O_RDONLY | O_NOFOLLOW;
goetz@7515 1131 #else
aoqi@0 1132 file_flags = O_RDONLY;
goetz@7515 1133 #endif
aoqi@0 1134 }
aoqi@0 1135 else if (mode == PerfMemory::PERF_MODE_RW) {
aoqi@0 1136 #ifdef LATER
aoqi@0 1137 mmap_prot = PROT_READ | PROT_WRITE;
goetz@7515 1138 file_flags = O_RDWR | O_NOFOLLOW;
aoqi@0 1139 #else
aoqi@0 1140 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1141 "Unsupported access mode");
aoqi@0 1142 #endif
aoqi@0 1143 }
aoqi@0 1144 else {
aoqi@0 1145 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1146 "Illegal access mode");
aoqi@0 1147 }
aoqi@0 1148
aoqi@0 1149 if (user == NULL || strlen(user) == 0) {
aoqi@0 1150 luser = get_user_name(vmid, CHECK);
aoqi@0 1151 }
aoqi@0 1152 else {
aoqi@0 1153 luser = user;
aoqi@0 1154 }
aoqi@0 1155
aoqi@0 1156 if (luser == NULL) {
aoqi@0 1157 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1158 "Could not map vmid to user Name");
aoqi@0 1159 }
aoqi@0 1160
aoqi@0 1161 char* dirname = get_user_tmp_dir(luser);
aoqi@0 1162
aoqi@0 1163 // since we don't follow symbolic links when creating the backing
aoqi@0 1164 // store file, we don't follow them when attaching either.
aoqi@0 1165 //
aoqi@0 1166 if (!is_directory_secure(dirname)) {
aoqi@0 1167 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
goetz@7515 1168 if (luser != user) {
goetz@7515 1169 FREE_C_HEAP_ARRAY(char, luser, mtInternal);
goetz@7515 1170 }
aoqi@0 1171 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 1172 "Process not found");
aoqi@0 1173 }
aoqi@0 1174
aoqi@0 1175 char* filename = get_sharedmem_filename(dirname, vmid);
aoqi@0 1176
aoqi@0 1177 // copy heap memory to resource memory. the open_sharedmem_file
aoqi@0 1178 // method below need to use the filename, but could throw an
aoqi@0 1179 // exception. using a resource array prevents the leak that
aoqi@0 1180 // would otherwise occur.
aoqi@0 1181 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
aoqi@0 1182 strcpy(rfilename, filename);
aoqi@0 1183
aoqi@0 1184 // free the c heap resources that are no longer needed
aoqi@0 1185 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
aoqi@0 1186 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
aoqi@0 1187 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
aoqi@0 1188
aoqi@0 1189 // open the shared memory file for the give vmid
aoqi@0 1190 fd = open_sharedmem_file(rfilename, file_flags, CHECK);
aoqi@0 1191 assert(fd != OS_ERR, "unexpected value");
aoqi@0 1192
aoqi@0 1193 if (*sizep == 0) {
aoqi@0 1194 size = sharedmem_filesize(fd, CHECK);
aoqi@0 1195 assert(size != 0, "unexpected size");
aoqi@0 1196 } else {
aoqi@0 1197 size = *sizep;
aoqi@0 1198 }
aoqi@0 1199
aoqi@0 1200 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
aoqi@0 1201
aoqi@0 1202 // attempt to close the file - restart if it gets interrupted,
aoqi@0 1203 // but ignore other failures
aoqi@0 1204 RESTARTABLE(::close(fd), result);
aoqi@0 1205 assert(result != OS_ERR, "could not close file");
aoqi@0 1206
aoqi@0 1207 if (mapAddress == MAP_FAILED) {
aoqi@0 1208 if (PrintMiscellaneous && Verbose) {
aoqi@0 1209 warning("mmap failed: %s\n", strerror(errno));
aoqi@0 1210 }
aoqi@0 1211 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
aoqi@0 1212 "Could not map PerfMemory");
aoqi@0 1213 }
aoqi@0 1214
goetz@7515 1215 // It does not go through os api, the operation has to record from here.
goetz@7515 1216 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
goetz@7515 1217
aoqi@0 1218 *addr = mapAddress;
aoqi@0 1219 *sizep = size;
aoqi@0 1220
aoqi@0 1221 if (PerfTraceMemOps) {
aoqi@0 1222 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
aoqi@0 1223 INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress);
aoqi@0 1224 }
aoqi@0 1225 }
aoqi@0 1226
aoqi@0 1227
aoqi@0 1228
aoqi@0 1229
aoqi@0 1230 // create the PerfData memory region
aoqi@0 1231 //
aoqi@0 1232 // This method creates the memory region used to store performance
aoqi@0 1233 // data for the JVM. The memory may be created in standard or
aoqi@0 1234 // shared memory.
aoqi@0 1235 //
aoqi@0 1236 void PerfMemory::create_memory_region(size_t size) {
aoqi@0 1237
aoqi@0 1238 if (PerfDisableSharedMem) {
aoqi@0 1239 // do not share the memory for the performance data.
aoqi@0 1240 _start = create_standard_memory(size);
aoqi@0 1241 }
aoqi@0 1242 else {
aoqi@0 1243 _start = create_shared_memory(size);
aoqi@0 1244 if (_start == NULL) {
aoqi@0 1245
aoqi@0 1246 // creation of the shared memory region failed, attempt
aoqi@0 1247 // to create a contiguous, non-shared memory region instead.
aoqi@0 1248 //
aoqi@0 1249 if (PrintMiscellaneous && Verbose) {
aoqi@0 1250 warning("Reverting to non-shared PerfMemory region.\n");
aoqi@0 1251 }
aoqi@0 1252 PerfDisableSharedMem = true;
aoqi@0 1253 _start = create_standard_memory(size);
aoqi@0 1254 }
aoqi@0 1255 }
aoqi@0 1256
aoqi@0 1257 if (_start != NULL) _capacity = size;
aoqi@0 1258
aoqi@0 1259 }
aoqi@0 1260
aoqi@0 1261 // delete the PerfData memory region
aoqi@0 1262 //
aoqi@0 1263 // This method deletes the memory region used to store performance
aoqi@0 1264 // data for the JVM. The memory region indicated by the <address, size>
aoqi@0 1265 // tuple will be inaccessible after a call to this method.
aoqi@0 1266 //
aoqi@0 1267 void PerfMemory::delete_memory_region() {
aoqi@0 1268
aoqi@0 1269 assert((start() != NULL && capacity() > 0), "verify proper state");
aoqi@0 1270
aoqi@0 1271 // If user specifies PerfDataSaveFile, it will save the performance data
aoqi@0 1272 // to the specified file name no matter whether PerfDataSaveToFile is specified
aoqi@0 1273 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
aoqi@0 1274 // -XX:+PerfDataSaveToFile.
aoqi@0 1275 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
aoqi@0 1276 save_memory_to_file(start(), capacity());
aoqi@0 1277 }
aoqi@0 1278
aoqi@0 1279 if (PerfDisableSharedMem) {
aoqi@0 1280 delete_standard_memory(start(), capacity());
aoqi@0 1281 }
aoqi@0 1282 else {
aoqi@0 1283 delete_shared_memory(start(), capacity());
aoqi@0 1284 }
aoqi@0 1285 }
aoqi@0 1286
aoqi@0 1287 // attach to the PerfData memory region for another JVM
aoqi@0 1288 //
aoqi@0 1289 // This method returns an <address, size> tuple that points to
aoqi@0 1290 // a memory buffer that is kept reasonably synchronized with
aoqi@0 1291 // the PerfData memory region for the indicated JVM. This
aoqi@0 1292 // buffer may be kept in synchronization via shared memory
aoqi@0 1293 // or some other mechanism that keeps the buffer updated.
aoqi@0 1294 //
aoqi@0 1295 // If the JVM chooses not to support the attachability feature,
aoqi@0 1296 // this method should throw an UnsupportedOperation exception.
aoqi@0 1297 //
aoqi@0 1298 // This implementation utilizes named shared memory to map
aoqi@0 1299 // the indicated process's PerfData memory region into this JVMs
aoqi@0 1300 // address space.
aoqi@0 1301 //
aoqi@0 1302 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
aoqi@0 1303
aoqi@0 1304 if (vmid == 0 || vmid == os::current_process_id()) {
aoqi@0 1305 *addrp = start();
aoqi@0 1306 *sizep = capacity();
aoqi@0 1307 return;
aoqi@0 1308 }
aoqi@0 1309
aoqi@0 1310 mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
aoqi@0 1311 }
aoqi@0 1312
aoqi@0 1313 // detach from the PerfData memory region of another JVM
aoqi@0 1314 //
aoqi@0 1315 // This method detaches the PerfData memory region of another
aoqi@0 1316 // JVM, specified as an <address, size> tuple of a buffer
aoqi@0 1317 // in this process's address space. This method may perform
aoqi@0 1318 // arbitrary actions to accomplish the detachment. The memory
aoqi@0 1319 // region specified by <address, size> will be inaccessible after
aoqi@0 1320 // a call to this method.
aoqi@0 1321 //
aoqi@0 1322 // If the JVM chooses not to support the attachability feature,
aoqi@0 1323 // this method should throw an UnsupportedOperation exception.
aoqi@0 1324 //
aoqi@0 1325 // This implementation utilizes named shared memory to detach
aoqi@0 1326 // the indicated process's PerfData memory region from this
aoqi@0 1327 // process's address space.
aoqi@0 1328 //
aoqi@0 1329 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
aoqi@0 1330
aoqi@0 1331 assert(addr != 0, "address sanity check");
aoqi@0 1332 assert(bytes > 0, "capacity sanity check");
aoqi@0 1333
aoqi@0 1334 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
aoqi@0 1335 // prevent accidental detachment of this process's PerfMemory region
aoqi@0 1336 return;
aoqi@0 1337 }
aoqi@0 1338
aoqi@0 1339 unmap_shared(addr, bytes);
aoqi@0 1340 }
aoqi@0 1341
aoqi@0 1342 char* PerfMemory::backing_store_filename() {
aoqi@0 1343 return backing_store_file_name;
aoqi@0 1344 }

mercurial