forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl_check_process.cpp
More file actions
316 lines (258 loc) · 7.84 KB
/
cl_check_process.cpp
File metadata and controls
316 lines (258 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Windows Only, does a check against all other processes to see how many other instances
// of this process is running concurrently
//
//=============================================================================//
//*********************************************************************************************
// Process Check
#include "cl_check_process.h"
#include "dbg.h"
#ifdef IS_WINDOWS_PC
#include <Windows.h>
#include <winternl.h>
#include <stdio.h>
#include <TlHelp32.h>
#include "strtools.h"
#include <Psapi.h>
#endif // IS_WINDOWS_PC
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef IS_WINDOWS_PC
#define HANDLE_QUERY_BUFFER_BLOCK_SIZE ( 1 * 1024 * 1024 )
#define SystemHandleInformation ( (SYSTEM_INFORMATION_CLASS)16 )
#define STATUS_INFO_LENGTH_MISMATCH ( (NTSTATUS)( 0xC0000004L ) )
typedef NTSTATUS (__stdcall *NtQuerySystemInformation1)
(
IN ULONG SysInfoClass,
IN OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG RetLen
);
typedef struct _HANDLE_INFORMATION
{
DWORD ProcessId;
BYTE ObjectType;
BYTE Flags;
USHORT Handle;
PVOID KernelObject;
ACCESS_MASK GrantedAccess;
} HANDLE_INFORMATION, *PHANDLE_INFORMATION;
typedef struct _SYSTEM_HANDLE_INFORMATION
{
ULONG HandleCount;
HANDLE_INFORMATION HandleInfoArray[ 1 ];
} SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
//
// Checks Process Count with CreateToolhelp32Snapshot
//
int CheckOtherInstancesRunningWithSnapShot( const char *thisProcessNameShort )
{
DWORD nLength;
char otherProcessNameShort[ MAX_PATH ];
nLength = MAX_PATH;
int iSnapShotCount = 0;
//
HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hSnapshot )
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if ( Process32First( hSnapshot, &pe32 ) )
{
do
{
V_FileBase( pe32.szExeFile, otherProcessNameShort, MAX_PATH );
if ( V_strcmp( thisProcessNameShort, otherProcessNameShort ) == 0 )
{
// DevMsg( "CreateToolhelp32Snapshot - Process Name [ %s ] - OtherName [ %s ] \n", thisProcessNameShort, pe32.szExeFile );
// We found an instance of this executable.
iSnapShotCount++;
}
} while( Process32Next( hSnapshot, &pe32 ) );
}
CloseHandle(hSnapshot);
}
return iSnapShotCount;
}
//
// Checks Process Count with QueryFullProcessImageName and OpenProcess
//
int CheckOtherInstancesWithEnumProcess( const char *thisProcessNameShort )
{
NTSTATUS status;
BOOL bStatus;
DWORD nLength;
DWORD nBufferSize;
DWORD nLastProcess;
DWORD i;
HANDLE process;
PSYSTEM_HANDLE_INFORMATION pHandleInfo = NULL;
char otherProcessName[ MAX_PATH ];
char otherProcessNameShort[ MAX_PATH ];
HINSTANCE hInst = NULL;
// Start with a count of zero, since we will find ourselves too.
int iProcessCount = 0;
// Get the path to the executable for this process.
nLength = MAX_PATH;
// Query all of the handles in the system. We have to do this in a loop, since we do
// not know how large of a buffer we need.
nBufferSize = 0;
// Load ntdll.dll so we can Query the system
/* load the ntdll.dll */
NtQuerySystemInformation1 NtQuerySystemInformation;
//PVOID Info;
HMODULE hModule = LoadLibrary( "ntdll.dll" );
if (!hModule)
{
iProcessCount = CHECK_PROCESS_UNSUPPORTED;
goto Cleanup;
}
while ( TRUE )
{
// Increase the buffer size and try the query.
if ( pHandleInfo != NULL )
{
free( pHandleInfo );
}
nBufferSize += HANDLE_QUERY_BUFFER_BLOCK_SIZE;
pHandleInfo = (PSYSTEM_HANDLE_INFORMATION)malloc( nBufferSize );
if ( pHandleInfo == NULL )
{
iProcessCount = CHECK_PROCESS_UNSUPPORTED;
goto Cleanup;
}
// Query the handles in the system.
NtQuerySystemInformation = (NtQuerySystemInformation1)GetProcAddress(hModule, "NtQuerySystemInformation");
if ( NtQuerySystemInformation == NULL )
{
iProcessCount = CHECK_PROCESS_UNSUPPORTED;
goto Cleanup;
}
status = NtQuerySystemInformation( SystemHandleInformation, pHandleInfo, nBufferSize, NULL );
// If our buffer was too small, try again.
if ( status == STATUS_INFO_LENGTH_MISMATCH )
{
continue;
}
// If the query failed, return the error.
if ( !NT_SUCCESS( status ) )
{
iProcessCount = CHECK_PROCESS_UNSUPPORTED;
goto Cleanup;
}
break;
}
//
// Walk all of the entries looking for process IDs we have not processed yet.
// Note that this code assumes that handles will be grouped by process, which is
// what Windows does. If that assumption ever turns out to be false, this code
// will have to be altered to keep a list of process IDs already seen.
//
// Check for the presence of GetModuleFileNameEx
hInst = LoadLibrary( "Psapi.dll" );
if ( !hInst )
return CHECK_PROCESS_UNSUPPORTED;
typedef DWORD (WINAPI *GetProcessImageFileNameFn)(HANDLE, LPTSTR, DWORD);
GetProcessImageFileNameFn fn = (GetProcessImageFileNameFn)GetProcAddress( hInst,
#ifdef UNICODE
"GetProcessImageFileNameW");
#else
"GetProcessImageFileNameA");
#endif
if ( !fn )
return CHECK_PROCESS_UNSUPPORTED;
nLastProcess = 0;
for ( i = 0; i < pHandleInfo->HandleCount; i++ )
{
if ( pHandleInfo->HandleInfoArray[ i ].ProcessId != nLastProcess )
{
//nLastProcess = pHandleInfo->HandleInfoArray[ i ].ProcessId;
nLastProcess = pHandleInfo->HandleInfoArray[ i ].ProcessId;
//
// Try to open a handle to this process. Note that we may not have
// access to all processes, so we ignore errors.
//
process = OpenProcess( PROCESS_QUERY_INFORMATION,
FALSE,
nLastProcess );
if ( process != NULL )
{
// Query the name of the executable for the process we opened. If the query
// fails, we ignore this process.
nLength = MAX_PATH;
bStatus = fn( process, otherProcessName, nLength );
if ( bStatus )
{
//
// We have the process name. See if it is the same name as our process.
//
V_FileBase( otherProcessName, otherProcessNameShort, MAX_PATH );
if ( V_strcmp( thisProcessNameShort, otherProcessNameShort ) == 0 )
{
// We found an instance of this executable.
// DevMsg( "EnumProcess - Process Name [ %s ] - OtherName [ %s ] \n", thisProcessNameShort, otherProcessName );
iProcessCount++;
}
}
CloseHandle( process );
}
}
}
Cleanup:
// Free allocated resources.
if ( pHandleInfo != NULL )
{
free( pHandleInfo );
}
if ( hInst != NULL )
{
FreeLibrary( hInst );
}
if ( hModule != NULL )
{
FreeLibrary( hModule );
}
return iProcessCount;
}
#endif // IS_WINDOWS_PC
int CheckOtherInstancesRunning( void )
{
#ifdef IS_WINDOWS_PC
BOOL bStatus = 0;
DWORD nLength = MAX_PATH;
char thisProcessName[ MAX_PATH ];
char thisProcessNameShort[ MAX_PATH ];
// Load the pspapi to get our current process' name
HINSTANCE hInst = LoadLibrary( "Psapi.dll" );
if ( hInst )
{
typedef DWORD (WINAPI *GetProcessImageFileNameFn)(HANDLE, LPTSTR, DWORD);
GetProcessImageFileNameFn fn = (GetProcessImageFileNameFn)GetProcAddress( hInst,
#ifdef UNICODE
"GetProcessImageFileNameW");
#else
"GetProcessImageFileNameA");
#endif
if ( fn )
{
bStatus = fn( GetCurrentProcess(), thisProcessName, nLength );
}
FreeLibrary( hInst );
}
if ( !bStatus )
{
return CHECK_PROCESS_UNSUPPORTED;
}
V_FileBase( thisProcessName, thisProcessNameShort, MAX_PATH );
// Msg( "Checking Other Instances Running : ProcessShortName [ %s - %s ] \n", thisProcessName, thisProcessNameShort );
int iSnapShotCount = CheckOtherInstancesRunningWithSnapShot( thisProcessNameShort );
if ( iSnapShotCount > 1 )
{
return iSnapShotCount;
}
int iEnumCount = CheckOtherInstancesWithEnumProcess( thisProcessNameShort );
return iEnumCount > iSnapShotCount ? iEnumCount : iSnapShotCount;
#endif // IS_WINDOWS_PC
return CHECK_PROCESS_UNSUPPORTED; // -1 UNSUPPORTED
}