forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinservice.cpp
More file actions
248 lines (215 loc) · 7.12 KB
/
winservice.cpp
File metadata and controls
248 lines (215 loc) · 7.12 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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include "winservice.h"
#include <booster/nowide/convert.h>
#include <booster/system_error.h>
#include <cppcms/cppcms_error.h>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <winsvc.h>
#include <process.h>
#include <booster/function.h>
#include <booster/log.h>
namespace cppcms {
namespace impl {
winservice::winservice()
{
}
winservice::~winservice()
{
}
winservice &winservice::instance()
{
static winservice inst;
return inst;
}
void winservice::uninstall()
{
std::wstring name = booster::nowide::convert(settings_.get<std::string>("winservice.name"));
SC_HANDLE schm = OpenSCManagerW(0,0,SC_MANAGER_ALL_ACCESS);
if(!schm) {
throw booster::system::system_error(
GetLastError(),
booster::system::windows_category,
"Failed to open sevice imager");
}
SC_HANDLE service_handle = OpenServiceW(schm,name.c_str(),DELETE);
if(service_handle == NULL) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
CloseServiceHandle(schm);
throw booster::system::system_error(e,"Failed to open the service");
}
if(!DeleteService(service_handle)) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
CloseServiceHandle(service_handle);
CloseServiceHandle(schm);
throw booster::system::system_error(e,"Failed to delete the service");
}
CloseServiceHandle(service_handle);
CloseServiceHandle(schm);
std::cout << "The service uninstalled sucessefully" << std::endl;
}
void winservice::install()
{
std::wstring name = booster::nowide::convert(settings_.get<std::string>("winservice.name"));
std::wstring display_name = booster::nowide::convert(settings_.get<std::string>("winservice.display_name"));
std::string start_type = settings_.get("winservice.start","auto");
std::wstring cmd_line;
std::wstring user_name,password;
wchar_t const *cuser_name=0,*cpassword=0;
if(!settings_.find("winservice.username").is_undefined()) {
user_name = booster::nowide::convert(settings_.get<std::string>("winservice.username"));
cuser_name = user_name.c_str();
}
if(!settings_.find("winservice.password").is_undefined()) {
password = booster::nowide::convert(settings_.get<std::string>("winservice.password"));
cpassword = password.c_str();
}
wchar_t exe[ MAX_PATH + 1];
if(GetModuleFileNameW(0,exe,MAX_PATH+1) == 0) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
throw booster::system::system_error(e,"Failed to get exe name");
}
cmd_line = L"\"";
cmd_line +=exe;
cmd_line +=L"\"";
bool found = false;
for(size_t i=1;i<args_.size();i++) {
std::wstring parameter = booster::nowide::convert(args_[i]);
if(parameter==L"--winservice-mode=install") {
parameter = L"--winservice-mode=run";
found = true;
}
cmd_line+=L" \"";
cmd_line+=parameter;
cmd_line+=L"\"";
}
if(!found) {
throw cppcms_error("Parameter --winservice-mode=install is not provided via command line!");
}
BOOSTER_DEBUG("cppcms") << "Installing with parameters" << booster::nowide::convert(cmd_line);
DWORD start_type_flag = 0;
if(start_type == "auto")
start_type_flag= SERVICE_AUTO_START;
else if(start_type == "demand")
start_type_flag= SERVICE_DEMAND_START;
else
throw cppcms_error("Parameter winservice.mode should be one of auto or demand");
SC_HANDLE schm = OpenSCManagerW(0,0,SC_MANAGER_ALL_ACCESS);
if(!schm) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
throw booster::system::system_error(e,"Failed to open sevice imager");
}
SC_HANDLE service_handle = CreateServiceW(
schm,
name.c_str(),
display_name.c_str(),
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
start_type_flag,
SERVICE_ERROR_NORMAL,
cmd_line.c_str(),
0, // load order group
0, // tagid
0, // dependencies
cuser_name,
cpassword
);
if(service_handle == NULL) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
CloseServiceHandle(schm);
throw booster::system::system_error(e,"Failed to install service");
}
CloseServiceHandle(service_handle);
CloseServiceHandle(schm);
std::cout << "The service installed sucessefully" << std::endl;
}
namespace {
SERVICE_STATUS_HANDLE status_handle;
SERVICE_STATUS status;
void WINAPI win_service_handler_proc(DWORD code)
{
if(code == SERVICE_CONTROL_SHUTDOWN || code == SERVICE_CONTROL_STOP) {
status.dwCurrentState = SERVICE_STOP_PENDING;
status.dwControlsAccepted = 0;
status.dwWaitHint = 10000;
SetServiceStatus(status_handle,&status);
winservice::instance().stop();
return;
}
SetServiceStatus(status_handle,&status);
}
void WINAPI win_service_main(DWORD,wchar_t **)
{
status_handle = RegisterServiceCtrlHandlerW(L"",win_service_handler_proc);
if(status_handle==0) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
BOOSTER_ERROR("cppcms") << "Failed to register windows service handle:" << e.message();
return;
}
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
try {
status.dwWaitHint = 10000;
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
SetServiceStatus(status_handle,&status);
winservice::instance().prepare();
status.dwWaitHint = 0;
status.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(status_handle,&status);
winservice::instance().exec();
}
catch(std::exception const &e ){
BOOSTER_ERROR("cppcms") << "Main loop stopped:" << e.what() << booster::trace(e);
status.dwWin32ExitCode = 1;
}
catch(...) {
BOOSTER_ERROR("cppcms") << "Main loop stopped for unknown exception";
status.dwWin32ExitCode = 1;
}
status.dwCurrentState = SERVICE_STOPPED;
status.dwWaitHint = 0;
SetServiceStatus(status_handle,&status);
return;
}
} // anonymous
void winservice::service()
{
static wchar_t empty_wide_string = 0;
SERVICE_TABLE_ENTRYW entry[2] = {
{ &empty_wide_string ,win_service_main },
{ 0,0 }
};
if(!StartServiceCtrlDispatcherW(entry)) {
booster::system::error_code e(GetLastError(),booster::system::windows_category);
throw booster::system::system_error(e,"Failed to start windows service");
}
}
void winservice::run(json::value &conf,int argc,char **argv)
{
args_.assign(argv,argv+argc);
settings_=conf;
std::string mode = settings_.get("winservice.mode","console");
if(mode == "console")
throw cppcms_error("console mode should not use winservice class");
else if(mode == "install")
install();
else if(mode == "uninstall")
uninstall();
else if(mode == "run") {
conf.set("service.disable_global_exit_handling",true);
service();
}
else
throw cppcms_error("Invalid option winservice.mode=" + mode);
}
} // impl
} // cppcms