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