From efd1985565b97c91850be56e94beacb805cd6f67 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Fri, 25 May 2012 12:30:54 +0300 Subject: signal_pidfile: support sig=0 on win32 To avoid pywin32 dependency for such basic functionality, use kernel32.dll via ctypes directly. --- python/skytools/scripting.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'python/skytools/scripting.py') diff --git a/python/skytools/scripting.py b/python/skytools/scripting.py index 0e0cdacf..6e6716e9 100644 --- a/python/skytools/scripting.py +++ b/python/skytools/scripting.py @@ -43,6 +43,8 @@ def signal_pidfile(pidfile, sig): ln = f.readline().strip() f.close() pid = int(ln) + if sig == 0 and sys.platform == 'win32': + return win32_detect_pid(pid) os.kill(pid, sig) return True except IOError, ex: @@ -59,6 +61,37 @@ def signal_pidfile(pidfile, sig): raise ValueError('Corrupt pidfile: %s' % pidfile) return False +def win32_detect_pid(pid): + """Process detection for win32.""" + + # avoid pywin32 dependecy, use ctypes instead + import ctypes + + # win32 constants + PROCESS_QUERY_INFORMATION = 1024 + STILL_ACTIVE = 259 + ERROR_INVALID_PARAMETER = 87 + ERROR_ACCESS_DENIED = 5 + + # Load kernel32.dll + k = ctypes.windll.kernel32 + OpenProcess = k.OpenProcess + OpenProcess.restype = ctypes.c_void_p + + # query pid exit code + h = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid) + if h == None: + err = k.GetLastError() + if err == ERROR_INVALID_PARAMETER: + return False + if err == ERROR_ACCESS_DENIED: + return True + raise OSError(errno.EFAULT, "Unknown win32error: " + str(err)) + code = ctypes.c_int() + k.GetExitCodeProcess(h, ctypes.byref(code)) + k.CloseHandle(h) + return code.value == STILL_ACTIVE + # # daemon mode # -- cgit v1.2.3