1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: winbase.py 61833 2016-06-22 21:21:53Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | This module is here to externalize some Windows specifics that gives pychecker
|
---|
6 | a hard time when running on non-Windows systems.
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2010-2015 Oracle Corporation
|
---|
12 |
|
---|
13 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
14 | available from http://www.virtualbox.org. This file is free software;
|
---|
15 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
16 | General Public License (GPL) as published by the Free Software
|
---|
17 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
18 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
19 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
20 |
|
---|
21 | The contents of this file may alternatively be used under the terms
|
---|
22 | of the Common Development and Distribution License Version 1.0
|
---|
23 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
24 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
25 | CDDL are applicable instead of those of the GPL.
|
---|
26 |
|
---|
27 | You may elect to license modified versions of this file under the
|
---|
28 | terms and conditions of either the GPL or the CDDL or both.
|
---|
29 | """
|
---|
30 | __version__ = "$Revision: 61833 $"
|
---|
31 |
|
---|
32 |
|
---|
33 | # Standard Python imports.
|
---|
34 | import os
|
---|
35 |
|
---|
36 | # Windows specific imports.
|
---|
37 | import win32api; # pylint: disable=import-error
|
---|
38 | import win32con; # pylint: disable=import-error
|
---|
39 | import win32console; # pylint: disable=import-error
|
---|
40 | import win32event; # pylint: disable=import-error
|
---|
41 | import win32process; # pylint: disable=import-error
|
---|
42 |
|
---|
43 | # Validation Kit imports.
|
---|
44 | from testdriver import reporter;
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | #
|
---|
49 | # Windows specific implementation of base functions.
|
---|
50 | #
|
---|
51 |
|
---|
52 | def processInterrupt(uPid):
|
---|
53 | """
|
---|
54 | The Windows version of base.processInterrupt
|
---|
55 |
|
---|
56 | Note! This doesn't work terribly well with a lot of processes.
|
---|
57 | """
|
---|
58 | try:
|
---|
59 | # pylint: disable=no-member
|
---|
60 | win32console.GenerateConsoleCtrlEvent(win32con.CTRL_BREAK_EVENT, uPid);
|
---|
61 | #GenerateConsoleCtrlEvent = ctypes.windll.kernel32.GenerateConsoleCtrlEvent
|
---|
62 | #rc = GenerateConsoleCtrlEvent(1, uPid);
|
---|
63 | #reporter.log('GenerateConsoleCtrlEvent -> %s' % (rc,));
|
---|
64 | fRc = True;
|
---|
65 | except:
|
---|
66 | reporter.logXcpt('uPid=%s' % (uPid,));
|
---|
67 | fRc = False;
|
---|
68 | return fRc;
|
---|
69 |
|
---|
70 | def postThreadMesssageClose(uTid):
|
---|
71 | """ Posts a WM_CLOSE message to the specified thread."""
|
---|
72 | fRc = False;
|
---|
73 | try:
|
---|
74 | win32api.PostThreadMessage(uTid, win32con.WM_CLOSE, 0, 0); # pylint: disable=no-member
|
---|
75 | fRc = True;
|
---|
76 | except:
|
---|
77 | reporter.logXcpt('uTid=%s' % (uTid,));
|
---|
78 | return fRc;
|
---|
79 |
|
---|
80 | def postThreadMesssageQuit(uTid):
|
---|
81 | """ Posts a WM_QUIT message to the specified thread."""
|
---|
82 | fRc = False;
|
---|
83 | try:
|
---|
84 | win32api.PostThreadMessage(uTid, win32con.WM_QUIT, 0x40010004, 0); # DBG_TERMINATE_PROCESS # pylint: disable=no-member
|
---|
85 | fRc = True;
|
---|
86 | except:
|
---|
87 | reporter.logXcpt('uTid=%s' % (uTid,));
|
---|
88 | return fRc;
|
---|
89 |
|
---|
90 | def processTerminate(uPid):
|
---|
91 | """ The Windows version of base.processTerminate """
|
---|
92 | # pylint: disable=no-member
|
---|
93 | fRc = False;
|
---|
94 | try:
|
---|
95 | hProcess = win32api.OpenProcess(win32con.PROCESS_TERMINATE, False, uPid);
|
---|
96 | except:
|
---|
97 | reporter.logXcpt('uPid=%s' % (uPid,));
|
---|
98 | else:
|
---|
99 | try:
|
---|
100 | win32process.TerminateProcess(hProcess, 0x40010004); # DBG_TERMINATE_PROCESS
|
---|
101 | fRc = True;
|
---|
102 | except:
|
---|
103 | reporter.logXcpt('uPid=%s' % (uPid,));
|
---|
104 | win32api.CloseHandle(hProcess)
|
---|
105 | return fRc;
|
---|
106 |
|
---|
107 | def processKill(uPid):
|
---|
108 | """ The Windows version of base.processKill """
|
---|
109 | return processTerminate(uPid);
|
---|
110 |
|
---|
111 | def processExists(uPid):
|
---|
112 | """ The Windows version of base.processExists """
|
---|
113 | # pylint: disable=no-member
|
---|
114 | fRc = False;
|
---|
115 | try:
|
---|
116 | hProcess = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, uPid);
|
---|
117 | except:
|
---|
118 | reporter.logXcpt('uPid=%s' % (uPid,));
|
---|
119 | else:
|
---|
120 | win32api.CloseHandle(hProcess)
|
---|
121 | fRc = True;
|
---|
122 | return fRc;
|
---|
123 |
|
---|
124 | def processCheckPidAndName(uPid, sName):
|
---|
125 | """ The Windows version of base.processCheckPidAndName """
|
---|
126 | fRc = processExists(uPid);
|
---|
127 | if fRc is True:
|
---|
128 | try:
|
---|
129 | from win32com.client import GetObject; # pylint: disable=F0401
|
---|
130 | oWmi = GetObject('winmgmts:');
|
---|
131 | aoProcesses = oWmi.InstancesOf('Win32_Process');
|
---|
132 | for oProcess in aoProcesses:
|
---|
133 | if long(oProcess.Properties_("ProcessId").Value) == uPid:
|
---|
134 | sCurName = oProcess.Properties_("Name").Value;
|
---|
135 | reporter.log2('uPid=%s sName=%s sCurName=%s' % (uPid, sName, sCurName));
|
---|
136 | sName = sName.lower();
|
---|
137 | sCurName = sCurName.lower();
|
---|
138 | if os.path.basename(sName) == sName:
|
---|
139 | sCurName = os.path.basename(sCurName);
|
---|
140 |
|
---|
141 | if sCurName == sName \
|
---|
142 | or sCurName + '.exe' == sName \
|
---|
143 | or sCurName == sName + '.exe':
|
---|
144 | fRc = True;
|
---|
145 | break;
|
---|
146 | except:
|
---|
147 | reporter.logXcpt('uPid=%s sName=%s' % (uPid, sName));
|
---|
148 | return fRc;
|
---|
149 |
|
---|
150 | #
|
---|
151 | # Some helper functions.
|
---|
152 | #
|
---|
153 | def processCreate(sName, asArgs):
|
---|
154 | """
|
---|
155 | Returns a (pid, handle, tid) tuple on success. (-1, None) on failure (logged).
|
---|
156 | """
|
---|
157 |
|
---|
158 | # Construct a command line.
|
---|
159 | sCmdLine = '';
|
---|
160 | for sArg in asArgs:
|
---|
161 | if sCmdLine == '':
|
---|
162 | sCmdLine += '"';
|
---|
163 | else:
|
---|
164 | sCmdLine += ' "';
|
---|
165 | sCmdLine += sArg;
|
---|
166 | sCmdLine += '"';
|
---|
167 |
|
---|
168 | # Try start the process.
|
---|
169 | # pylint: disable=no-member
|
---|
170 | dwCreationFlags = win32con.CREATE_NEW_PROCESS_GROUP;
|
---|
171 | oStartupInfo = win32process.STARTUPINFO();
|
---|
172 | try:
|
---|
173 | (hProcess, hThread, uPid, uTid) = win32process.CreateProcess(sName,
|
---|
174 | sCmdLine, # CommandLine
|
---|
175 | None, # ProcessAttributes
|
---|
176 | None, # ThreadAttibutes
|
---|
177 | 1, # fInheritHandles
|
---|
178 | dwCreationFlags,
|
---|
179 | None, # Environment
|
---|
180 | None, # CurrentDirectory.
|
---|
181 | oStartupInfo);
|
---|
182 | except:
|
---|
183 | reporter.logXcpt('sName="%s" sCmdLine="%s"' % (sName, sCmdLine));
|
---|
184 | return (-1, None, -1);
|
---|
185 |
|
---|
186 | # Dispense with the thread handle.
|
---|
187 | try:
|
---|
188 | win32api.CloseHandle(hThread);
|
---|
189 | except:
|
---|
190 | reporter.logXcpt();
|
---|
191 |
|
---|
192 | # Try get full access to the process.
|
---|
193 | try:
|
---|
194 | hProcessFullAccess = win32api.DuplicateHandle(
|
---|
195 | win32api.GetCurrentProcess(),
|
---|
196 | hProcess,
|
---|
197 | win32api.GetCurrentProcess(),
|
---|
198 | win32con.PROCESS_TERMINATE
|
---|
199 | | win32con.PROCESS_QUERY_INFORMATION
|
---|
200 | | win32con.SYNCHRONIZE
|
---|
201 | | win32con.DELETE,
|
---|
202 | False,
|
---|
203 | 0);
|
---|
204 | win32api.CloseHandle(hProcess);
|
---|
205 | hProcess = hProcessFullAccess;
|
---|
206 | except:
|
---|
207 | reporter.logXcpt();
|
---|
208 | reporter.log2('processCreate -> %#x, hProcess=%#x' % (uPid, hProcess,));
|
---|
209 | return (uPid, hProcess, uTid);
|
---|
210 |
|
---|
211 | def processPollByHandle(hProcess):
|
---|
212 | """
|
---|
213 | Polls the process handle to see if it has finished (True) or not (False).
|
---|
214 | """
|
---|
215 | try:
|
---|
216 | dwWait = win32event.WaitForSingleObject(hProcess, 0); # pylint: disable=no-member
|
---|
217 | except:
|
---|
218 | reporter.logXcpt('hProcess=%s %#x' % (hProcess, hProcess,));
|
---|
219 | return True;
|
---|
220 | return dwWait != win32con.WAIT_TIMEOUT; #0x102; #
|
---|
221 |
|
---|
222 |
|
---|
223 | def processTerminateByHandle(hProcess):
|
---|
224 | """
|
---|
225 | Terminates the process.
|
---|
226 | """
|
---|
227 | try:
|
---|
228 | win32api.TerminateProcess(hProcess, 0x40010004); # DBG_TERMINATE_PROCESS # pylint: disable=no-member
|
---|
229 | except:
|
---|
230 | reporter.logXcpt('hProcess=%s %#x' % (hProcess, hProcess,));
|
---|
231 | return False;
|
---|
232 | return True;
|
---|
233 |
|
---|