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