src/os/windows/vm/perfMemory_windows.cpp

Fri, 03 Feb 2012 12:08:55 -0800

author
jcoomes
date
Fri, 03 Feb 2012 12:08:55 -0800
changeset 3502
379b22e03c32
parent 2543
de14f1eee390
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

Merge

duke@435 1 /*
dcubed@2543 2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/vmSymbols.hpp"
stefank@2314 27 #include "memory/allocation.inline.hpp"
stefank@2314 28 #include "memory/resourceArea.hpp"
stefank@2314 29 #include "oops/oop.inline.hpp"
stefank@2314 30 #include "os_windows.inline.hpp"
stefank@2314 31 #include "runtime/handles.inline.hpp"
stefank@2314 32 #include "runtime/perfMemory.hpp"
stefank@2314 33 #include "utilities/exceptions.hpp"
duke@435 34
duke@435 35 #include <windows.h>
duke@435 36 #include <sys/types.h>
duke@435 37 #include <sys/stat.h>
duke@435 38 #include <errno.h>
duke@435 39 #include <lmcons.h>
duke@435 40
duke@435 41 typedef BOOL (WINAPI *SetSecurityDescriptorControlFnPtr)(
duke@435 42 IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
duke@435 43 IN SECURITY_DESCRIPTOR_CONTROL ControlBitsOfInterest,
duke@435 44 IN SECURITY_DESCRIPTOR_CONTROL ControlBitsToSet);
duke@435 45
duke@435 46 // Standard Memory Implementation Details
duke@435 47
duke@435 48 // create the PerfData memory region in standard memory.
duke@435 49 //
duke@435 50 static char* create_standard_memory(size_t size) {
duke@435 51
duke@435 52 // allocate an aligned chuck of memory
duke@435 53 char* mapAddress = os::reserve_memory(size);
duke@435 54
duke@435 55 if (mapAddress == NULL) {
duke@435 56 return NULL;
duke@435 57 }
duke@435 58
duke@435 59 // commit memory
duke@435 60 if (!os::commit_memory(mapAddress, size)) {
duke@435 61 if (PrintMiscellaneous && Verbose) {
duke@435 62 warning("Could not commit PerfData memory\n");
duke@435 63 }
duke@435 64 os::release_memory(mapAddress, size);
duke@435 65 return NULL;
duke@435 66 }
duke@435 67
duke@435 68 return mapAddress;
duke@435 69 }
duke@435 70
duke@435 71 // delete the PerfData memory region
duke@435 72 //
duke@435 73 static void delete_standard_memory(char* addr, size_t size) {
duke@435 74
duke@435 75 // there are no persistent external resources to cleanup for standard
duke@435 76 // memory. since DestroyJavaVM does not support unloading of the JVM,
duke@435 77 // cleanup of the memory resource is not performed. The memory will be
duke@435 78 // reclaimed by the OS upon termination of the process.
duke@435 79 //
duke@435 80 return;
duke@435 81
duke@435 82 }
duke@435 83
duke@435 84 // save the specified memory region to the given file
duke@435 85 //
duke@435 86 static void save_memory_to_file(char* addr, size_t size) {
duke@435 87
duke@435 88 const char* destfile = PerfMemory::get_perfdata_file_path();
duke@435 89 assert(destfile[0] != '\0', "invalid Perfdata file path");
duke@435 90
duke@435 91 int fd = ::_open(destfile, _O_BINARY|_O_CREAT|_O_WRONLY|_O_TRUNC,
duke@435 92 _S_IREAD|_S_IWRITE);
duke@435 93
duke@435 94 if (fd == OS_ERR) {
duke@435 95 if (PrintMiscellaneous && Verbose) {
duke@435 96 warning("Could not create Perfdata save file: %s: %s\n",
duke@435 97 destfile, strerror(errno));
duke@435 98 }
duke@435 99 } else {
duke@435 100 for (size_t remaining = size; remaining > 0;) {
duke@435 101
duke@435 102 int nbytes = ::_write(fd, addr, (unsigned int)remaining);
duke@435 103 if (nbytes == OS_ERR) {
duke@435 104 if (PrintMiscellaneous && Verbose) {
duke@435 105 warning("Could not write Perfdata save file: %s: %s\n",
duke@435 106 destfile, strerror(errno));
duke@435 107 }
duke@435 108 break;
duke@435 109 }
duke@435 110
duke@435 111 remaining -= (size_t)nbytes;
duke@435 112 addr += nbytes;
duke@435 113 }
duke@435 114
duke@435 115 int result = ::_close(fd);
duke@435 116 if (PrintMiscellaneous && Verbose) {
duke@435 117 if (result == OS_ERR) {
duke@435 118 warning("Could not close %s: %s\n", destfile, strerror(errno));
duke@435 119 }
duke@435 120 }
duke@435 121 }
duke@435 122
duke@435 123 FREE_C_HEAP_ARRAY(char, destfile);
duke@435 124 }
duke@435 125
duke@435 126 // Shared Memory Implementation Details
duke@435 127
duke@435 128 // Note: the win32 shared memory implementation uses two objects to represent
duke@435 129 // the shared memory: a windows kernel based file mapping object and a backing
duke@435 130 // store file. On windows, the name space for shared memory is a kernel
duke@435 131 // based name space that is disjoint from other win32 name spaces. Since Java
duke@435 132 // is unaware of this name space, a parallel file system based name space is
duke@435 133 // maintained, which provides a common file system based shared memory name
duke@435 134 // space across the supported platforms and one that Java apps can deal with
duke@435 135 // through simple file apis.
duke@435 136 //
duke@435 137 // For performance and resource cleanup reasons, it is recommended that the
duke@435 138 // user specific directory and the backing store file be stored in either a
duke@435 139 // RAM based file system or a local disk based file system. Network based
duke@435 140 // file systems are not recommended for performance reasons. In addition,
duke@435 141 // use of SMB network based file systems may result in unsuccesful cleanup
duke@435 142 // of the disk based resource on exit of the VM. The Windows TMP and TEMP
duke@435 143 // environement variables, as used by the GetTempPath() Win32 API (see
duke@435 144 // os::get_temp_directory() in os_win32.cpp), control the location of the
duke@435 145 // user specific directory and the shared memory backing store file.
duke@435 146
duke@435 147 static HANDLE sharedmem_fileMapHandle = NULL;
duke@435 148 static HANDLE sharedmem_fileHandle = INVALID_HANDLE_VALUE;
duke@435 149 static char* sharedmem_fileName = NULL;
duke@435 150
duke@435 151 // return the user specific temporary directory name.
duke@435 152 //
duke@435 153 // the caller is expected to free the allocated memory.
duke@435 154 //
duke@435 155 static char* get_user_tmp_dir(const char* user) {
duke@435 156
duke@435 157 const char* tmpdir = os::get_temp_directory();
duke@435 158 const char* perfdir = PERFDATA_NAME;
coleenp@1788 159 size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
duke@435 160 char* dirname = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 161
duke@435 162 // construct the path name to user specific tmp directory
coleenp@1788 163 _snprintf(dirname, nbytes, "%s\\%s_%s", tmpdir, perfdir, user);
duke@435 164
duke@435 165 return dirname;
duke@435 166 }
duke@435 167
duke@435 168 // convert the given file name into a process id. if the file
duke@435 169 // does not meet the file naming constraints, return 0.
duke@435 170 //
duke@435 171 static int filename_to_pid(const char* filename) {
duke@435 172
duke@435 173 // a filename that doesn't begin with a digit is not a
duke@435 174 // candidate for conversion.
duke@435 175 //
duke@435 176 if (!isdigit(*filename)) {
duke@435 177 return 0;
duke@435 178 }
duke@435 179
duke@435 180 // check if file name can be converted to an integer without
duke@435 181 // any leftover characters.
duke@435 182 //
duke@435 183 char* remainder = NULL;
duke@435 184 errno = 0;
duke@435 185 int pid = (int)strtol(filename, &remainder, 10);
duke@435 186
duke@435 187 if (errno != 0) {
duke@435 188 return 0;
duke@435 189 }
duke@435 190
duke@435 191 // check for left over characters. If any, then the filename is
duke@435 192 // not a candidate for conversion.
duke@435 193 //
duke@435 194 if (remainder != NULL && *remainder != '\0') {
duke@435 195 return 0;
duke@435 196 }
duke@435 197
duke@435 198 // successful conversion, return the pid
duke@435 199 return pid;
duke@435 200 }
duke@435 201
duke@435 202 // check if the given path is considered a secure directory for
duke@435 203 // the backing store files. Returns true if the directory exists
duke@435 204 // and is considered a secure location. Returns false if the path
twisti@1040 205 // is a symbolic link or if an error occurred.
duke@435 206 //
duke@435 207 static bool is_directory_secure(const char* path) {
duke@435 208
duke@435 209 DWORD fa;
duke@435 210
duke@435 211 fa = GetFileAttributes(path);
duke@435 212 if (fa == 0xFFFFFFFF) {
duke@435 213 DWORD lasterror = GetLastError();
duke@435 214 if (lasterror == ERROR_FILE_NOT_FOUND) {
duke@435 215 return false;
duke@435 216 }
duke@435 217 else {
duke@435 218 // unexpected error, declare the path insecure
duke@435 219 if (PrintMiscellaneous && Verbose) {
duke@435 220 warning("could not get attributes for file %s: ",
duke@435 221 " lasterror = %d\n", path, lasterror);
duke@435 222 }
duke@435 223 return false;
duke@435 224 }
duke@435 225 }
duke@435 226
duke@435 227 if (fa & FILE_ATTRIBUTE_REPARSE_POINT) {
duke@435 228 // we don't accept any redirection for the user specific directory
duke@435 229 // so declare the path insecure. This may be too conservative,
duke@435 230 // as some types of reparse points might be acceptable, but it
duke@435 231 // is probably more secure to avoid these conditions.
duke@435 232 //
duke@435 233 if (PrintMiscellaneous && Verbose) {
duke@435 234 warning("%s is a reparse point\n", path);
duke@435 235 }
duke@435 236 return false;
duke@435 237 }
duke@435 238
duke@435 239 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
duke@435 240 // this is the expected case. Since windows supports symbolic
duke@435 241 // links to directories only, not to files, there is no need
duke@435 242 // to check for open write permissions on the directory. If the
duke@435 243 // directory has open write permissions, any files deposited that
duke@435 244 // are not expected will be removed by the cleanup code.
duke@435 245 //
duke@435 246 return true;
duke@435 247 }
duke@435 248 else {
duke@435 249 // this is either a regular file or some other type of file,
duke@435 250 // any of which are unexpected and therefore insecure.
duke@435 251 //
duke@435 252 if (PrintMiscellaneous && Verbose) {
duke@435 253 warning("%s is not a directory, file attributes = "
duke@435 254 INTPTR_FORMAT "\n", path, fa);
duke@435 255 }
duke@435 256 return false;
duke@435 257 }
duke@435 258 }
duke@435 259
duke@435 260 // return the user name for the owner of this process
duke@435 261 //
duke@435 262 // the caller is expected to free the allocated memory.
duke@435 263 //
duke@435 264 static char* get_user_name() {
duke@435 265
duke@435 266 /* get the user name. This code is adapted from code found in
duke@435 267 * the jdk in src/windows/native/java/lang/java_props_md.c
duke@435 268 * java_props_md.c 1.29 02/02/06. According to the original
duke@435 269 * source, the call to GetUserName is avoided because of a resulting
duke@435 270 * increase in footprint of 100K.
duke@435 271 */
duke@435 272 char* user = getenv("USERNAME");
duke@435 273 char buf[UNLEN+1];
duke@435 274 DWORD buflen = sizeof(buf);
duke@435 275 if (user == NULL || strlen(user) == 0) {
duke@435 276 if (GetUserName(buf, &buflen)) {
duke@435 277 user = buf;
duke@435 278 }
duke@435 279 else {
duke@435 280 return NULL;
duke@435 281 }
duke@435 282 }
duke@435 283
duke@435 284 char* user_name = NEW_C_HEAP_ARRAY(char, strlen(user)+1);
duke@435 285 strcpy(user_name, user);
duke@435 286
duke@435 287 return user_name;
duke@435 288 }
duke@435 289
duke@435 290 // return the name of the user that owns the process identified by vmid.
duke@435 291 //
duke@435 292 // This method uses a slow directory search algorithm to find the backing
duke@435 293 // store file for the specified vmid and returns the user name, as determined
duke@435 294 // by the user name suffix of the hsperfdata_<username> directory name.
duke@435 295 //
duke@435 296 // the caller is expected to free the allocated memory.
duke@435 297 //
duke@435 298 static char* get_user_name_slow(int vmid) {
duke@435 299
duke@435 300 // directory search
dcubed@2543 301 char* latest_user = NULL;
dcubed@2543 302 time_t latest_ctime = 0;
duke@435 303
duke@435 304 const char* tmpdirname = os::get_temp_directory();
duke@435 305
duke@435 306 DIR* tmpdirp = os::opendir(tmpdirname);
duke@435 307
duke@435 308 if (tmpdirp == NULL) {
duke@435 309 return NULL;
duke@435 310 }
duke@435 311
duke@435 312 // for each entry in the directory that matches the pattern hsperfdata_*,
duke@435 313 // open the directory and check if the file for the given vmid exists.
duke@435 314 // The file with the expected name and the latest creation date is used
duke@435 315 // to determine the user name for the process id.
duke@435 316 //
duke@435 317 struct dirent* dentry;
duke@435 318 char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname));
duke@435 319 errno = 0;
duke@435 320 while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
duke@435 321
duke@435 322 // check if the directory entry is a hsperfdata file
duke@435 323 if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
duke@435 324 continue;
duke@435 325 }
duke@435 326
duke@435 327 char* usrdir_name = NEW_C_HEAP_ARRAY(char,
coleenp@1788 328 strlen(tmpdirname) + strlen(dentry->d_name) + 2);
duke@435 329 strcpy(usrdir_name, tmpdirname);
coleenp@1788 330 strcat(usrdir_name, "\\");
duke@435 331 strcat(usrdir_name, dentry->d_name);
duke@435 332
duke@435 333 DIR* subdirp = os::opendir(usrdir_name);
duke@435 334
duke@435 335 if (subdirp == NULL) {
duke@435 336 FREE_C_HEAP_ARRAY(char, usrdir_name);
duke@435 337 continue;
duke@435 338 }
duke@435 339
duke@435 340 // Since we don't create the backing store files in directories
duke@435 341 // pointed to by symbolic links, we also don't follow them when
duke@435 342 // looking for the files. We check for a symbolic link after the
duke@435 343 // call to opendir in order to eliminate a small window where the
duke@435 344 // symlink can be exploited.
duke@435 345 //
duke@435 346 if (!is_directory_secure(usrdir_name)) {
duke@435 347 FREE_C_HEAP_ARRAY(char, usrdir_name);
duke@435 348 os::closedir(subdirp);
duke@435 349 continue;
duke@435 350 }
duke@435 351
duke@435 352 struct dirent* udentry;
duke@435 353 char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name));
duke@435 354 errno = 0;
duke@435 355 while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
duke@435 356
duke@435 357 if (filename_to_pid(udentry->d_name) == vmid) {
duke@435 358 struct stat statbuf;
duke@435 359
duke@435 360 char* filename = NEW_C_HEAP_ARRAY(char,
duke@435 361 strlen(usrdir_name) + strlen(udentry->d_name) + 2);
duke@435 362
duke@435 363 strcpy(filename, usrdir_name);
duke@435 364 strcat(filename, "\\");
duke@435 365 strcat(filename, udentry->d_name);
duke@435 366
duke@435 367 if (::stat(filename, &statbuf) == OS_ERR) {
duke@435 368 FREE_C_HEAP_ARRAY(char, filename);
duke@435 369 continue;
duke@435 370 }
duke@435 371
duke@435 372 // skip over files that are not regular files.
duke@435 373 if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
duke@435 374 FREE_C_HEAP_ARRAY(char, filename);
duke@435 375 continue;
duke@435 376 }
duke@435 377
dcubed@2543 378 // If we found a matching file with a newer creation time, then
dcubed@2543 379 // save the user name. The newer creation time indicates that
dcubed@2543 380 // we found a newer incarnation of the process associated with
dcubed@2543 381 // vmid. Due to the way that Windows recycles pids and the fact
dcubed@2543 382 // that we can't delete the file from the file system namespace
dcubed@2543 383 // until last close, it is possible for there to be more than
dcubed@2543 384 // one hsperfdata file with a name matching vmid (diff users).
dcubed@2543 385 //
dcubed@2543 386 // We no longer ignore hsperfdata files where (st_size == 0).
dcubed@2543 387 // In this function, all we're trying to do is determine the
dcubed@2543 388 // name of the user that owns the process associated with vmid
dcubed@2543 389 // so the size doesn't matter. Very rarely, we have observed
dcubed@2543 390 // hsperfdata files where (st_size == 0) and the st_size field
dcubed@2543 391 // later becomes the expected value.
dcubed@2543 392 //
dcubed@2543 393 if (statbuf.st_ctime > latest_ctime) {
dcubed@2543 394 char* user = strchr(dentry->d_name, '_') + 1;
duke@435 395
dcubed@2543 396 if (latest_user != NULL) FREE_C_HEAP_ARRAY(char, latest_user);
dcubed@2543 397 latest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1);
duke@435 398
dcubed@2543 399 strcpy(latest_user, user);
dcubed@2543 400 latest_ctime = statbuf.st_ctime;
duke@435 401 }
duke@435 402
duke@435 403 FREE_C_HEAP_ARRAY(char, filename);
duke@435 404 }
duke@435 405 }
duke@435 406 os::closedir(subdirp);
duke@435 407 FREE_C_HEAP_ARRAY(char, udbuf);
duke@435 408 FREE_C_HEAP_ARRAY(char, usrdir_name);
duke@435 409 }
duke@435 410 os::closedir(tmpdirp);
duke@435 411 FREE_C_HEAP_ARRAY(char, tdbuf);
duke@435 412
dcubed@2543 413 return(latest_user);
duke@435 414 }
duke@435 415
duke@435 416 // return the name of the user that owns the process identified by vmid.
duke@435 417 //
duke@435 418 // note: this method should only be used via the Perf native methods.
duke@435 419 // There are various costs to this method and limiting its use to the
duke@435 420 // Perf native methods limits the impact to monitoring applications only.
duke@435 421 //
duke@435 422 static char* get_user_name(int vmid) {
duke@435 423
duke@435 424 // A fast implementation is not provided at this time. It's possible
duke@435 425 // to provide a fast process id to user name mapping function using
duke@435 426 // the win32 apis, but the default ACL for the process object only
duke@435 427 // allows processes with the same owner SID to acquire the process
duke@435 428 // handle (via OpenProcess(PROCESS_QUERY_INFORMATION)). It's possible
duke@435 429 // to have the JVM change the ACL for the process object to allow arbitrary
duke@435 430 // users to access the process handle and the process security token.
duke@435 431 // The security ramifications need to be studied before providing this
duke@435 432 // mechanism.
duke@435 433 //
duke@435 434 return get_user_name_slow(vmid);
duke@435 435 }
duke@435 436
duke@435 437 // return the name of the shared memory file mapping object for the
duke@435 438 // named shared memory region for the given user name and vmid.
duke@435 439 //
duke@435 440 // The file mapping object's name is not the file name. It is a name
duke@435 441 // in a separate name space.
duke@435 442 //
duke@435 443 // the caller is expected to free the allocated memory.
duke@435 444 //
duke@435 445 static char *get_sharedmem_objectname(const char* user, int vmid) {
duke@435 446
duke@435 447 // construct file mapping object's name, add 3 for two '_' and a
duke@435 448 // null terminator.
duke@435 449 int nbytes = (int)strlen(PERFDATA_NAME) + (int)strlen(user) + 3;
duke@435 450
duke@435 451 // the id is converted to an unsigned value here because win32 allows
duke@435 452 // negative process ids. However, OpenFileMapping API complains
duke@435 453 // about a name containing a '-' characters.
duke@435 454 //
duke@435 455 nbytes += UINT_CHARS;
duke@435 456 char* name = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 457 _snprintf(name, nbytes, "%s_%s_%u", PERFDATA_NAME, user, vmid);
duke@435 458
duke@435 459 return name;
duke@435 460 }
duke@435 461
duke@435 462 // return the file name of the backing store file for the named
duke@435 463 // shared memory region for the given user name and vmid.
duke@435 464 //
duke@435 465 // the caller is expected to free the allocated memory.
duke@435 466 //
duke@435 467 static char* get_sharedmem_filename(const char* dirname, int vmid) {
duke@435 468
duke@435 469 // add 2 for the file separator and a null terminator.
duke@435 470 size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
duke@435 471
duke@435 472 char* name = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 473 _snprintf(name, nbytes, "%s\\%d", dirname, vmid);
duke@435 474
duke@435 475 return name;
duke@435 476 }
duke@435 477
duke@435 478 // remove file
duke@435 479 //
duke@435 480 // this method removes the file with the given file name.
duke@435 481 //
duke@435 482 // Note: if the indicated file is on an SMB network file system, this
duke@435 483 // method may be unsuccessful in removing the file.
duke@435 484 //
duke@435 485 static void remove_file(const char* dirname, const char* filename) {
duke@435 486
duke@435 487 size_t nbytes = strlen(dirname) + strlen(filename) + 2;
duke@435 488 char* path = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 489
duke@435 490 strcpy(path, dirname);
duke@435 491 strcat(path, "\\");
duke@435 492 strcat(path, filename);
duke@435 493
duke@435 494 if (::unlink(path) == OS_ERR) {
duke@435 495 if (PrintMiscellaneous && Verbose) {
duke@435 496 if (errno != ENOENT) {
duke@435 497 warning("Could not unlink shared memory backing"
duke@435 498 " store file %s : %s\n", path, strerror(errno));
duke@435 499 }
duke@435 500 }
duke@435 501 }
duke@435 502
duke@435 503 FREE_C_HEAP_ARRAY(char, path);
duke@435 504 }
duke@435 505
duke@435 506 // returns true if the process represented by pid is alive, otherwise
duke@435 507 // returns false. the validity of the result is only accurate if the
duke@435 508 // target process is owned by the same principal that owns this process.
duke@435 509 // this method should not be used if to test the status of an otherwise
duke@435 510 // arbitrary process unless it is know that this process has the appropriate
duke@435 511 // privileges to guarantee a result valid.
duke@435 512 //
duke@435 513 static bool is_alive(int pid) {
duke@435 514
duke@435 515 HANDLE ph = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
duke@435 516 if (ph == NULL) {
duke@435 517 // the process does not exist.
duke@435 518 if (PrintMiscellaneous && Verbose) {
duke@435 519 DWORD lastError = GetLastError();
duke@435 520 if (lastError != ERROR_INVALID_PARAMETER) {
duke@435 521 warning("OpenProcess failed: %d\n", GetLastError());
duke@435 522 }
duke@435 523 }
duke@435 524 return false;
duke@435 525 }
duke@435 526
duke@435 527 DWORD exit_status;
duke@435 528 if (!GetExitCodeProcess(ph, &exit_status)) {
duke@435 529 if (PrintMiscellaneous && Verbose) {
duke@435 530 warning("GetExitCodeProcess failed: %d\n", GetLastError());
duke@435 531 }
duke@435 532 CloseHandle(ph);
duke@435 533 return false;
duke@435 534 }
duke@435 535
duke@435 536 CloseHandle(ph);
duke@435 537 return (exit_status == STILL_ACTIVE) ? true : false;
duke@435 538 }
duke@435 539
duke@435 540 // check if the file system is considered secure for the backing store files
duke@435 541 //
duke@435 542 static bool is_filesystem_secure(const char* path) {
duke@435 543
duke@435 544 char root_path[MAX_PATH];
duke@435 545 char fs_type[MAX_PATH];
duke@435 546
duke@435 547 if (PerfBypassFileSystemCheck) {
duke@435 548 if (PrintMiscellaneous && Verbose) {
duke@435 549 warning("bypassing file system criteria checks for %s\n", path);
duke@435 550 }
duke@435 551 return true;
duke@435 552 }
duke@435 553
duke@435 554 char* first_colon = strchr((char *)path, ':');
duke@435 555 if (first_colon == NULL) {
duke@435 556 if (PrintMiscellaneous && Verbose) {
duke@435 557 warning("expected device specifier in path: %s\n", path);
duke@435 558 }
duke@435 559 return false;
duke@435 560 }
duke@435 561
duke@435 562 size_t len = (size_t)(first_colon - path);
duke@435 563 assert(len + 2 <= MAX_PATH, "unexpected device specifier length");
duke@435 564 strncpy(root_path, path, len + 1);
duke@435 565 root_path[len + 1] = '\\';
duke@435 566 root_path[len + 2] = '\0';
duke@435 567
duke@435 568 // check that we have something like "C:\" or "AA:\"
duke@435 569 assert(strlen(root_path) >= 3, "device specifier too short");
duke@435 570 assert(strchr(root_path, ':') != NULL, "bad device specifier format");
duke@435 571 assert(strchr(root_path, '\\') != NULL, "bad device specifier format");
duke@435 572
duke@435 573 DWORD maxpath;
duke@435 574 DWORD flags;
duke@435 575
duke@435 576 if (!GetVolumeInformation(root_path, NULL, 0, NULL, &maxpath,
duke@435 577 &flags, fs_type, MAX_PATH)) {
duke@435 578 // we can't get information about the volume, so assume unsafe.
duke@435 579 if (PrintMiscellaneous && Verbose) {
duke@435 580 warning("could not get device information for %s: "
duke@435 581 " path = %s: lasterror = %d\n",
duke@435 582 root_path, path, GetLastError());
duke@435 583 }
duke@435 584 return false;
duke@435 585 }
duke@435 586
duke@435 587 if ((flags & FS_PERSISTENT_ACLS) == 0) {
duke@435 588 // file system doesn't support ACLs, declare file system unsafe
duke@435 589 if (PrintMiscellaneous && Verbose) {
duke@435 590 warning("file system type %s on device %s does not support"
duke@435 591 " ACLs\n", fs_type, root_path);
duke@435 592 }
duke@435 593 return false;
duke@435 594 }
duke@435 595
duke@435 596 if ((flags & FS_VOL_IS_COMPRESSED) != 0) {
duke@435 597 // file system is compressed, declare file system unsafe
duke@435 598 if (PrintMiscellaneous && Verbose) {
duke@435 599 warning("file system type %s on device %s is compressed\n",
duke@435 600 fs_type, root_path);
duke@435 601 }
duke@435 602 return false;
duke@435 603 }
duke@435 604
duke@435 605 return true;
duke@435 606 }
duke@435 607
duke@435 608 // cleanup stale shared memory resources
duke@435 609 //
duke@435 610 // This method attempts to remove all stale shared memory files in
duke@435 611 // the named user temporary directory. It scans the named directory
duke@435 612 // for files matching the pattern ^$[0-9]*$. For each file found, the
duke@435 613 // process id is extracted from the file name and a test is run to
duke@435 614 // determine if the process is alive. If the process is not alive,
duke@435 615 // any stale file resources are removed.
duke@435 616 //
duke@435 617 static void cleanup_sharedmem_resources(const char* dirname) {
duke@435 618
duke@435 619 // open the user temp directory
duke@435 620 DIR* dirp = os::opendir(dirname);
duke@435 621
duke@435 622 if (dirp == NULL) {
duke@435 623 // directory doesn't exist, so there is nothing to cleanup
duke@435 624 return;
duke@435 625 }
duke@435 626
duke@435 627 if (!is_directory_secure(dirname)) {
duke@435 628 // the directory is not secure, don't attempt any cleanup
duke@435 629 return;
duke@435 630 }
duke@435 631
duke@435 632 // for each entry in the directory that matches the expected file
duke@435 633 // name pattern, determine if the file resources are stale and if
duke@435 634 // so, remove the file resources. Note, instrumented HotSpot processes
duke@435 635 // for this user may start and/or terminate during this search and
duke@435 636 // remove or create new files in this directory. The behavior of this
duke@435 637 // loop under these conditions is dependent upon the implementation of
duke@435 638 // opendir/readdir.
duke@435 639 //
duke@435 640 struct dirent* entry;
duke@435 641 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname));
duke@435 642 errno = 0;
duke@435 643 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
duke@435 644
duke@435 645 int pid = filename_to_pid(entry->d_name);
duke@435 646
duke@435 647 if (pid == 0) {
duke@435 648
duke@435 649 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
duke@435 650
duke@435 651 // attempt to remove all unexpected files, except "." and ".."
duke@435 652 remove_file(dirname, entry->d_name);
duke@435 653 }
duke@435 654
duke@435 655 errno = 0;
duke@435 656 continue;
duke@435 657 }
duke@435 658
duke@435 659 // we now have a file name that converts to a valid integer
duke@435 660 // that could represent a process id . if this process id
duke@435 661 // matches the current process id or the process is not running,
duke@435 662 // then remove the stale file resources.
duke@435 663 //
duke@435 664 // process liveness is detected by checking the exit status
duke@435 665 // of the process. if the process id is valid and the exit status
duke@435 666 // indicates that it is still running, the file file resources
duke@435 667 // are not removed. If the process id is invalid, or if we don't
duke@435 668 // have permissions to check the process status, or if the process
duke@435 669 // id is valid and the process has terminated, the the file resources
duke@435 670 // are assumed to be stale and are removed.
duke@435 671 //
duke@435 672 if (pid == os::current_process_id() || !is_alive(pid)) {
duke@435 673
duke@435 674 // we can only remove the file resources. Any mapped views
duke@435 675 // of the file can only be unmapped by the processes that
duke@435 676 // opened those views and the file mapping object will not
duke@435 677 // get removed until all views are unmapped.
duke@435 678 //
duke@435 679 remove_file(dirname, entry->d_name);
duke@435 680 }
duke@435 681 errno = 0;
duke@435 682 }
duke@435 683 os::closedir(dirp);
duke@435 684 FREE_C_HEAP_ARRAY(char, dbuf);
duke@435 685 }
duke@435 686
duke@435 687 // create a file mapping object with the requested name, and size
duke@435 688 // from the file represented by the given Handle object
duke@435 689 //
duke@435 690 static HANDLE create_file_mapping(const char* name, HANDLE fh, LPSECURITY_ATTRIBUTES fsa, size_t size) {
duke@435 691
duke@435 692 DWORD lowSize = (DWORD)size;
duke@435 693 DWORD highSize = 0;
duke@435 694 HANDLE fmh = NULL;
duke@435 695
duke@435 696 // Create a file mapping object with the given name. This function
duke@435 697 // will grow the file to the specified size.
duke@435 698 //
duke@435 699 fmh = CreateFileMapping(
duke@435 700 fh, /* HANDLE file handle for backing store */
duke@435 701 fsa, /* LPSECURITY_ATTRIBUTES Not inheritable */
duke@435 702 PAGE_READWRITE, /* DWORD protections */
duke@435 703 highSize, /* DWORD High word of max size */
duke@435 704 lowSize, /* DWORD Low word of max size */
duke@435 705 name); /* LPCTSTR name for object */
duke@435 706
duke@435 707 if (fmh == NULL) {
duke@435 708 if (PrintMiscellaneous && Verbose) {
duke@435 709 warning("CreateFileMapping failed, lasterror = %d\n", GetLastError());
duke@435 710 }
duke@435 711 return NULL;
duke@435 712 }
duke@435 713
duke@435 714 if (GetLastError() == ERROR_ALREADY_EXISTS) {
duke@435 715
duke@435 716 // a stale file mapping object was encountered. This object may be
duke@435 717 // owned by this or some other user and cannot be removed until
duke@435 718 // the other processes either exit or close their mapping objects
duke@435 719 // and/or mapped views of this mapping object.
duke@435 720 //
duke@435 721 if (PrintMiscellaneous && Verbose) {
duke@435 722 warning("file mapping already exists, lasterror = %d\n", GetLastError());
duke@435 723 }
duke@435 724
duke@435 725 CloseHandle(fmh);
duke@435 726 return NULL;
duke@435 727 }
duke@435 728
duke@435 729 return fmh;
duke@435 730 }
duke@435 731
duke@435 732
duke@435 733 // method to free the given security descriptor and the contained
duke@435 734 // access control list.
duke@435 735 //
duke@435 736 static void free_security_desc(PSECURITY_DESCRIPTOR pSD) {
duke@435 737
duke@435 738 BOOL success, exists, isdefault;
duke@435 739 PACL pACL;
duke@435 740
duke@435 741 if (pSD != NULL) {
duke@435 742
duke@435 743 // get the access control list from the security descriptor
duke@435 744 success = GetSecurityDescriptorDacl(pSD, &exists, &pACL, &isdefault);
duke@435 745
duke@435 746 // if an ACL existed and it was not a default acl, then it must
duke@435 747 // be an ACL we enlisted. free the resources.
duke@435 748 //
duke@435 749 if (success && exists && pACL != NULL && !isdefault) {
duke@435 750 FREE_C_HEAP_ARRAY(char, pACL);
duke@435 751 }
duke@435 752
duke@435 753 // free the security descriptor
duke@435 754 FREE_C_HEAP_ARRAY(char, pSD);
duke@435 755 }
duke@435 756 }
duke@435 757
duke@435 758 // method to free up a security attributes structure and any
duke@435 759 // contained security descriptors and ACL
duke@435 760 //
duke@435 761 static void free_security_attr(LPSECURITY_ATTRIBUTES lpSA) {
duke@435 762
duke@435 763 if (lpSA != NULL) {
duke@435 764 // free the contained security descriptor and the ACL
duke@435 765 free_security_desc(lpSA->lpSecurityDescriptor);
duke@435 766 lpSA->lpSecurityDescriptor = NULL;
duke@435 767
duke@435 768 // free the security attributes structure
duke@435 769 FREE_C_HEAP_ARRAY(char, lpSA);
duke@435 770 }
duke@435 771 }
duke@435 772
duke@435 773 // get the user SID for the process indicated by the process handle
duke@435 774 //
duke@435 775 static PSID get_user_sid(HANDLE hProcess) {
duke@435 776
duke@435 777 HANDLE hAccessToken;
duke@435 778 PTOKEN_USER token_buf = NULL;
duke@435 779 DWORD rsize = 0;
duke@435 780
duke@435 781 if (hProcess == NULL) {
duke@435 782 return NULL;
duke@435 783 }
duke@435 784
duke@435 785 // get the process token
duke@435 786 if (!OpenProcessToken(hProcess, TOKEN_READ, &hAccessToken)) {
duke@435 787 if (PrintMiscellaneous && Verbose) {
duke@435 788 warning("OpenProcessToken failure: lasterror = %d \n", GetLastError());
duke@435 789 }
duke@435 790 return NULL;
duke@435 791 }
duke@435 792
duke@435 793 // determine the size of the token structured needed to retrieve
duke@435 794 // the user token information from the access token.
duke@435 795 //
duke@435 796 if (!GetTokenInformation(hAccessToken, TokenUser, NULL, rsize, &rsize)) {
duke@435 797 DWORD lasterror = GetLastError();
duke@435 798 if (lasterror != ERROR_INSUFFICIENT_BUFFER) {
duke@435 799 if (PrintMiscellaneous && Verbose) {
duke@435 800 warning("GetTokenInformation failure: lasterror = %d,"
duke@435 801 " rsize = %d\n", lasterror, rsize);
duke@435 802 }
duke@435 803 CloseHandle(hAccessToken);
duke@435 804 return NULL;
duke@435 805 }
duke@435 806 }
duke@435 807
duke@435 808 token_buf = (PTOKEN_USER) NEW_C_HEAP_ARRAY(char, rsize);
duke@435 809
duke@435 810 // get the user token information
duke@435 811 if (!GetTokenInformation(hAccessToken, TokenUser, token_buf, rsize, &rsize)) {
duke@435 812 if (PrintMiscellaneous && Verbose) {
duke@435 813 warning("GetTokenInformation failure: lasterror = %d,"
duke@435 814 " rsize = %d\n", GetLastError(), rsize);
duke@435 815 }
duke@435 816 FREE_C_HEAP_ARRAY(char, token_buf);
duke@435 817 CloseHandle(hAccessToken);
duke@435 818 return NULL;
duke@435 819 }
duke@435 820
duke@435 821 DWORD nbytes = GetLengthSid(token_buf->User.Sid);
duke@435 822 PSID pSID = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 823
duke@435 824 if (!CopySid(nbytes, pSID, token_buf->User.Sid)) {
duke@435 825 if (PrintMiscellaneous && Verbose) {
duke@435 826 warning("GetTokenInformation failure: lasterror = %d,"
duke@435 827 " rsize = %d\n", GetLastError(), rsize);
duke@435 828 }
duke@435 829 FREE_C_HEAP_ARRAY(char, token_buf);
duke@435 830 FREE_C_HEAP_ARRAY(char, pSID);
duke@435 831 CloseHandle(hAccessToken);
duke@435 832 return NULL;
duke@435 833 }
duke@435 834
duke@435 835 // close the access token.
duke@435 836 CloseHandle(hAccessToken);
duke@435 837 FREE_C_HEAP_ARRAY(char, token_buf);
duke@435 838
duke@435 839 return pSID;
duke@435 840 }
duke@435 841
duke@435 842 // structure used to consolidate access control entry information
duke@435 843 //
duke@435 844 typedef struct ace_data {
duke@435 845 PSID pSid; // SID of the ACE
duke@435 846 DWORD mask; // mask for the ACE
duke@435 847 } ace_data_t;
duke@435 848
duke@435 849
duke@435 850 // method to add an allow access control entry with the access rights
duke@435 851 // indicated in mask for the principal indicated in SID to the given
duke@435 852 // security descriptor. Much of the DACL handling was adapted from
duke@435 853 // the example provided here:
duke@435 854 // http://support.microsoft.com/kb/102102/EN-US/
duke@435 855 //
duke@435 856
duke@435 857 static bool add_allow_aces(PSECURITY_DESCRIPTOR pSD,
duke@435 858 ace_data_t aces[], int ace_count) {
duke@435 859 PACL newACL = NULL;
duke@435 860 PACL oldACL = NULL;
duke@435 861
duke@435 862 if (pSD == NULL) {
duke@435 863 return false;
duke@435 864 }
duke@435 865
duke@435 866 BOOL exists, isdefault;
duke@435 867
duke@435 868 // retrieve any existing access control list.
duke@435 869 if (!GetSecurityDescriptorDacl(pSD, &exists, &oldACL, &isdefault)) {
duke@435 870 if (PrintMiscellaneous && Verbose) {
duke@435 871 warning("GetSecurityDescriptor failure: lasterror = %d \n",
duke@435 872 GetLastError());
duke@435 873 }
duke@435 874 return false;
duke@435 875 }
duke@435 876
duke@435 877 // get the size of the DACL
duke@435 878 ACL_SIZE_INFORMATION aclinfo;
duke@435 879
duke@435 880 // GetSecurityDescriptorDacl may return true value for exists (lpbDaclPresent)
duke@435 881 // while oldACL is NULL for some case.
duke@435 882 if (oldACL == NULL) {
duke@435 883 exists = FALSE;
duke@435 884 }
duke@435 885
duke@435 886 if (exists) {
duke@435 887 if (!GetAclInformation(oldACL, &aclinfo,
duke@435 888 sizeof(ACL_SIZE_INFORMATION),
duke@435 889 AclSizeInformation)) {
duke@435 890 if (PrintMiscellaneous && Verbose) {
duke@435 891 warning("GetAclInformation failure: lasterror = %d \n", GetLastError());
duke@435 892 return false;
duke@435 893 }
duke@435 894 }
duke@435 895 } else {
duke@435 896 aclinfo.AceCount = 0; // assume NULL DACL
duke@435 897 aclinfo.AclBytesFree = 0;
duke@435 898 aclinfo.AclBytesInUse = sizeof(ACL);
duke@435 899 }
duke@435 900
duke@435 901 // compute the size needed for the new ACL
duke@435 902 // initial size of ACL is sum of the following:
duke@435 903 // * size of ACL structure.
duke@435 904 // * size of each ACE structure that ACL is to contain minus the sid
duke@435 905 // sidStart member (DWORD) of the ACE.
duke@435 906 // * length of the SID that each ACE is to contain.
duke@435 907 DWORD newACLsize = aclinfo.AclBytesInUse +
duke@435 908 (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) * ace_count;
duke@435 909 for (int i = 0; i < ace_count; i++) {
poonam@2310 910 assert(aces[i].pSid != 0, "pSid should not be 0");
duke@435 911 newACLsize += GetLengthSid(aces[i].pSid);
duke@435 912 }
duke@435 913
duke@435 914 // create the new ACL
duke@435 915 newACL = (PACL) NEW_C_HEAP_ARRAY(char, newACLsize);
duke@435 916
duke@435 917 if (!InitializeAcl(newACL, newACLsize, ACL_REVISION)) {
duke@435 918 if (PrintMiscellaneous && Verbose) {
duke@435 919 warning("InitializeAcl failure: lasterror = %d \n", GetLastError());
duke@435 920 }
duke@435 921 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 922 return false;
duke@435 923 }
duke@435 924
duke@435 925 unsigned int ace_index = 0;
duke@435 926 // copy any existing ACEs from the old ACL (if any) to the new ACL.
duke@435 927 if (aclinfo.AceCount != 0) {
duke@435 928 while (ace_index < aclinfo.AceCount) {
duke@435 929 LPVOID ace;
duke@435 930 if (!GetAce(oldACL, ace_index, &ace)) {
duke@435 931 if (PrintMiscellaneous && Verbose) {
duke@435 932 warning("InitializeAcl failure: lasterror = %d \n", GetLastError());
duke@435 933 }
duke@435 934 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 935 return false;
duke@435 936 }
duke@435 937 if (((ACCESS_ALLOWED_ACE *)ace)->Header.AceFlags && INHERITED_ACE) {
duke@435 938 // this is an inherited, allowed ACE; break from loop so we can
duke@435 939 // add the new access allowed, non-inherited ACE in the correct
duke@435 940 // position, immediately following all non-inherited ACEs.
duke@435 941 break;
duke@435 942 }
duke@435 943
duke@435 944 // determine if the SID of this ACE matches any of the SIDs
duke@435 945 // for which we plan to set ACEs.
duke@435 946 int matches = 0;
duke@435 947 for (int i = 0; i < ace_count; i++) {
duke@435 948 if (EqualSid(aces[i].pSid, &(((ACCESS_ALLOWED_ACE *)ace)->SidStart))) {
duke@435 949 matches++;
duke@435 950 break;
duke@435 951 }
duke@435 952 }
duke@435 953
duke@435 954 // if there are no SID matches, then add this existing ACE to the new ACL
duke@435 955 if (matches == 0) {
duke@435 956 if (!AddAce(newACL, ACL_REVISION, MAXDWORD, ace,
duke@435 957 ((PACE_HEADER)ace)->AceSize)) {
duke@435 958 if (PrintMiscellaneous && Verbose) {
duke@435 959 warning("AddAce failure: lasterror = %d \n", GetLastError());
duke@435 960 }
duke@435 961 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 962 return false;
duke@435 963 }
duke@435 964 }
duke@435 965 ace_index++;
duke@435 966 }
duke@435 967 }
duke@435 968
duke@435 969 // add the passed-in access control entries to the new ACL
duke@435 970 for (int i = 0; i < ace_count; i++) {
duke@435 971 if (!AddAccessAllowedAce(newACL, ACL_REVISION,
duke@435 972 aces[i].mask, aces[i].pSid)) {
duke@435 973 if (PrintMiscellaneous && Verbose) {
duke@435 974 warning("AddAccessAllowedAce failure: lasterror = %d \n",
duke@435 975 GetLastError());
duke@435 976 }
duke@435 977 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 978 return false;
duke@435 979 }
duke@435 980 }
duke@435 981
duke@435 982 // now copy the rest of the inherited ACEs from the old ACL
duke@435 983 if (aclinfo.AceCount != 0) {
duke@435 984 // picking up at ace_index, where we left off in the
duke@435 985 // previous ace_index loop
duke@435 986 while (ace_index < aclinfo.AceCount) {
duke@435 987 LPVOID ace;
duke@435 988 if (!GetAce(oldACL, ace_index, &ace)) {
duke@435 989 if (PrintMiscellaneous && Verbose) {
duke@435 990 warning("InitializeAcl failure: lasterror = %d \n", GetLastError());
duke@435 991 }
duke@435 992 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 993 return false;
duke@435 994 }
duke@435 995 if (!AddAce(newACL, ACL_REVISION, MAXDWORD, ace,
duke@435 996 ((PACE_HEADER)ace)->AceSize)) {
duke@435 997 if (PrintMiscellaneous && Verbose) {
duke@435 998 warning("AddAce failure: lasterror = %d \n", GetLastError());
duke@435 999 }
duke@435 1000 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 1001 return false;
duke@435 1002 }
duke@435 1003 ace_index++;
duke@435 1004 }
duke@435 1005 }
duke@435 1006
duke@435 1007 // add the new ACL to the security descriptor.
duke@435 1008 if (!SetSecurityDescriptorDacl(pSD, TRUE, newACL, FALSE)) {
duke@435 1009 if (PrintMiscellaneous && Verbose) {
duke@435 1010 warning("SetSecurityDescriptorDacl failure:"
duke@435 1011 " lasterror = %d \n", GetLastError());
duke@435 1012 }
duke@435 1013 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 1014 return false;
duke@435 1015 }
duke@435 1016
twisti@1040 1017 // if running on windows 2000 or later, set the automatic inheritance
duke@435 1018 // control flags.
duke@435 1019 SetSecurityDescriptorControlFnPtr _SetSecurityDescriptorControl;
duke@435 1020 _SetSecurityDescriptorControl = (SetSecurityDescriptorControlFnPtr)
duke@435 1021 GetProcAddress(GetModuleHandle(TEXT("advapi32.dll")),
duke@435 1022 "SetSecurityDescriptorControl");
duke@435 1023
duke@435 1024 if (_SetSecurityDescriptorControl != NULL) {
twisti@1040 1025 // We do not want to further propagate inherited DACLs, so making them
duke@435 1026 // protected prevents that.
duke@435 1027 if (!_SetSecurityDescriptorControl(pSD, SE_DACL_PROTECTED,
duke@435 1028 SE_DACL_PROTECTED)) {
duke@435 1029 if (PrintMiscellaneous && Verbose) {
duke@435 1030 warning("SetSecurityDescriptorControl failure:"
duke@435 1031 " lasterror = %d \n", GetLastError());
duke@435 1032 }
duke@435 1033 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 1034 return false;
duke@435 1035 }
duke@435 1036 }
duke@435 1037 // Note, the security descriptor maintains a reference to the newACL, not
duke@435 1038 // a copy of it. Therefore, the newACL is not freed here. It is freed when
duke@435 1039 // the security descriptor containing its reference is freed.
duke@435 1040 //
duke@435 1041 return true;
duke@435 1042 }
duke@435 1043
duke@435 1044 // method to create a security attributes structure, which contains a
duke@435 1045 // security descriptor and an access control list comprised of 0 or more
duke@435 1046 // access control entries. The method take an array of ace_data structures
duke@435 1047 // that indicate the ACE to be added to the security descriptor.
duke@435 1048 //
duke@435 1049 // the caller must free the resources associated with the security
duke@435 1050 // attributes structure created by this method by calling the
duke@435 1051 // free_security_attr() method.
duke@435 1052 //
duke@435 1053 static LPSECURITY_ATTRIBUTES make_security_attr(ace_data_t aces[], int count) {
duke@435 1054
duke@435 1055 // allocate space for a security descriptor
duke@435 1056 PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR)
duke@435 1057 NEW_C_HEAP_ARRAY(char, SECURITY_DESCRIPTOR_MIN_LENGTH);
duke@435 1058
duke@435 1059 // initialize the security descriptor
duke@435 1060 if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) {
duke@435 1061 if (PrintMiscellaneous && Verbose) {
duke@435 1062 warning("InitializeSecurityDescriptor failure: "
duke@435 1063 "lasterror = %d \n", GetLastError());
duke@435 1064 }
duke@435 1065 free_security_desc(pSD);
duke@435 1066 return NULL;
duke@435 1067 }
duke@435 1068
duke@435 1069 // add the access control entries
duke@435 1070 if (!add_allow_aces(pSD, aces, count)) {
duke@435 1071 free_security_desc(pSD);
duke@435 1072 return NULL;
duke@435 1073 }
duke@435 1074
duke@435 1075 // allocate and initialize the security attributes structure and
duke@435 1076 // return it to the caller.
duke@435 1077 //
duke@435 1078 LPSECURITY_ATTRIBUTES lpSA = (LPSECURITY_ATTRIBUTES)
duke@435 1079 NEW_C_HEAP_ARRAY(char, sizeof(SECURITY_ATTRIBUTES));
duke@435 1080 lpSA->nLength = sizeof(SECURITY_ATTRIBUTES);
duke@435 1081 lpSA->lpSecurityDescriptor = pSD;
duke@435 1082 lpSA->bInheritHandle = FALSE;
duke@435 1083
duke@435 1084 return(lpSA);
duke@435 1085 }
duke@435 1086
duke@435 1087 // method to create a security attributes structure with a restrictive
duke@435 1088 // access control list that creates a set access rights for the user/owner
duke@435 1089 // of the securable object and a separate set access rights for everyone else.
duke@435 1090 // also provides for full access rights for the administrator group.
duke@435 1091 //
duke@435 1092 // the caller must free the resources associated with the security
duke@435 1093 // attributes structure created by this method by calling the
duke@435 1094 // free_security_attr() method.
duke@435 1095 //
duke@435 1096
duke@435 1097 static LPSECURITY_ATTRIBUTES make_user_everybody_admin_security_attr(
duke@435 1098 DWORD umask, DWORD emask, DWORD amask) {
duke@435 1099
duke@435 1100 ace_data_t aces[3];
duke@435 1101
duke@435 1102 // initialize the user ace data
duke@435 1103 aces[0].pSid = get_user_sid(GetCurrentProcess());
duke@435 1104 aces[0].mask = umask;
duke@435 1105
poonam@2310 1106 if (aces[0].pSid == 0)
poonam@2310 1107 return NULL;
poonam@2310 1108
duke@435 1109 // get the well known SID for BUILTIN\Administrators
duke@435 1110 PSID administratorsSid = NULL;
duke@435 1111 SID_IDENTIFIER_AUTHORITY SIDAuthAdministrators = SECURITY_NT_AUTHORITY;
duke@435 1112
duke@435 1113 if (!AllocateAndInitializeSid( &SIDAuthAdministrators, 2,
duke@435 1114 SECURITY_BUILTIN_DOMAIN_RID,
duke@435 1115 DOMAIN_ALIAS_RID_ADMINS,
duke@435 1116 0, 0, 0, 0, 0, 0, &administratorsSid)) {
duke@435 1117
duke@435 1118 if (PrintMiscellaneous && Verbose) {
duke@435 1119 warning("AllocateAndInitializeSid failure: "
duke@435 1120 "lasterror = %d \n", GetLastError());
duke@435 1121 }
duke@435 1122 return NULL;
duke@435 1123 }
duke@435 1124
duke@435 1125 // initialize the ace data for administrator group
duke@435 1126 aces[1].pSid = administratorsSid;
duke@435 1127 aces[1].mask = amask;
duke@435 1128
duke@435 1129 // get the well known SID for the universal Everybody
duke@435 1130 PSID everybodySid = NULL;
duke@435 1131 SID_IDENTIFIER_AUTHORITY SIDAuthEverybody = SECURITY_WORLD_SID_AUTHORITY;
duke@435 1132
duke@435 1133 if (!AllocateAndInitializeSid( &SIDAuthEverybody, 1, SECURITY_WORLD_RID,
duke@435 1134 0, 0, 0, 0, 0, 0, 0, &everybodySid)) {
duke@435 1135
duke@435 1136 if (PrintMiscellaneous && Verbose) {
duke@435 1137 warning("AllocateAndInitializeSid failure: "
duke@435 1138 "lasterror = %d \n", GetLastError());
duke@435 1139 }
duke@435 1140 return NULL;
duke@435 1141 }
duke@435 1142
duke@435 1143 // initialize the ace data for everybody else.
duke@435 1144 aces[2].pSid = everybodySid;
duke@435 1145 aces[2].mask = emask;
duke@435 1146
duke@435 1147 // create a security attributes structure with access control
duke@435 1148 // entries as initialized above.
duke@435 1149 LPSECURITY_ATTRIBUTES lpSA = make_security_attr(aces, 3);
duke@435 1150 FREE_C_HEAP_ARRAY(char, aces[0].pSid);
duke@435 1151 FreeSid(everybodySid);
duke@435 1152 FreeSid(administratorsSid);
duke@435 1153 return(lpSA);
duke@435 1154 }
duke@435 1155
duke@435 1156
duke@435 1157 // method to create the security attributes structure for restricting
duke@435 1158 // access to the user temporary directory.
duke@435 1159 //
duke@435 1160 // the caller must free the resources associated with the security
duke@435 1161 // attributes structure created by this method by calling the
duke@435 1162 // free_security_attr() method.
duke@435 1163 //
duke@435 1164 static LPSECURITY_ATTRIBUTES make_tmpdir_security_attr() {
duke@435 1165
duke@435 1166 // create full access rights for the user/owner of the directory
duke@435 1167 // and read-only access rights for everybody else. This is
duke@435 1168 // effectively equivalent to UNIX 755 permissions on a directory.
duke@435 1169 //
duke@435 1170 DWORD umask = STANDARD_RIGHTS_REQUIRED | FILE_ALL_ACCESS;
duke@435 1171 DWORD emask = GENERIC_READ | FILE_LIST_DIRECTORY | FILE_TRAVERSE;
duke@435 1172 DWORD amask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS;
duke@435 1173
duke@435 1174 return make_user_everybody_admin_security_attr(umask, emask, amask);
duke@435 1175 }
duke@435 1176
duke@435 1177 // method to create the security attributes structure for restricting
duke@435 1178 // access to the shared memory backing store file.
duke@435 1179 //
duke@435 1180 // the caller must free the resources associated with the security
duke@435 1181 // attributes structure created by this method by calling the
duke@435 1182 // free_security_attr() method.
duke@435 1183 //
duke@435 1184 static LPSECURITY_ATTRIBUTES make_file_security_attr() {
duke@435 1185
duke@435 1186 // create extensive access rights for the user/owner of the file
duke@435 1187 // and attribute read-only access rights for everybody else. This
duke@435 1188 // is effectively equivalent to UNIX 600 permissions on a file.
duke@435 1189 //
duke@435 1190 DWORD umask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS;
duke@435 1191 DWORD emask = STANDARD_RIGHTS_READ | FILE_READ_ATTRIBUTES |
duke@435 1192 FILE_READ_EA | FILE_LIST_DIRECTORY | FILE_TRAVERSE;
duke@435 1193 DWORD amask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS;
duke@435 1194
duke@435 1195 return make_user_everybody_admin_security_attr(umask, emask, amask);
duke@435 1196 }
duke@435 1197
duke@435 1198 // method to create the security attributes structure for restricting
duke@435 1199 // access to the name shared memory file mapping object.
duke@435 1200 //
duke@435 1201 // the caller must free the resources associated with the security
duke@435 1202 // attributes structure created by this method by calling the
duke@435 1203 // free_security_attr() method.
duke@435 1204 //
duke@435 1205 static LPSECURITY_ATTRIBUTES make_smo_security_attr() {
duke@435 1206
duke@435 1207 // create extensive access rights for the user/owner of the shared
duke@435 1208 // memory object and attribute read-only access rights for everybody
duke@435 1209 // else. This is effectively equivalent to UNIX 600 permissions on
duke@435 1210 // on the shared memory object.
duke@435 1211 //
duke@435 1212 DWORD umask = STANDARD_RIGHTS_REQUIRED | FILE_MAP_ALL_ACCESS;
duke@435 1213 DWORD emask = STANDARD_RIGHTS_READ; // attributes only
duke@435 1214 DWORD amask = STANDARD_RIGHTS_ALL | FILE_MAP_ALL_ACCESS;
duke@435 1215
duke@435 1216 return make_user_everybody_admin_security_attr(umask, emask, amask);
duke@435 1217 }
duke@435 1218
duke@435 1219 // make the user specific temporary directory
duke@435 1220 //
duke@435 1221 static bool make_user_tmp_dir(const char* dirname) {
duke@435 1222
duke@435 1223
duke@435 1224 LPSECURITY_ATTRIBUTES pDirSA = make_tmpdir_security_attr();
duke@435 1225 if (pDirSA == NULL) {
duke@435 1226 return false;
duke@435 1227 }
duke@435 1228
duke@435 1229
duke@435 1230 // create the directory with the given security attributes
duke@435 1231 if (!CreateDirectory(dirname, pDirSA)) {
duke@435 1232 DWORD lasterror = GetLastError();
duke@435 1233 if (lasterror == ERROR_ALREADY_EXISTS) {
duke@435 1234 // The directory already exists and was probably created by another
duke@435 1235 // JVM instance. However, this could also be the result of a
duke@435 1236 // deliberate symlink. Verify that the existing directory is safe.
duke@435 1237 //
duke@435 1238 if (!is_directory_secure(dirname)) {
duke@435 1239 // directory is not secure
duke@435 1240 if (PrintMiscellaneous && Verbose) {
duke@435 1241 warning("%s directory is insecure\n", dirname);
duke@435 1242 }
duke@435 1243 return false;
duke@435 1244 }
duke@435 1245 // The administrator should be able to delete this directory.
duke@435 1246 // But the directory created by previous version of JVM may not
duke@435 1247 // have permission for administrators to delete this directory.
duke@435 1248 // So add full permission to the administrator. Also setting new
duke@435 1249 // DACLs might fix the corrupted the DACLs.
duke@435 1250 SECURITY_INFORMATION secInfo = DACL_SECURITY_INFORMATION;
duke@435 1251 if (!SetFileSecurity(dirname, secInfo, pDirSA->lpSecurityDescriptor)) {
duke@435 1252 if (PrintMiscellaneous && Verbose) {
duke@435 1253 lasterror = GetLastError();
duke@435 1254 warning("SetFileSecurity failed for %s directory. lasterror %d \n",
duke@435 1255 dirname, lasterror);
duke@435 1256 }
duke@435 1257 }
duke@435 1258 }
duke@435 1259 else {
duke@435 1260 if (PrintMiscellaneous && Verbose) {
duke@435 1261 warning("CreateDirectory failed: %d\n", GetLastError());
duke@435 1262 }
duke@435 1263 return false;
duke@435 1264 }
duke@435 1265 }
duke@435 1266
duke@435 1267 // free the security attributes structure
duke@435 1268 free_security_attr(pDirSA);
duke@435 1269
duke@435 1270 return true;
duke@435 1271 }
duke@435 1272
duke@435 1273 // create the shared memory resources
duke@435 1274 //
duke@435 1275 // This function creates the shared memory resources. This includes
duke@435 1276 // the backing store file and the file mapping shared memory object.
duke@435 1277 //
duke@435 1278 static HANDLE create_sharedmem_resources(const char* dirname, const char* filename, const char* objectname, size_t size) {
duke@435 1279
duke@435 1280 HANDLE fh = INVALID_HANDLE_VALUE;
duke@435 1281 HANDLE fmh = NULL;
duke@435 1282
duke@435 1283
duke@435 1284 // create the security attributes for the backing store file
duke@435 1285 LPSECURITY_ATTRIBUTES lpFileSA = make_file_security_attr();
duke@435 1286 if (lpFileSA == NULL) {
duke@435 1287 return NULL;
duke@435 1288 }
duke@435 1289
duke@435 1290 // create the security attributes for the shared memory object
duke@435 1291 LPSECURITY_ATTRIBUTES lpSmoSA = make_smo_security_attr();
duke@435 1292 if (lpSmoSA == NULL) {
duke@435 1293 free_security_attr(lpFileSA);
duke@435 1294 return NULL;
duke@435 1295 }
duke@435 1296
duke@435 1297 // create the user temporary directory
duke@435 1298 if (!make_user_tmp_dir(dirname)) {
duke@435 1299 // could not make/find the directory or the found directory
duke@435 1300 // was not secure
duke@435 1301 return NULL;
duke@435 1302 }
duke@435 1303
duke@435 1304 // Create the file - the FILE_FLAG_DELETE_ON_CLOSE flag allows the
duke@435 1305 // file to be deleted by the last process that closes its handle to
duke@435 1306 // the file. This is important as the apis do not allow a terminating
duke@435 1307 // JVM being monitored by another process to remove the file name.
duke@435 1308 //
duke@435 1309 // the FILE_SHARE_DELETE share mode is valid only in winnt
duke@435 1310 //
duke@435 1311 fh = CreateFile(
duke@435 1312 filename, /* LPCTSTR file name */
duke@435 1313
duke@435 1314 GENERIC_READ|GENERIC_WRITE, /* DWORD desired access */
duke@435 1315
duke@435 1316 (os::win32::is_nt() ? FILE_SHARE_DELETE : 0)|
duke@435 1317 FILE_SHARE_READ, /* DWORD share mode, future READONLY
duke@435 1318 * open operations allowed
duke@435 1319 */
duke@435 1320 lpFileSA, /* LPSECURITY security attributes */
duke@435 1321 CREATE_ALWAYS, /* DWORD creation disposition
duke@435 1322 * create file, if it already
duke@435 1323 * exists, overwrite it.
duke@435 1324 */
duke@435 1325 FILE_FLAG_DELETE_ON_CLOSE, /* DWORD flags and attributes */
duke@435 1326
duke@435 1327 NULL); /* HANDLE template file access */
duke@435 1328
duke@435 1329 free_security_attr(lpFileSA);
duke@435 1330
duke@435 1331 if (fh == INVALID_HANDLE_VALUE) {
duke@435 1332 DWORD lasterror = GetLastError();
duke@435 1333 if (PrintMiscellaneous && Verbose) {
duke@435 1334 warning("could not create file %s: %d\n", filename, lasterror);
duke@435 1335 }
duke@435 1336 return NULL;
duke@435 1337 }
duke@435 1338
duke@435 1339 // try to create the file mapping
duke@435 1340 fmh = create_file_mapping(objectname, fh, lpSmoSA, size);
duke@435 1341
duke@435 1342 free_security_attr(lpSmoSA);
duke@435 1343
duke@435 1344 if (fmh == NULL) {
duke@435 1345 // closing the file handle here will decrement the reference count
duke@435 1346 // on the file. When all processes accessing the file close their
duke@435 1347 // handle to it, the reference count will decrement to 0 and the
duke@435 1348 // OS will delete the file. These semantics are requested by the
duke@435 1349 // FILE_FLAG_DELETE_ON_CLOSE flag in CreateFile call above.
duke@435 1350 CloseHandle(fh);
duke@435 1351 fh = NULL;
duke@435 1352 return NULL;
dcubed@2543 1353 } else {
dcubed@2543 1354 // We created the file mapping, but rarely the size of the
dcubed@2543 1355 // backing store file is reported as zero (0) which can cause
dcubed@2543 1356 // failures when trying to use the hsperfdata file.
dcubed@2543 1357 struct stat statbuf;
dcubed@2543 1358 int ret_code = ::stat(filename, &statbuf);
dcubed@2543 1359 if (ret_code == OS_ERR) {
dcubed@2543 1360 if (PrintMiscellaneous && Verbose) {
dcubed@2543 1361 warning("Could not get status information from file %s: %s\n",
dcubed@2543 1362 filename, strerror(errno));
dcubed@2543 1363 }
dcubed@2543 1364 CloseHandle(fmh);
dcubed@2543 1365 CloseHandle(fh);
dcubed@2543 1366 fh = NULL;
dcubed@2543 1367 fmh = NULL;
dcubed@2543 1368 return NULL;
dcubed@2543 1369 }
dcubed@2543 1370
dcubed@2543 1371 // We could always call FlushFileBuffers() but the Microsoft
dcubed@2543 1372 // docs indicate that it is considered expensive so we only
dcubed@2543 1373 // call it when we observe the size as zero (0).
dcubed@2543 1374 if (statbuf.st_size == 0 && FlushFileBuffers(fh) != TRUE) {
dcubed@2543 1375 DWORD lasterror = GetLastError();
dcubed@2543 1376 if (PrintMiscellaneous && Verbose) {
dcubed@2543 1377 warning("could not flush file %s: %d\n", filename, lasterror);
dcubed@2543 1378 }
dcubed@2543 1379 CloseHandle(fmh);
dcubed@2543 1380 CloseHandle(fh);
dcubed@2543 1381 fh = NULL;
dcubed@2543 1382 fmh = NULL;
dcubed@2543 1383 return NULL;
dcubed@2543 1384 }
duke@435 1385 }
duke@435 1386
duke@435 1387 // the file has been successfully created and the file mapping
duke@435 1388 // object has been created.
duke@435 1389 sharedmem_fileHandle = fh;
duke@435 1390 sharedmem_fileName = strdup(filename);
duke@435 1391
duke@435 1392 return fmh;
duke@435 1393 }
duke@435 1394
duke@435 1395 // open the shared memory object for the given vmid.
duke@435 1396 //
duke@435 1397 static HANDLE open_sharedmem_object(const char* objectname, DWORD ofm_access, TRAPS) {
duke@435 1398
duke@435 1399 HANDLE fmh;
duke@435 1400
duke@435 1401 // open the file mapping with the requested mode
duke@435 1402 fmh = OpenFileMapping(
duke@435 1403 ofm_access, /* DWORD access mode */
duke@435 1404 FALSE, /* BOOL inherit flag - Do not allow inherit */
duke@435 1405 objectname); /* name for object */
duke@435 1406
duke@435 1407 if (fmh == NULL) {
duke@435 1408 if (PrintMiscellaneous && Verbose) {
duke@435 1409 warning("OpenFileMapping failed for shared memory object %s:"
duke@435 1410 " lasterror = %d\n", objectname, GetLastError());
duke@435 1411 }
duke@435 1412 THROW_MSG_(vmSymbols::java_lang_Exception(),
duke@435 1413 "Could not open PerfMemory", INVALID_HANDLE_VALUE);
duke@435 1414 }
duke@435 1415
duke@435 1416 return fmh;;
duke@435 1417 }
duke@435 1418
duke@435 1419 // create a named shared memory region
duke@435 1420 //
duke@435 1421 // On Win32, a named shared memory object has a name space that
duke@435 1422 // is independent of the file system name space. Shared memory object,
duke@435 1423 // or more precisely, file mapping objects, provide no mechanism to
duke@435 1424 // inquire the size of the memory region. There is also no api to
duke@435 1425 // enumerate the memory regions for various processes.
duke@435 1426 //
duke@435 1427 // This implementation utilizes the shared memory name space in parallel
duke@435 1428 // with the file system name space. This allows us to determine the
duke@435 1429 // size of the shared memory region from the size of the file and it
duke@435 1430 // allows us to provide a common, file system based name space for
duke@435 1431 // shared memory across platforms.
duke@435 1432 //
duke@435 1433 static char* mapping_create_shared(size_t size) {
duke@435 1434
duke@435 1435 void *mapAddress;
duke@435 1436 int vmid = os::current_process_id();
duke@435 1437
duke@435 1438 // get the name of the user associated with this process
duke@435 1439 char* user = get_user_name();
duke@435 1440
duke@435 1441 if (user == NULL) {
duke@435 1442 return NULL;
duke@435 1443 }
duke@435 1444
duke@435 1445 // construct the name of the user specific temporary directory
duke@435 1446 char* dirname = get_user_tmp_dir(user);
duke@435 1447
duke@435 1448 // check that the file system is secure - i.e. it supports ACLs.
duke@435 1449 if (!is_filesystem_secure(dirname)) {
duke@435 1450 return NULL;
duke@435 1451 }
duke@435 1452
duke@435 1453 // create the names of the backing store files and for the
duke@435 1454 // share memory object.
duke@435 1455 //
duke@435 1456 char* filename = get_sharedmem_filename(dirname, vmid);
duke@435 1457 char* objectname = get_sharedmem_objectname(user, vmid);
duke@435 1458
duke@435 1459 // cleanup any stale shared memory resources
duke@435 1460 cleanup_sharedmem_resources(dirname);
duke@435 1461
duke@435 1462 assert(((size != 0) && (size % os::vm_page_size() == 0)),
duke@435 1463 "unexpected PerfMemry region size");
duke@435 1464
duke@435 1465 FREE_C_HEAP_ARRAY(char, user);
duke@435 1466
duke@435 1467 // create the shared memory resources
duke@435 1468 sharedmem_fileMapHandle =
duke@435 1469 create_sharedmem_resources(dirname, filename, objectname, size);
duke@435 1470
duke@435 1471 FREE_C_HEAP_ARRAY(char, filename);
duke@435 1472 FREE_C_HEAP_ARRAY(char, objectname);
duke@435 1473 FREE_C_HEAP_ARRAY(char, dirname);
duke@435 1474
duke@435 1475 if (sharedmem_fileMapHandle == NULL) {
duke@435 1476 return NULL;
duke@435 1477 }
duke@435 1478
duke@435 1479 // map the file into the address space
duke@435 1480 mapAddress = MapViewOfFile(
duke@435 1481 sharedmem_fileMapHandle, /* HANDLE = file mapping object */
duke@435 1482 FILE_MAP_ALL_ACCESS, /* DWORD access flags */
duke@435 1483 0, /* DWORD High word of offset */
duke@435 1484 0, /* DWORD Low word of offset */
duke@435 1485 (DWORD)size); /* DWORD Number of bytes to map */
duke@435 1486
duke@435 1487 if (mapAddress == NULL) {
duke@435 1488 if (PrintMiscellaneous && Verbose) {
duke@435 1489 warning("MapViewOfFile failed, lasterror = %d\n", GetLastError());
duke@435 1490 }
duke@435 1491 CloseHandle(sharedmem_fileMapHandle);
duke@435 1492 sharedmem_fileMapHandle = NULL;
duke@435 1493 return NULL;
duke@435 1494 }
duke@435 1495
duke@435 1496 // clear the shared memory region
duke@435 1497 (void)memset(mapAddress, '\0', size);
duke@435 1498
duke@435 1499 return (char*) mapAddress;
duke@435 1500 }
duke@435 1501
duke@435 1502 // this method deletes the file mapping object.
duke@435 1503 //
duke@435 1504 static void delete_file_mapping(char* addr, size_t size) {
duke@435 1505
duke@435 1506 // cleanup the persistent shared memory resources. since DestroyJavaVM does
duke@435 1507 // not support unloading of the JVM, unmapping of the memory resource is not
duke@435 1508 // performed. The memory will be reclaimed by the OS upon termination of all
duke@435 1509 // processes mapping the resource. The file mapping handle and the file
duke@435 1510 // handle are closed here to expedite the remove of the file by the OS. The
duke@435 1511 // file is not removed directly because it was created with
duke@435 1512 // FILE_FLAG_DELETE_ON_CLOSE semantics and any attempt to remove it would
duke@435 1513 // be unsuccessful.
duke@435 1514
duke@435 1515 // close the fileMapHandle. the file mapping will still be retained
duke@435 1516 // by the OS as long as any other JVM processes has an open file mapping
duke@435 1517 // handle or a mapped view of the file.
duke@435 1518 //
duke@435 1519 if (sharedmem_fileMapHandle != NULL) {
duke@435 1520 CloseHandle(sharedmem_fileMapHandle);
duke@435 1521 sharedmem_fileMapHandle = NULL;
duke@435 1522 }
duke@435 1523
duke@435 1524 // close the file handle. This will decrement the reference count on the
duke@435 1525 // backing store file. When the reference count decrements to 0, the OS
duke@435 1526 // will delete the file. These semantics apply because the file was
duke@435 1527 // created with the FILE_FLAG_DELETE_ON_CLOSE flag.
duke@435 1528 //
duke@435 1529 if (sharedmem_fileHandle != INVALID_HANDLE_VALUE) {
duke@435 1530 CloseHandle(sharedmem_fileHandle);
duke@435 1531 sharedmem_fileHandle = INVALID_HANDLE_VALUE;
duke@435 1532 }
duke@435 1533 }
duke@435 1534
duke@435 1535 // this method determines the size of the shared memory file
duke@435 1536 //
duke@435 1537 static size_t sharedmem_filesize(const char* filename, TRAPS) {
duke@435 1538
duke@435 1539 struct stat statbuf;
duke@435 1540
duke@435 1541 // get the file size
duke@435 1542 //
duke@435 1543 // on win95/98/me, _stat returns a file size of 0 bytes, but on
duke@435 1544 // winnt/2k the appropriate file size is returned. support for
duke@435 1545 // the sharable aspects of performance counters was abandonded
duke@435 1546 // on the non-nt win32 platforms due to this and other api
duke@435 1547 // inconsistencies
duke@435 1548 //
duke@435 1549 if (::stat(filename, &statbuf) == OS_ERR) {
duke@435 1550 if (PrintMiscellaneous && Verbose) {
duke@435 1551 warning("stat %s failed: %s\n", filename, strerror(errno));
duke@435 1552 }
duke@435 1553 THROW_MSG_0(vmSymbols::java_io_IOException(),
duke@435 1554 "Could not determine PerfMemory size");
duke@435 1555 }
duke@435 1556
duke@435 1557 if ((statbuf.st_size == 0) || (statbuf.st_size % os::vm_page_size() != 0)) {
duke@435 1558 if (PrintMiscellaneous && Verbose) {
duke@435 1559 warning("unexpected file size: size = " SIZE_FORMAT "\n",
duke@435 1560 statbuf.st_size);
duke@435 1561 }
duke@435 1562 THROW_MSG_0(vmSymbols::java_lang_Exception(),
duke@435 1563 "Invalid PerfMemory size");
duke@435 1564 }
duke@435 1565
duke@435 1566 return statbuf.st_size;
duke@435 1567 }
duke@435 1568
duke@435 1569 // this method opens a file mapping object and maps the object
duke@435 1570 // into the address space of the process
duke@435 1571 //
duke@435 1572 static void open_file_mapping(const char* user, int vmid,
duke@435 1573 PerfMemory::PerfMemoryMode mode,
duke@435 1574 char** addrp, size_t* sizep, TRAPS) {
duke@435 1575
duke@435 1576 ResourceMark rm;
duke@435 1577
duke@435 1578 void *mapAddress = 0;
duke@435 1579 size_t size;
duke@435 1580 HANDLE fmh;
duke@435 1581 DWORD ofm_access;
duke@435 1582 DWORD mv_access;
duke@435 1583 const char* luser = NULL;
duke@435 1584
duke@435 1585 if (mode == PerfMemory::PERF_MODE_RO) {
duke@435 1586 ofm_access = FILE_MAP_READ;
duke@435 1587 mv_access = FILE_MAP_READ;
duke@435 1588 }
duke@435 1589 else if (mode == PerfMemory::PERF_MODE_RW) {
duke@435 1590 #ifdef LATER
duke@435 1591 ofm_access = FILE_MAP_READ | FILE_MAP_WRITE;
duke@435 1592 mv_access = FILE_MAP_READ | FILE_MAP_WRITE;
duke@435 1593 #else
duke@435 1594 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1595 "Unsupported access mode");
duke@435 1596 #endif
duke@435 1597 }
duke@435 1598 else {
duke@435 1599 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1600 "Illegal access mode");
duke@435 1601 }
duke@435 1602
duke@435 1603 // if a user name wasn't specified, then find the user name for
duke@435 1604 // the owner of the target vm.
duke@435 1605 if (user == NULL || strlen(user) == 0) {
duke@435 1606 luser = get_user_name(vmid);
duke@435 1607 }
duke@435 1608 else {
duke@435 1609 luser = user;
duke@435 1610 }
duke@435 1611
duke@435 1612 if (luser == NULL) {
duke@435 1613 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1614 "Could not map vmid to user name");
duke@435 1615 }
duke@435 1616
duke@435 1617 // get the names for the resources for the target vm
duke@435 1618 char* dirname = get_user_tmp_dir(luser);
duke@435 1619
duke@435 1620 // since we don't follow symbolic links when creating the backing
duke@435 1621 // store file, we also don't following them when attaching
duke@435 1622 //
duke@435 1623 if (!is_directory_secure(dirname)) {
duke@435 1624 FREE_C_HEAP_ARRAY(char, dirname);
duke@435 1625 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1626 "Process not found");
duke@435 1627 }
duke@435 1628
duke@435 1629 char* filename = get_sharedmem_filename(dirname, vmid);
duke@435 1630 char* objectname = get_sharedmem_objectname(luser, vmid);
duke@435 1631
duke@435 1632 // copy heap memory to resource memory. the objectname and
duke@435 1633 // filename are passed to methods that may throw exceptions.
duke@435 1634 // using resource arrays for these names prevents the leaks
duke@435 1635 // that would otherwise occur.
duke@435 1636 //
duke@435 1637 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
duke@435 1638 char* robjectname = NEW_RESOURCE_ARRAY(char, strlen(objectname) + 1);
duke@435 1639 strcpy(rfilename, filename);
duke@435 1640 strcpy(robjectname, objectname);
duke@435 1641
duke@435 1642 // free the c heap resources that are no longer needed
duke@435 1643 if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
duke@435 1644 FREE_C_HEAP_ARRAY(char, dirname);
duke@435 1645 FREE_C_HEAP_ARRAY(char, filename);
duke@435 1646 FREE_C_HEAP_ARRAY(char, objectname);
duke@435 1647
duke@435 1648 if (*sizep == 0) {
duke@435 1649 size = sharedmem_filesize(rfilename, CHECK);
duke@435 1650 assert(size != 0, "unexpected size");
duke@435 1651 }
duke@435 1652
duke@435 1653 // Open the file mapping object with the given name
duke@435 1654 fmh = open_sharedmem_object(robjectname, ofm_access, CHECK);
duke@435 1655
duke@435 1656 assert(fmh != INVALID_HANDLE_VALUE, "unexpected handle value");
duke@435 1657
duke@435 1658 // map the entire file into the address space
duke@435 1659 mapAddress = MapViewOfFile(
duke@435 1660 fmh, /* HANDLE Handle of file mapping object */
duke@435 1661 mv_access, /* DWORD access flags */
duke@435 1662 0, /* DWORD High word of offset */
duke@435 1663 0, /* DWORD Low word of offset */
duke@435 1664 size); /* DWORD Number of bytes to map */
duke@435 1665
duke@435 1666 if (mapAddress == NULL) {
duke@435 1667 if (PrintMiscellaneous && Verbose) {
duke@435 1668 warning("MapViewOfFile failed, lasterror = %d\n", GetLastError());
duke@435 1669 }
duke@435 1670 CloseHandle(fmh);
duke@435 1671 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
duke@435 1672 "Could not map PerfMemory");
duke@435 1673 }
duke@435 1674
duke@435 1675 *addrp = (char*)mapAddress;
duke@435 1676 *sizep = size;
duke@435 1677
duke@435 1678 // File mapping object can be closed at this time without
duke@435 1679 // invalidating the mapped view of the file
duke@435 1680 CloseHandle(fmh);
duke@435 1681
duke@435 1682 if (PerfTraceMemOps) {
duke@435 1683 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
duke@435 1684 INTPTR_FORMAT "\n", size, vmid, mapAddress);
duke@435 1685 }
duke@435 1686 }
duke@435 1687
duke@435 1688 // this method unmaps the the mapped view of the the
duke@435 1689 // file mapping object.
duke@435 1690 //
duke@435 1691 static void remove_file_mapping(char* addr) {
duke@435 1692
duke@435 1693 // the file mapping object was closed in open_file_mapping()
duke@435 1694 // after the file map view was created. We only need to
duke@435 1695 // unmap the file view here.
duke@435 1696 UnmapViewOfFile(addr);
duke@435 1697 }
duke@435 1698
duke@435 1699 // create the PerfData memory region in shared memory.
duke@435 1700 static char* create_shared_memory(size_t size) {
duke@435 1701
duke@435 1702 return mapping_create_shared(size);
duke@435 1703 }
duke@435 1704
duke@435 1705 // release a named, shared memory region
duke@435 1706 //
duke@435 1707 void delete_shared_memory(char* addr, size_t size) {
duke@435 1708
duke@435 1709 delete_file_mapping(addr, size);
duke@435 1710 }
duke@435 1711
duke@435 1712
duke@435 1713
duke@435 1714
duke@435 1715 // create the PerfData memory region
duke@435 1716 //
duke@435 1717 // This method creates the memory region used to store performance
duke@435 1718 // data for the JVM. The memory may be created in standard or
duke@435 1719 // shared memory.
duke@435 1720 //
duke@435 1721 void PerfMemory::create_memory_region(size_t size) {
duke@435 1722
duke@435 1723 if (PerfDisableSharedMem || !os::win32::is_nt()) {
duke@435 1724 // do not share the memory for the performance data.
duke@435 1725 PerfDisableSharedMem = true;
duke@435 1726 _start = create_standard_memory(size);
duke@435 1727 }
duke@435 1728 else {
duke@435 1729 _start = create_shared_memory(size);
duke@435 1730 if (_start == NULL) {
duke@435 1731
duke@435 1732 // creation of the shared memory region failed, attempt
duke@435 1733 // to create a contiguous, non-shared memory region instead.
duke@435 1734 //
duke@435 1735 if (PrintMiscellaneous && Verbose) {
duke@435 1736 warning("Reverting to non-shared PerfMemory region.\n");
duke@435 1737 }
duke@435 1738 PerfDisableSharedMem = true;
duke@435 1739 _start = create_standard_memory(size);
duke@435 1740 }
duke@435 1741 }
duke@435 1742
duke@435 1743 if (_start != NULL) _capacity = size;
duke@435 1744
duke@435 1745 }
duke@435 1746
duke@435 1747 // delete the PerfData memory region
duke@435 1748 //
duke@435 1749 // This method deletes the memory region used to store performance
duke@435 1750 // data for the JVM. The memory region indicated by the <address, size>
duke@435 1751 // tuple will be inaccessible after a call to this method.
duke@435 1752 //
duke@435 1753 void PerfMemory::delete_memory_region() {
duke@435 1754
duke@435 1755 assert((start() != NULL && capacity() > 0), "verify proper state");
duke@435 1756
duke@435 1757 // If user specifies PerfDataSaveFile, it will save the performance data
duke@435 1758 // to the specified file name no matter whether PerfDataSaveToFile is specified
duke@435 1759 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
duke@435 1760 // -XX:+PerfDataSaveToFile.
duke@435 1761 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
duke@435 1762 save_memory_to_file(start(), capacity());
duke@435 1763 }
duke@435 1764
duke@435 1765 if (PerfDisableSharedMem) {
duke@435 1766 delete_standard_memory(start(), capacity());
duke@435 1767 }
duke@435 1768 else {
duke@435 1769 delete_shared_memory(start(), capacity());
duke@435 1770 }
duke@435 1771 }
duke@435 1772
duke@435 1773 // attach to the PerfData memory region for another JVM
duke@435 1774 //
duke@435 1775 // This method returns an <address, size> tuple that points to
duke@435 1776 // a memory buffer that is kept reasonably synchronized with
duke@435 1777 // the PerfData memory region for the indicated JVM. This
duke@435 1778 // buffer may be kept in synchronization via shared memory
duke@435 1779 // or some other mechanism that keeps the buffer updated.
duke@435 1780 //
duke@435 1781 // If the JVM chooses not to support the attachability feature,
duke@435 1782 // this method should throw an UnsupportedOperation exception.
duke@435 1783 //
duke@435 1784 // This implementation utilizes named shared memory to map
duke@435 1785 // the indicated process's PerfData memory region into this JVMs
duke@435 1786 // address space.
duke@435 1787 //
duke@435 1788 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode,
duke@435 1789 char** addrp, size_t* sizep, TRAPS) {
duke@435 1790
duke@435 1791 if (vmid == 0 || vmid == os::current_process_id()) {
duke@435 1792 *addrp = start();
duke@435 1793 *sizep = capacity();
duke@435 1794 return;
duke@435 1795 }
duke@435 1796
duke@435 1797 open_file_mapping(user, vmid, mode, addrp, sizep, CHECK);
duke@435 1798 }
duke@435 1799
duke@435 1800 // detach from the PerfData memory region of another JVM
duke@435 1801 //
duke@435 1802 // This method detaches the PerfData memory region of another
duke@435 1803 // JVM, specified as an <address, size> tuple of a buffer
duke@435 1804 // in this process's address space. This method may perform
duke@435 1805 // arbitrary actions to accomplish the detachment. The memory
duke@435 1806 // region specified by <address, size> will be inaccessible after
duke@435 1807 // a call to this method.
duke@435 1808 //
duke@435 1809 // If the JVM chooses not to support the attachability feature,
duke@435 1810 // this method should throw an UnsupportedOperation exception.
duke@435 1811 //
duke@435 1812 // This implementation utilizes named shared memory to detach
duke@435 1813 // the indicated process's PerfData memory region from this
duke@435 1814 // process's address space.
duke@435 1815 //
duke@435 1816 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
duke@435 1817
duke@435 1818 assert(addr != 0, "address sanity check");
duke@435 1819 assert(bytes > 0, "capacity sanity check");
duke@435 1820
duke@435 1821 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
duke@435 1822 // prevent accidental detachment of this process's PerfMemory region
duke@435 1823 return;
duke@435 1824 }
duke@435 1825
duke@435 1826 remove_file_mapping(addr);
duke@435 1827 }
duke@435 1828
duke@435 1829 char* PerfMemory::backing_store_filename() {
duke@435 1830 return sharedmem_fileName;
duke@435 1831 }

mercurial