-
Notifications
You must be signed in to change notification settings - Fork 0
/
drvmgnt.cpp
271 lines (231 loc) · 7.94 KB
/
drvmgnt.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
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
#include "drvmgnt.h"
#include "utility.h"
// Install the driver service
bool SvcInstall (std::wstring svcName, std::wstring fullPath)
{
bool ret = false;
SC_HANDLE schSCManager = NULL,
schSCWinRing0 = NULL;
schSCManager =
OpenSCManager (
nullptr, // localhost
nullptr, // ServicesActive database
SC_MANAGER_CREATE_SERVICE // Desired Access
);
if (nullptr != schSCManager)
{
schSCWinRing0 =
CreateService (
schSCManager,
svcName.c_str(), // Service Name
svcName.c_str(), // Display Name
SERVICE_ALL_ACCESS, // Desired Access
SERVICE_KERNEL_DRIVER, // Service Type
SERVICE_SYSTEM_START, // Start Method
SERVICE_ERROR_NORMAL, // Error Control
fullPath.c_str(), // Fully qualified path
NULL, // Load Order Group (none)
NULL, // Tag ID (none)
NULL, // Dependencies (none)
NULL, // Driver Object Name (unused; doesn't seem to work)
NULL // Password (none)
);
if (nullptr != schSCWinRing0)
{
PLOG_INFO << "Driver service " << svcName << " was successfully installed.";
CloseServiceHandle (schSCWinRing0);
ret = true;
}
else if (ERROR_SERVICE_EXISTS == GetLastError ( ))
{
PLOG_WARNING << "Driver service " << svcName << " already exists.";
ret = true;
}
else
PLOG_ERROR << "Could not create the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCManager);
} else
PLOG_ERROR << "Could not open the service manager: " << SKIF_Util_GetLastError ( );
return ret;
}
// Uninstall the driver service
bool SvcUninstall (std::wstring svcName)
{
bool ret = false;
SC_HANDLE schSCManager = NULL,
schSCWinRing0 = NULL;
schSCManager =
OpenSCManager (
nullptr, // localhost
nullptr, // ServicesActive database
GENERIC_READ // Desired Access
);
if (nullptr != schSCManager)
{
schSCWinRing0 =
OpenService (
schSCManager,
svcName.c_str(), // Service Name
DELETE // Desired Access
);
if (nullptr != schSCWinRing0)
{
if (DeleteService (schSCWinRing0))
{
PLOG_INFO << "Driver service " << svcName << " was marked for deletion.";
ret = true;
}
else if (ERROR_SERVICE_MARKED_FOR_DELETE == GetLastError ( ))
{
PLOG_INFO << "Driver service " << svcName << " have already been marked for deletion.";
ret = true;
}
else
PLOG_ERROR << "Could not uninstall the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCWinRing0);
}
else if (ERROR_SERVICE_DOES_NOT_EXIST == GetLastError ( ))
{
PLOG_INFO << "Driver service " << svcName << " have already been deleted.";
// It is technically not "correct" to return true here when the
// service does not exist, but in our case we do so since the
// whole point of stopping the service is to uninstall it.
ret = true;
}
else
PLOG_ERROR << "Could not open the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCManager);
} else
PLOG_ERROR << "Could not open the service manager: " << SKIF_Util_GetLastError ( );
return ret;
}
// Start the driver service
bool SvcStart (std::wstring svcName)
{
bool ret = false;
SC_HANDLE schSCManager = NULL,
schSCWinRing0 = NULL;
schSCManager =
OpenSCManager (
nullptr, // localhost
nullptr, // ServicesActive database
GENERIC_READ // Desired Access
);
if (nullptr != schSCManager)
{
schSCWinRing0 =
OpenService (
schSCManager,
svcName.c_str(), // Service Name
SERVICE_START // Desired Access
);
if (nullptr != schSCWinRing0)
{
if (StartService (schSCWinRing0, 0, nullptr))
{
PLOG_INFO << "Driver service " << svcName << " was successfully started.";
ret = true;
}
else if (ERROR_SERVICE_ALREADY_RUNNING == GetLastError ( ))
{
PLOG_INFO << "Driver service " << svcName << " was already running.";
ret = true;
}
else if (ERROR_ALREADY_EXISTS == GetLastError ( ))
{
// It is technically not "correct" to return true here when we could not
// start up the service because the NT device already exist, but in our
// case we do so since it should resolve itself after a system restart.
PLOG_WARNING << "Could not start the " << svcName << " driver service because the NT device already exists.";
PLOG_WARNING << "This is typically an indication of another service already running that relies on the same driver.";
PLOG_WARNING << "The system might need to be restarted before Special K's service can be started properly.";
ret = true;
}
else
PLOG_ERROR << "Could not start the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCWinRing0);
}
else
PLOG_ERROR << "Could not open the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCManager);
} else
PLOG_ERROR << "Could not open the service manager: " << SKIF_Util_GetLastError ( );
return ret;
}
// Stop the driver service
bool SvcStop (std::wstring svcName)
{
bool ret = false;
SC_HANDLE schSCManager = NULL,
schSCWinRing0 = NULL;
schSCManager =
OpenSCManager (
nullptr, // localhost
nullptr, // ServicesActive database
GENERIC_READ // Desired Access
);
if (nullptr != schSCManager)
{
schSCWinRing0 =
OpenService (
schSCManager,
svcName.c_str(), // Service Name
SERVICE_STOP // Desired Access
);
if (nullptr != schSCWinRing0)
{
SERVICE_STATUS status;
if (ControlService (schSCWinRing0, SERVICE_CONTROL_STOP, &status))
{
PLOG_INFO << "Driver service " << svcName << " was signaled to stop.";
ret = true;
}
else if (ERROR_SERVICE_NOT_ACTIVE == GetLastError ( ))
{
PLOG_INFO << "Driver service " << svcName << " is already stopped.";
ret = true;
}
else
PLOG_ERROR << "Could not stop the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCWinRing0);
}
else if (ERROR_SERVICE_DOES_NOT_EXIST == GetLastError ( ))
{
PLOG_INFO << "Driver service " << svcName << " have already been deleted.";
// It is technically not "correct" to return true here when the
// service does not exist, but in our case we do so since the
// whole point of stopping the service is to uninstall it.
ret = true;
}
else
PLOG_ERROR << "Could not open the " << svcName << " driver service: " << SKIF_Util_GetLastError ( );
CloseServiceHandle (schSCManager);
} else
PLOG_ERROR << "Could not open the service manager: " << SKIF_Util_GetLastError ( );
return ret;
}
// Open the device to ensure the driver was installed properly
bool DevOpen (std::wstring devName)
{
bool ret = false;
HANDLE h = CreateFile (
devName.c_str(), // DeviceName / FileName
GENERIC_READ | GENERIC_WRITE, // Desired Access
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (INVALID_HANDLE_VALUE == h)
{
PLOG_WARNING << "Could not open the " << devName << " NT device: " << SKIF_Util_GetLastError();
}
else
{
PLOG_INFO << "NT device " << devName << " was succesfully opened.";
ret = true;
CloseHandle (h);
}
return ret;
}