src/os/aix/vm/perfMemory_aix.cpp

Wed, 31 Jan 2018 19:24:57 -0500

author
dbuck
date
Wed, 31 Jan 2018 19:24:57 -0500
changeset 9289
427b2fb1944f
parent 8210
2d23269a45a0
child 9507
7e72702243a4
permissions
-rw-r--r--

8189170: Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM
Reviewed-by: dcubed

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

mercurial