-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathentry.cpp
129 lines (112 loc) · 3.18 KB
/
entry.cpp
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
#include <windows.h>
#include "libProcessorGroupExtender/LogOut.h"
#include "libProcessorGroupExtender/helpers.h"
#include "libProcessorGroupExtender/groupextend.h"
#include "entry.h"
HANDLE g_hExitEvent = NULL;
void ShowUsage()
{
wprintf(L"\nUsage: groupextend [pid|name]\n");
}
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
case CTRL_SHUTDOWN_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
wprintf(L"\n > Ctrl event");
if (g_hExitEvent) SetEvent(g_hExitEvent);
return TRUE;
default:
return FALSE;
}
}
int wmain(int argc, const wchar_t* argv[])
{
LogOut Log(GroupExtend::DefaultLogTarget);
Log.Write(GroupExtend::INTRO_STRING);
Log.Write(GroupExtend::BUILD_STRING_FMT, GroupExtend::BUILD_NUM_STR, __DATE__);
Log.Write(L"\n");
if (argc < 2)
{
ShowUsage();
return 1;
}
// try to resolve command line argument(s) to PIDs from exeName
// if that fails, assume is numeric PID
// this allows for processes with exeNames of integers (if that ever happens)
std::vector<unsigned long> vecTargetPIDs;
for (int i = 1; i < argc; i++)
{
if (GroupExtend::GetPIDsForProcessName(argv[i], vecTargetPIDs))
{
Log.Write(L"\n%s has instances of PID(s)", argv[i]);
for (auto& pid : vecTargetPIDs)
{
Log.Write(L" %u", pid);
}
}
else
{
unsigned long pid = wcstoul(argv[i], nullptr, 10);
if (pid)
{
vecTargetPIDs.push_back(pid);
}
}
}
if (vecTargetPIDs.size() == 0)
{
Log.Write(L"\nERROR: No processes found that match specification.\n");
return 2;
}
if (vecTargetPIDs.size() > 1)
{
Log.Write(L"\nWARNING: Multiple process instances were found, but groupextend can currently only manage one (per instance). Managing %u", vecTargetPIDs[0]);
}
// required priv tokens, by name
const WCHAR* pwszSecTokens[] =
{
SE_ASSIGNPRIMARYTOKEN_NAME,
SE_DEBUG_NAME,
SE_INC_BASE_PRIORITY_NAME
};
for (const WCHAR* pwszToken : pwszSecTokens)
{
if (!GroupExtend::NtGetPrivByName(pwszToken))
{
Log.Write(L"\n WARNING: Couldn't get privilege %s", pwszToken);
}
}
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
//SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
// create before SetConsoelCtrlHandler
g_hExitEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
SetConsoleCtrlHandler(CtrlHandler, TRUE);
// for signalling caller that thread stopped (e.g. process no longer exists or error)
HANDLE hThreadStoppedEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (!g_hExitEvent || !hThreadStoppedEvent)
{
Log.Write(L"\n ERROR creating events. Aborting");
if (hThreadStoppedEvent) CloseHandle(hThreadStoppedEvent);
if (g_hExitEvent) CloseHandle(g_hExitEvent);
return 3;
}
//
// start management of target process threads
// magic is in libProcessorGroupExtender
//
ProcessorGroupExtender_SingleProcess cExtender;
if (cExtender.StartAsync(vecTargetPIDs[0], 0, GroupExtend::DefaultLogTarget, hThreadStoppedEvent))
{
HANDLE hWaits[2] = { g_hExitEvent, hThreadStoppedEvent };
WaitForMultipleObjects(_countof(hWaits), hWaits, FALSE, INFINITE);
cExtender.Stop();
}
CloseHandle(hThreadStoppedEvent);
CloseHandle(g_hExitEvent);
return 0;
}