installer-process-helper.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #define WIN32_LEAN_AND_MEAN
  2. #define _WIN32_WINNT 0x0601
  3. #include <windows.h>
  4. #include <tlhelp32.h>
  5. #include <cstdio>
  6. #include <cwchar>
  7. #include <string>
  8. #include <vector>
  9. namespace {
  10. struct Handle {
  11. HANDLE value;
  12. explicit Handle(HANDLE handle) : value(handle) {}
  13. ~Handle() { if (value && value != INVALID_HANDLE_VALUE) CloseHandle(value); }
  14. Handle(const Handle&) = delete;
  15. Handle& operator=(const Handle&) = delete;
  16. };
  17. void report(const char* stage, DWORD pid, DWORD error = 0) {
  18. char message[128];
  19. const int size = std::snprintf(message, sizeof(message), "stage=%s pid=%lu win32-error=%lu\n", stage, pid, error);
  20. if (size <= 0) return;
  21. std::fputs(message, stdout);
  22. std::fflush(stdout);
  23. // 修复原因:诊断日志被占用或不可写时,不能改变进程检查结果。
  24. wchar_t temp[MAX_PATH];
  25. DWORD length = GetTempPathW(MAX_PATH, temp);
  26. if (length == 0 || length >= MAX_PATH) return;
  27. const std::wstring path = std::wstring(temp) + L"ZCode-process-cleanup.log";
  28. Handle log(CreateFileW(path.c_str(), FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE,
  29. nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr));
  30. if (log.value == INVALID_HANDLE_VALUE) return;
  31. DWORD written = 0;
  32. WriteFile(log.value, message, static_cast<DWORD>(size), &written, nullptr);
  33. }
  34. bool imagePath(HANDLE process, std::wstring& path, DWORD& error) {
  35. std::vector<wchar_t> buffer(32768);
  36. DWORD length = static_cast<DWORD>(buffer.size());
  37. if (!QueryFullProcessImageNameW(process, 0, buffer.data(), &length)) {
  38. error = GetLastError();
  39. return false;
  40. }
  41. path.assign(buffer.data(), length);
  42. return true;
  43. }
  44. bool exited(HANDLE process) {
  45. DWORD code = STILL_ACTIVE;
  46. return GetExitCodeProcess(process, &code) && code != STILL_ACTIVE;
  47. }
  48. bool sameProcess(HANDLE first, HANDLE second, DWORD& error) {
  49. FILETIME firstCreation{}, firstExit{}, firstKernel{}, firstUser{};
  50. FILETIME secondCreation{}, secondExit{}, secondKernel{}, secondUser{};
  51. if (!GetProcessTimes(first, &firstCreation, &firstExit, &firstKernel, &firstUser) ||
  52. !GetProcessTimes(second, &secondCreation, &secondExit, &secondKernel, &secondUser)) {
  53. error = GetLastError();
  54. return false;
  55. }
  56. if (firstCreation.dwLowDateTime != secondCreation.dwLowDateTime ||
  57. firstCreation.dwHighDateTime != secondCreation.dwHighDateTime) {
  58. error = ERROR_INVALID_DATA;
  59. return false;
  60. }
  61. return true;
  62. }
  63. bool samePath(const std::wstring& left, const std::wstring& right) {
  64. return _wcsicmp(left.c_str(), right.c_str()) == 0;
  65. }
  66. int scan(const std::wstring& expected, bool terminate) {
  67. Handle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
  68. if (snapshot.value == INVALID_HANDLE_VALUE) {
  69. report("enumerate-failed", 0, GetLastError());
  70. return 2;
  71. }
  72. PROCESSENTRY32W entry{};
  73. entry.dwSize = sizeof(entry);
  74. if (!Process32FirstW(snapshot.value, &entry)) {
  75. DWORD error = GetLastError();
  76. if (error == ERROR_NO_MORE_FILES) return 0;
  77. report("enumerate-failed", 0, error);
  78. return 2;
  79. }
  80. const wchar_t* name = wcsrchr(expected.c_str(), L'\\');
  81. name = name ? name + 1 : expected.c_str();
  82. do {
  83. if (_wcsicmp(entry.szExeFile, name) != 0) continue;
  84. const DWORD pid = entry.th32ProcessID;
  85. // 修复原因:其他目录的同名进程可能拒绝终止权限;识别前只申请查询权限。
  86. Handle query(OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid));
  87. if (!query.value) {
  88. const DWORD error = GetLastError();
  89. if (error == ERROR_INVALID_PARAMETER) continue;
  90. report("query-open-failed", pid, error);
  91. return 2;
  92. }
  93. if (exited(query.value)) continue;
  94. std::wstring path;
  95. DWORD error = 0;
  96. if (!imagePath(query.value, path, error)) {
  97. if (exited(query.value)) continue;
  98. report("query-failed", pid, error);
  99. return 2;
  100. }
  101. if (!samePath(path, expected)) continue;
  102. if (!terminate) {
  103. report("rescan-failed", pid);
  104. return 2;
  105. }
  106. Handle target(OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE | SYNCHRONIZE,
  107. FALSE, pid));
  108. if (!target.value) {
  109. error = GetLastError();
  110. if (exited(query.value)) continue;
  111. report("terminate-open-failed", pid, error);
  112. return 2;
  113. }
  114. if (WaitForSingleObject(target.value, 0) == WAIT_OBJECT_0) continue;
  115. // 查询句柄保持旧进程对象存活;创建时间和路径都必须与终止句柄核对。
  116. if (!sameProcess(query.value, target.value, error)) {
  117. if (exited(query.value)) continue;
  118. report("target-identity-failed", pid, error);
  119. return 2;
  120. }
  121. if (!imagePath(target.value, path, error)) {
  122. if (WaitForSingleObject(target.value, 0) == WAIT_OBJECT_0) continue;
  123. report("target-query-failed", pid, error);
  124. return 2;
  125. }
  126. if (!samePath(path, expected)) continue;
  127. report("matched", pid);
  128. if (!TerminateProcess(target.value, 0)) {
  129. error = GetLastError();
  130. if (WaitForSingleObject(target.value, 0) != WAIT_OBJECT_0) {
  131. report("terminate-failed", pid, error);
  132. return 2;
  133. }
  134. }
  135. const DWORD wait = WaitForSingleObject(target.value, 5000);
  136. if (wait != WAIT_OBJECT_0) {
  137. report("wait-failed", pid, wait == WAIT_FAILED ? GetLastError() : wait);
  138. return 2;
  139. }
  140. report("exited", pid);
  141. } while (Process32NextW(snapshot.value, &entry));
  142. const DWORD error = GetLastError();
  143. if (error != ERROR_NO_MORE_FILES) {
  144. report("enumerate-failed", 0, error);
  145. return 2;
  146. }
  147. return 0;
  148. }
  149. } // namespace
  150. int wmain(int argc, wchar_t** argv) {
  151. if (argc != 2) {
  152. report("arguments-failed", 0);
  153. return 2;
  154. }
  155. std::vector<wchar_t> buffer(32768);
  156. DWORD length = GetFullPathNameW(argv[1], static_cast<DWORD>(buffer.size()), buffer.data(), nullptr);
  157. if (length == 0 || length >= buffer.size()) {
  158. report("path-failed", 0, GetLastError());
  159. return 2;
  160. }
  161. const std::wstring expected(buffer.data(), length);
  162. if (scan(expected, true) != 0 || scan(expected, false) != 0) return 2;
  163. report("stop-completed", 0);
  164. return 0;
  165. }