/PC/VS8.0/kill_python.c

http://unladen-swallow.googlecode.com/ · C · 178 lines · 101 code · 35 blank · 42 comment · 20 complexity · bc87ff1d85de62b2edf161ed7fc0939d MD5 · raw file

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