forked from jan-hoelscher-mss/safmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntservice.cpp
executable file
·254 lines (222 loc) · 9.63 KB
/
ntservice.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
/*
Copyright 2004-2008 Matthew J. Battey
Licensed 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.
This software implements a platform independent Store and Forward Message Queue.
*/
#include <windows.h>
#include <iostream>
#include <fstream>
#include "main.h"
#include "winnt/regutil.h"
using namespace std;
const char* serviceName = "safmq";
const char* serviceDesc = "SAFMQ Message Queue Server";
const char* description = "Allows applications to post a message, to a message queue, which is stored until the message is read by the receiving application.";
SERVICE_STATUS_HANDLE thisServiceStatus = NULL;
SERVICE_STATUS status;
void createService(const char* path)
{
SC_HANDLE manager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
if (manager) {
SC_HANDLE schService = CreateService(
manager,
serviceName,
serviceDesc,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
path, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService) {
try {
CRegKey key(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\safmq");
key.setString("Description",description);
} catch (CRegKeyException ex) {
cerr << "While setting description: " << ex.what() << endl;
}
cout << serviceName << " installed successfully." << endl;
CloseServiceHandle(schService);
} else {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED: // The handle to the specified service control manager database does not have SC_MANAGER_CREATE_SERVICE access.
cerr << "Error creating service: access denied." << endl; break;
case ERROR_DUP_NAME: //The display name already exists in the service control manager database either as a service name or as another display name.
cerr << "Error creating service: duplicate service name." << endl; break;
case ERROR_INVALID_SERVICE_ACCOUNT: //The user account name specified in the lpServiceStartName parameter does not exist.
cerr << "Error creating service: invalid service account." << endl; break;
case ERROR_SERVICE_EXISTS: //The specified service already exists in this database.
cerr << "Error creating service: service already exists in database." << endl; break;
}
}
CloseServiceHandle(manager);
} else {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED:
cerr << "Error opening service control manager: Cannot create service, access denied." << endl; break;
case ERROR_DATABASE_DOES_NOT_EXIST:
cerr << "Error opening service control manager: Unable to access service database, database does not exist." << endl; break;
case ERROR_INVALID_PARAMETER:
cerr << "Error opening service control manager invalid parameter." << endl; break;
}
}
}
void removeService()
{
SC_HANDLE manager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
if (manager) {
SC_HANDLE schService = OpenService(manager,serviceName,SERVICE_ALL_ACCESS);
if (schService) {
if (!DeleteService(schService)) {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED:
cerr << "Error removing service: access denied." << endl; break;
case ERROR_SERVICE_MARKED_FOR_DELETE:
cerr << "Service already marked for deletion." << endl; break;
default:
cerr << "Error removing service." << endl; break;
}
} else {
cout << serviceName << " removed successfully.";
}
} else {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED: // The handle to the specified service control manager database does not have SC_MANAGER_CREATE_SERVICE access.
cerr << "Error removing service: access denied." << endl; break;
default:
cerr << "Error removing service while opening service." << endl; break;
}
}
CloseServiceHandle(manager);
} else {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED:
cerr << "Error opening service control manager: Cannot create service, access denied." << endl; break;
case ERROR_DATABASE_DOES_NOT_EXIST:
cerr << "Error opening service control manager: Unable to access service database, database does not exist." << endl; break;
case ERROR_INVALID_PARAMETER:
cerr << "Error opening service control manager invalid parameter." << endl; break;
}
}
}
void startService()
{
SC_HANDLE manager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (manager) {
SC_HANDLE service = OpenService(manager,serviceName,SERVICE_START);
if (service) {
const char* param[] = {"safmq.exe",""};
cout << "attempting to start service" << endl;
if (StartService(service,1,param))
cout << serviceName << " started.";
else {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED:
cerr << "The specified handle was not opened with SERVICE_START access." << endl; break;
case ERROR_INVALID_HANDLE:
cerr << "The specified handle is invalid." << endl; break;
case ERROR_PATH_NOT_FOUND:
cerr << "The service binary file could not be found." << endl; break;
case ERROR_SERVICE_ALREADY_RUNNING:
cerr << "An instance of the service is already running." << endl; break;
case ERROR_SERVICE_DATABASE_LOCKED:
cerr << "The database is locked." << endl; break;
case ERROR_SERVICE_DEPENDENCY_DELETED:
cerr << "The service depends on a service that does not exist or has been marked for deletion." << endl; break;
case ERROR_SERVICE_DEPENDENCY_FAIL:
cerr << "The service depends on another service that has failed to start." << endl; break;
case ERROR_SERVICE_DISABLED:
cerr << "The service has been disabled." << endl; break;
case ERROR_SERVICE_LOGON_FAILED:
cerr << "The service could not be logged on. This error occurs if the service was started from an account that does not have the \"Log on as a service\" right." << endl; break;
case ERROR_SERVICE_MARKED_FOR_DELETE:
cerr << "The service has been marked for deletion." << endl; break;
case ERROR_SERVICE_NO_THREAD:
cerr << "A thread could not be created for the service." << endl; break;
case ERROR_SERVICE_REQUEST_TIMEOUT:
cerr << "The process for the service was started, but it did not call StartServiceCtrlDispatcher, or the thread that called StartServiceCtrlDispatcher may be blocked in a control handler function." << endl; break;
default:
cerr << "An error occured attempting to start the service." << endl; break;
}
}
CloseServiceHandle(service);
} else {
switch (GetLastError()) {
case ERROR_ACCESS_DENIED:
cerr << "Error: Access denied accessing service to start." << endl; break;
case ERROR_SERVICE_DOES_NOT_EXIST:
cerr << "Error: Service does not exist, please install first." << endl; break;
default:
cerr << "Error occurred accessing service." << endl;
}
}
CloseServiceHandle(manager);
} else {
switch (GetLastError()) {
case ERROR_ACCESS_DENIED:
cerr << "Error: Access denied accessing service manager." << endl; break;
default:
cerr << "Error occurred accessing service." << endl;
}
}
}
void WINAPI ServiceControlHandler(DWORD fdwControl)
{
switch (fdwControl) {
default:
case SERVICE_CONTROL_CONTINUE: // Notifies a paused service that it should resume.
case SERVICE_CONTROL_INTERROGATE: // Notifies a service that it should report its current status information to the service control manager.
case SERVICE_CONTROL_PAUSE: // Notifies a service that it should pause.
break;
case SERVICE_CONTROL_SHUTDOWN: // Notifies a service that the system is shutting down so the service can perform cleanup tasks.
case SERVICE_CONTROL_STOP:// Notifies a service that it should stop.
stopSafmq();
status.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(thisServiceStatus, &status);
shutdownSafmq();
break;
}
}
void WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
memset(&status,0,sizeof(status));
thisServiceStatus = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
SetServiceStatus(thisServiceStatus, &status);
if (startSafmq() >= 0) {
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
status.dwCurrentState = SERVICE_RUNNING;
status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
SetServiceStatus(thisServiceStatus, &status);
shutdownSafmq();
}
status.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(thisServiceStatus, &status);
}
bool runAsService()
{
SERVICE_TABLE_ENTRY DispatchTable[] = {
{(char*)serviceName, ServiceMain},
{NULL, NULL}
};
return StartServiceCtrlDispatcher(DispatchTable)!=0;
}