|
| 1 | +# -*- coding=utf-8 -*- |
| 2 | +# psutil is painfully slow in win32. So to avoid adding big |
| 3 | +# dependencies like pywin32 a ctypes based solution is preferred |
| 4 | + |
| 5 | +# Code based on the winappdbg project http://winappdbg.sourceforge.net/ |
| 6 | +# (BSD License) - adapted from Celery |
| 7 | +# https://github.com/celery/celery/blob/2.5-archived/celery/concurrency/processes/_win.py |
| 8 | +import os |
| 9 | +from ctypes import ( |
| 10 | + byref, sizeof, windll, Structure, WinError, POINTER, |
| 11 | + c_size_t, c_char, c_void_p |
| 12 | +) |
| 13 | +from ctypes.wintypes import DWORD, LONG |
| 14 | + |
| 15 | +ERROR_NO_MORE_FILES = 18 |
| 16 | +INVALID_HANDLE_VALUE = c_void_p(-1).value |
| 17 | + |
| 18 | + |
| 19 | +class PROCESSENTRY32(Structure): |
| 20 | + _fields_ = [ |
| 21 | + ('dwSize', DWORD), |
| 22 | + ('cntUsage', DWORD), |
| 23 | + ('th32ProcessID', DWORD), |
| 24 | + ('th32DefaultHeapID', c_size_t), |
| 25 | + ('th32ModuleID', DWORD), |
| 26 | + ('cntThreads', DWORD), |
| 27 | + ('th32ParentProcessID', DWORD), |
| 28 | + ('pcPriClassBase', LONG), |
| 29 | + ('dwFlags', DWORD), |
| 30 | + ('szExeFile', c_char * 260), |
| 31 | + ] |
| 32 | + |
| 33 | + |
| 34 | +LPPROCESSENTRY32 = POINTER(PROCESSENTRY32) |
| 35 | + |
| 36 | + |
| 37 | +def CreateToolhelp32Snapshot(dwFlags=2, th32ProcessID=0): |
| 38 | + hSnapshot = windll.kernel32.CreateToolhelp32Snapshot( |
| 39 | + dwFlags, |
| 40 | + th32ProcessID |
| 41 | + ) |
| 42 | + if hSnapshot == INVALID_HANDLE_VALUE: |
| 43 | + raise WinError() |
| 44 | + return hSnapshot |
| 45 | + |
| 46 | + |
| 47 | +def Process32First(hSnapshot): |
| 48 | + pe = PROCESSENTRY32() |
| 49 | + pe.dwSize = sizeof(PROCESSENTRY32) |
| 50 | + success = windll.kernel32.Process32First(hSnapshot, byref(pe)) |
| 51 | + if not success: |
| 52 | + if windll.kernel32.GetLastError() == ERROR_NO_MORE_FILES: |
| 53 | + return |
| 54 | + raise WinError() |
| 55 | + return pe |
| 56 | + |
| 57 | + |
| 58 | +def Process32Next(hSnapshot, pe=None): |
| 59 | + if pe is None: |
| 60 | + pe = PROCESSENTRY32() |
| 61 | + pe.dwSize = sizeof(PROCESSENTRY32) |
| 62 | + success = windll.kernel32.Process32Next(hSnapshot, byref(pe)) |
| 63 | + if not success: |
| 64 | + if windll.kernel32.GetLastError() == ERROR_NO_MORE_FILES: |
| 65 | + return |
| 66 | + raise WinError() |
| 67 | + return pe |
| 68 | + |
| 69 | + |
| 70 | +def get_all_processes(): |
| 71 | + """Return a dictionary of properties about all processes. |
| 72 | +
|
| 73 | + >>> get_all_processes() |
| 74 | + { |
| 75 | + 1509: { |
| 76 | + 'parent_pid': 1201, |
| 77 | + 'executable': 'C:\\Program\\\\ Files\\Python36\\python.exe' |
| 78 | + } |
| 79 | + } |
| 80 | + """ |
| 81 | + h_process = CreateToolhelp32Snapshot() |
| 82 | + pids = {} |
| 83 | + pe = Process32First(h_process) |
| 84 | + while pe: |
| 85 | + pids[pe.th32ProcessID] = { |
| 86 | + 'executable': '{0}'.format(pe.szExeFile) |
| 87 | + } |
| 88 | + if pe.th32ParentProcessID: |
| 89 | + pids[pe.th32ProcessID]['parent_pid'] = pe.th32ParentProcessID |
| 90 | + pe = Process32Next(h_process, pe) |
| 91 | + |
| 92 | + return pids |
| 93 | + |
| 94 | + |
| 95 | +def get_grandparent_process(pid=None): |
| 96 | + """Get grandparent process name of the supplied pid or os.getpid(). |
| 97 | +
|
| 98 | + :param int pid: The pid to track. |
| 99 | + :return: Name of the grandparent process. |
| 100 | + """ |
| 101 | + if not pid: |
| 102 | + pid = os.getpid() |
| 103 | + processes = get_all_processes() |
| 104 | + ppid = processes[pid]['parent_pid'] |
| 105 | + parent = processes[ppid] |
| 106 | + grandparent = processes[parent['parent_pid']] |
| 107 | + return grandparent['executable'] |
0 commit comments