hg: add process start time query function

Summary:
This will later be used to add a bit of uniqueness to PIDs on Windows, to
address the stale lock problem.

Reviewed By: quark-zju

Differential Revision: D7489325

fbshipit-source-id: b866bd239eacfc7fe64433f0989a73dff1246644
This commit is contained in:
Kostia Balytskyi 2018-04-10 03:59:00 -07:00 committed by Saurabh Singh
parent 3f7758625a
commit d612e92d9f

View File

@ -88,6 +88,7 @@ _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000
# Process Security and Access Rights # Process Security and Access Rights
_PROCESS_QUERY_INFORMATION = 0x0400 _PROCESS_QUERY_INFORMATION = 0x0400
_PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
# GetExitCodeProcess # GetExitCodeProcess
_STILL_ACTIVE = 259 _STILL_ACTIVE = 259
@ -308,6 +309,40 @@ def _getfileinfo(name):
finally: finally:
_kernel32.CloseHandle(fh) _kernel32.CloseHandle(fh)
def getcurrentprocstarttime():
"""Get current process start time
See _getprocstarttime docstring for more info"""
pid = _kernel32.GetCurrentProcessId()
return getprocstarttime(pid)
def getprocstarttime(pid):
"""Get the windows timestamp of process start time
Windows Timestamp in this context is a number of 100-nanosecond
time units since January 1, 1601 at Greenwich, England.
See https://msdn.microsoft.com/en-us/library/ms683223(VS.85).aspx"""
ph = _kernel32.OpenProcess(_PROCESS_QUERY_LIMITED_INFORMATION, 1, pid)
if not ph or ph == _INVALID_HANDLE_VALUE:
raise ctypes.WinError(_kernel32.GetLastError())
try:
creationtime = _FILETIME()
exittime = _FILETIME()
kerneltime = _FILETIME()
usertime = _FILETIME()
success = _kernel32.GetProcessTimes(ph,
ctypes.byref(creationtime),
ctypes.byref(exittime),
ctypes.byref(kerneltime),
ctypes.byref(usertime))
if not success:
raise ctypes.WinError(_kernel32.GetLastError())
ct = (creationtime.dwHighDateTime << 32) + creationtime.dwLowDateTime
return ct
finally:
_kernel32.CloseHandle(ph)
def checkcertificatechain(cert, build=True): def checkcertificatechain(cert, build=True):
'''Tests the given certificate to see if there is a complete chain to a '''Tests the given certificate to see if there is a complete chain to a
trusted root certificate. As a side effect, missing certificates are trusted root certificate. As a side effect, missing certificates are