forked from somma/_MyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscm_context.cpp
More file actions
413 lines (375 loc) · 9.73 KB
/
scm_context.cpp
File metadata and controls
413 lines (375 loc) · 9.73 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
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
/******************************************************************************
* scm_context.cpp
******************************************************************************
*
******************************************************************************
* All rights reserved by somma (fixbrain@gmail.com)
******************************************************************************
* 2011/11/30 created
******************************************************************************/
#include "stdafx.h"
#include "scm_context.h"
#include <memory> // std::shared_ptr
//> todo - SCM 접근시 권한은 필요한 만큼만 정해서 호출하게 하자.
struct sc_handle_deleter
{
void operator()(SC_HANDLE* phandle) const
{
CloseServiceHandle(*phandle);
delete phandle;
}
};
typedef std::shared_ptr<SC_HANDLE> sc_handle_ptr;
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
scm_context::scm_context(
_In_z_ const wchar_t* driver_path,
_In_z_ const wchar_t* service_name,
_In_z_ const wchar_t* service_display_name,
_In_ bool uninstall_service_on_free_context
)
: _uninstall_service_on_free(uninstall_service_on_free_context),
_driver_handle(INVALID_HANDLE_VALUE),
_driver_path(driver_path),
_service_name(service_name),
_service_display_name(service_display_name),
_installed(false),
_running(false)
{
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
scm_context::~scm_context()
{
if(INVALID_HANDLE_VALUE != _driver_handle)
{
CloseHandle(_driver_handle); _driver_handle = INVALID_HANDLE_VALUE;
}
stop_driver();
if (true == _uninstall_service_on_free)
{
uninstall_driver();
}
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool scm_context::install_driver()
{
SC_HANDLE scm_handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == scm_handle)
{
log_err "OpenSCManagerW() faield. gle = %u", GetLastError() log_end
return false;
}
sc_handle_ptr scm_handle_ptr(new SC_HANDLE(scm_handle), sc_handle_deleter());
// already exists?
SC_HANDLE service_handle = OpenServiceW(scm_handle, _service_name.c_str(), SERVICE_QUERY_CONFIG);
if (NULL != service_handle)
{
log_dbg "service=%ws. already exists", _service_name.c_str() log_end
CloseServiceHandle(service_handle);
}
else
{
// Install the driver
service_handle = CreateServiceW(
scm_handle,
_service_name.c_str(),
_service_display_name.c_str(),
GENERIC_READ, // SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
_driver_path.c_str(),
NULL,
NULL,
NULL,
NULL,
NULL
);
if (service_handle == NULL)
{
log_err
"CreateServcieW(path=%ws, svc_name=%ws, svc_display=%ws) failed. gle = %u",
_driver_path.c_str(), _service_name.c_str(), _service_display_name.c_str(), GetLastError()
log_end
return false;
}
CloseServiceHandle(service_handle);
log_info "service=%ws installed successfully", _service_name.c_str() log_end
}
_installed = true;
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool scm_context::uninstall_driver()
{
if (true != _installed) return true;
if (true == _running)
{
if(true != stop_driver())
{
log_err "scm_context::stop_driver() failed, can not uninstall driver..." log_end
//> stop_driver() 가 실패해도 삭제시도를 해야 한다.
// - driver :: DriverEntry() 에서 STATUS_SUCCESS 를 리턴했으나 아무짓도 안하고, 리턴한 경우
// - driver handle 을 누군가 물고 있는 경우
// 강제로 서비스를 삭제 (registry 에서 서비스 제거)하고, 리부팅하면 서비스가
// 제거된 상태로 (정상) 돌아올 수 있다.
//
//return false;
_running = false;
}
}
SC_HANDLE scm_handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == scm_handle)
{
log_err "OpenSCManagerW() faield. gle = %u", GetLastError() log_end
return false;
}
sc_handle_ptr scm_handle_ptr(new SC_HANDLE(scm_handle), sc_handle_deleter());
SC_HANDLE service_handle = OpenServiceW(
scm_handle,
_service_name.c_str(),
SERVICE_ALL_ACCESS
);
if (NULL == service_handle)
{
log_err
"OpenServiceW( service_name=%ws ) failed. gle = %u",
_service_name.c_str(), GetLastError()
log_end
return false;
}
sc_handle_ptr service_handle_ptr(new SC_HANDLE(service_handle), sc_handle_deleter());
if (FALSE == DeleteService(service_handle))
{
DWORD err = GetLastError();
if (ERROR_SERVICE_MARKED_FOR_DELETE != err)
{
log_err
"DeleteService( service name=%ws ) failed, gle = %u",
_service_name.c_str(), err
log_end
return false;
}
}
_installed = false;
log_info "service=%ws deleted successfully", _service_name.c_str() log_end
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool scm_context::start_driver()
{
_ASSERTE(true == _installed);
if (true != _installed) return false;
if (true == _running) return true;
SC_HANDLE scm_handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == scm_handle)
{
log_err "OpenSCManagerW() faield. gle = %u", GetLastError() log_end
return false;
}
sc_handle_ptr scm_handle_ptr(new SC_HANDLE(scm_handle), sc_handle_deleter());
SC_HANDLE service_handle = OpenServiceW(
scm_handle,
_service_name.c_str(),
SERVICE_ALL_ACCESS
);
if (NULL == service_handle)
{
log_err
"OpenServiceW( service_name=%ws ) failed. gle = %u",
_service_name.c_str(), GetLastError()
log_end
return false;
}
sc_handle_ptr service_handle_ptr(new SC_HANDLE(service_handle), sc_handle_deleter());
if (TRUE != StartService(service_handle, 0, NULL))
{
DWORD err = GetLastError();
if (err != ERROR_SERVICE_ALREADY_RUNNING)
{
log_err
"StartService( service name=%ws ) failed, gle = %u",
_service_name.c_str(), err
log_end
return false;
}
}
_running = true;
log_info "service=%ws started successfully", _service_name.c_str() log_end
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool scm_context::stop_driver()
{
if (true != _installed) return false;
if (true != _running) return true;
SC_HANDLE scm_handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == scm_handle)
{
log_err "OpenSCManagerW() faield. gle = %u", GetLastError() log_end
return false;
}
sc_handle_ptr scm_handle_ptr(new SC_HANDLE(scm_handle), sc_handle_deleter());
SC_HANDLE service_handle = OpenServiceW(
scm_handle,
_service_name.c_str(),
SERVICE_ALL_ACCESS
);
if (NULL == service_handle)
{
log_err
"OpenServiceW( service_name=%ws ) failed. gle = %u",
_service_name.c_str(), GetLastError()
log_end
return false;
}
sc_handle_ptr service_handle_ptr(new SC_HANDLE(service_handle), sc_handle_deleter());
// 2007.05.17 by somma
// 다른 프로세스가 SCM 을 통해서 SERVICE_CONTROL_STOP 을 이미요청한 경우
// 여기서 호출한 ControlService() 함수는 FALSE 를 리턴한다.
// 그러나 서비스는 정상 종료된다.
//
SERVICE_STATUS service_status={0};
if (FALSE == ControlService(service_handle, SERVICE_CONTROL_STOP, &service_status))
{
log_err
"ControlService( service name=%ws ) failed, gle = %u",
_service_name.c_str(), GetLastError()
log_end
return false;
}
_running = false;
log_info "service=%ws stopped successfully", _service_name.c_str() log_end
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool
scm_context::send_command(
_In_ uint32_t io_code,
_In_ uint32_t input_buffer_size,
_In_bytecount_(input_buffer_size) void* input_buffer,
_In_ uint32_t output_buffer_size,
_In_bytecount_(output_buffer_size) void* output_buffer,
_Out_ uint32_t* bytes_returned
)
{
_ASSERTE(true == _installed);
_ASSERTE(true == _running);
if (true != _installed || true != _running) return false;
if (INVALID_HANDLE_VALUE == _driver_handle)
{
_driver_handle = open_driver();
if (INVALID_HANDLE_VALUE == _driver_handle)
{
log_err "scm_context::open_driver() failed" log_end
return false;
}
}
BOOL ret = DeviceIoControl(
_driver_handle,
io_code,
input_buffer,
input_buffer_size,
output_buffer,
output_buffer_size,
reinterpret_cast<LPDWORD>(bytes_returned),
NULL
);
if(TRUE != ret)
{
log_err "DeviceIoControl( io_code=0x%08x ) failed", io_code log_end
return false;
}
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
HANDLE scm_context::open_driver()
{
_ASSERTE(true == _installed);
_ASSERTE(true == _running);
_ASSERTE(INVALID_HANDLE_VALUE == _driver_handle);
if (true != _installed || true != _running) return INVALID_HANDLE_VALUE;
if (INVALID_HANDLE_VALUE != _driver_handle) return _driver_handle;
std::wstring driver_object_name= L"\\\\.\\" + _service_name;
HANDLE driver_handle = CreateFileW(
driver_object_name.c_str(),
GENERIC_READ | GENERIC_WRITE,
0, // exclusive open
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if (INVALID_HANDLE_VALUE == driver_handle)
{
log_err
"CreateFileW(driver name=%ws) failed, gle = %u",
driver_object_name.c_str(), GetLastError()
log_end
return INVALID_HANDLE_VALUE;
}
return driver_handle;
}