PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/MRI-J/jdk/src/windows/classes/sun/tools/attach/WindowsAttachProvider.java

http://github.com/GregBowyer/ManagedRuntimeInitiative
Java | 194 lines | 105 code | 32 blank | 57 comment | 19 complexity | c8713c1a69e5963e137b7e505b8688b3 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-3.0
  1. /*
  2. * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Sun designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Sun in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22. * CA 95054 USA or visit www.sun.com if you need additional information or
  23. * have any questions.
  24. */
  25. package sun.tools.attach;
  26. import com.sun.tools.attach.VirtualMachine;
  27. import com.sun.tools.attach.VirtualMachineDescriptor;
  28. import com.sun.tools.attach.AttachNotSupportedException;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.io.IOException;
  32. import java.net.InetAddress;
  33. import java.net.UnknownHostException;
  34. public class WindowsAttachProvider extends HotSpotAttachProvider {
  35. public WindowsAttachProvider() {
  36. String os = System.getProperty("os.name");
  37. if (os.startsWith("Windows 9") || os.equals("Windows Me")) {
  38. throw new RuntimeException(
  39. "This provider is not supported on this version of Windows");
  40. }
  41. String arch = System.getProperty("os.arch");
  42. if (!arch.equals("x86") && !arch.equals("amd64")) {
  43. throw new RuntimeException(
  44. "This provider is not supported on this processor architecture");
  45. }
  46. }
  47. public String name() {
  48. return "sun";
  49. }
  50. public String type() {
  51. return "windows";
  52. }
  53. public VirtualMachine attachVirtualMachine(String vmid)
  54. throws AttachNotSupportedException, IOException
  55. {
  56. checkAttachPermission();
  57. // AttachNotSupportedException will be thrown if the target VM can be determined
  58. // to be not attachable.
  59. testAttachable(vmid);
  60. return new WindowsVirtualMachine(this, vmid);
  61. }
  62. public List<VirtualMachineDescriptor> listVirtualMachines() {
  63. // If the temporary file system is secure then we use the default
  64. // implementation, otherwise we create a list of Windows processes.
  65. if (isTempPathSecure()) {
  66. return super.listVirtualMachines();
  67. } else {
  68. return listJavaProcesses();
  69. }
  70. }
  71. /**
  72. * Returns true if the temporary file system supports security
  73. */
  74. private static boolean isTempPathSecure() {
  75. if (!wasTempPathChecked) {
  76. synchronized (WindowsAttachProvider.class) {
  77. if (!wasTempPathChecked) {
  78. // get the value of TMP/TEMP, ignoring UNC, and paths that
  79. // aren't absolute
  80. String temp = tempPath();
  81. if ((temp != null) && (temp.length() >= 3) &&
  82. (temp.charAt(1) == ':') && (temp.charAt(2) == '\\'))
  83. {
  84. // check if the volume supports security
  85. long flags = volumeFlags(temp.substring(0, 3));
  86. isTempPathSecure = ((flags & FS_PERSISTENT_ACLS) != 0);
  87. }
  88. wasTempPathChecked = true;
  89. }
  90. }
  91. }
  92. return isTempPathSecure;
  93. }
  94. // flag to indicate persistent ACLs are supported
  95. private static final long FS_PERSISTENT_ACLS = 0x8L;
  96. // indicates if we've checked the temporary file system
  97. private static volatile boolean wasTempPathChecked;
  98. // indicates if the temporary file system is secure (only valid when
  99. // wasTempPathChecked is true)
  100. private static boolean isTempPathSecure;
  101. // returns the value of TMP/TEMP
  102. private static native String tempPath();
  103. // returns the flags for the given volume
  104. private static native long volumeFlags(String volume);
  105. /**
  106. * Returns a list of virtual machine descriptors derived from an enumeration
  107. * of the process list.
  108. */
  109. private List<VirtualMachineDescriptor> listJavaProcesses() {
  110. // ensure that process status helper is loaded (psapi.dll)
  111. if (!isProcessStatusHelperInitialized) {
  112. synchronized (WindowsAttachProvider.class) {
  113. if (!isProcessStatusHelperInitialized) {
  114. initializeProcessStatusHelper();
  115. isProcessStatusHelperInitialized = true;
  116. }
  117. }
  118. }
  119. ArrayList<VirtualMachineDescriptor> list =
  120. new ArrayList<VirtualMachineDescriptor>();
  121. // Use localhost in the display name
  122. String host = "localhost";
  123. try {
  124. host = InetAddress.getLocalHost().getHostName();
  125. } catch (UnknownHostException uhe) {
  126. // ignore
  127. }
  128. // Enumerate all processes.
  129. // For those processes that have loaded a library named "jvm.dll"
  130. // then we attempt to attach. If we succeed then we have a 6.0+ VM.
  131. int processes[] = new int[1024];
  132. int count = enumProcesses(processes, processes.length);
  133. for (int i=0; i<count; i++) {
  134. if (isLibraryLoadedByProcess("jvm.dll", processes[i])) {
  135. String pid = Integer.toString(processes[i]);
  136. try {
  137. new WindowsVirtualMachine(this, pid).detach();
  138. // FIXME - for now we don't have an appropriate display
  139. // name so we use pid@hostname
  140. String name = pid + "@" + host;
  141. list.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
  142. } catch (AttachNotSupportedException x) {
  143. } catch (IOException ioe) {
  144. }
  145. }
  146. }
  147. return list;
  148. }
  149. // indicates if psapi.dll has been initialized
  150. private static volatile boolean isProcessStatusHelperInitialized;
  151. // loads psapi
  152. private static native void initializeProcessStatusHelper();
  153. // enumerates processes using psapi's EnumProcesses
  154. private static native int enumProcesses(int[] processes, int max);
  155. // indicates if a library of a given name has been loaded by a process
  156. private static native boolean isLibraryLoadedByProcess(String library,
  157. int processId);
  158. // native functions in this library
  159. static {
  160. System.loadLibrary("attach");
  161. }
  162. }