Index: PC/VS8.0/kill_python.c =================================================================== --- PC/VS8.0/kill_python.c (revision 85227) +++ PC/VS8.0/kill_python.c (working copy) @@ -23,13 +23,68 @@ #define KILL_PYTHON_EXE_LEN (15) #endif +static HANDLE +create_file(const wchar_t *path) +{ + return CreateFileW( + path, + 0, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, + NULL); +} + +static int +same_file(const wchar_t *path1, const wchar_t *path2) +{ + HANDLE h1, h2; + BY_HANDLE_FILE_INFORMATION info1, info2; + + h1 = create_file(path1); + if (h1 == INVALID_HANDLE_VALUE) { + printf("CreateFileW[1] failed: %d\n", GetLastError()); + return -1; + } + + h2 = create_file(path2); + if (h2 == INVALID_HANDLE_VALUE) { + printf("CreateFileW[2] failed: %d\n", GetLastError()); + CloseHandle(h1); + return -1; + } + + if (!GetFileInformationByHandle(h1, &info1)) { + printf("GetFileInformationByHandle[1] failed: %d\n", GetLastError()); + CloseHandle(h1); + CloseHandle(h2); + return -1; + } + + if (!GetFileInformationByHandle(h2, &info2)) { + printf("GetFileInformationByHandle[2] failed: %d\n", GetLastError()); + CloseHandle(h1); + CloseHandle(h2); + return -1; + } + + CloseHandle(h1); + CloseHandle(h2); + + return info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber + && info1.nFileIndexLow == info2.nFileIndexLow + && info1.nFileIndexHigh == info2.nFileIndexHigh ? 1 : 0; +} + int main(int argc, char **argv) { HANDLE hp, hsp, hsm; /* process, snapshot processes, snapshot modules */ DWORD dac, our_pid; size_t len; - wchar_t path[MAX_PATH+1]; + wchar_t path[MAX_PATH+1], path2[MAX_PATH+1]; + int code; MODULEENTRY32W me; PROCESSENTRY32W pe; @@ -37,7 +92,8 @@ me.dwSize = sizeof(MODULEENTRY32W); pe.dwSize = sizeof(PROCESSENTRY32W); - memset(path, 0, MAX_PATH+1); + memset(path, 0, sizeof(path)); + memset(path2, 0, sizeof(path2)); our_pid = GetCurrentProcessId(); @@ -70,11 +126,6 @@ CloseHandle(hsm); - if (path == NULL) { - printf("failed to discern directory of running process\n"); - return 1; - } - /* * Take a snapshot of system processes. Enumerate over the snapshot, * looking for python processes. When we find one, verify it lives @@ -140,7 +191,16 @@ /* Wrong module, we're looking for python[_d].exe... */ continue; - if (_wcsnicmp(path, me.szExePath, len)) + len = wcsnlen_s(me.szExePath, MAX_PATH) - PYTHON_EXE_LEN; + wcsncpy_s(path2, MAX_PATH+1, me.szExePath, len); + + code = same_file(path, path2); + if (code < 0) { + CloseHandle(hsp); + CloseHandle(hsm); + return 1; + } + if (code == 0) /* Process doesn't live in our directory. */ break;