-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathService.cpp
496 lines (426 loc) · 14.4 KB
/
Service.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the LICENSE file distributed with
* this work for additional information regarding copyright ownership.
*
* Also, if exists, check the Licenses directory for information about
* third-party modules.
*
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Service.h"
#include <AutoHandle.h>
#include <Strings\Strings.h>
#include <AutoPtr.h>
#include <AutoHandle.h>
#include <Debug.h>
#include <Threads.h>
#include "SingleInstance.h"
//-----------------------------------------------------------
static MX::CWindowsEvent cShutdownEv;
static SERVICE_STATUS_HANDLE hServiceStatus = NULL;
static SERVICE_STATUS sServiceStatus = {
SERVICE_WIN32_OWN_PROCESS, SERVICE_STOPPED, SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN, NO_ERROR, 0, 0, 0
};
static MX::CStringW cStrServiceNameW;
static MX::Service::OnStartCallback cStartCallback = MX::NullCallback();
static MX::Service::OnStopCallback cStopCallback = MX::NullCallback();
static MX::Service::OnDeviceChangeCallback cDeviceChangeCallback = MX::NullCallback();
static BOOL bRunningAsConsole = FALSE;
static int nArgumentsCount = 0;
static WCHAR** lpArguments = NULL;
static BOOL bDisableStop = FALSE;
static MX::CWorkerThread cConsoleDeviceChangeListenerThread;
//-----------------------------------------------------------
static BOOL WINAPI _ConsoleHandlerRoutine(_In_ DWORD dwCtrlType);
static VOID WINAPI _ServiceMain(_In_ DWORD dwArgc, _In_ LPWSTR *pszArgv);
static DWORD WINAPI _ServiceCtrlHandlerEx(_In_ DWORD dwControl, _In_ DWORD dwEventType, _In_ LPVOID lpEventData,
_In_ LPVOID lpContext);
static HRESULT _SetServiceStatus(_In_ DWORD dwCurrentState, _In_opt_ HRESULT hResExitCode=S_OK,
_In_opt_ DWORD dwWaitHint=0, _In_opt_ HRESULT hResServiceSpecificExitCode=S_OK);
static HRESULT _UpdateServiceStatus();
static HRESULT IsInteractiveRunningApp();
static HRESULT CreateConsoleDeviceChangeListener();
static VOID DestroyConsoleDeviceChangeListener();
static VOID ConsoleDeviceChangeListenerThreadProc(_In_ MX::CWorkerThread *lpWrkThread, _In_opt_ LPVOID lpParam);
static LRESULT WINAPI ConsoleDeviceChangeListenerWinProc(_In_ HWND hWnd, _In_ UINT message, _In_ WPARAM wParam,
_In_ LPARAM lParam);
//-----------------------------------------------------------
namespace MX {
namespace Service {
HRESULT Run(_In_opt_z_ LPCWSTR szServiceNameW, _In_ OnStartCallback _cStartCallback, _In_ OnStopCallback _cStopCallback,
_In_opt_ OnDeviceChangeCallback _cDeviceChangeCallback, _In_ int argc, _In_ WCHAR* argv[])
{
SERVICE_TABLE_ENTRYW aServiceTableW[2];
HRESULT hRes;
if ((!_cStartCallback) || (!_cStopCallback))
return E_POINTER;
if (szServiceNameW == NULL)
szServiceNameW = L"";
//check for single instance
if (*szServiceNameW != 0)
{
hRes = MX::SingleInstanceCheck(szServiceNameW);
if (FAILED(hRes))
return hRes;
if (hRes == S_FALSE)
return MX_E_AlreadyInitialized;
}
//check for interactive or service process
hRes = IsInteractiveRunningApp();
if (FAILED(hRes))
return hRes;
bRunningAsConsole = (hRes == S_OK) ? TRUE : FALSE;
//setup internal info
nArgumentsCount = argc;
lpArguments = argv;
if (cStrServiceNameW.Copy(szServiceNameW) == FALSE)
return E_OUTOFMEMORY;
//set callbacks
cStartCallback = _cStartCallback;
cStopCallback = _cStopCallback;
cDeviceChangeCallback = _cDeviceChangeCallback;
//create shutdown event
hRes = cShutdownEv.Create(TRUE, FALSE);
if (FAILED(hRes))
return hRes;
//run
if (bRunningAsConsole == FALSE)
{
::MxMemSet(&aServiceTableW, 0, sizeof(aServiceTableW));
aServiceTableW[0].lpServiceName = (LPWSTR)cStrServiceNameW;
aServiceTableW[0].lpServiceProc = &_ServiceMain;
hRes = (::StartServiceCtrlDispatcherW(aServiceTableW) != FALSE) ? S_OK : MX_HRESULT_FROM_LASTERROR();
}
else
{
BOOL bCallStop = FALSE;
//setup console handler
if (::SetConsoleCtrlHandler(_ConsoleHandlerRoutine, TRUE) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
//send start callback
hRes = cStartCallback(nArgumentsCount, lpArguments);
if (SUCCEEDED(hRes))
{
bCallStop = TRUE;
if (cDeviceChangeCallback)
{
hRes = CreateConsoleDeviceChangeListener();
}
}
//wait until termination event
if (SUCCEEDED(hRes))
{
cShutdownEv.Wait(INFINITE);
}
//shutdown console device change listener if running
DestroyConsoleDeviceChangeListener();
//send stop callback
if (bCallStop != FALSE)
{
if (SUCCEEDED(hRes))
{
hRes = cStopCallback();
}
else
{
cStopCallback();
}
}
//remove console handler
::SetConsoleCtrlHandler(_ConsoleHandlerRoutine, FALSE);
}
//done
return hRes;
}
VOID SignalShutdown()
{
cShutdownEv.Set();
return;
}
VOID SignalStarting()
{
if (bRunningAsConsole == FALSE)
_SetServiceStatus(SERVICE_START_PENDING, ERROR_SUCCESS, 5000);
return;
}
VOID SignalStopping()
{
if (bRunningAsConsole == FALSE)
_SetServiceStatus(SERVICE_STOP_PENDING, ERROR_SUCCESS, 5000);
return;
}
VOID EnableStop()
{
bDisableStop = FALSE;
if (bRunningAsConsole == FALSE && sServiceStatus.dwCurrentState != SERVICE_START_PENDING)
{
sServiceStatus.dwControlsAccepted |= SERVICE_ACCEPT_STOP;
_UpdateServiceStatus();
}
return;
}
VOID DisableStop()
{
bDisableStop = TRUE;
if (bRunningAsConsole == FALSE && sServiceStatus.dwCurrentState != SERVICE_START_PENDING)
{
sServiceStatus.dwControlsAccepted &= ~SERVICE_ACCEPT_STOP;
_UpdateServiceStatus();
}
return;
}
BOOL IsInteractive()
{
return bRunningAsConsole;
}
}; //namespace Service
}; //namespace MX
//-----------------------------------------------------------
static BOOL WINAPI _ConsoleHandlerRoutine(_In_ DWORD dwCtrlType)
{
switch (dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
cShutdownEv.Set();
return TRUE;
}
return FALSE;
}
static VOID WINAPI _ServiceMain(_In_ DWORD dwArgc, _In_ LPWSTR *pszArgv)
{
HRESULT hRes, hResServiceExitCode;
hServiceStatus = ::RegisterServiceCtrlHandlerExW((LPCWSTR)cStrServiceNameW, &_ServiceCtrlHandlerEx, NULL);
if (hServiceStatus == NULL)
return;
sServiceStatus.dwControlsAccepted = 0;
_SetServiceStatus(SERVICE_START_PENDING, ERROR_SUCCESS, 5000);
hRes = cShutdownEv.Create(TRUE, FALSE);
if (SUCCEEDED(hRes))
{
_SetServiceStatus(SERVICE_START_PENDING, ERROR_SUCCESS, 5000);
//send start callback
hRes = cStartCallback(nArgumentsCount, lpArguments);
}
if (SUCCEEDED(hRes))
{
HDEVNOTIFY hDevNotify = NULL;
if (cDeviceChangeCallback)
{
DEV_BROADCAST_DEVICEINTERFACE sNotificationFilter = { 0 };
sNotificationFilter.dbcc_size = (DWORD)sizeof(sNotificationFilter);
sNotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
hDevNotify = ::RegisterDeviceNotificationW(hServiceStatus, &sNotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE |
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
if (hDevNotify == NULL)
hRes = MX_HRESULT_FROM_LASTERROR();
}
if (SUCCEEDED(hRes))
{
//wait until termination event
sServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN;
if (bDisableStop == FALSE)
sServiceStatus.dwControlsAccepted |= SERVICE_ACCEPT_STOP;
_SetServiceStatus(SERVICE_RUNNING);
cShutdownEv.Wait(INFINITE);
_SetServiceStatus(SERVICE_STOP_PENDING, ERROR_SUCCESS, 5000);
if (hDevNotify != NULL)
{
::UnregisterDeviceNotification(hDevNotify);
hDevNotify = NULL;
}
}
//send stop callback
if (SUCCEEDED(hRes))
{
hResServiceExitCode = cStopCallback();
}
else
{
cStopCallback();
hResServiceExitCode = hRes;
}
}
else
{
hResServiceExitCode = hRes;
}
_SetServiceStatus(SERVICE_STOPPED, hRes, 0, hResServiceExitCode);
return;
}
static DWORD WINAPI _ServiceCtrlHandlerEx(_In_ DWORD dwControl, _In_ DWORD dwEventType, _In_ LPVOID lpEventData,
_In_ LPVOID lpContext)
{
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
cShutdownEv.Set();
return NO_ERROR;
case SERVICE_CONTROL_INTERROGATE:
::SetServiceStatus(hServiceStatus, &sServiceStatus);
return NO_ERROR;
case SERVICE_CONTROL_DEVICEEVENT:
if (dwEventType == DBT_DEVICEARRIVAL || dwEventType == DBT_DEVICEREMOVECOMPLETE)
{
cDeviceChangeCallback(dwEventType, (PDEV_BROADCAST_HDR)lpEventData);
}
return NO_ERROR;
}
return ERROR_CALL_NOT_IMPLEMENTED;
}
static HRESULT _SetServiceStatus(_In_ DWORD dwCurrentState, _In_opt_ HRESULT hResExitCode, _In_opt_ DWORD dwWaitHint,
_In_opt_ HRESULT hResServiceSpecificExitCode)
{
static DWORD dwCheckPoint = 1;
sServiceStatus.dwCurrentState = dwCurrentState;
sServiceStatus.dwWin32ExitCode = (DWORD)hResExitCode;
sServiceStatus.dwServiceSpecificExitCode = (DWORD)hResServiceSpecificExitCode;
sServiceStatus.dwWaitHint = dwWaitHint;
switch (dwCurrentState)
{
case SERVICE_START_PENDING:
case SERVICE_STOP_PENDING:
case SERVICE_CONTINUE_PENDING:
case SERVICE_PAUSE_PENDING:
sServiceStatus.dwCheckPoint = dwCheckPoint++;
break;
default:
dwCheckPoint = 1;
sServiceStatus.dwCheckPoint = 0;
break;
}
return _UpdateServiceStatus();
}
static HRESULT _UpdateServiceStatus()
{
HRESULT hRes;
hRes = (::SetServiceStatus(hServiceStatus, &sServiceStatus) != FALSE) ? S_OK : MX_HRESULT_FROM_LASTERROR();
return hRes;
}
static HRESULT IsInteractiveRunningApp()
{
MX::TAutoFreePtr<TOKEN_GROUPS> cTokenGroups;
MX::CWindowsHandle cProcToken;
DWORD dw, dwTokenGroupLength;
SID_IDENTIFIER_AUTHORITY sSia = SECURITY_NT_AUTHORITY;
PSID lpInteractiveSid, lpServiceSid;
HRESULT hRes;
if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &cProcToken) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
dwTokenGroupLength = 128;
cTokenGroups.Attach((PTOKEN_GROUPS)MX_MALLOC(dwTokenGroupLength));
if (!cTokenGroups)
return E_OUTOFMEMORY;
if (::GetTokenInformation(cProcToken, TokenGroups, cTokenGroups.Get(), dwTokenGroupLength,
&dwTokenGroupLength) == FALSE)
{
hRes = MX_HRESULT_FROM_LASTERROR();
if (hRes != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
return hRes;
cTokenGroups.Attach((PTOKEN_GROUPS)MX_MALLOC(dwTokenGroupLength));
if (!cTokenGroups)
return E_OUTOFMEMORY;
if (::GetTokenInformation(cProcToken, TokenGroups, cTokenGroups.Get(), dwTokenGroupLength,
&dwTokenGroupLength) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
}
if (::AllocateAndInitializeSid(&sSia, 1, SECURITY_INTERACTIVE_RID, 0, 0, 0, 0, 0, 0, 0, &lpInteractiveSid) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
if (::AllocateAndInitializeSid(&sSia, 1, SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, &lpServiceSid) == FALSE)
{
hRes = MX_HRESULT_FROM_LASTERROR();
::FreeSid(lpInteractiveSid);
return hRes;
}
hRes = S_FALSE; //assume a service process by default
for (dw=0; dw<cTokenGroups->GroupCount; dw++)
{
if (::EqualSid(cTokenGroups->Groups[dw].Sid, lpInteractiveSid) != FALSE)
{
hRes = S_OK; //interactive process
break;
}
if (::EqualSid(cTokenGroups->Groups[dw].Sid, lpServiceSid) != FALSE)
break;
}
//done
::FreeSid(lpInteractiveSid);
::FreeSid(lpServiceSid);
return hRes;
}
static HRESULT CreateConsoleDeviceChangeListener()
{
if (cConsoleDeviceChangeListenerThread.SetRoutine(&ConsoleDeviceChangeListenerThreadProc) == FALSE ||
cConsoleDeviceChangeListenerThread.Start() == FALSE)
{
return E_OUTOFMEMORY;
}
//done
return S_OK;
}
static VOID DestroyConsoleDeviceChangeListener()
{
DWORD dwTid;
dwTid = cConsoleDeviceChangeListenerThread.GetThreadId();
if (dwTid != 0)
::PostThreadMessageW(dwTid, WM_QUIT, 0, 0);
cConsoleDeviceChangeListenerThread.Stop();
return;
}
static VOID ConsoleDeviceChangeListenerThreadProc(_In_ MX::CWorkerThread *lpWrkThread, _In_opt_ LPVOID lpParam)
{
WNDCLASS sWndClassW = { 0 };
DEV_BROADCAST_DEVICEINTERFACE_W sNotifyFilterW = { 0 };
HWND hWnd;
HDEVNOTIFY hDevNotify;
MSG sMsg;
sWndClassW.lpfnWndProc = &ConsoleDeviceChangeListenerWinProc;
sWndClassW.hInstance = ::GetModuleHandleW(NULL);
sWndClassW.lpszClassName = L"TrapmineDC";
::RegisterClassW(&sWndClassW);
hWnd = ::CreateWindowExW(0, sWndClassW.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
if (hWnd != NULL)
{
sNotifyFilterW.dbcc_size = (DWORD)sizeof(sNotifyFilterW);
sNotifyFilterW.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
hDevNotify = ::RegisterDeviceNotificationW(hWnd, &sNotifyFilterW,
DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
if (hDevNotify != NULL)
{
while (cConsoleDeviceChangeListenerThread.CheckForAbort() == FALSE && ::GetMessageW(&sMsg, NULL, 0, 0) != 0)
{
::TranslateMessage(&sMsg);
::DispatchMessageW(&sMsg);
}
::UnregisterDeviceNotification(hDevNotify);
}
::DestroyWindow(hWnd);
}
//done
return;
}
static LRESULT WINAPI ConsoleDeviceChangeListenerWinProc(_In_ HWND hWnd, _In_ UINT message, _In_ WPARAM wParam,
_In_ LPARAM lParam)
{
if (message == WM_DEVICECHANGE && (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE))
{
if (cConsoleDeviceChangeListenerThread.CheckForAbort() == FALSE)
cDeviceChangeCallback((DWORD)wParam, (PDEV_BROADCAST_HDR)lParam);
return TRUE;
}
return ::DefWindowProcW(hWnd, message, wParam, lParam);
}