blob: 046d14b617c7d9138217cb5640031b0b6042324c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 */
25package sun.tools.attach;
26
27import com.sun.tools.attach.VirtualMachine;
28import com.sun.tools.attach.VirtualMachineDescriptor;
29import com.sun.tools.attach.AttachNotSupportedException;
30
31import java.util.ArrayList;
32import java.util.List;
33import java.io.IOException;
34import java.net.InetAddress;
35import java.net.UnknownHostException;
36
37public class WindowsAttachProvider extends HotSpotAttachProvider {
38
39 public WindowsAttachProvider() {
40 String os = System.getProperty("os.name");
41 if (os.startsWith("Windows 9") || os.equals("Windows Me")) {
42 throw new RuntimeException(
43 "This provider is not supported on this version of Windows");
44 }
45 String arch = System.getProperty("os.arch");
46 if (!arch.equals("x86") && !arch.equals("amd64")) {
47 throw new RuntimeException(
48 "This provider is not supported on this processor architecture");
49 }
50 }
51
52 public String name() {
53 return "sun";
54 }
55
56 public String type() {
57 return "windows";
58 }
59
60 public VirtualMachine attachVirtualMachine(String vmid)
61 throws AttachNotSupportedException, IOException
62 {
63 checkAttachPermission();
64
65 // AttachNotSupportedException will be thrown if the target VM can be determined
66 // to be not attachable.
67 testAttachable(vmid);
68
69 return new WindowsVirtualMachine(this, vmid);
70 }
71
72 public List<VirtualMachineDescriptor> listVirtualMachines() {
73 // If the temporary file system is secure then we use the default
74 // implementation, otherwise we create a list of Windows processes.
75 if (isTempPathSecure()) {
76 return super.listVirtualMachines();
77 } else {
78 return listJavaProcesses();
79 }
80 }
81
82 /**
83 * Returns true if the temporary file system supports security
84 */
85 private static boolean isTempPathSecure() {
86 if (!wasTempPathChecked) {
87 synchronized (WindowsAttachProvider.class) {
88 if (!wasTempPathChecked) {
89 // get the value of TMP/TEMP, ignoring UNC, and paths that
90 // aren't absolute
91 String temp = tempPath();
92 if ((temp != null) && (temp.length() >= 3) &&
93 (temp.charAt(1) == ':') && (temp.charAt(2) == '\\'))
94 {
95 // check if the volume supports security
96 long flags = volumeFlags(temp.substring(0, 3));
97 isTempPathSecure = ((flags & FS_PERSISTENT_ACLS) != 0);
98 }
99 wasTempPathChecked = true;
100 }
101 }
102 }
103
104 return isTempPathSecure;
105 }
106
107 // flag to indicate persistent ACLs are supported
108 private static final long FS_PERSISTENT_ACLS = 0x8L;
109
110 // indicates if we've checked the temporary file system
111 private static volatile boolean wasTempPathChecked;
112
113 // indicates if the temporary file system is secure (only valid when
114 // wasTempPathChecked is true)
115 private static boolean isTempPathSecure;
116
117 // returns the value of TMP/TEMP
118 private static native String tempPath();
119
120 // returns the flags for the given volume
121 private static native long volumeFlags(String volume);
122
123
124 /**
125 * Returns a list of virtual machine descriptors derived from an enumeration
126 * of the process list.
127 */
128 private List<VirtualMachineDescriptor> listJavaProcesses() {
129 // ensure that process status helper is loaded (psapi.dll)
130 if (!isProcessStatusHelperInitialized) {
131 synchronized (WindowsAttachProvider.class) {
132 if (!isProcessStatusHelperInitialized) {
133 initializeProcessStatusHelper();
134 isProcessStatusHelperInitialized = true;
135 }
136 }
137 }
138
139 ArrayList<VirtualMachineDescriptor> list =
140 new ArrayList<VirtualMachineDescriptor>();
141
142 // Use localhost in the display name
143 String host = "localhost";
144 try {
145 host = InetAddress.getLocalHost().getHostName();
146 } catch (UnknownHostException uhe) {
147 // ignore
148 }
149
150 // Enumerate all processes.
151 // For those processes that have loaded a library named "jvm.dll"
152 // then we attempt to attach. If we succeed then we have a 6.0+ VM.
153 int processes[] = new int[1024];
154 int count = enumProcesses(processes, processes.length);
155 for (int i=0; i<count; i++) {
156 if (isLibraryLoadedByProcess("jvm.dll", processes[i])) {
157 String pid = Integer.toString(processes[i]);
158 try {
159 new WindowsVirtualMachine(this, pid).detach();
160
161 // FIXME - for now we don't have an appropriate display
162 // name so we use pid@hostname
163 String name = pid + "@" + host;
164
165 list.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
166 } catch (AttachNotSupportedException x) {
167 } catch (IOException ioe) {
168 }
169 }
170 }
171
172 return list;
173 }
174
175 // indicates if psapi.dll has been initialized
176 private static volatile boolean isProcessStatusHelperInitialized;
177
178 // loads psapi
179 private static native void initializeProcessStatusHelper();
180
181 // enumerates processes using psapi's EnumProcesses
182 private static native int enumProcesses(int[] processes, int max);
183
184 // indicates if a library of a given name has been loaded by a process
185 private static native boolean isLibraryLoadedByProcess(String library,
186 int processId);
187
188
189 // native functions in this library
190 static {
191 System.loadLibrary("attach");
192 }
193
194}