-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
424 lines (364 loc) · 10.1 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
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#define LOG_FILE "C:\\Users\\dimas\\Desktop\\log.txt"
#define CONF_FILE "C:\\Users\\dimas\\Desktop\\config.txt"
LPCSTR serviceName = "MyService";
LPCSTR servicePath = "C:\\Users\\dimas\\Desktop\\service.exe";
SERVICE_STATUS serviceStatus;
SERVICE_STATUS_HANDLE serviceStatusHandle;
std::string cmd_7z;
/*
MyService functions
*/
int addLogMessage(const char* str);
void ControlHandler(DWORD request);
int InstallService();
int StartMyService();
int InitService();
int StopService();
int RemoveService();
void ServiceMain(int argc, char** argv)
{
int i = 0;
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
serviceStatusHandle = RegisterServiceCtrlHandlerA(serviceName, (LPHANDLER_FUNCTION)ControlHandler);
if (serviceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}
int error = InitService();
if (error)
{
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwWin32ExitCode = -1;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
}
serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
while (serviceStatus.dwCurrentState == SERVICE_RUNNING)
{
system(cmd_7z.c_str());
addLogMessage("Backup is complete.");
Sleep(5000);
}
return;
}
int main(int argc, char* argv[]) {
int ret;
if (argc - 1 == 0) {
SERVICE_TABLE_ENTRY ServiceTable[1];
ServiceTable[0].lpServiceName = (LPSTR)serviceName;
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
if (!StartServiceCtrlDispatcher(ServiceTable))
{
addLogMessage("StartServiceCtrlDispatcher err");
}
}
else if (strcmp(argv[argc - 1], "install") == 0)
{
printf("Installing MyService...\n");
ret = InstallService();
printf("ret = %d", ret);
}
else if (strcmp(argv[argc - 1], "remove") == 0)
{
printf("Removing MyService...\n");
ret = RemoveService();
printf("ret = %d", ret);
}
else if (strcmp(argv[argc - 1], "start") == 0)
{
printf("Starting MyService...\n");
ret = StartMyService();
printf("ret = %d", ret);
}
else if (strcmp(argv[argc - 1], "stop") == 0)
{
printf("Stoping MyService...\n");
ret = StopService();
printf("ret = %d", ret);
}
}
int InitService()
{
std::string str;
std::string backup_dir;
std::string arch_name;
char buf[1000] = { 0 };
FILE* confFile = NULL;
addLogMessage("Start init MyService.");
if (fopen_s(&confFile, CONF_FILE, "r") != 0)
{
addLogMessage("ERR: cannot open config file");
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwWin32ExitCode = -1;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return -1;
}
fgets(buf, sizeof(buf), confFile);
buf[strlen(buf) - 1] = 0;
backup_dir = buf;
fgets(buf, sizeof(buf), confFile);
buf[strlen(buf) - 1] = 0;
arch_name = buf;
str = "Backup dir is " + backup_dir;
addLogMessage(str.c_str());
str = "Baclup arch name is " + arch_name;
addLogMessage(str.c_str());
cmd_7z = std::string(" \"C:\\Program Files\\7-Zip\\7z.exe\" u ") + arch_name + std::string(" ");
while (fgets(buf, sizeof(buf), confFile) != NULL) {
buf[strlen(buf) - 1] = 0;
cmd_7z += backup_dir + buf + std::string(" ");
}
cmd_7z += std::string("-tzip -ssw -r");
str = "7z command:" + cmd_7z;
addLogMessage(str.c_str());
return 0;
}
int addLogMessage(const char* str)
{
errno_t err;
FILE* log;
if ((err = fopen_s(&log, LOG_FILE, "a+")) != 0)
{
return -1;
}
fprintf(log, "LOG_MSG: %s\n", str);
fclose(log);
return 0;
}
void ControlHandler(DWORD request)
{
switch (request)
{
case SERVICE_CONTROL_STOP:
addLogMessage("MyService is stopped.");
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
addLogMessage("Shutdowning MyService...");
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
default:
break;
}
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
}
int InstallService()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (!hSCManager)
{
addLogMessage("Can't open Service Control Manager");
return -1;
}
SC_HANDLE hService = CreateServiceA(hSCManager,
serviceName,
serviceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
servicePath,
NULL,
NULL,
NULL,
NULL,
NULL);
if (!hService)
{
int err = GetLastError();
switch (err)
{
case ERROR_ACCESS_DENIED:
addLogMessage("ERROR: ERROR_ACCESS_DENIED");
break;
case ERROR_CIRCULAR_DEPENDENCY:
addLogMessage("ERROR: ERROR_CIRCULAR_DEPENDENCY");
break;
case ERROR_DUPLICATE_SERVICE_NAME:
addLogMessage("ERROR: ERROR_DUPLICATE_SERVICE_NAME");
break;
case ERROR_INVALID_HANDLE:
addLogMessage("ERROR: ERROR_INVALID_HANDLE");
break;
case ERROR_INVALID_NAME:
addLogMessage("ERROR: ERROR_INVALID_NAME");
break;
case ERROR_INVALID_PARAMETER:
addLogMessage("ERROR: ERROR_INVALID_PARAMETER");
break;
case ERROR_INVALID_SERVICE_ACCOUNT:
addLogMessage("ERROR: ERROR_INVALID_SERVICE_ACCOUNT");
break;
case ERROR_SERVICE_EXISTS:
addLogMessage("ERROR: ERROR_SERVICE_EXISTS");
break;
default:
addLogMessage("ERROR: Undefined");
}
CloseServiceHandle(hSCManager);
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
addLogMessage("MyService is installed.");
return 0;
}
int RemoveService()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!hSCManager)
{
addLogMessage("Can't open Service Control Manager");
return -1;
}
SC_HANDLE hService = OpenServiceA(hSCManager, serviceName, SERVICE_STOP | DELETE);
if (!hService)
{
addLogMessage("Can't remove service");
CloseServiceHandle(hSCManager);
return -1;
}
DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
addLogMessage("MyService is removed.");
return 0;
}
int StartMyService()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
{
printf("OpenSCManager error");
}
SC_HANDLE hService = OpenServiceA(hSCManager, serviceName, SERVICE_START);
if (hService == NULL)
{
printf("OpenServiceA error");
}
if (!StartService(hService, 0, NULL))
{
int err = GetLastError();
switch (err)
{
case ERROR_ACCESS_DENIED:
addLogMessage("StartService case ERROR_ACCESS_DENIED");
break;
case ERROR_INVALID_HANDLE:
addLogMessage("StartService case ERROR_INVALID_HANDLE");
break;
case ERROR_PATH_NOT_FOUND:
addLogMessage("StartService case ERROR_PATH_NOT_FOUND");
break;
case ERROR_SERVICE_ALREADY_RUNNING:
addLogMessage("StartService case ERROR_SERVICE_ALREADY_RUNNING");
break;
case ERROR_SERVICE_DATABASE_LOCKED:
addLogMessage("StartService case ERROR_SERVICE_DATABASE_LOCKED");
break;
case ERROR_SERVICE_DEPENDENCY_DELETED:
addLogMessage("StartService case ERROR_SERVICE_DEPENDENCY_DELETED");
break;
case ERROR_SERVICE_DEPENDENCY_FAIL:
addLogMessage("StartService case ERROR_SERVICE_DEPENDENCY_FAIL");
break;
case ERROR_SERVICE_DISABLED:
addLogMessage("StartService case ERROR_SERVICE_DISABLED");
break;
case ERROR_SERVICE_LOGON_FAILED:
addLogMessage("StartService case returned ERROR_SERVICE_LOGON_FAILED");
break;
case ERROR_SERVICE_MARKED_FOR_DELETE:
addLogMessage("StartService case ERROR_SERVICE_MARKED_FOR_DELETE");
break;
case ERROR_SERVICE_NO_THREAD:
addLogMessage("StartService case ERROR_SERVICE_NO_THREAD");
break;
case ERROR_SERVICE_REQUEST_TIMEOUT:
addLogMessage("StartService case ERROR_SERVICE_REQUEST_TIMEOUT");
break;
default:
addLogMessage("StartService case default: error!");
break;
}
CloseServiceHandle(hSCManager);
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return 0;
}
int StopService()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
SC_HANDLE hService = OpenServiceA(hSCManager, serviceName, SERVICE_STOP);
if (!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus))
{
int err = GetLastError();
switch (err)
{
case ERROR_ACCESS_DENIED:
addLogMessage("ControlService case ERROR_ACCESS_DENIED");
break;
case ERROR_INVALID_HANDLE:
addLogMessage("ControlService case ERROR_INVALID_HANDLE");
break;
case ERROR_PATH_NOT_FOUND:
addLogMessage("ControlService case ERROR_PATH_NOT_FOUND");
break;
case ERROR_SERVICE_ALREADY_RUNNING:
addLogMessage("ControlService case ERROR_SERVICE_ALREADY_RUNNING");
break;
case ERROR_SERVICE_DATABASE_LOCKED:
addLogMessage("ControlService case ERROR_SERVICE_DATABASE_LOCKED");
break;
case ERROR_SERVICE_DEPENDENCY_DELETED:
addLogMessage("ControlService case ERROR_SERVICE_DEPENDENCY_DELETED");
break;
case ERROR_SERVICE_DEPENDENCY_FAIL:
addLogMessage("ControlService case ERROR_SERVICE_DEPENDENCY_FAIL");
break;
case ERROR_SERVICE_DISABLED:
addLogMessage("ControlService case ERROR_SERVICE_DISABLED");
break;
case ERROR_SERVICE_LOGON_FAILED:
addLogMessage("ControlService case ERROR_SERVICE_LOGON_FAILED");
break;
case ERROR_SERVICE_MARKED_FOR_DELETE:
addLogMessage("ControlService case ERROR_SERVICE_MARKED_FOR_DELETE");
break;
case ERROR_SERVICE_NO_THREAD:
addLogMessage("ControlService case ERROR_SERVICE_NO_THREAD");
break;
case ERROR_SERVICE_REQUEST_TIMEOUT:
addLogMessage("ControlService case ERROR_SERVICE_REQUEST_TIMEOUT");
break;
default:
addLogMessage("ControlService case: error!");
break;
}
CloseServiceHandle(hSCManager);
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return 0;
}