blob: 023ff2deffca401ba723810c6c59534cac18efb4 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* 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
7int 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 Wouters0e3f5912006-08-11 14:57:12 +000045
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 */
56 if ((strstr(path, "build\\pcbuild\\python_d.exe") != NULL) ||
57 (strstr(path, "\\build\\python.exe") != NULL)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 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}