Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame^] | 1 | /* This program looks for processes which have build\PCbuild\python.exe |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | } |
| 38 | if (!GetModuleFileNameEx(hProcess, NULL, path, sizeof(path))) { |
| 39 | printf("GetProcessImageFileName failed\n"); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | _strlwr(path); |
| 44 | /* printf("%s\n", path); */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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 |
Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame^] | 49 | PCbuild directory. build\\PCbuild\\python_d.exe |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | |
| 51 | On Cygwin, the pathname is similar to other Unixes. |
| 52 | Use \\build\\python.exe to ensure we don't match |
Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame^] | 53 | PCbuild\\python.exe which could be a normal instance |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | of Python running on vanilla Windows. |
| 55 | */ |
Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame^] | 56 | if ((strstr(path, "pcbuild\\python_d.exe") != NULL) || |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | (strstr(path, "\\build\\python.exe") != NULL)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | } |
| 68 | } |