annotate dep/animone/src/fd/win32.cc @ 285:65df2813d0de

dep/animone: header cleanup
author Paper <paper@paper.us.eu.org>
date Wed, 08 May 2024 16:43:32 -0400
parents 09c5bd74fe93
children b1f625b0227c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "animone/fd/win32.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #include "animone.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 #include "animone/util/win32.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <stdexcept>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <string>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <unordered_map>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include <vector>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 #include <fileapi.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include <handleapi.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 #include <libloaderapi.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 #include <ntdef.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 #include <psapi.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 #include <shlobj.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 #include <stringapiset.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 #include <tlhelp32.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 #include <windows.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 #include <winternl.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 /* This file is noticably more complex than Unix and Linux, and that's because
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 * there is no "simple" way to get the paths of a file. In fact, this thing requires
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 * you to use *internal functions* that can't even be linked to, hence why we have to
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 * use GetProcAddress and such. What a mess.
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 *
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 * Speaking of which, because this file uses internal functions of the OS, it is not
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 * guaranteed to work far into the future. However, it has worked since NT 6.0 (Vista)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 * at least, so it's unlikely to be changed much ever.
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 /* SystemExtendedHandleInformation is only available in NT 5.1+ (XP and higher) and provides information for
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 * 32-bit PIDs, unlike SystemHandleInformation
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 static constexpr SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = static_cast<SYSTEM_INFORMATION_CLASS>(0x40);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 static constexpr NTSTATUS STATUS_INFO_LENGTH_MISMATCH = 0xC0000004UL;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 /* this is filled in at runtime because it's not guaranteed to be (and isn't)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 * constant between different versions of Windows */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 static unsigned short file_type_index = 0;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 struct SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 PVOID Object;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 ULONG_PTR UniqueProcessId;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 HANDLE HandleValue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 ACCESS_MASK GrantedAccess;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 USHORT CreatorBackTraceIndex;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 USHORT ObjectTypeIndex;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 ULONG HandleAttributes;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 ULONG Reserved;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 };
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 struct SYSTEM_HANDLE_INFORMATION_EX {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 ULONG_PTR NumberOfHandles;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 ULONG_PTR Reserved;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handles[1];
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 };
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 namespace animone::internal::win32 {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 class Ntdll {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 public:
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 Ntdll() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 ntdll = ::GetModuleHandleW(L"ntdll.dll");
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 nt_query_system_information = reinterpret_cast<decltype(::NtQuerySystemInformation)*>(
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 ::GetProcAddress(ntdll, "NtQuerySystemInformation"));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 nt_query_object = reinterpret_cast<decltype(::NtQueryObject)*>(::GetProcAddress(ntdll, "NtQueryObject"));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 NTSTATUS QuerySystemInformation(SYSTEM_INFORMATION_CLASS cls, PVOID sysinfo, ULONG len,
267
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
70 PULONG retlen){
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
71 return nt_query_system_information(cls, sysinfo, len, retlen);
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
72 }
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 NTSTATUS QueryObject(HANDLE handle, OBJECT_INFORMATION_CLASS cls, PVOID objinf, ULONG objinflen, PULONG retlen) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 return nt_query_object(handle, cls, objinf, objinflen, retlen);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 private:
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 HMODULE ntdll;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 decltype(::NtQuerySystemInformation)* nt_query_system_information;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 decltype(::NtQueryObject)* nt_query_object;
267
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
82 };
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 Ntdll ntdll;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 static HANDLE DuplicateHandle(HANDLE process_handle, HANDLE handle) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 HANDLE dup_handle = nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 const bool result =
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 ::DuplicateHandle(process_handle, handle, ::GetCurrentProcess(), &dup_handle, 0, false, DUPLICATE_SAME_ACCESS);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 return result ? dup_handle : nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 static std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> GetSystemHandleInformation() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 /* we should really put a cap on this */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 ULONG cb = 1 << 19;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 NTSTATUS status = STATUS_NO_MEMORY;
267
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
97 std::unique_ptr<SYSTEM_HANDLE_INFORMATION_EX> info;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99 do {
267
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
100 info.reset(reinterpret_cast<SYSTEM_HANDLE_INFORMATION_EX*>(malloc(cb *= 2)));
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 if (!info)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 status = ntdll.QuerySystemInformation(SystemExtendedHandleInformation, info.get(), cb, &cb);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 } while (status == STATUS_INFO_LENGTH_MISMATCH);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 if (!NT_SUCCESS(status))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 return {};
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> res;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 ULONG_PTR handles = info->NumberOfHandles;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 if (!handles)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 return {};
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 res.reserve(handles);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX* entry = info->Handles;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 do {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120 if (entry)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121 res.push_back(*(entry++));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 } while (--handles);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 return res;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 static std::wstring GetHandleType(HANDLE handle) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 OBJECT_TYPE_INFORMATION info = {0};
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129 ntdll.QueryObject(handle, ObjectTypeInformation, &info, sizeof(info), NULL);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 return std::wstring(info.TypeName.Buffer, info.TypeName.Length);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
133 static std::wstring GetFinalPathNameByHandle(HANDLE handle) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134 std::wstring buffer;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
135
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
136 DWORD size = ::GetFinalPathNameByHandleW(handle, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 buffer.resize(size);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138 ::GetFinalPathNameByHandleW(handle, &buffer.front(), buffer.size(), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
139
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
140 return buffer;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
141 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
142
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143 static bool IsFileHandle(HANDLE handle, unsigned short object_type_index) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144 if (file_type_index)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
145 return object_type_index == file_type_index;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
146 else if (!handle)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
147 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
148 else if (GetHandleType(handle) == L"File") {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
149 file_type_index = object_type_index;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
150 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
151 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155 static bool IsFileMaskOk(ACCESS_MASK access_mask) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 if (!(access_mask & FILE_READ_DATA))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 if ((access_mask & FILE_APPEND_DATA) || (access_mask & FILE_WRITE_EA) || (access_mask & FILE_WRITE_ATTRIBUTES))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
160 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
161
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
164
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
165 static bool IsFilePathOk(const std::wstring& path) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166 if (path.empty())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 if (IsSystemDirectory(path))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172 const auto file_attributes = GetFileAttributesW(path.c_str());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 if ((file_attributes == INVALID_FILE_ATTRIBUTES) || (file_attributes & FILE_ATTRIBUTE_DIRECTORY))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
174 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
175
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179 bool GetProcessName(pid_t pid, std::string& name) {
267
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
180 std::wstring wname = GetProcessPath(pid);
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 if (wname.empty())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183
267
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
184 name = ToUtf8String(GetFileNameWithoutExtension(GetFileNameFromPath(wname)));
09c5bd74fe93 win32: make builds work again
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
185 return true;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
188 bool EnumerateOpenProcesses(process_proc_t process_proc) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
189 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 if (hProcessSnap == INVALID_HANDLE_VALUE)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193 PROCESSENTRY32 pe32;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194 pe32.dwSize = sizeof(PROCESSENTRY32);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
196 if (!::Process32First(hProcessSnap, &pe32))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
197 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
198
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
199 if (!process_proc({pe32.th32ProcessID, pe32.szExeFile}))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
202 while (::Process32Next(hProcessSnap, &pe32))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
203 if (!process_proc({pe32.th32ProcessID, pe32.szExeFile}))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
206 ::CloseHandle(hProcessSnap);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
207
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 /* this could be changed to being a callback, but... I'm too lazy right now :) */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
212 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
213 if (!open_file_proc)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216 std::unordered_map<pid_t, Handle> proc_handles;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218 for (const pid_t& pid : pids) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
219 const HANDLE handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, pid);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
220 if (handle != INVALID_HANDLE_VALUE)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 proc_handles[pid] = Handle(handle);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
223
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
224 if (proc_handles.empty())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227 std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> info = GetSystemHandleInformation();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229 for (const auto& h : info) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
230 const pid_t pid = h.UniqueProcessId;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
231 if (!pids.count(pid))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
232 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
234 if (!IsFileHandle(nullptr, h.ObjectTypeIndex))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
235 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
236
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
237 if (!IsFileMaskOk(h.GrantedAccess))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
238 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
239
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
240 Handle handle(DuplicateHandle(proc_handles[pid].get(), h.HandleValue));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
241 if (handle.get() == INVALID_HANDLE_VALUE)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
242 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
243
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
244 if (GetFileType(handle.get()) != FILE_TYPE_DISK)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
245 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
246
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
247 const std::wstring path = GetFinalPathNameByHandle(handle.get());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
248 if (!IsFilePathOk(path))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
249 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
250
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
251 if (!open_file_proc({pid, ToUtf8String(path)}))
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
252 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
253 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
254
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
255 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
256 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
257
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
258 } // namespace animone::internal::win32