src/os/windows/vm/perfMemory_windows.cpp

Wed, 15 Dec 2010 07:11:31 -0800

author
sla
date
Wed, 15 Dec 2010 07:11:31 -0800
changeset 2369
aa6e219afbf1
parent 2314
f95d63e2154a
child 2543
de14f1eee390
permissions
-rw-r--r--

7006354: Updates to Visual Studio project creation and development launcher
Summary: Updates to Visual Studio project creation and development launcher
Reviewed-by: stefank, coleenp

duke@435 1 /*
stefank@2314 2 * Copyright (c) 2001, 2010, 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
duke@435 301 char* oldest_user = NULL;
duke@435 302 time_t oldest_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
duke@435 378 // compare and save filename with latest creation time
duke@435 379 if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
duke@435 380
duke@435 381 if (statbuf.st_ctime > oldest_ctime) {
duke@435 382 char* user = strchr(dentry->d_name, '_') + 1;
duke@435 383
duke@435 384 if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user);
duke@435 385 oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1);
duke@435 386
duke@435 387 strcpy(oldest_user, user);
duke@435 388 oldest_ctime = statbuf.st_ctime;
duke@435 389 }
duke@435 390 }
duke@435 391
duke@435 392 FREE_C_HEAP_ARRAY(char, filename);
duke@435 393 }
duke@435 394 }
duke@435 395 os::closedir(subdirp);
duke@435 396 FREE_C_HEAP_ARRAY(char, udbuf);
duke@435 397 FREE_C_HEAP_ARRAY(char, usrdir_name);
duke@435 398 }
duke@435 399 os::closedir(tmpdirp);
duke@435 400 FREE_C_HEAP_ARRAY(char, tdbuf);
duke@435 401
duke@435 402 return(oldest_user);
duke@435 403 }
duke@435 404
duke@435 405 // return the name of the user that owns the process identified by vmid.
duke@435 406 //
duke@435 407 // note: this method should only be used via the Perf native methods.
duke@435 408 // There are various costs to this method and limiting its use to the
duke@435 409 // Perf native methods limits the impact to monitoring applications only.
duke@435 410 //
duke@435 411 static char* get_user_name(int vmid) {
duke@435 412
duke@435 413 // A fast implementation is not provided at this time. It's possible
duke@435 414 // to provide a fast process id to user name mapping function using
duke@435 415 // the win32 apis, but the default ACL for the process object only
duke@435 416 // allows processes with the same owner SID to acquire the process
duke@435 417 // handle (via OpenProcess(PROCESS_QUERY_INFORMATION)). It's possible
duke@435 418 // to have the JVM change the ACL for the process object to allow arbitrary
duke@435 419 // users to access the process handle and the process security token.
duke@435 420 // The security ramifications need to be studied before providing this
duke@435 421 // mechanism.
duke@435 422 //
duke@435 423 return get_user_name_slow(vmid);
duke@435 424 }
duke@435 425
duke@435 426 // return the name of the shared memory file mapping object for the
duke@435 427 // named shared memory region for the given user name and vmid.
duke@435 428 //
duke@435 429 // The file mapping object's name is not the file name. It is a name
duke@435 430 // in a separate name space.
duke@435 431 //
duke@435 432 // the caller is expected to free the allocated memory.
duke@435 433 //
duke@435 434 static char *get_sharedmem_objectname(const char* user, int vmid) {
duke@435 435
duke@435 436 // construct file mapping object's name, add 3 for two '_' and a
duke@435 437 // null terminator.
duke@435 438 int nbytes = (int)strlen(PERFDATA_NAME) + (int)strlen(user) + 3;
duke@435 439
duke@435 440 // the id is converted to an unsigned value here because win32 allows
duke@435 441 // negative process ids. However, OpenFileMapping API complains
duke@435 442 // about a name containing a '-' characters.
duke@435 443 //
duke@435 444 nbytes += UINT_CHARS;
duke@435 445 char* name = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 446 _snprintf(name, nbytes, "%s_%s_%u", PERFDATA_NAME, user, vmid);
duke@435 447
duke@435 448 return name;
duke@435 449 }
duke@435 450
duke@435 451 // return the file name of the backing store file for the named
duke@435 452 // shared memory region for the given user name and vmid.
duke@435 453 //
duke@435 454 // the caller is expected to free the allocated memory.
duke@435 455 //
duke@435 456 static char* get_sharedmem_filename(const char* dirname, int vmid) {
duke@435 457
duke@435 458 // add 2 for the file separator and a null terminator.
duke@435 459 size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
duke@435 460
duke@435 461 char* name = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 462 _snprintf(name, nbytes, "%s\\%d", dirname, vmid);
duke@435 463
duke@435 464 return name;
duke@435 465 }
duke@435 466
duke@435 467 // remove file
duke@435 468 //
duke@435 469 // this method removes the file with the given file name.
duke@435 470 //
duke@435 471 // Note: if the indicated file is on an SMB network file system, this
duke@435 472 // method may be unsuccessful in removing the file.
duke@435 473 //
duke@435 474 static void remove_file(const char* dirname, const char* filename) {
duke@435 475
duke@435 476 size_t nbytes = strlen(dirname) + strlen(filename) + 2;
duke@435 477 char* path = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 478
duke@435 479 strcpy(path, dirname);
duke@435 480 strcat(path, "\\");
duke@435 481 strcat(path, filename);
duke@435 482
duke@435 483 if (::unlink(path) == OS_ERR) {
duke@435 484 if (PrintMiscellaneous && Verbose) {
duke@435 485 if (errno != ENOENT) {
duke@435 486 warning("Could not unlink shared memory backing"
duke@435 487 " store file %s : %s\n", path, strerror(errno));
duke@435 488 }
duke@435 489 }
duke@435 490 }
duke@435 491
duke@435 492 FREE_C_HEAP_ARRAY(char, path);
duke@435 493 }
duke@435 494
duke@435 495 // returns true if the process represented by pid is alive, otherwise
duke@435 496 // returns false. the validity of the result is only accurate if the
duke@435 497 // target process is owned by the same principal that owns this process.
duke@435 498 // this method should not be used if to test the status of an otherwise
duke@435 499 // arbitrary process unless it is know that this process has the appropriate
duke@435 500 // privileges to guarantee a result valid.
duke@435 501 //
duke@435 502 static bool is_alive(int pid) {
duke@435 503
duke@435 504 HANDLE ph = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
duke@435 505 if (ph == NULL) {
duke@435 506 // the process does not exist.
duke@435 507 if (PrintMiscellaneous && Verbose) {
duke@435 508 DWORD lastError = GetLastError();
duke@435 509 if (lastError != ERROR_INVALID_PARAMETER) {
duke@435 510 warning("OpenProcess failed: %d\n", GetLastError());
duke@435 511 }
duke@435 512 }
duke@435 513 return false;
duke@435 514 }
duke@435 515
duke@435 516 DWORD exit_status;
duke@435 517 if (!GetExitCodeProcess(ph, &exit_status)) {
duke@435 518 if (PrintMiscellaneous && Verbose) {
duke@435 519 warning("GetExitCodeProcess failed: %d\n", GetLastError());
duke@435 520 }
duke@435 521 CloseHandle(ph);
duke@435 522 return false;
duke@435 523 }
duke@435 524
duke@435 525 CloseHandle(ph);
duke@435 526 return (exit_status == STILL_ACTIVE) ? true : false;
duke@435 527 }
duke@435 528
duke@435 529 // check if the file system is considered secure for the backing store files
duke@435 530 //
duke@435 531 static bool is_filesystem_secure(const char* path) {
duke@435 532
duke@435 533 char root_path[MAX_PATH];
duke@435 534 char fs_type[MAX_PATH];
duke@435 535
duke@435 536 if (PerfBypassFileSystemCheck) {
duke@435 537 if (PrintMiscellaneous && Verbose) {
duke@435 538 warning("bypassing file system criteria checks for %s\n", path);
duke@435 539 }
duke@435 540 return true;
duke@435 541 }
duke@435 542
duke@435 543 char* first_colon = strchr((char *)path, ':');
duke@435 544 if (first_colon == NULL) {
duke@435 545 if (PrintMiscellaneous && Verbose) {
duke@435 546 warning("expected device specifier in path: %s\n", path);
duke@435 547 }
duke@435 548 return false;
duke@435 549 }
duke@435 550
duke@435 551 size_t len = (size_t)(first_colon - path);
duke@435 552 assert(len + 2 <= MAX_PATH, "unexpected device specifier length");
duke@435 553 strncpy(root_path, path, len + 1);
duke@435 554 root_path[len + 1] = '\\';
duke@435 555 root_path[len + 2] = '\0';
duke@435 556
duke@435 557 // check that we have something like "C:\" or "AA:\"
duke@435 558 assert(strlen(root_path) >= 3, "device specifier too short");
duke@435 559 assert(strchr(root_path, ':') != NULL, "bad device specifier format");
duke@435 560 assert(strchr(root_path, '\\') != NULL, "bad device specifier format");
duke@435 561
duke@435 562 DWORD maxpath;
duke@435 563 DWORD flags;
duke@435 564
duke@435 565 if (!GetVolumeInformation(root_path, NULL, 0, NULL, &maxpath,
duke@435 566 &flags, fs_type, MAX_PATH)) {
duke@435 567 // we can't get information about the volume, so assume unsafe.
duke@435 568 if (PrintMiscellaneous && Verbose) {
duke@435 569 warning("could not get device information for %s: "
duke@435 570 " path = %s: lasterror = %d\n",
duke@435 571 root_path, path, GetLastError());
duke@435 572 }
duke@435 573 return false;
duke@435 574 }
duke@435 575
duke@435 576 if ((flags & FS_PERSISTENT_ACLS) == 0) {
duke@435 577 // file system doesn't support ACLs, declare file system unsafe
duke@435 578 if (PrintMiscellaneous && Verbose) {
duke@435 579 warning("file system type %s on device %s does not support"
duke@435 580 " ACLs\n", fs_type, root_path);
duke@435 581 }
duke@435 582 return false;
duke@435 583 }
duke@435 584
duke@435 585 if ((flags & FS_VOL_IS_COMPRESSED) != 0) {
duke@435 586 // file system is compressed, declare file system unsafe
duke@435 587 if (PrintMiscellaneous && Verbose) {
duke@435 588 warning("file system type %s on device %s is compressed\n",
duke@435 589 fs_type, root_path);
duke@435 590 }
duke@435 591 return false;
duke@435 592 }
duke@435 593
duke@435 594 return true;
duke@435 595 }
duke@435 596
duke@435 597 // cleanup stale shared memory resources
duke@435 598 //
duke@435 599 // This method attempts to remove all stale shared memory files in
duke@435 600 // the named user temporary directory. It scans the named directory
duke@435 601 // for files matching the pattern ^$[0-9]*$. For each file found, the
duke@435 602 // process id is extracted from the file name and a test is run to
duke@435 603 // determine if the process is alive. If the process is not alive,
duke@435 604 // any stale file resources are removed.
duke@435 605 //
duke@435 606 static void cleanup_sharedmem_resources(const char* dirname) {
duke@435 607
duke@435 608 // open the user temp directory
duke@435 609 DIR* dirp = os::opendir(dirname);
duke@435 610
duke@435 611 if (dirp == NULL) {
duke@435 612 // directory doesn't exist, so there is nothing to cleanup
duke@435 613 return;
duke@435 614 }
duke@435 615
duke@435 616 if (!is_directory_secure(dirname)) {
duke@435 617 // the directory is not secure, don't attempt any cleanup
duke@435 618 return;
duke@435 619 }
duke@435 620
duke@435 621 // for each entry in the directory that matches the expected file
duke@435 622 // name pattern, determine if the file resources are stale and if
duke@435 623 // so, remove the file resources. Note, instrumented HotSpot processes
duke@435 624 // for this user may start and/or terminate during this search and
duke@435 625 // remove or create new files in this directory. The behavior of this
duke@435 626 // loop under these conditions is dependent upon the implementation of
duke@435 627 // opendir/readdir.
duke@435 628 //
duke@435 629 struct dirent* entry;
duke@435 630 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname));
duke@435 631 errno = 0;
duke@435 632 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
duke@435 633
duke@435 634 int pid = filename_to_pid(entry->d_name);
duke@435 635
duke@435 636 if (pid == 0) {
duke@435 637
duke@435 638 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
duke@435 639
duke@435 640 // attempt to remove all unexpected files, except "." and ".."
duke@435 641 remove_file(dirname, entry->d_name);
duke@435 642 }
duke@435 643
duke@435 644 errno = 0;
duke@435 645 continue;
duke@435 646 }
duke@435 647
duke@435 648 // we now have a file name that converts to a valid integer
duke@435 649 // that could represent a process id . if this process id
duke@435 650 // matches the current process id or the process is not running,
duke@435 651 // then remove the stale file resources.
duke@435 652 //
duke@435 653 // process liveness is detected by checking the exit status
duke@435 654 // of the process. if the process id is valid and the exit status
duke@435 655 // indicates that it is still running, the file file resources
duke@435 656 // are not removed. If the process id is invalid, or if we don't
duke@435 657 // have permissions to check the process status, or if the process
duke@435 658 // id is valid and the process has terminated, the the file resources
duke@435 659 // are assumed to be stale and are removed.
duke@435 660 //
duke@435 661 if (pid == os::current_process_id() || !is_alive(pid)) {
duke@435 662
duke@435 663 // we can only remove the file resources. Any mapped views
duke@435 664 // of the file can only be unmapped by the processes that
duke@435 665 // opened those views and the file mapping object will not
duke@435 666 // get removed until all views are unmapped.
duke@435 667 //
duke@435 668 remove_file(dirname, entry->d_name);
duke@435 669 }
duke@435 670 errno = 0;
duke@435 671 }
duke@435 672 os::closedir(dirp);
duke@435 673 FREE_C_HEAP_ARRAY(char, dbuf);
duke@435 674 }
duke@435 675
duke@435 676 // create a file mapping object with the requested name, and size
duke@435 677 // from the file represented by the given Handle object
duke@435 678 //
duke@435 679 static HANDLE create_file_mapping(const char* name, HANDLE fh, LPSECURITY_ATTRIBUTES fsa, size_t size) {
duke@435 680
duke@435 681 DWORD lowSize = (DWORD)size;
duke@435 682 DWORD highSize = 0;
duke@435 683 HANDLE fmh = NULL;
duke@435 684
duke@435 685 // Create a file mapping object with the given name. This function
duke@435 686 // will grow the file to the specified size.
duke@435 687 //
duke@435 688 fmh = CreateFileMapping(
duke@435 689 fh, /* HANDLE file handle for backing store */
duke@435 690 fsa, /* LPSECURITY_ATTRIBUTES Not inheritable */
duke@435 691 PAGE_READWRITE, /* DWORD protections */
duke@435 692 highSize, /* DWORD High word of max size */
duke@435 693 lowSize, /* DWORD Low word of max size */
duke@435 694 name); /* LPCTSTR name for object */
duke@435 695
duke@435 696 if (fmh == NULL) {
duke@435 697 if (PrintMiscellaneous && Verbose) {
duke@435 698 warning("CreateFileMapping failed, lasterror = %d\n", GetLastError());
duke@435 699 }
duke@435 700 return NULL;
duke@435 701 }
duke@435 702
duke@435 703 if (GetLastError() == ERROR_ALREADY_EXISTS) {
duke@435 704
duke@435 705 // a stale file mapping object was encountered. This object may be
duke@435 706 // owned by this or some other user and cannot be removed until
duke@435 707 // the other processes either exit or close their mapping objects
duke@435 708 // and/or mapped views of this mapping object.
duke@435 709 //
duke@435 710 if (PrintMiscellaneous && Verbose) {
duke@435 711 warning("file mapping already exists, lasterror = %d\n", GetLastError());
duke@435 712 }
duke@435 713
duke@435 714 CloseHandle(fmh);
duke@435 715 return NULL;
duke@435 716 }
duke@435 717
duke@435 718 return fmh;
duke@435 719 }
duke@435 720
duke@435 721
duke@435 722 // method to free the given security descriptor and the contained
duke@435 723 // access control list.
duke@435 724 //
duke@435 725 static void free_security_desc(PSECURITY_DESCRIPTOR pSD) {
duke@435 726
duke@435 727 BOOL success, exists, isdefault;
duke@435 728 PACL pACL;
duke@435 729
duke@435 730 if (pSD != NULL) {
duke@435 731
duke@435 732 // get the access control list from the security descriptor
duke@435 733 success = GetSecurityDescriptorDacl(pSD, &exists, &pACL, &isdefault);
duke@435 734
duke@435 735 // if an ACL existed and it was not a default acl, then it must
duke@435 736 // be an ACL we enlisted. free the resources.
duke@435 737 //
duke@435 738 if (success && exists && pACL != NULL && !isdefault) {
duke@435 739 FREE_C_HEAP_ARRAY(char, pACL);
duke@435 740 }
duke@435 741
duke@435 742 // free the security descriptor
duke@435 743 FREE_C_HEAP_ARRAY(char, pSD);
duke@435 744 }
duke@435 745 }
duke@435 746
duke@435 747 // method to free up a security attributes structure and any
duke@435 748 // contained security descriptors and ACL
duke@435 749 //
duke@435 750 static void free_security_attr(LPSECURITY_ATTRIBUTES lpSA) {
duke@435 751
duke@435 752 if (lpSA != NULL) {
duke@435 753 // free the contained security descriptor and the ACL
duke@435 754 free_security_desc(lpSA->lpSecurityDescriptor);
duke@435 755 lpSA->lpSecurityDescriptor = NULL;
duke@435 756
duke@435 757 // free the security attributes structure
duke@435 758 FREE_C_HEAP_ARRAY(char, lpSA);
duke@435 759 }
duke@435 760 }
duke@435 761
duke@435 762 // get the user SID for the process indicated by the process handle
duke@435 763 //
duke@435 764 static PSID get_user_sid(HANDLE hProcess) {
duke@435 765
duke@435 766 HANDLE hAccessToken;
duke@435 767 PTOKEN_USER token_buf = NULL;
duke@435 768 DWORD rsize = 0;
duke@435 769
duke@435 770 if (hProcess == NULL) {
duke@435 771 return NULL;
duke@435 772 }
duke@435 773
duke@435 774 // get the process token
duke@435 775 if (!OpenProcessToken(hProcess, TOKEN_READ, &hAccessToken)) {
duke@435 776 if (PrintMiscellaneous && Verbose) {
duke@435 777 warning("OpenProcessToken failure: lasterror = %d \n", GetLastError());
duke@435 778 }
duke@435 779 return NULL;
duke@435 780 }
duke@435 781
duke@435 782 // determine the size of the token structured needed to retrieve
duke@435 783 // the user token information from the access token.
duke@435 784 //
duke@435 785 if (!GetTokenInformation(hAccessToken, TokenUser, NULL, rsize, &rsize)) {
duke@435 786 DWORD lasterror = GetLastError();
duke@435 787 if (lasterror != ERROR_INSUFFICIENT_BUFFER) {
duke@435 788 if (PrintMiscellaneous && Verbose) {
duke@435 789 warning("GetTokenInformation failure: lasterror = %d,"
duke@435 790 " rsize = %d\n", lasterror, rsize);
duke@435 791 }
duke@435 792 CloseHandle(hAccessToken);
duke@435 793 return NULL;
duke@435 794 }
duke@435 795 }
duke@435 796
duke@435 797 token_buf = (PTOKEN_USER) NEW_C_HEAP_ARRAY(char, rsize);
duke@435 798
duke@435 799 // get the user token information
duke@435 800 if (!GetTokenInformation(hAccessToken, TokenUser, token_buf, rsize, &rsize)) {
duke@435 801 if (PrintMiscellaneous && Verbose) {
duke@435 802 warning("GetTokenInformation failure: lasterror = %d,"
duke@435 803 " rsize = %d\n", GetLastError(), rsize);
duke@435 804 }
duke@435 805 FREE_C_HEAP_ARRAY(char, token_buf);
duke@435 806 CloseHandle(hAccessToken);
duke@435 807 return NULL;
duke@435 808 }
duke@435 809
duke@435 810 DWORD nbytes = GetLengthSid(token_buf->User.Sid);
duke@435 811 PSID pSID = NEW_C_HEAP_ARRAY(char, nbytes);
duke@435 812
duke@435 813 if (!CopySid(nbytes, pSID, token_buf->User.Sid)) {
duke@435 814 if (PrintMiscellaneous && Verbose) {
duke@435 815 warning("GetTokenInformation failure: lasterror = %d,"
duke@435 816 " rsize = %d\n", GetLastError(), rsize);
duke@435 817 }
duke@435 818 FREE_C_HEAP_ARRAY(char, token_buf);
duke@435 819 FREE_C_HEAP_ARRAY(char, pSID);
duke@435 820 CloseHandle(hAccessToken);
duke@435 821 return NULL;
duke@435 822 }
duke@435 823
duke@435 824 // close the access token.
duke@435 825 CloseHandle(hAccessToken);
duke@435 826 FREE_C_HEAP_ARRAY(char, token_buf);
duke@435 827
duke@435 828 return pSID;
duke@435 829 }
duke@435 830
duke@435 831 // structure used to consolidate access control entry information
duke@435 832 //
duke@435 833 typedef struct ace_data {
duke@435 834 PSID pSid; // SID of the ACE
duke@435 835 DWORD mask; // mask for the ACE
duke@435 836 } ace_data_t;
duke@435 837
duke@435 838
duke@435 839 // method to add an allow access control entry with the access rights
duke@435 840 // indicated in mask for the principal indicated in SID to the given
duke@435 841 // security descriptor. Much of the DACL handling was adapted from
duke@435 842 // the example provided here:
duke@435 843 // http://support.microsoft.com/kb/102102/EN-US/
duke@435 844 //
duke@435 845
duke@435 846 static bool add_allow_aces(PSECURITY_DESCRIPTOR pSD,
duke@435 847 ace_data_t aces[], int ace_count) {
duke@435 848 PACL newACL = NULL;
duke@435 849 PACL oldACL = NULL;
duke@435 850
duke@435 851 if (pSD == NULL) {
duke@435 852 return false;
duke@435 853 }
duke@435 854
duke@435 855 BOOL exists, isdefault;
duke@435 856
duke@435 857 // retrieve any existing access control list.
duke@435 858 if (!GetSecurityDescriptorDacl(pSD, &exists, &oldACL, &isdefault)) {
duke@435 859 if (PrintMiscellaneous && Verbose) {
duke@435 860 warning("GetSecurityDescriptor failure: lasterror = %d \n",
duke@435 861 GetLastError());
duke@435 862 }
duke@435 863 return false;
duke@435 864 }
duke@435 865
duke@435 866 // get the size of the DACL
duke@435 867 ACL_SIZE_INFORMATION aclinfo;
duke@435 868
duke@435 869 // GetSecurityDescriptorDacl may return true value for exists (lpbDaclPresent)
duke@435 870 // while oldACL is NULL for some case.
duke@435 871 if (oldACL == NULL) {
duke@435 872 exists = FALSE;
duke@435 873 }
duke@435 874
duke@435 875 if (exists) {
duke@435 876 if (!GetAclInformation(oldACL, &aclinfo,
duke@435 877 sizeof(ACL_SIZE_INFORMATION),
duke@435 878 AclSizeInformation)) {
duke@435 879 if (PrintMiscellaneous && Verbose) {
duke@435 880 warning("GetAclInformation failure: lasterror = %d \n", GetLastError());
duke@435 881 return false;
duke@435 882 }
duke@435 883 }
duke@435 884 } else {
duke@435 885 aclinfo.AceCount = 0; // assume NULL DACL
duke@435 886 aclinfo.AclBytesFree = 0;
duke@435 887 aclinfo.AclBytesInUse = sizeof(ACL);
duke@435 888 }
duke@435 889
duke@435 890 // compute the size needed for the new ACL
duke@435 891 // initial size of ACL is sum of the following:
duke@435 892 // * size of ACL structure.
duke@435 893 // * size of each ACE structure that ACL is to contain minus the sid
duke@435 894 // sidStart member (DWORD) of the ACE.
duke@435 895 // * length of the SID that each ACE is to contain.
duke@435 896 DWORD newACLsize = aclinfo.AclBytesInUse +
duke@435 897 (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) * ace_count;
duke@435 898 for (int i = 0; i < ace_count; i++) {
poonam@2310 899 assert(aces[i].pSid != 0, "pSid should not be 0");
duke@435 900 newACLsize += GetLengthSid(aces[i].pSid);
duke@435 901 }
duke@435 902
duke@435 903 // create the new ACL
duke@435 904 newACL = (PACL) NEW_C_HEAP_ARRAY(char, newACLsize);
duke@435 905
duke@435 906 if (!InitializeAcl(newACL, newACLsize, ACL_REVISION)) {
duke@435 907 if (PrintMiscellaneous && Verbose) {
duke@435 908 warning("InitializeAcl failure: lasterror = %d \n", GetLastError());
duke@435 909 }
duke@435 910 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 911 return false;
duke@435 912 }
duke@435 913
duke@435 914 unsigned int ace_index = 0;
duke@435 915 // copy any existing ACEs from the old ACL (if any) to the new ACL.
duke@435 916 if (aclinfo.AceCount != 0) {
duke@435 917 while (ace_index < aclinfo.AceCount) {
duke@435 918 LPVOID ace;
duke@435 919 if (!GetAce(oldACL, ace_index, &ace)) {
duke@435 920 if (PrintMiscellaneous && Verbose) {
duke@435 921 warning("InitializeAcl failure: lasterror = %d \n", GetLastError());
duke@435 922 }
duke@435 923 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 924 return false;
duke@435 925 }
duke@435 926 if (((ACCESS_ALLOWED_ACE *)ace)->Header.AceFlags && INHERITED_ACE) {
duke@435 927 // this is an inherited, allowed ACE; break from loop so we can
duke@435 928 // add the new access allowed, non-inherited ACE in the correct
duke@435 929 // position, immediately following all non-inherited ACEs.
duke@435 930 break;
duke@435 931 }
duke@435 932
duke@435 933 // determine if the SID of this ACE matches any of the SIDs
duke@435 934 // for which we plan to set ACEs.
duke@435 935 int matches = 0;
duke@435 936 for (int i = 0; i < ace_count; i++) {
duke@435 937 if (EqualSid(aces[i].pSid, &(((ACCESS_ALLOWED_ACE *)ace)->SidStart))) {
duke@435 938 matches++;
duke@435 939 break;
duke@435 940 }
duke@435 941 }
duke@435 942
duke@435 943 // if there are no SID matches, then add this existing ACE to the new ACL
duke@435 944 if (matches == 0) {
duke@435 945 if (!AddAce(newACL, ACL_REVISION, MAXDWORD, ace,
duke@435 946 ((PACE_HEADER)ace)->AceSize)) {
duke@435 947 if (PrintMiscellaneous && Verbose) {
duke@435 948 warning("AddAce failure: lasterror = %d \n", GetLastError());
duke@435 949 }
duke@435 950 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 951 return false;
duke@435 952 }
duke@435 953 }
duke@435 954 ace_index++;
duke@435 955 }
duke@435 956 }
duke@435 957
duke@435 958 // add the passed-in access control entries to the new ACL
duke@435 959 for (int i = 0; i < ace_count; i++) {
duke@435 960 if (!AddAccessAllowedAce(newACL, ACL_REVISION,
duke@435 961 aces[i].mask, aces[i].pSid)) {
duke@435 962 if (PrintMiscellaneous && Verbose) {
duke@435 963 warning("AddAccessAllowedAce failure: lasterror = %d \n",
duke@435 964 GetLastError());
duke@435 965 }
duke@435 966 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 967 return false;
duke@435 968 }
duke@435 969 }
duke@435 970
duke@435 971 // now copy the rest of the inherited ACEs from the old ACL
duke@435 972 if (aclinfo.AceCount != 0) {
duke@435 973 // picking up at ace_index, where we left off in the
duke@435 974 // previous ace_index loop
duke@435 975 while (ace_index < aclinfo.AceCount) {
duke@435 976 LPVOID ace;
duke@435 977 if (!GetAce(oldACL, ace_index, &ace)) {
duke@435 978 if (PrintMiscellaneous && Verbose) {
duke@435 979 warning("InitializeAcl failure: lasterror = %d \n", GetLastError());
duke@435 980 }
duke@435 981 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 982 return false;
duke@435 983 }
duke@435 984 if (!AddAce(newACL, ACL_REVISION, MAXDWORD, ace,
duke@435 985 ((PACE_HEADER)ace)->AceSize)) {
duke@435 986 if (PrintMiscellaneous && Verbose) {
duke@435 987 warning("AddAce failure: lasterror = %d \n", GetLastError());
duke@435 988 }
duke@435 989 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 990 return false;
duke@435 991 }
duke@435 992 ace_index++;
duke@435 993 }
duke@435 994 }
duke@435 995
duke@435 996 // add the new ACL to the security descriptor.
duke@435 997 if (!SetSecurityDescriptorDacl(pSD, TRUE, newACL, FALSE)) {
duke@435 998 if (PrintMiscellaneous && Verbose) {
duke@435 999 warning("SetSecurityDescriptorDacl failure:"
duke@435 1000 " lasterror = %d \n", GetLastError());
duke@435 1001 }
duke@435 1002 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 1003 return false;
duke@435 1004 }
duke@435 1005
twisti@1040 1006 // if running on windows 2000 or later, set the automatic inheritance
duke@435 1007 // control flags.
duke@435 1008 SetSecurityDescriptorControlFnPtr _SetSecurityDescriptorControl;
duke@435 1009 _SetSecurityDescriptorControl = (SetSecurityDescriptorControlFnPtr)
duke@435 1010 GetProcAddress(GetModuleHandle(TEXT("advapi32.dll")),
duke@435 1011 "SetSecurityDescriptorControl");
duke@435 1012
duke@435 1013 if (_SetSecurityDescriptorControl != NULL) {
twisti@1040 1014 // We do not want to further propagate inherited DACLs, so making them
duke@435 1015 // protected prevents that.
duke@435 1016 if (!_SetSecurityDescriptorControl(pSD, SE_DACL_PROTECTED,
duke@435 1017 SE_DACL_PROTECTED)) {
duke@435 1018 if (PrintMiscellaneous && Verbose) {
duke@435 1019 warning("SetSecurityDescriptorControl failure:"
duke@435 1020 " lasterror = %d \n", GetLastError());
duke@435 1021 }
duke@435 1022 FREE_C_HEAP_ARRAY(char, newACL);
duke@435 1023 return false;
duke@435 1024 }
duke@435 1025 }
duke@435 1026 // Note, the security descriptor maintains a reference to the newACL, not
duke@435 1027 // a copy of it. Therefore, the newACL is not freed here. It is freed when
duke@435 1028 // the security descriptor containing its reference is freed.
duke@435 1029 //
duke@435 1030 return true;
duke@435 1031 }
duke@435 1032
duke@435 1033 // method to create a security attributes structure, which contains a
duke@435 1034 // security descriptor and an access control list comprised of 0 or more
duke@435 1035 // access control entries. The method take an array of ace_data structures
duke@435 1036 // that indicate the ACE to be added to the security descriptor.
duke@435 1037 //
duke@435 1038 // the caller must free the resources associated with the security
duke@435 1039 // attributes structure created by this method by calling the
duke@435 1040 // free_security_attr() method.
duke@435 1041 //
duke@435 1042 static LPSECURITY_ATTRIBUTES make_security_attr(ace_data_t aces[], int count) {
duke@435 1043
duke@435 1044 // allocate space for a security descriptor
duke@435 1045 PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR)
duke@435 1046 NEW_C_HEAP_ARRAY(char, SECURITY_DESCRIPTOR_MIN_LENGTH);
duke@435 1047
duke@435 1048 // initialize the security descriptor
duke@435 1049 if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) {
duke@435 1050 if (PrintMiscellaneous && Verbose) {
duke@435 1051 warning("InitializeSecurityDescriptor failure: "
duke@435 1052 "lasterror = %d \n", GetLastError());
duke@435 1053 }
duke@435 1054 free_security_desc(pSD);
duke@435 1055 return NULL;
duke@435 1056 }
duke@435 1057
duke@435 1058 // add the access control entries
duke@435 1059 if (!add_allow_aces(pSD, aces, count)) {
duke@435 1060 free_security_desc(pSD);
duke@435 1061 return NULL;
duke@435 1062 }
duke@435 1063
duke@435 1064 // allocate and initialize the security attributes structure and
duke@435 1065 // return it to the caller.
duke@435 1066 //
duke@435 1067 LPSECURITY_ATTRIBUTES lpSA = (LPSECURITY_ATTRIBUTES)
duke@435 1068 NEW_C_HEAP_ARRAY(char, sizeof(SECURITY_ATTRIBUTES));
duke@435 1069 lpSA->nLength = sizeof(SECURITY_ATTRIBUTES);
duke@435 1070 lpSA->lpSecurityDescriptor = pSD;
duke@435 1071 lpSA->bInheritHandle = FALSE;
duke@435 1072
duke@435 1073 return(lpSA);
duke@435 1074 }
duke@435 1075
duke@435 1076 // method to create a security attributes structure with a restrictive
duke@435 1077 // access control list that creates a set access rights for the user/owner
duke@435 1078 // of the securable object and a separate set access rights for everyone else.
duke@435 1079 // also provides for full access rights for the administrator group.
duke@435 1080 //
duke@435 1081 // the caller must free the resources associated with the security
duke@435 1082 // attributes structure created by this method by calling the
duke@435 1083 // free_security_attr() method.
duke@435 1084 //
duke@435 1085
duke@435 1086 static LPSECURITY_ATTRIBUTES make_user_everybody_admin_security_attr(
duke@435 1087 DWORD umask, DWORD emask, DWORD amask) {
duke@435 1088
duke@435 1089 ace_data_t aces[3];
duke@435 1090
duke@435 1091 // initialize the user ace data
duke@435 1092 aces[0].pSid = get_user_sid(GetCurrentProcess());
duke@435 1093 aces[0].mask = umask;
duke@435 1094
poonam@2310 1095 if (aces[0].pSid == 0)
poonam@2310 1096 return NULL;
poonam@2310 1097
duke@435 1098 // get the well known SID for BUILTIN\Administrators
duke@435 1099 PSID administratorsSid = NULL;
duke@435 1100 SID_IDENTIFIER_AUTHORITY SIDAuthAdministrators = SECURITY_NT_AUTHORITY;
duke@435 1101
duke@435 1102 if (!AllocateAndInitializeSid( &SIDAuthAdministrators, 2,
duke@435 1103 SECURITY_BUILTIN_DOMAIN_RID,
duke@435 1104 DOMAIN_ALIAS_RID_ADMINS,
duke@435 1105 0, 0, 0, 0, 0, 0, &administratorsSid)) {
duke@435 1106
duke@435 1107 if (PrintMiscellaneous && Verbose) {
duke@435 1108 warning("AllocateAndInitializeSid failure: "
duke@435 1109 "lasterror = %d \n", GetLastError());
duke@435 1110 }
duke@435 1111 return NULL;
duke@435 1112 }
duke@435 1113
duke@435 1114 // initialize the ace data for administrator group
duke@435 1115 aces[1].pSid = administratorsSid;
duke@435 1116 aces[1].mask = amask;
duke@435 1117
duke@435 1118 // get the well known SID for the universal Everybody
duke@435 1119 PSID everybodySid = NULL;
duke@435 1120 SID_IDENTIFIER_AUTHORITY SIDAuthEverybody = SECURITY_WORLD_SID_AUTHORITY;
duke@435 1121
duke@435 1122 if (!AllocateAndInitializeSid( &SIDAuthEverybody, 1, SECURITY_WORLD_RID,
duke@435 1123 0, 0, 0, 0, 0, 0, 0, &everybodySid)) {
duke@435 1124
duke@435 1125 if (PrintMiscellaneous && Verbose) {
duke@435 1126 warning("AllocateAndInitializeSid failure: "
duke@435 1127 "lasterror = %d \n", GetLastError());
duke@435 1128 }
duke@435 1129 return NULL;
duke@435 1130 }
duke@435 1131
duke@435 1132 // initialize the ace data for everybody else.
duke@435 1133 aces[2].pSid = everybodySid;
duke@435 1134 aces[2].mask = emask;
duke@435 1135
duke@435 1136 // create a security attributes structure with access control
duke@435 1137 // entries as initialized above.
duke@435 1138 LPSECURITY_ATTRIBUTES lpSA = make_security_attr(aces, 3);
duke@435 1139 FREE_C_HEAP_ARRAY(char, aces[0].pSid);
duke@435 1140 FreeSid(everybodySid);
duke@435 1141 FreeSid(administratorsSid);
duke@435 1142 return(lpSA);
duke@435 1143 }
duke@435 1144
duke@435 1145
duke@435 1146 // method to create the security attributes structure for restricting
duke@435 1147 // access to the user temporary directory.
duke@435 1148 //
duke@435 1149 // the caller must free the resources associated with the security
duke@435 1150 // attributes structure created by this method by calling the
duke@435 1151 // free_security_attr() method.
duke@435 1152 //
duke@435 1153 static LPSECURITY_ATTRIBUTES make_tmpdir_security_attr() {
duke@435 1154
duke@435 1155 // create full access rights for the user/owner of the directory
duke@435 1156 // and read-only access rights for everybody else. This is
duke@435 1157 // effectively equivalent to UNIX 755 permissions on a directory.
duke@435 1158 //
duke@435 1159 DWORD umask = STANDARD_RIGHTS_REQUIRED | FILE_ALL_ACCESS;
duke@435 1160 DWORD emask = GENERIC_READ | FILE_LIST_DIRECTORY | FILE_TRAVERSE;
duke@435 1161 DWORD amask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS;
duke@435 1162
duke@435 1163 return make_user_everybody_admin_security_attr(umask, emask, amask);
duke@435 1164 }
duke@435 1165
duke@435 1166 // method to create the security attributes structure for restricting
duke@435 1167 // access to the shared memory backing store file.
duke@435 1168 //
duke@435 1169 // the caller must free the resources associated with the security
duke@435 1170 // attributes structure created by this method by calling the
duke@435 1171 // free_security_attr() method.
duke@435 1172 //
duke@435 1173 static LPSECURITY_ATTRIBUTES make_file_security_attr() {
duke@435 1174
duke@435 1175 // create extensive access rights for the user/owner of the file
duke@435 1176 // and attribute read-only access rights for everybody else. This
duke@435 1177 // is effectively equivalent to UNIX 600 permissions on a file.
duke@435 1178 //
duke@435 1179 DWORD umask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS;
duke@435 1180 DWORD emask = STANDARD_RIGHTS_READ | FILE_READ_ATTRIBUTES |
duke@435 1181 FILE_READ_EA | FILE_LIST_DIRECTORY | FILE_TRAVERSE;
duke@435 1182 DWORD amask = STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS;
duke@435 1183
duke@435 1184 return make_user_everybody_admin_security_attr(umask, emask, amask);
duke@435 1185 }
duke@435 1186
duke@435 1187 // method to create the security attributes structure for restricting
duke@435 1188 // access to the name shared memory file mapping object.
duke@435 1189 //
duke@435 1190 // the caller must free the resources associated with the security
duke@435 1191 // attributes structure created by this method by calling the
duke@435 1192 // free_security_attr() method.
duke@435 1193 //
duke@435 1194 static LPSECURITY_ATTRIBUTES make_smo_security_attr() {
duke@435 1195
duke@435 1196 // create extensive access rights for the user/owner of the shared
duke@435 1197 // memory object and attribute read-only access rights for everybody
duke@435 1198 // else. This is effectively equivalent to UNIX 600 permissions on
duke@435 1199 // on the shared memory object.
duke@435 1200 //
duke@435 1201 DWORD umask = STANDARD_RIGHTS_REQUIRED | FILE_MAP_ALL_ACCESS;
duke@435 1202 DWORD emask = STANDARD_RIGHTS_READ; // attributes only
duke@435 1203 DWORD amask = STANDARD_RIGHTS_ALL | FILE_MAP_ALL_ACCESS;
duke@435 1204
duke@435 1205 return make_user_everybody_admin_security_attr(umask, emask, amask);
duke@435 1206 }
duke@435 1207
duke@435 1208 // make the user specific temporary directory
duke@435 1209 //
duke@435 1210 static bool make_user_tmp_dir(const char* dirname) {
duke@435 1211
duke@435 1212
duke@435 1213 LPSECURITY_ATTRIBUTES pDirSA = make_tmpdir_security_attr();
duke@435 1214 if (pDirSA == NULL) {
duke@435 1215 return false;
duke@435 1216 }
duke@435 1217
duke@435 1218
duke@435 1219 // create the directory with the given security attributes
duke@435 1220 if (!CreateDirectory(dirname, pDirSA)) {
duke@435 1221 DWORD lasterror = GetLastError();
duke@435 1222 if (lasterror == ERROR_ALREADY_EXISTS) {
duke@435 1223 // The directory already exists and was probably created by another
duke@435 1224 // JVM instance. However, this could also be the result of a
duke@435 1225 // deliberate symlink. Verify that the existing directory is safe.
duke@435 1226 //
duke@435 1227 if (!is_directory_secure(dirname)) {
duke@435 1228 // directory is not secure
duke@435 1229 if (PrintMiscellaneous && Verbose) {
duke@435 1230 warning("%s directory is insecure\n", dirname);
duke@435 1231 }
duke@435 1232 return false;
duke@435 1233 }
duke@435 1234 // The administrator should be able to delete this directory.
duke@435 1235 // But the directory created by previous version of JVM may not
duke@435 1236 // have permission for administrators to delete this directory.
duke@435 1237 // So add full permission to the administrator. Also setting new
duke@435 1238 // DACLs might fix the corrupted the DACLs.
duke@435 1239 SECURITY_INFORMATION secInfo = DACL_SECURITY_INFORMATION;
duke@435 1240 if (!SetFileSecurity(dirname, secInfo, pDirSA->lpSecurityDescriptor)) {
duke@435 1241 if (PrintMiscellaneous && Verbose) {
duke@435 1242 lasterror = GetLastError();
duke@435 1243 warning("SetFileSecurity failed for %s directory. lasterror %d \n",
duke@435 1244 dirname, lasterror);
duke@435 1245 }
duke@435 1246 }
duke@435 1247 }
duke@435 1248 else {
duke@435 1249 if (PrintMiscellaneous && Verbose) {
duke@435 1250 warning("CreateDirectory failed: %d\n", GetLastError());
duke@435 1251 }
duke@435 1252 return false;
duke@435 1253 }
duke@435 1254 }
duke@435 1255
duke@435 1256 // free the security attributes structure
duke@435 1257 free_security_attr(pDirSA);
duke@435 1258
duke@435 1259 return true;
duke@435 1260 }
duke@435 1261
duke@435 1262 // create the shared memory resources
duke@435 1263 //
duke@435 1264 // This function creates the shared memory resources. This includes
duke@435 1265 // the backing store file and the file mapping shared memory object.
duke@435 1266 //
duke@435 1267 static HANDLE create_sharedmem_resources(const char* dirname, const char* filename, const char* objectname, size_t size) {
duke@435 1268
duke@435 1269 HANDLE fh = INVALID_HANDLE_VALUE;
duke@435 1270 HANDLE fmh = NULL;
duke@435 1271
duke@435 1272
duke@435 1273 // create the security attributes for the backing store file
duke@435 1274 LPSECURITY_ATTRIBUTES lpFileSA = make_file_security_attr();
duke@435 1275 if (lpFileSA == NULL) {
duke@435 1276 return NULL;
duke@435 1277 }
duke@435 1278
duke@435 1279 // create the security attributes for the shared memory object
duke@435 1280 LPSECURITY_ATTRIBUTES lpSmoSA = make_smo_security_attr();
duke@435 1281 if (lpSmoSA == NULL) {
duke@435 1282 free_security_attr(lpFileSA);
duke@435 1283 return NULL;
duke@435 1284 }
duke@435 1285
duke@435 1286 // create the user temporary directory
duke@435 1287 if (!make_user_tmp_dir(dirname)) {
duke@435 1288 // could not make/find the directory or the found directory
duke@435 1289 // was not secure
duke@435 1290 return NULL;
duke@435 1291 }
duke@435 1292
duke@435 1293 // Create the file - the FILE_FLAG_DELETE_ON_CLOSE flag allows the
duke@435 1294 // file to be deleted by the last process that closes its handle to
duke@435 1295 // the file. This is important as the apis do not allow a terminating
duke@435 1296 // JVM being monitored by another process to remove the file name.
duke@435 1297 //
duke@435 1298 // the FILE_SHARE_DELETE share mode is valid only in winnt
duke@435 1299 //
duke@435 1300 fh = CreateFile(
duke@435 1301 filename, /* LPCTSTR file name */
duke@435 1302
duke@435 1303 GENERIC_READ|GENERIC_WRITE, /* DWORD desired access */
duke@435 1304
duke@435 1305 (os::win32::is_nt() ? FILE_SHARE_DELETE : 0)|
duke@435 1306 FILE_SHARE_READ, /* DWORD share mode, future READONLY
duke@435 1307 * open operations allowed
duke@435 1308 */
duke@435 1309 lpFileSA, /* LPSECURITY security attributes */
duke@435 1310 CREATE_ALWAYS, /* DWORD creation disposition
duke@435 1311 * create file, if it already
duke@435 1312 * exists, overwrite it.
duke@435 1313 */
duke@435 1314 FILE_FLAG_DELETE_ON_CLOSE, /* DWORD flags and attributes */
duke@435 1315
duke@435 1316 NULL); /* HANDLE template file access */
duke@435 1317
duke@435 1318 free_security_attr(lpFileSA);
duke@435 1319
duke@435 1320 if (fh == INVALID_HANDLE_VALUE) {
duke@435 1321 DWORD lasterror = GetLastError();
duke@435 1322 if (PrintMiscellaneous && Verbose) {
duke@435 1323 warning("could not create file %s: %d\n", filename, lasterror);
duke@435 1324 }
duke@435 1325 return NULL;
duke@435 1326 }
duke@435 1327
duke@435 1328 // try to create the file mapping
duke@435 1329 fmh = create_file_mapping(objectname, fh, lpSmoSA, size);
duke@435 1330
duke@435 1331 free_security_attr(lpSmoSA);
duke@435 1332
duke@435 1333 if (fmh == NULL) {
duke@435 1334 // closing the file handle here will decrement the reference count
duke@435 1335 // on the file. When all processes accessing the file close their
duke@435 1336 // handle to it, the reference count will decrement to 0 and the
duke@435 1337 // OS will delete the file. These semantics are requested by the
duke@435 1338 // FILE_FLAG_DELETE_ON_CLOSE flag in CreateFile call above.
duke@435 1339 CloseHandle(fh);
duke@435 1340 fh = NULL;
duke@435 1341 return NULL;
duke@435 1342 }
duke@435 1343
duke@435 1344 // the file has been successfully created and the file mapping
duke@435 1345 // object has been created.
duke@435 1346 sharedmem_fileHandle = fh;
duke@435 1347 sharedmem_fileName = strdup(filename);
duke@435 1348
duke@435 1349 return fmh;
duke@435 1350 }
duke@435 1351
duke@435 1352 // open the shared memory object for the given vmid.
duke@435 1353 //
duke@435 1354 static HANDLE open_sharedmem_object(const char* objectname, DWORD ofm_access, TRAPS) {
duke@435 1355
duke@435 1356 HANDLE fmh;
duke@435 1357
duke@435 1358 // open the file mapping with the requested mode
duke@435 1359 fmh = OpenFileMapping(
duke@435 1360 ofm_access, /* DWORD access mode */
duke@435 1361 FALSE, /* BOOL inherit flag - Do not allow inherit */
duke@435 1362 objectname); /* name for object */
duke@435 1363
duke@435 1364 if (fmh == NULL) {
duke@435 1365 if (PrintMiscellaneous && Verbose) {
duke@435 1366 warning("OpenFileMapping failed for shared memory object %s:"
duke@435 1367 " lasterror = %d\n", objectname, GetLastError());
duke@435 1368 }
duke@435 1369 THROW_MSG_(vmSymbols::java_lang_Exception(),
duke@435 1370 "Could not open PerfMemory", INVALID_HANDLE_VALUE);
duke@435 1371 }
duke@435 1372
duke@435 1373 return fmh;;
duke@435 1374 }
duke@435 1375
duke@435 1376 // create a named shared memory region
duke@435 1377 //
duke@435 1378 // On Win32, a named shared memory object has a name space that
duke@435 1379 // is independent of the file system name space. Shared memory object,
duke@435 1380 // or more precisely, file mapping objects, provide no mechanism to
duke@435 1381 // inquire the size of the memory region. There is also no api to
duke@435 1382 // enumerate the memory regions for various processes.
duke@435 1383 //
duke@435 1384 // This implementation utilizes the shared memory name space in parallel
duke@435 1385 // with the file system name space. This allows us to determine the
duke@435 1386 // size of the shared memory region from the size of the file and it
duke@435 1387 // allows us to provide a common, file system based name space for
duke@435 1388 // shared memory across platforms.
duke@435 1389 //
duke@435 1390 static char* mapping_create_shared(size_t size) {
duke@435 1391
duke@435 1392 void *mapAddress;
duke@435 1393 int vmid = os::current_process_id();
duke@435 1394
duke@435 1395 // get the name of the user associated with this process
duke@435 1396 char* user = get_user_name();
duke@435 1397
duke@435 1398 if (user == NULL) {
duke@435 1399 return NULL;
duke@435 1400 }
duke@435 1401
duke@435 1402 // construct the name of the user specific temporary directory
duke@435 1403 char* dirname = get_user_tmp_dir(user);
duke@435 1404
duke@435 1405 // check that the file system is secure - i.e. it supports ACLs.
duke@435 1406 if (!is_filesystem_secure(dirname)) {
duke@435 1407 return NULL;
duke@435 1408 }
duke@435 1409
duke@435 1410 // create the names of the backing store files and for the
duke@435 1411 // share memory object.
duke@435 1412 //
duke@435 1413 char* filename = get_sharedmem_filename(dirname, vmid);
duke@435 1414 char* objectname = get_sharedmem_objectname(user, vmid);
duke@435 1415
duke@435 1416 // cleanup any stale shared memory resources
duke@435 1417 cleanup_sharedmem_resources(dirname);
duke@435 1418
duke@435 1419 assert(((size != 0) && (size % os::vm_page_size() == 0)),
duke@435 1420 "unexpected PerfMemry region size");
duke@435 1421
duke@435 1422 FREE_C_HEAP_ARRAY(char, user);
duke@435 1423
duke@435 1424 // create the shared memory resources
duke@435 1425 sharedmem_fileMapHandle =
duke@435 1426 create_sharedmem_resources(dirname, filename, objectname, size);
duke@435 1427
duke@435 1428 FREE_C_HEAP_ARRAY(char, filename);
duke@435 1429 FREE_C_HEAP_ARRAY(char, objectname);
duke@435 1430 FREE_C_HEAP_ARRAY(char, dirname);
duke@435 1431
duke@435 1432 if (sharedmem_fileMapHandle == NULL) {
duke@435 1433 return NULL;
duke@435 1434 }
duke@435 1435
duke@435 1436 // map the file into the address space
duke@435 1437 mapAddress = MapViewOfFile(
duke@435 1438 sharedmem_fileMapHandle, /* HANDLE = file mapping object */
duke@435 1439 FILE_MAP_ALL_ACCESS, /* DWORD access flags */
duke@435 1440 0, /* DWORD High word of offset */
duke@435 1441 0, /* DWORD Low word of offset */
duke@435 1442 (DWORD)size); /* DWORD Number of bytes to map */
duke@435 1443
duke@435 1444 if (mapAddress == NULL) {
duke@435 1445 if (PrintMiscellaneous && Verbose) {
duke@435 1446 warning("MapViewOfFile failed, lasterror = %d\n", GetLastError());
duke@435 1447 }
duke@435 1448 CloseHandle(sharedmem_fileMapHandle);
duke@435 1449 sharedmem_fileMapHandle = NULL;
duke@435 1450 return NULL;
duke@435 1451 }
duke@435 1452
duke@435 1453 // clear the shared memory region
duke@435 1454 (void)memset(mapAddress, '\0', size);
duke@435 1455
duke@435 1456 return (char*) mapAddress;
duke@435 1457 }
duke@435 1458
duke@435 1459 // this method deletes the file mapping object.
duke@435 1460 //
duke@435 1461 static void delete_file_mapping(char* addr, size_t size) {
duke@435 1462
duke@435 1463 // cleanup the persistent shared memory resources. since DestroyJavaVM does
duke@435 1464 // not support unloading of the JVM, unmapping of the memory resource is not
duke@435 1465 // performed. The memory will be reclaimed by the OS upon termination of all
duke@435 1466 // processes mapping the resource. The file mapping handle and the file
duke@435 1467 // handle are closed here to expedite the remove of the file by the OS. The
duke@435 1468 // file is not removed directly because it was created with
duke@435 1469 // FILE_FLAG_DELETE_ON_CLOSE semantics and any attempt to remove it would
duke@435 1470 // be unsuccessful.
duke@435 1471
duke@435 1472 // close the fileMapHandle. the file mapping will still be retained
duke@435 1473 // by the OS as long as any other JVM processes has an open file mapping
duke@435 1474 // handle or a mapped view of the file.
duke@435 1475 //
duke@435 1476 if (sharedmem_fileMapHandle != NULL) {
duke@435 1477 CloseHandle(sharedmem_fileMapHandle);
duke@435 1478 sharedmem_fileMapHandle = NULL;
duke@435 1479 }
duke@435 1480
duke@435 1481 // close the file handle. This will decrement the reference count on the
duke@435 1482 // backing store file. When the reference count decrements to 0, the OS
duke@435 1483 // will delete the file. These semantics apply because the file was
duke@435 1484 // created with the FILE_FLAG_DELETE_ON_CLOSE flag.
duke@435 1485 //
duke@435 1486 if (sharedmem_fileHandle != INVALID_HANDLE_VALUE) {
duke@435 1487 CloseHandle(sharedmem_fileHandle);
duke@435 1488 sharedmem_fileHandle = INVALID_HANDLE_VALUE;
duke@435 1489 }
duke@435 1490 }
duke@435 1491
duke@435 1492 // this method determines the size of the shared memory file
duke@435 1493 //
duke@435 1494 static size_t sharedmem_filesize(const char* filename, TRAPS) {
duke@435 1495
duke@435 1496 struct stat statbuf;
duke@435 1497
duke@435 1498 // get the file size
duke@435 1499 //
duke@435 1500 // on win95/98/me, _stat returns a file size of 0 bytes, but on
duke@435 1501 // winnt/2k the appropriate file size is returned. support for
duke@435 1502 // the sharable aspects of performance counters was abandonded
duke@435 1503 // on the non-nt win32 platforms due to this and other api
duke@435 1504 // inconsistencies
duke@435 1505 //
duke@435 1506 if (::stat(filename, &statbuf) == OS_ERR) {
duke@435 1507 if (PrintMiscellaneous && Verbose) {
duke@435 1508 warning("stat %s failed: %s\n", filename, strerror(errno));
duke@435 1509 }
duke@435 1510 THROW_MSG_0(vmSymbols::java_io_IOException(),
duke@435 1511 "Could not determine PerfMemory size");
duke@435 1512 }
duke@435 1513
duke@435 1514 if ((statbuf.st_size == 0) || (statbuf.st_size % os::vm_page_size() != 0)) {
duke@435 1515 if (PrintMiscellaneous && Verbose) {
duke@435 1516 warning("unexpected file size: size = " SIZE_FORMAT "\n",
duke@435 1517 statbuf.st_size);
duke@435 1518 }
duke@435 1519 THROW_MSG_0(vmSymbols::java_lang_Exception(),
duke@435 1520 "Invalid PerfMemory size");
duke@435 1521 }
duke@435 1522
duke@435 1523 return statbuf.st_size;
duke@435 1524 }
duke@435 1525
duke@435 1526 // this method opens a file mapping object and maps the object
duke@435 1527 // into the address space of the process
duke@435 1528 //
duke@435 1529 static void open_file_mapping(const char* user, int vmid,
duke@435 1530 PerfMemory::PerfMemoryMode mode,
duke@435 1531 char** addrp, size_t* sizep, TRAPS) {
duke@435 1532
duke@435 1533 ResourceMark rm;
duke@435 1534
duke@435 1535 void *mapAddress = 0;
duke@435 1536 size_t size;
duke@435 1537 HANDLE fmh;
duke@435 1538 DWORD ofm_access;
duke@435 1539 DWORD mv_access;
duke@435 1540 const char* luser = NULL;
duke@435 1541
duke@435 1542 if (mode == PerfMemory::PERF_MODE_RO) {
duke@435 1543 ofm_access = FILE_MAP_READ;
duke@435 1544 mv_access = FILE_MAP_READ;
duke@435 1545 }
duke@435 1546 else if (mode == PerfMemory::PERF_MODE_RW) {
duke@435 1547 #ifdef LATER
duke@435 1548 ofm_access = FILE_MAP_READ | FILE_MAP_WRITE;
duke@435 1549 mv_access = FILE_MAP_READ | FILE_MAP_WRITE;
duke@435 1550 #else
duke@435 1551 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1552 "Unsupported access mode");
duke@435 1553 #endif
duke@435 1554 }
duke@435 1555 else {
duke@435 1556 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1557 "Illegal access mode");
duke@435 1558 }
duke@435 1559
duke@435 1560 // if a user name wasn't specified, then find the user name for
duke@435 1561 // the owner of the target vm.
duke@435 1562 if (user == NULL || strlen(user) == 0) {
duke@435 1563 luser = get_user_name(vmid);
duke@435 1564 }
duke@435 1565 else {
duke@435 1566 luser = user;
duke@435 1567 }
duke@435 1568
duke@435 1569 if (luser == NULL) {
duke@435 1570 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1571 "Could not map vmid to user name");
duke@435 1572 }
duke@435 1573
duke@435 1574 // get the names for the resources for the target vm
duke@435 1575 char* dirname = get_user_tmp_dir(luser);
duke@435 1576
duke@435 1577 // since we don't follow symbolic links when creating the backing
duke@435 1578 // store file, we also don't following them when attaching
duke@435 1579 //
duke@435 1580 if (!is_directory_secure(dirname)) {
duke@435 1581 FREE_C_HEAP_ARRAY(char, dirname);
duke@435 1582 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@435 1583 "Process not found");
duke@435 1584 }
duke@435 1585
duke@435 1586 char* filename = get_sharedmem_filename(dirname, vmid);
duke@435 1587 char* objectname = get_sharedmem_objectname(luser, vmid);
duke@435 1588
duke@435 1589 // copy heap memory to resource memory. the objectname and
duke@435 1590 // filename are passed to methods that may throw exceptions.
duke@435 1591 // using resource arrays for these names prevents the leaks
duke@435 1592 // that would otherwise occur.
duke@435 1593 //
duke@435 1594 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
duke@435 1595 char* robjectname = NEW_RESOURCE_ARRAY(char, strlen(objectname) + 1);
duke@435 1596 strcpy(rfilename, filename);
duke@435 1597 strcpy(robjectname, objectname);
duke@435 1598
duke@435 1599 // free the c heap resources that are no longer needed
duke@435 1600 if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
duke@435 1601 FREE_C_HEAP_ARRAY(char, dirname);
duke@435 1602 FREE_C_HEAP_ARRAY(char, filename);
duke@435 1603 FREE_C_HEAP_ARRAY(char, objectname);
duke@435 1604
duke@435 1605 if (*sizep == 0) {
duke@435 1606 size = sharedmem_filesize(rfilename, CHECK);
duke@435 1607 assert(size != 0, "unexpected size");
duke@435 1608 }
duke@435 1609
duke@435 1610 // Open the file mapping object with the given name
duke@435 1611 fmh = open_sharedmem_object(robjectname, ofm_access, CHECK);
duke@435 1612
duke@435 1613 assert(fmh != INVALID_HANDLE_VALUE, "unexpected handle value");
duke@435 1614
duke@435 1615 // map the entire file into the address space
duke@435 1616 mapAddress = MapViewOfFile(
duke@435 1617 fmh, /* HANDLE Handle of file mapping object */
duke@435 1618 mv_access, /* DWORD access flags */
duke@435 1619 0, /* DWORD High word of offset */
duke@435 1620 0, /* DWORD Low word of offset */
duke@435 1621 size); /* DWORD Number of bytes to map */
duke@435 1622
duke@435 1623 if (mapAddress == NULL) {
duke@435 1624 if (PrintMiscellaneous && Verbose) {
duke@435 1625 warning("MapViewOfFile failed, lasterror = %d\n", GetLastError());
duke@435 1626 }
duke@435 1627 CloseHandle(fmh);
duke@435 1628 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
duke@435 1629 "Could not map PerfMemory");
duke@435 1630 }
duke@435 1631
duke@435 1632 *addrp = (char*)mapAddress;
duke@435 1633 *sizep = size;
duke@435 1634
duke@435 1635 // File mapping object can be closed at this time without
duke@435 1636 // invalidating the mapped view of the file
duke@435 1637 CloseHandle(fmh);
duke@435 1638
duke@435 1639 if (PerfTraceMemOps) {
duke@435 1640 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
duke@435 1641 INTPTR_FORMAT "\n", size, vmid, mapAddress);
duke@435 1642 }
duke@435 1643 }
duke@435 1644
duke@435 1645 // this method unmaps the the mapped view of the the
duke@435 1646 // file mapping object.
duke@435 1647 //
duke@435 1648 static void remove_file_mapping(char* addr) {
duke@435 1649
duke@435 1650 // the file mapping object was closed in open_file_mapping()
duke@435 1651 // after the file map view was created. We only need to
duke@435 1652 // unmap the file view here.
duke@435 1653 UnmapViewOfFile(addr);
duke@435 1654 }
duke@435 1655
duke@435 1656 // create the PerfData memory region in shared memory.
duke@435 1657 static char* create_shared_memory(size_t size) {
duke@435 1658
duke@435 1659 return mapping_create_shared(size);
duke@435 1660 }
duke@435 1661
duke@435 1662 // release a named, shared memory region
duke@435 1663 //
duke@435 1664 void delete_shared_memory(char* addr, size_t size) {
duke@435 1665
duke@435 1666 delete_file_mapping(addr, size);
duke@435 1667 }
duke@435 1668
duke@435 1669
duke@435 1670
duke@435 1671
duke@435 1672 // create the PerfData memory region
duke@435 1673 //
duke@435 1674 // This method creates the memory region used to store performance
duke@435 1675 // data for the JVM. The memory may be created in standard or
duke@435 1676 // shared memory.
duke@435 1677 //
duke@435 1678 void PerfMemory::create_memory_region(size_t size) {
duke@435 1679
duke@435 1680 if (PerfDisableSharedMem || !os::win32::is_nt()) {
duke@435 1681 // do not share the memory for the performance data.
duke@435 1682 PerfDisableSharedMem = true;
duke@435 1683 _start = create_standard_memory(size);
duke@435 1684 }
duke@435 1685 else {
duke@435 1686 _start = create_shared_memory(size);
duke@435 1687 if (_start == NULL) {
duke@435 1688
duke@435 1689 // creation of the shared memory region failed, attempt
duke@435 1690 // to create a contiguous, non-shared memory region instead.
duke@435 1691 //
duke@435 1692 if (PrintMiscellaneous && Verbose) {
duke@435 1693 warning("Reverting to non-shared PerfMemory region.\n");
duke@435 1694 }
duke@435 1695 PerfDisableSharedMem = true;
duke@435 1696 _start = create_standard_memory(size);
duke@435 1697 }
duke@435 1698 }
duke@435 1699
duke@435 1700 if (_start != NULL) _capacity = size;
duke@435 1701
duke@435 1702 }
duke@435 1703
duke@435 1704 // delete the PerfData memory region
duke@435 1705 //
duke@435 1706 // This method deletes the memory region used to store performance
duke@435 1707 // data for the JVM. The memory region indicated by the <address, size>
duke@435 1708 // tuple will be inaccessible after a call to this method.
duke@435 1709 //
duke@435 1710 void PerfMemory::delete_memory_region() {
duke@435 1711
duke@435 1712 assert((start() != NULL && capacity() > 0), "verify proper state");
duke@435 1713
duke@435 1714 // If user specifies PerfDataSaveFile, it will save the performance data
duke@435 1715 // to the specified file name no matter whether PerfDataSaveToFile is specified
duke@435 1716 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
duke@435 1717 // -XX:+PerfDataSaveToFile.
duke@435 1718 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
duke@435 1719 save_memory_to_file(start(), capacity());
duke@435 1720 }
duke@435 1721
duke@435 1722 if (PerfDisableSharedMem) {
duke@435 1723 delete_standard_memory(start(), capacity());
duke@435 1724 }
duke@435 1725 else {
duke@435 1726 delete_shared_memory(start(), capacity());
duke@435 1727 }
duke@435 1728 }
duke@435 1729
duke@435 1730 // attach to the PerfData memory region for another JVM
duke@435 1731 //
duke@435 1732 // This method returns an <address, size> tuple that points to
duke@435 1733 // a memory buffer that is kept reasonably synchronized with
duke@435 1734 // the PerfData memory region for the indicated JVM. This
duke@435 1735 // buffer may be kept in synchronization via shared memory
duke@435 1736 // or some other mechanism that keeps the buffer updated.
duke@435 1737 //
duke@435 1738 // If the JVM chooses not to support the attachability feature,
duke@435 1739 // this method should throw an UnsupportedOperation exception.
duke@435 1740 //
duke@435 1741 // This implementation utilizes named shared memory to map
duke@435 1742 // the indicated process's PerfData memory region into this JVMs
duke@435 1743 // address space.
duke@435 1744 //
duke@435 1745 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode,
duke@435 1746 char** addrp, size_t* sizep, TRAPS) {
duke@435 1747
duke@435 1748 if (vmid == 0 || vmid == os::current_process_id()) {
duke@435 1749 *addrp = start();
duke@435 1750 *sizep = capacity();
duke@435 1751 return;
duke@435 1752 }
duke@435 1753
duke@435 1754 open_file_mapping(user, vmid, mode, addrp, sizep, CHECK);
duke@435 1755 }
duke@435 1756
duke@435 1757 // detach from the PerfData memory region of another JVM
duke@435 1758 //
duke@435 1759 // This method detaches the PerfData memory region of another
duke@435 1760 // JVM, specified as an <address, size> tuple of a buffer
duke@435 1761 // in this process's address space. This method may perform
duke@435 1762 // arbitrary actions to accomplish the detachment. The memory
duke@435 1763 // region specified by <address, size> will be inaccessible after
duke@435 1764 // a call to this method.
duke@435 1765 //
duke@435 1766 // If the JVM chooses not to support the attachability feature,
duke@435 1767 // this method should throw an UnsupportedOperation exception.
duke@435 1768 //
duke@435 1769 // This implementation utilizes named shared memory to detach
duke@435 1770 // the indicated process's PerfData memory region from this
duke@435 1771 // process's address space.
duke@435 1772 //
duke@435 1773 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
duke@435 1774
duke@435 1775 assert(addr != 0, "address sanity check");
duke@435 1776 assert(bytes > 0, "capacity sanity check");
duke@435 1777
duke@435 1778 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
duke@435 1779 // prevent accidental detachment of this process's PerfMemory region
duke@435 1780 return;
duke@435 1781 }
duke@435 1782
duke@435 1783 remove_file_mapping(addr);
duke@435 1784 }
duke@435 1785
duke@435 1786 char* PerfMemory::backing_store_filename() {
duke@435 1787 return sharedmem_fileName;
duke@435 1788 }

mercurial