Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | from ctypes import sizeof, windll, addressof, c_wchar, create_unicode_buffer |
michael@0 | 6 | from ctypes.wintypes import DWORD, HANDLE |
michael@0 | 7 | |
michael@0 | 8 | PROCESS_TERMINATE = 0x0001 |
michael@0 | 9 | PROCESS_QUERY_INFORMATION = 0x0400 |
michael@0 | 10 | PROCESS_VM_READ = 0x0010 |
michael@0 | 11 | |
michael@0 | 12 | def get_pids(process_name): |
michael@0 | 13 | BIG_ARRAY = DWORD * 4096 |
michael@0 | 14 | processes = BIG_ARRAY() |
michael@0 | 15 | needed = DWORD() |
michael@0 | 16 | |
michael@0 | 17 | pids = [] |
michael@0 | 18 | result = windll.psapi.EnumProcesses(processes, |
michael@0 | 19 | sizeof(processes), |
michael@0 | 20 | addressof(needed)) |
michael@0 | 21 | if not result: |
michael@0 | 22 | return pids |
michael@0 | 23 | |
michael@0 | 24 | num_results = needed.value / sizeof(DWORD) |
michael@0 | 25 | |
michael@0 | 26 | for i in range(num_results): |
michael@0 | 27 | pid = processes[i] |
michael@0 | 28 | process = windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | |
michael@0 | 29 | PROCESS_VM_READ, |
michael@0 | 30 | 0, pid) |
michael@0 | 31 | if process: |
michael@0 | 32 | module = HANDLE() |
michael@0 | 33 | result = windll.psapi.EnumProcessModules(process, |
michael@0 | 34 | addressof(module), |
michael@0 | 35 | sizeof(module), |
michael@0 | 36 | addressof(needed)) |
michael@0 | 37 | if result: |
michael@0 | 38 | name = create_unicode_buffer(1024) |
michael@0 | 39 | result = windll.psapi.GetModuleBaseNameW(process, module, |
michael@0 | 40 | name, len(name)) |
michael@0 | 41 | # TODO: This might not be the best way to |
michael@0 | 42 | # match a process name; maybe use a regexp instead. |
michael@0 | 43 | if name.value.startswith(process_name): |
michael@0 | 44 | pids.append(pid) |
michael@0 | 45 | windll.kernel32.CloseHandle(module) |
michael@0 | 46 | windll.kernel32.CloseHandle(process) |
michael@0 | 47 | |
michael@0 | 48 | return pids |
michael@0 | 49 | |
michael@0 | 50 | def kill_pid(pid): |
michael@0 | 51 | process = windll.kernel32.OpenProcess(PROCESS_TERMINATE, 0, pid) |
michael@0 | 52 | if process: |
michael@0 | 53 | windll.kernel32.TerminateProcess(process, 0) |
michael@0 | 54 | windll.kernel32.CloseHandle(process) |
michael@0 | 55 | |
michael@0 | 56 | if __name__ == '__main__': |
michael@0 | 57 | import subprocess |
michael@0 | 58 | import time |
michael@0 | 59 | |
michael@0 | 60 | # This test just opens a new notepad instance and kills it. |
michael@0 | 61 | |
michael@0 | 62 | name = 'notepad' |
michael@0 | 63 | |
michael@0 | 64 | old_pids = set(get_pids(name)) |
michael@0 | 65 | subprocess.Popen([name]) |
michael@0 | 66 | time.sleep(0.25) |
michael@0 | 67 | new_pids = set(get_pids(name)).difference(old_pids) |
michael@0 | 68 | |
michael@0 | 69 | if len(new_pids) != 1: |
michael@0 | 70 | raise Exception('%s was not opened or get_pids() is ' |
michael@0 | 71 | 'malfunctioning' % name) |
michael@0 | 72 | |
michael@0 | 73 | kill_pid(tuple(new_pids)[0]) |
michael@0 | 74 | |
michael@0 | 75 | newest_pids = set(get_pids(name)).difference(old_pids) |
michael@0 | 76 | |
michael@0 | 77 | if len(newest_pids) != 0: |
michael@0 | 78 | raise Exception('kill_pid() is malfunctioning') |
michael@0 | 79 | |
michael@0 | 80 | print "Test passed." |