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