tests/killdaemons.py
branchstable
changeset 1329 10d0d9d5c0f0
parent 1128 7f09e7ac63a7
child 1330 e2fe607966f6
equal deleted inserted replaced
1327:953d562c3ebf 1329:10d0d9d5c0f0
     2 
     2 
     3 import os, sys, time, errno, signal
     3 import os, sys, time, errno, signal
     4 
     4 
     5 if os.name =='nt':
     5 if os.name =='nt':
     6     import ctypes
     6     import ctypes
       
     7 
       
     8     def _check(ret, expectederr=None):
       
     9         if ret == 0:
       
    10             winerrno = ctypes.GetLastError()
       
    11             if winerrno == expectederr:
       
    12                 return True
       
    13             raise ctypes.WinError(winerrno)
       
    14 
     7     def kill(pid, logfn, tryhard=True):
    15     def kill(pid, logfn, tryhard=True):
     8         logfn('# Killing daemon process %d' % pid)
    16         logfn('# Killing daemon process %d' % pid)
     9         PROCESS_TERMINATE = 1
    17         PROCESS_TERMINATE = 1
    10         handle = ctypes.windll.kernel32.OpenProcess(
    18         handle = ctypes.windll.kernel32.OpenProcess(
    11                 PROCESS_TERMINATE, False, pid)
    19                 PROCESS_TERMINATE, False, pid)
    12         ctypes.windll.kernel32.TerminateProcess(handle, -1)
    20         if handle == 0:
    13         ctypes.windll.kernel32.CloseHandle(handle)
    21             # TODO: call _check(0, expected) to check if "process not found"
       
    22             return # process not found, already finished
       
    23         try:
       
    24             _check(ctypes.windll.kernel32.TerminateProcess(handle, -1), 5)
       
    25             #      windows error 5 when process does not exist or no access TODO
       
    26         except: #re-raises
       
    27             ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error
       
    28             raise
       
    29         _check(ctypes.windll.kernel32.CloseHandle(handle))
       
    30 
    14 else:
    31 else:
    15     def kill(pid, logfn, tryhard=True):
    32     def kill(pid, logfn, tryhard=True):
    16         try:
    33         try:
    17             os.kill(pid, 0)
    34             os.kill(pid, 0)
    18             logfn('# Killing daemon process %d' % pid)
    35             logfn('# Killing daemon process %d' % pid)