PageRenderTime 60ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/src/windows/classes/sun/tools/attach/WindowsAttachProvider.java

https://bitbucket.org/screenconnect/openjdk8-jdk
Java | 178 lines | 95 code | 29 blank | 54 comment | 17 complexity | 8e9b4b0ddcfef917a05f25df92131455 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-3.0
  1. /*
  2. * Copyright (c) 2005, 2011, Oracle and/or its affiliates. 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. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * 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. ArrayList<VirtualMachineDescriptor> list =
  111. new ArrayList<VirtualMachineDescriptor>();
  112. // Use localhost in the display name
  113. String host = "localhost";
  114. try {
  115. host = InetAddress.getLocalHost().getHostName();
  116. } catch (UnknownHostException uhe) {
  117. // ignore
  118. }
  119. // Enumerate all processes.
  120. // For those processes that have loaded a library named "jvm.dll"
  121. // then we attempt to attach. If we succeed then we have a 6.0+ VM.
  122. int processes[] = new int[1024];
  123. int count = enumProcesses(processes, processes.length);
  124. for (int i=0; i<count; i++) {
  125. if (isLibraryLoadedByProcess("jvm.dll", processes[i])) {
  126. String pid = Integer.toString(processes[i]);
  127. try {
  128. new WindowsVirtualMachine(this, pid).detach();
  129. // FIXME - for now we don't have an appropriate display
  130. // name so we use pid@hostname
  131. String name = pid + "@" + host;
  132. list.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
  133. } catch (AttachNotSupportedException x) {
  134. } catch (IOException ioe) {
  135. }
  136. }
  137. }
  138. return list;
  139. }
  140. // enumerates processes using psapi's EnumProcesses
  141. private static native int enumProcesses(int[] processes, int max);
  142. // indicates if a library of a given name has been loaded by a process
  143. private static native boolean isLibraryLoadedByProcess(String library,
  144. int processId);
  145. // native functions in this library
  146. static {
  147. System.loadLibrary("attach");
  148. }
  149. }