summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorMarko Kreen2012-05-25 09:30:54 +0000
committerMarko Kreen2012-05-25 09:30:54 +0000
commitefd1985565b97c91850be56e94beacb805cd6f67 (patch)
treee0c543a02dcf74b630de29477ec792e9b0e0760a /python
parent0f3cb09bd4eaf1a61e55b27992298590f98f3532 (diff)
signal_pidfile: support sig=0 on win32
To avoid pywin32 dependency for such basic functionality, use kernel32.dll via ctypes directly.
Diffstat (limited to 'python')
-rw-r--r--python/skytools/scripting.py33
1 files changed, 33 insertions, 0 deletions
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
#