winprocess.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # A module to expose various thread/process/job related structures and
  2. # methods from kernel32
  3. #
  4. # The MIT License
  5. #
  6. # Copyright (c) 2003-2004 by Peter Astrand <astrand@lysator.liu.se>
  7. #
  8. # Additions and modifications written by Benjamin Smedberg
  9. # <benjamin@smedbergs.us> are Copyright (c) 2006 by the Mozilla Foundation
  10. # <http://www.mozilla.org/>
  11. #
  12. # More Modifications
  13. # Copyright (c) 2006-2007 by Mike Taylor <bear@code-bear.com>
  14. # Copyright (c) 2007-2008 by Mikeal Rogers <mikeal@mozilla.com>
  15. #
  16. # By obtaining, using, and/or copying this software and/or its
  17. # associated documentation, you agree that you have read, understood,
  18. # and will comply with the following terms and conditions:
  19. #
  20. # Permission to use, copy, modify, and distribute this software and
  21. # its associated documentation for any purpose and without fee is
  22. # hereby granted, provided that the above copyright notice appears in
  23. # all copies, and that both that copyright notice and this permission
  24. # notice appear in supporting documentation, and that the name of the
  25. # author not be used in advertising or publicity pertaining to
  26. # distribution of the software without specific, written prior
  27. # permission.
  28. #
  29. # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  30. # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  31. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  32. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  33. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  34. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  35. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  36. from ctypes import c_void_p, POINTER, sizeof, Structure, windll, WinError, WINFUNCTYPE
  37. from ctypes.wintypes import BOOL, BYTE, DWORD, HANDLE, LPCWSTR, LPWSTR, UINT, WORD, \
  38. c_buffer, c_ulong, byref
  39. from qijo import QueryInformationJobObject
  40. LPVOID = c_void_p
  41. LPBYTE = POINTER(BYTE)
  42. LPDWORD = POINTER(DWORD)
  43. LPBOOL = POINTER(BOOL)
  44. def ErrCheckBool(result, func, args):
  45. """errcheck function for Windows functions that return a BOOL True
  46. on success"""
  47. if not result:
  48. raise WinError()
  49. return args
  50. # AutoHANDLE
  51. class AutoHANDLE(HANDLE):
  52. """Subclass of HANDLE which will call CloseHandle() on deletion."""
  53. CloseHandleProto = WINFUNCTYPE(BOOL, HANDLE)
  54. CloseHandle = CloseHandleProto(("CloseHandle", windll.kernel32))
  55. CloseHandle.errcheck = ErrCheckBool
  56. def Close(self):
  57. if self.value and self.value != HANDLE(-1).value:
  58. self.CloseHandle(self)
  59. self.value = 0
  60. def __del__(self):
  61. self.Close()
  62. def __int__(self):
  63. return self.value
  64. def ErrCheckHandle(result, func, args):
  65. """errcheck function for Windows functions that return a HANDLE."""
  66. if not result:
  67. raise WinError()
  68. return AutoHANDLE(result)
  69. # PROCESS_INFORMATION structure
  70. class PROCESS_INFORMATION(Structure):
  71. _fields_ = [("hProcess", HANDLE),
  72. ("hThread", HANDLE),
  73. ("dwProcessID", DWORD),
  74. ("dwThreadID", DWORD)]
  75. def __init__(self):
  76. Structure.__init__(self)
  77. self.cb = sizeof(self)
  78. LPPROCESS_INFORMATION = POINTER(PROCESS_INFORMATION)
  79. # STARTUPINFO structure
  80. class STARTUPINFO(Structure):
  81. _fields_ = [("cb", DWORD),
  82. ("lpReserved", LPWSTR),
  83. ("lpDesktop", LPWSTR),
  84. ("lpTitle", LPWSTR),
  85. ("dwX", DWORD),
  86. ("dwY", DWORD),
  87. ("dwXSize", DWORD),
  88. ("dwYSize", DWORD),
  89. ("dwXCountChars", DWORD),
  90. ("dwYCountChars", DWORD),
  91. ("dwFillAttribute", DWORD),
  92. ("dwFlags", DWORD),
  93. ("wShowWindow", WORD),
  94. ("cbReserved2", WORD),
  95. ("lpReserved2", LPBYTE),
  96. ("hStdInput", HANDLE),
  97. ("hStdOutput", HANDLE),
  98. ("hStdError", HANDLE)
  99. ]
  100. LPSTARTUPINFO = POINTER(STARTUPINFO)
  101. SW_HIDE = 0
  102. STARTF_USESHOWWINDOW = 0x01
  103. STARTF_USESIZE = 0x02
  104. STARTF_USEPOSITION = 0x04
  105. STARTF_USECOUNTCHARS = 0x08
  106. STARTF_USEFILLATTRIBUTE = 0x10
  107. STARTF_RUNFULLSCREEN = 0x20
  108. STARTF_FORCEONFEEDBACK = 0x40
  109. STARTF_FORCEOFFFEEDBACK = 0x80
  110. STARTF_USESTDHANDLES = 0x100
  111. # EnvironmentBlock
  112. class EnvironmentBlock:
  113. """An object which can be passed as the lpEnv parameter of CreateProcess.
  114. It is initialized with a dictionary."""
  115. def __init__(self, dict):
  116. if not dict:
  117. self._as_parameter_ = None
  118. else:
  119. values = ["%s=%s" % (key, value)
  120. for (key, value) in dict.iteritems()]
  121. values.append("")
  122. self._as_parameter_ = LPCWSTR("\0".join(values))
  123. # CreateProcess()
  124. CreateProcessProto = WINFUNCTYPE(BOOL, # Return type
  125. LPCWSTR, # lpApplicationName
  126. LPWSTR, # lpCommandLine
  127. LPVOID, # lpProcessAttributes
  128. LPVOID, # lpThreadAttributes
  129. BOOL, # bInheritHandles
  130. DWORD, # dwCreationFlags
  131. LPVOID, # lpEnvironment
  132. LPCWSTR, # lpCurrentDirectory
  133. LPSTARTUPINFO, # lpStartupInfo
  134. LPPROCESS_INFORMATION # lpProcessInformation
  135. )
  136. CreateProcessFlags = ((1, "lpApplicationName", None),
  137. (1, "lpCommandLine"),
  138. (1, "lpProcessAttributes", None),
  139. (1, "lpThreadAttributes", None),
  140. (1, "bInheritHandles", True),
  141. (1, "dwCreationFlags", 0),
  142. (1, "lpEnvironment", None),
  143. (1, "lpCurrentDirectory", None),
  144. (1, "lpStartupInfo"),
  145. (2, "lpProcessInformation"))
  146. def ErrCheckCreateProcess(result, func, args):
  147. ErrCheckBool(result, func, args)
  148. # return a tuple (hProcess, hThread, dwProcessID, dwThreadID)
  149. pi = args[9]
  150. return AutoHANDLE(pi.hProcess), AutoHANDLE(pi.hThread), pi.dwProcessID, pi.dwThreadID
  151. CreateProcess = CreateProcessProto(("CreateProcessW", windll.kernel32),
  152. CreateProcessFlags)
  153. CreateProcess.errcheck = ErrCheckCreateProcess
  154. # flags for CreateProcess
  155. CREATE_BREAKAWAY_FROM_JOB = 0x01000000
  156. CREATE_DEFAULT_ERROR_MODE = 0x04000000
  157. CREATE_NEW_CONSOLE = 0x00000010
  158. CREATE_NEW_PROCESS_GROUP = 0x00000200
  159. CREATE_NO_WINDOW = 0x08000000
  160. CREATE_SUSPENDED = 0x00000004
  161. CREATE_UNICODE_ENVIRONMENT = 0x00000400
  162. # flags for job limit information
  163. # see http://msdn.microsoft.com/en-us/library/ms684147%28VS.85%29.aspx
  164. JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x00000800
  165. JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK = 0x00001000
  166. # XXX these flags should be documented
  167. DEBUG_ONLY_THIS_PROCESS = 0x00000002
  168. DEBUG_PROCESS = 0x00000001
  169. DETACHED_PROCESS = 0x00000008
  170. # CreateJobObject()
  171. CreateJobObjectProto = WINFUNCTYPE(HANDLE, # Return type
  172. LPVOID, # lpJobAttributes
  173. LPCWSTR # lpName
  174. )
  175. CreateJobObjectFlags = ((1, "lpJobAttributes", None),
  176. (1, "lpName", None))
  177. CreateJobObject = CreateJobObjectProto(("CreateJobObjectW", windll.kernel32),
  178. CreateJobObjectFlags)
  179. CreateJobObject.errcheck = ErrCheckHandle
  180. # AssignProcessToJobObject()
  181. AssignProcessToJobObjectProto = WINFUNCTYPE(BOOL, # Return type
  182. HANDLE, # hJob
  183. HANDLE # hProcess
  184. )
  185. AssignProcessToJobObjectFlags = ((1, "hJob"),
  186. (1, "hProcess"))
  187. AssignProcessToJobObject = AssignProcessToJobObjectProto(
  188. ("AssignProcessToJobObject", windll.kernel32),
  189. AssignProcessToJobObjectFlags)
  190. AssignProcessToJobObject.errcheck = ErrCheckBool
  191. # GetCurrentProcess()
  192. # because os.getPid() is way too easy
  193. GetCurrentProcessProto = WINFUNCTYPE(HANDLE # Return type
  194. )
  195. GetCurrentProcessFlags = ()
  196. GetCurrentProcess = GetCurrentProcessProto(
  197. ("GetCurrentProcess", windll.kernel32),
  198. GetCurrentProcessFlags)
  199. GetCurrentProcess.errcheck = ErrCheckHandle
  200. # IsProcessInJob()
  201. try:
  202. IsProcessInJobProto = WINFUNCTYPE(BOOL, # Return type
  203. HANDLE, # Process Handle
  204. HANDLE, # Job Handle
  205. LPBOOL # Result
  206. )
  207. IsProcessInJobFlags = ((1, "ProcessHandle"),
  208. (1, "JobHandle", HANDLE(0)),
  209. (2, "Result"))
  210. IsProcessInJob = IsProcessInJobProto(
  211. ("IsProcessInJob", windll.kernel32),
  212. IsProcessInJobFlags)
  213. IsProcessInJob.errcheck = ErrCheckBool
  214. except AttributeError:
  215. # windows 2k doesn't have this API
  216. def IsProcessInJob(process):
  217. return False
  218. # ResumeThread()
  219. def ErrCheckResumeThread(result, func, args):
  220. if result == -1:
  221. raise WinError()
  222. return args
  223. ResumeThreadProto = WINFUNCTYPE(DWORD, # Return type
  224. HANDLE # hThread
  225. )
  226. ResumeThreadFlags = ((1, "hThread"),)
  227. ResumeThread = ResumeThreadProto(("ResumeThread", windll.kernel32),
  228. ResumeThreadFlags)
  229. ResumeThread.errcheck = ErrCheckResumeThread
  230. # TerminateProcess()
  231. TerminateProcessProto = WINFUNCTYPE(BOOL, # Return type
  232. HANDLE, # hProcess
  233. UINT # uExitCode
  234. )
  235. TerminateProcessFlags = ((1, "hProcess"),
  236. (1, "uExitCode", 127))
  237. TerminateProcess = TerminateProcessProto(
  238. ("TerminateProcess", windll.kernel32),
  239. TerminateProcessFlags)
  240. TerminateProcess.errcheck = ErrCheckBool
  241. # TerminateJobObject()
  242. TerminateJobObjectProto = WINFUNCTYPE(BOOL, # Return type
  243. HANDLE, # hJob
  244. UINT # uExitCode
  245. )
  246. TerminateJobObjectFlags = ((1, "hJob"),
  247. (1, "uExitCode", 127))
  248. TerminateJobObject = TerminateJobObjectProto(
  249. ("TerminateJobObject", windll.kernel32),
  250. TerminateJobObjectFlags)
  251. TerminateJobObject.errcheck = ErrCheckBool
  252. # WaitForSingleObject()
  253. WaitForSingleObjectProto = WINFUNCTYPE(DWORD, # Return type
  254. HANDLE, # hHandle
  255. DWORD, # dwMilliseconds
  256. )
  257. WaitForSingleObjectFlags = ((1, "hHandle"),
  258. (1, "dwMilliseconds", -1))
  259. WaitForSingleObject = WaitForSingleObjectProto(
  260. ("WaitForSingleObject", windll.kernel32),
  261. WaitForSingleObjectFlags)
  262. INFINITE = -1
  263. WAIT_TIMEOUT = 0x0102
  264. WAIT_OBJECT_0 = 0x0
  265. WAIT_ABANDONED = 0x0080
  266. WAIT_FAILED = 0xFFFFFFFF
  267. # GetExitCodeProcess()
  268. GetExitCodeProcessProto = WINFUNCTYPE(BOOL, # Return type
  269. HANDLE, # hProcess
  270. LPDWORD, # lpExitCode
  271. )
  272. GetExitCodeProcessFlags = ((1, "hProcess"),
  273. (2, "lpExitCode"))
  274. GetExitCodeProcess = GetExitCodeProcessProto(
  275. ("GetExitCodeProcess", windll.kernel32),
  276. GetExitCodeProcessFlags)
  277. GetExitCodeProcess.errcheck = ErrCheckBool
  278. def CanCreateJobObject():
  279. # Running firefox in a job (from cfx) hangs on sites using flash plugin
  280. # so job creation is turned off for now. (see Bug 768651).
  281. return False
  282. ### testing functions
  283. def parent():
  284. print 'Starting parent'
  285. currentProc = GetCurrentProcess()
  286. if IsProcessInJob(currentProc):
  287. print >> sys.stderr, "You should not be in a job object to test"
  288. sys.exit(1)
  289. assert CanCreateJobObject()
  290. print 'File: %s' % __file__
  291. command = [sys.executable, __file__, '-child']
  292. print 'Running command: %s' % command
  293. process = Popen(command)
  294. process.kill()
  295. code = process.returncode
  296. print 'Child code: %s' % code
  297. assert code == 127
  298. def child():
  299. print 'Starting child'
  300. currentProc = GetCurrentProcess()
  301. injob = IsProcessInJob(currentProc)
  302. print "Is in a job?: %s" % injob
  303. can_create = CanCreateJobObject()
  304. print 'Can create job?: %s' % can_create
  305. process = Popen('c:\\windows\\notepad.exe')
  306. assert process._job
  307. jobinfo = QueryInformationJobObject(process._job, 'JobObjectExtendedLimitInformation')
  308. print 'Job info: %s' % jobinfo
  309. limitflags = jobinfo['BasicLimitInformation']['LimitFlags']
  310. print 'LimitFlags: %s' % limitflags
  311. process.kill()
  312. if __name__ == '__main__':
  313. import sys
  314. from killableprocess import Popen
  315. nargs = len(sys.argv[1:])
  316. if nargs:
  317. if nargs != 1 or sys.argv[1] != '-child':
  318. raise AssertionError('Wrong flags; run like `python /path/to/winprocess.py`')
  319. child()
  320. else:
  321. parent()