src/os/windows/vm/perfMemory_windows.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 435
a61af66fc99e
child 1788
a2ea687fdc7c
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

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

mercurial