Martin v. Löwis | 2d12372 | 2006-04-17 09:46:47 +0000 | [diff] [blame] | 1 | /* This program looks for processes which have build\PCbuild\python.exe |
| 2 | in their path and terminates them. */ |
| 3 | #include <windows.h> |
| 4 | #include <psapi.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | DWORD pids[1024], cbNeeded; |
| 10 | int i, num_processes; |
| 11 | if (!EnumProcesses(pids, sizeof(pids), &cbNeeded)) { |
| 12 | printf("EnumProcesses failed\n"); |
| 13 | return 1; |
| 14 | } |
| 15 | num_processes = cbNeeded/sizeof(pids[0]); |
| 16 | for (i = 0; i < num_processes; i++) { |
| 17 | HANDLE hProcess; |
| 18 | char path[MAX_PATH]; |
| 19 | HMODULE mods[1024]; |
| 20 | int k, num_mods; |
| 21 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
| 22 | | PROCESS_VM_READ |
| 23 | | PROCESS_TERMINATE , |
| 24 | FALSE, pids[i]); |
| 25 | if (!hProcess) |
| 26 | /* process not accessible */ |
| 27 | continue; |
| 28 | if (!EnumProcessModules(hProcess, mods, sizeof(mods), &cbNeeded)) { |
| 29 | /* For unknown reasons, this sometimes returns ERROR_PARTIAL_COPY; |
| 30 | this apparently means we are not supposed to read the process. */ |
| 31 | if (GetLastError() == ERROR_PARTIAL_COPY) { |
| 32 | CloseHandle(hProcess); |
| 33 | continue; |
| 34 | } |
| 35 | printf("EnumProcessModules failed: %d\n", GetLastError()); |
| 36 | return 1; |
| 37 | } |
Martin v. Löwis | 70f5f7a | 2006-04-17 17:26:42 +0000 | [diff] [blame] | 38 | if (!GetModuleFileNameEx(hProcess, NULL, path, sizeof(path))) { |
Martin v. Löwis | 2d12372 | 2006-04-17 09:46:47 +0000 | [diff] [blame] | 39 | printf("GetProcessImageFileName failed\n"); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | _strlwr(path); |
Martin v. Löwis | ce8607d | 2006-04-17 10:39:39 +0000 | [diff] [blame] | 44 | /* printf("%s\n", path); */ |
Neal Norwitz | e4abc23 | 2006-07-27 03:51:58 +0000 | [diff] [blame] | 45 | |
| 46 | /* Check if we are running a buildbot version of Python. |
| 47 | |
| 48 | On Windows, this will always be a debug build from the |
| 49 | PCbuild directory. build\\PCbuild\\python_d.exe |
| 50 | |
| 51 | On Cygwin, the pathname is similar to other Unixes. |
| 52 | Use \\build\\python.exe to ensure we don't match |
| 53 | PCbuild\\python.exe which could be a normal instance |
| 54 | of Python running on vanilla Windows. |
| 55 | */ |
Martin v. Löwis | 787fe6d | 2006-06-10 08:14:03 +0000 | [diff] [blame] | 56 | if ((strstr(path, "build\\pcbuild\\python_d.exe") != NULL) || |
Neal Norwitz | e4abc23 | 2006-07-27 03:51:58 +0000 | [diff] [blame] | 57 | (strstr(path, "\\build\\python.exe") != NULL)) { |
Martin v. Löwis | 2d12372 | 2006-04-17 09:46:47 +0000 | [diff] [blame] | 58 | printf("Terminating %s (pid %d)\n", path, pids[i]); |
| 59 | if (!TerminateProcess(hProcess, 1)) { |
| 60 | printf("Termination failed: %d\n", GetLastError()); |
| 61 | return 1; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | CloseHandle(hProcess); |
| 67 | } |
Martin v. Löwis | 70f5f7a | 2006-04-17 17:26:42 +0000 | [diff] [blame] | 68 | } |