-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsService.cpp
More file actions
628 lines (534 loc) · 16.5 KB
/
Copy pathWindowsService.cpp
File metadata and controls
628 lines (534 loc) · 16.5 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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
power_switch: control Argus PDU SW-0816
Copyright (C) 2025 Mario Klebsch, DG1AM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "WindowsService.h"
#include "case_insensitive.h"
#include <cassert>
#include <chrono>
#include <iostream>
#include <system_error>
#include <thread>
#include <Windows.h>
using namespace std::string_literals;
namespace
{
enum service_state
{
stopped = SERVICE_STOPPED,
start_pending = SERVICE_START_PENDING,
stop_pending = SERVICE_STOP_PENDING,
running = SERVICE_RUNNING,
continue_pending = SERVICE_CONTINUE_PENDING,
pause_pending = SERVICE_PAUSE_PENDING,
paused = SERVICE_PAUSED,
};
static const char* to_string(service_state status)
{
switch (status)
{
case stopped: return "stopped";
case start_pending: return "start pending";
case stop_pending: return "stop pending";
case running: return "running";
case continue_pending: return "continue pending";
case pause_pending: return "pause pending";
case paused: return "paused";
default: return "???";
}
}
inline std::ostream& operator<<(std::ostream& s, service_state status) { return s << to_string(status); }
static const char* start_type_to_string(DWORD start_type)
{
switch (start_type)
{
case SERVICE_BOOT_START: return "boot start";
case SERVICE_SYSTEM_START: return "system start";
case SERVICE_AUTO_START: return "auto start";
case SERVICE_DEMAND_START: return "start on demand";
case SERVICE_DISABLED: return "disabled";
default: return "???";
}
}
struct service_closer
{
void operator()(SC_HANDLE h) { CloseServiceHandle(h); }
};
using service_h = std::unique_ptr<std::remove_reference<decltype(*std::declval<SC_HANDLE>())>::type, service_closer >;
class manager
{
service_h handle;
public:
class service
{
friend class manager;
service_h handle;
explicit service(SC_HANDLE handle) :handle{ std::move(handle) } {}
public:
service() = default;
service(service&&) = default;
service& operator=(service&&) = default;
explicit operator bool() const { return handle.get(); }
operator SC_HANDLE() const { return handle.get(); }
service_state get_status(std::error_code& ec) const
{
SERVICE_STATUS_PROCESS ret;
DWORD dwBytesNeeded;
if (!QueryServiceStatusEx(
*this, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE)&ret, // address of structure
sizeof(ret), // size of structure
&dwBytesNeeded)) // size needed if buffer is too small
{
ec = { int(GetLastError()), std::system_category() };
return {};
}
ec = {};
return service_state(ret.dwCurrentState);
}
QUERY_SERVICE_CONFIGA get_config(std::vector<char>& buffer, std::error_code& ec) const
{
DWORD bytes_needed;
if (!QueryServiceConfigA(*this, nullptr, 0, &bytes_needed) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
ec = { int(GetLastError()), std::system_category() };
return {};
}
buffer.resize(bytes_needed);
if (!QueryServiceConfigA(*this, (LPQUERY_SERVICE_CONFIGA)buffer.data(), (DWORD)buffer.size(), &bytes_needed))
{
ec = { int(GetLastError()), std::system_category() };
return {};
}
ec = {};
return reinterpret_cast<QUERY_SERVICE_CONFIGA&>(buffer[0]);
}
};
manager(DWORD access, std::error_code& ec) :
handle{ OpenSCManager(NULL, NULL, access) }
{
if (!handle)
ec = { int(GetLastError()), std::system_category() };
else
ec = {};
}
manager(std::error_code& ec) : manager(SC_MANAGER_ALL_ACCESS, ec)
{
}
explicit operator bool() const { return handle.get(); }
operator SC_HANDLE() const { return handle.get(); }
service open(const std::string& name, DWORD access, std::error_code& ec)
{
service s{ OpenServiceA(*this, name.c_str(), access) };
if (!s)
{
ec = { int(GetLastError()), std::system_category() };
return {};
}
ec = {};
return s;
}
};
}
#define SERVICE_MAIN_COMMAND "service_main"
namespace windows
{
void* instance{ nullptr };
class service::impl
{
service& inst;
public:
impl(service& inst) : inst{ inst } {}
inline std::string name() const { return inst.name(); }
inline std::string display_name() const { return inst.display_name(); }
inline std::string path() const { return inst.path(); }
inline std::string arguments() const { return inst.arguments() + " " + SERVICE_MAIN_COMMAND; }
int install()
{
const auto cmd = "\""s + path() + "\" " + arguments();
std::error_code ec;
manager m{ ec };
if (ec)
{
std::cerr << "OpenSCManager() failed: " << ec.message() << "\n";
return -1;
}
SC_HANDLE h = CreateServiceA(
m, // SCM database
name().c_str(), // name of service
display_name().c_str(), // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
cmd.c_str(), // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (!h)
{
ec = { int(GetLastError()), std::system_category() };
std::cerr << "CreateService() failed: " << ec.message() << "\n";
return -1;
}
return 0;
}
int uninstall()
{
std::error_code ec;
manager m{ ec };
if (ec)
{
std::cerr << "OpenSCManager() failed: " << ec.message() << "\n";
return -1;
}
auto s = m.open(name(), DELETE | SERVICE_QUERY_STATUS, ec);
if (ec)
return -1;
auto state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
if (state != stopped)
{
auto ret = stop();
if (ret)
return ret;
}
if (!DeleteService(s))
{
std::error_code ec{ int(GetLastError()), std::system_category() };
std::cerr << "DeleteService(" << name() << ") failed: " << ec.message() << "\n";
return -1;
}
return 0;
}
int start()
{
std::error_code ec;
manager m{ ec };
if (ec)
{
std::cerr << "OpenSCManager() failed: " << ec.message() << "\n";
return -1;
}
auto s = m.open(name(), SERVICE_START | SERVICE_QUERY_STATUS, ec);
if (ec == std::error_code{ ERROR_SERVICE_DOES_NOT_EXIST, std::system_category() })
{
auto ret = install();
if (ret)
return ret;
s = std::move(m.open(name(), SERVICE_START | SERVICE_QUERY_STATUS, ec));
}
if (ec)
{
std::cerr << "OpenService(" << name() << ") failed: " << ec.message() << "\n";
return -1;
}
auto state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
if (state != stopped && state != stop_pending)
{
std::cerr << "Cannot start the service " << name() << " because it is already running\n";
return -1;
}
auto start = std::chrono::system_clock::now();
while (state == stop_pending)
{
if (std::chrono::system_clock::now() - start > std::chrono::seconds(10))
{
std::cerr << "timeout stopping service " << name() << "\n";
return -1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
}
if (!StartService(s, 0, NULL) )
{
std::error_code ec{ int(GetLastError()), std::system_category() };
std::cerr << "StartService(" << name() << ") failed: " << ec.message() << "\n";
return -1;
}
state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
start = std::chrono::system_clock::now();
while (state == start_pending)
{
if (std::chrono::system_clock::now() - start > std::chrono::seconds(10))
{
std::cerr << "timeout starting service " << name() << "\n";
return -1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
}
if (state != running)
{
std::cerr << "service " << name() << " is not running\n";
return -1;
}
return 0;
}
int stop()
{
std::error_code ec;
manager m{ ec };
if (ec)
{
std::cerr << "OpenSCManager() failed: " << ec.message() << "\n";
return -1;
}
auto s = m.open(name(), SERVICE_STOP | SERVICE_QUERY_STATUS, ec);
if (ec)
{
std::cerr << "OpenService(" << name() << ") failed: " << ec.message() << "\n";
return -1;
}
auto start = std::chrono::system_clock::now();
service_state state;
for (;;)
{
state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
if (state != stop_pending)
break;
if (std::chrono::system_clock::now() - start > std::chrono::seconds(10))
{
std::cerr << "timeout waiting for termination of " << name() << "service\n";
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (state == stopped)
{
std::cerr << "Service " << name() << " is already stopped.\n";
return 0;
}
SERVICE_STATUS req;
if (!ControlService(s, SERVICE_CONTROL_STOP, &req))
{
ec = { int(GetLastError()), std::system_category() };
std::cerr << "ControlService(" << name() << ", SERVICE_CONTROL_STOP) failed: " << ec.message() << "\n";
return -1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(15));
start = std::chrono::system_clock::now();
for (;;)
{
state = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
if (state != stop_pending)
break;
if (std::chrono::system_clock::now() - start > std::chrono::seconds(10))
{
std::cerr << "timeout waiting for termination of " << name() << "service\n";
return -1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (state != stopped)
{
std::cerr << "Service " << name() << " not stopped.\n";
return -1;
}
return 0;
}
int status()
{
std::error_code ec;
manager m{ SC_MANAGER_ENUMERATE_SERVICE | SERVICE_QUERY_CONFIG, ec };
if (ec)
{
std::cerr << "OpenSCManager() failed: " << ec.message() << "\n";
return -1;
}
auto s = m.open(name(), SC_MANAGER_ENUMERATE_SERVICE | SERVICE_QUERY_CONFIG, ec);
if (ec)
{
if (ec == std::error_code{ ERROR_SERVICE_DOES_NOT_EXIST, std::system_category() })
{
std::cout << "name: " << name() << "\n";
std::cout << "status: " << "not installed\n";
return 0;
}
std::cerr << "OpenService(" << name() << ") failed: " << ec.message() << "\n";
return -1;
}
auto status = s.get_status(ec);
if (ec)
{
std::cerr << "QueryServiceStatusEx() failed: " << ec.message() << "\n";
return -1;
}
std::vector<char> buffer;
auto config = s.get_config(buffer, ec);
if (ec)
{
std::cerr << "QueryServiceConfig() failed: " << ec.message() << "\n";
return -1;
}
std::cout << "name: " << name() << "\n";
std::cout << "display name: " << config.lpDisplayName << "\n";
std::cout << "status: " << status << "\n";
std::cout << "start type: " << start_type_to_string(config.dwStartType) << "\n";
std::cout << "path name: " << config.lpBinaryPathName << "\n";
std::cout << "user name: " << config.lpServiceStartName << "\n";
if (config.lpLoadOrderGroup && *config.lpLoadOrderGroup)
std::cout << "load order group: " << config.lpLoadOrderGroup << "\n";
if (config.lpDependencies && *config.lpDependencies)
std::cout << "dependencies: " << config.lpDependencies << "\n";
return 0;
}
SERVICE_STATUS_HANDLE SvcStatusHandle;
SERVICE_STATUS SvcStatus;
void setStatus(service_state status)
{
static DWORD dwCheckPoint = 1;
SvcStatus.dwCurrentState = DWORD(status);
SvcStatus.dwWin32ExitCode = 0;
SvcStatus.dwWaitHint = 3000;
SvcStatus.dwControlsAccepted = status == start_pending ? 0 : SERVICE_ACCEPT_STOP;
if ((status == running) ||
(status == stopped))
SvcStatus.dwCheckPoint = 0;
else
SvcStatus.dwCheckPoint = dwCheckPoint++;
SetServiceStatus(SvcStatusHandle, &SvcStatus);
}
static void WINAPI control(DWORD ctrl)
{
assert(instance);
auto This = static_cast<impl*>(instance);
switch (ctrl)
{
case SERVICE_CONTROL_STOP:
This->inst.stop();
break;
}
}
int main(int argc, char* argv[])
{
SvcStatusHandle = RegisterServiceCtrlHandlerA(name().c_str(), &impl::control);
if (!SvcStatusHandle)
{
std::error_code ec{ int(GetLastError()), std::system_category() };
std::cerr << "RegisterServiceCtrlHandler(" << name() << ") failed: " << ec.message() << "\n";
return -1;
}
SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
SvcStatus.dwServiceSpecificExitCode = 0;
setStatus(start_pending);
auto ret = inst.init();
if (ret)
return ret;
setStatus(running);
return inst.mainloop();
}
static VOID WINAPI callServiceMain(DWORD dwNumServicesArgs, LPSTR* lpServiceArgVectors)
{
assert(instance);
auto This = static_cast<impl*>(instance);
This->SvcStatus.dwServiceSpecificExitCode = This->main(dwNumServicesArgs, lpServiceArgVectors);
This->setStatus(stopped);
}
int handle_command(std::string_view cmd, std::string_view command_and_prefix)
{
if (iequals(cmd, "status"))
return status();
if (iequals(cmd, "install"))
return install();
if (iequals(cmd, "uninstall"))
return uninstall();
if (iequals(cmd, "start"))
return start();
if (iequals(cmd, "stop"))
return stop();
if (iequals(cmd, "service_main"))
{
assert(!instance);
instance = this;
SERVICE_TABLE_ENTRYA DispatchTable[] =
{
{ (char*)name().c_str(), (LPSERVICE_MAIN_FUNCTIONA)impl::callServiceMain},
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcherA(DispatchTable))
{
std::error_code ec{ int(GetLastError()), std::system_category() };
std::cerr << "StartServiceCtrlDispatcher() failed: " << ec.message() << "\n";
return -1;
}
return 0;
}
if (!cmd.empty())
std::cerr << "usage:\n";
std::cerr << " " << command_and_prefix << " install : install " << display_name() << " service\n";
std::cerr << " " << command_and_prefix << " uninstall : uninstall " << display_name() << " service\n";;
std::cerr << " " << command_and_prefix << " start : start " << display_name() << " service\n";;
std::cerr << " " << command_and_prefix << " stop : stop " << display_name() << " service\n";;
std::cerr << " " << command_and_prefix << " status : show " << display_name() << " service status\n";;
return -1;
}
};
service::service() :
pimpl{ std::make_unique<impl>(*this) }
{
}
std::string service::path() const
{
static char path[MAX_PATH] = "";
if (!path[0] && !GetModuleFileNameA(NULL, path, MAX_PATH))
{
std::error_code ec{ int(GetLastError()), std::system_category() };
std::cerr << "GetModuleFileName() failed: " << ec.message() << "\n";
return {};
}
return path;
}
service::~service() = default;
int service::handle_command(std::string_view cmd, std::string_view command_and_prefix)
{
return pimpl->handle_command(cmd, command_and_prefix);
}
}