blob: 7fd4d94ae54c5ceb823e66303926f16776e598f0 [file] [log] [blame]
Trent Nelsond6dffbc2008-04-03 18:27:06 +00001/*
2 * Helper program for killing lingering python[_d].exe processes before
3 * building, thus attempting to avoid build failures due to files being
4 * locked.
5 */
6
7#include <windows.h>
8#include <wchar.h>
9#include <tlhelp32.h>
10#include <stdio.h>
11
12#pragma comment(lib, "psapi")
13
14#ifdef _DEBUG
15#define PYTHON_EXE (L"python_d.exe")
16#define PYTHON_EXE_LEN (12)
17#define KILL_PYTHON_EXE (L"kill_python_d.exe")
18#define KILL_PYTHON_EXE_LEN (17)
19#else
20#define PYTHON_EXE (L"python.exe")
21#define PYTHON_EXE_LEN (10)
22#define KILL_PYTHON_EXE (L"kill_python.exe")
23#define KILL_PYTHON_EXE_LEN (15)
24#endif
25
26int
27main(int argc, char **argv)
28{
29 HANDLE hp, hsp, hsm; /* process, snapshot processes, snapshot modules */
30 DWORD dac, our_pid;
31 size_t len;
32 wchar_t path[MAX_PATH+1];
33
34 MODULEENTRY32W me;
35 PROCESSENTRY32W pe;
36
37 me.dwSize = sizeof(MODULEENTRY32W);
38 pe.dwSize = sizeof(PROCESSENTRY32W);
39
40 memset(path, 0, MAX_PATH+1);
41
42 our_pid = GetCurrentProcessId();
43
44 hsm = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, our_pid);
45 if (hsm == INVALID_HANDLE_VALUE) {
46 printf("CreateToolhelp32Snapshot[1] failed: %d\n", GetLastError());
47 return 1;
48 }
49
50 if (!Module32FirstW(hsm, &me)) {
51 printf("Module32FirstW[1] failed: %d\n", GetLastError());
52 CloseHandle(hsm);
53 return 1;
54 }
55
56 /*
57 * Enumerate over the modules for the current process in order to find
58 * kill_process[_d].exe, then take a note of the directory it lives in.
59 */
60 do {
61 if (_wcsnicmp(me.szModule, KILL_PYTHON_EXE, KILL_PYTHON_EXE_LEN))
62 continue;
63
64 len = wcsnlen_s(me.szExePath, MAX_PATH) - KILL_PYTHON_EXE_LEN;
65 wcsncpy_s(path, MAX_PATH+1, me.szExePath, len);
66
67 break;
68
69 } while (Module32NextW(hsm, &me));
70
71 CloseHandle(hsm);
72
73 if (path == NULL) {
74 printf("failed to discern directory of running process\n");
75 return 1;
76 }
77
78 /*
79 * Take a snapshot of system processes. Enumerate over the snapshot,
80 * looking for python processes. When we find one, verify it lives
81 * in the same directory we live in. If it does, kill it. If we're
82 * unable to kill it, treat this as a fatal error and return 1.
83 *
84 * The rationale behind this is that we're called at the start of the
85 * build process on the basis that we'll take care of killing any
86 * running instances, such that the build won't encounter permission
87 * denied errors during linking. If we can't kill one of the processes,
88 * we can't provide this assurance, and the build shouldn't start.
89 */
90
91 hsp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
92 if (hsp == INVALID_HANDLE_VALUE) {
93 printf("CreateToolhelp32Snapshot[2] failed: %d\n", GetLastError());
94 return 1;
95 }
96
97 if (!Process32FirstW(hsp, &pe)) {
98 printf("Process32FirstW failed: %d\n", GetLastError());
99 CloseHandle(hsp);
100 return 1;
101 }
102
103 dac = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE;
104 do {
105
106 /*
107 * XXX TODO: if we really wanted to be fancy, we could check the
108 * modules for all processes (not just the python[_d].exe ones)
109 * and see if any of our DLLs are loaded (i.e. python30[_d].dll),
110 * as that would also inhibit our ability to rebuild the solution.
111 * Not worth loosing sleep over though; for now, a simple check
112 * for just the python executable should be sufficient.
113 */
114
115 if (_wcsnicmp(pe.szExeFile, PYTHON_EXE, PYTHON_EXE_LEN))
116 /* This isn't a python process. */
117 continue;
118
119 /* It's a python process, so figure out which directory it's in... */
120 hsm = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pe.th32ProcessID);
121 if (hsm == INVALID_HANDLE_VALUE) {
122 printf("CreateToolhelp32Snapshot[3] failed: %d\n", GetLastError());
123 CloseHandle(hsp);
124 return 1;
125 }
126
127 if (!Module32FirstW(hsm, &me)) {
128 printf("Module32FirstW[2] failed: %d\n", GetLastError());
129 CloseHandle(hsp);
130 CloseHandle(hsm);
131 return 1;
132 }
133
134 do {
135 if (_wcsnicmp(me.szModule, PYTHON_EXE, PYTHON_EXE_LEN))
136 /* Wrong module, we're looking for python[_d].exe... */
137 continue;
138
139 if (_wcsnicmp(path, me.szExePath, len))
140 /* Process doesn't live in our directory. */
141 break;
142
143 /* Python process residing in the right directory, kill it! */
144 hp = OpenProcess(dac, FALSE, pe.th32ProcessID);
145 if (!hp) {
146 printf("OpenProcess failed: %d\n", GetLastError());
147 CloseHandle(hsp);
148 CloseHandle(hsm);
149 return 1;
150 }
151
152 if (!TerminateProcess(hp, 1)) {
153 printf("TerminateProcess failed: %d\n", GetLastError());
154 CloseHandle(hsp);
155 CloseHandle(hsm);
156 CloseHandle(hp);
157 return 1;
158 }
159
160 CloseHandle(hp);
161 break;
162
163 } while (Module32NextW(hsm, &me));
164
165 CloseHandle(hsm);
166
167 } while (Process32NextW(hsp, &pe));
168
169 CloseHandle(hsp);
170
171 return 0;
172}
173
174/* vi: set ts=8 sw=4 sts=4 expandtab */