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