Electroneum
epee::service_impl_base Class Referenceabstract

#include <service_impl_base.h>

Public Member Functions

 service_impl_base ()
 
virtual ~service_impl_base ()
 
virtual const char * get_name ()=0
 
virtual const char * get_caption ()=0
 
virtual const char * get_description ()=0
 
bool run_service ()
 
virtual bool install ()
 
virtual bool remove ()
 
virtual bool init ()
 
void set_control_accepted (unsigned controls)
 
void set_status (unsigned state, unsigned pending=0)
 
unsigned get_control_accepted ()
 

Detailed Description

Definition at line 36 of file service_impl_base.h.

Constructor & Destructor Documentation

◆ service_impl_base()

epee::service_impl_base::service_impl_base ( )
inline

Definition at line 72 of file service_impl_base.h.

72  {
73  m_manager = 0;
74  m_service = 0;
75  m_status_handle = 0;
76  m_accepted_control = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN
77  | SERVICE_ACCEPT_PAUSE_CONTINUE;
78 
79  instance() = this;
80 }

◆ ~service_impl_base()

epee::service_impl_base::~service_impl_base ( )
inlinevirtual

Definition at line 82 of file service_impl_base.h.

82  {
83  if (m_service) {
84  ::CloseServiceHandle(m_service);
85  }
86  m_service = 0;
87  if (m_manager) {
88  ::CloseServiceHandle(m_manager);
89  }
90  m_manager = 0;
91  instance() = 0;
92 }

Member Function Documentation

◆ get_caption()

virtual const char* epee::service_impl_base::get_caption ( )
pure virtual
Here is the caller graph for this function:

◆ get_control_accepted()

unsigned epee::service_impl_base::get_control_accepted ( )
inline

Definition at line 304 of file service_impl_base.h.

304  {
305  return m_accepted_control;
306 }

◆ get_description()

virtual const char* epee::service_impl_base::get_description ( )
pure virtual
Here is the caller graph for this function:

◆ get_name()

virtual const char* epee::service_impl_base::get_name ( )
pure virtual
Here is the caller graph for this function:

◆ init()

bool epee::service_impl_base::init ( )
inlinevirtual

Definition at line 216 of file service_impl_base.h.

216  {
217  return true;
218 }

◆ install()

bool epee::service_impl_base::install ( )
inlinevirtual

Definition at line 100 of file service_impl_base.h.

100  {
101  CHECK_AND_ASSERT(!m_service, false);
102  const char *psz_descr = get_description();
103  SERVICE_FAILURE_ACTIONSA* fail_acts = get_failure_actions();
104 
105  char sz_path[MAX_PATH];
106  ::GetModuleFileNameA(0, sz_path, sizeof(sz_path));
107  ::GetShortPathNameA(sz_path, sz_path, sizeof(sz_path));
108 
109  while (TRUE) {
110  if (!m_manager) {
111  m_manager = ::OpenSCManager(NULL, NULL, GENERIC_ALL);
112  if (!m_manager) {
113  int err = GetLastError();
114  LOG_ERROR(
115  "Failed to OpenSCManager(), last err="
116  << log_space::get_win32_err_descr(err));
117  break;
118  }
119  }
120  m_service = ::CreateServiceA(m_manager, get_name(), get_caption(),
121  SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START,
122  SERVICE_ERROR_IGNORE, sz_path, 0, 0, 0, 0, 0);
123  if (!m_service) {
124  int err = GetLastError();
125  LOG_ERROR(
126  "Failed to CreateService(), last err="
127  << log_space::get_win32_err_descr(err));
128  break;
129  }
130 
131  if (psz_descr) {
132  SERVICE_DESCRIPTIONA sd = { (char*) psz_descr };
133  if (!::ChangeServiceConfig2A(m_service, SERVICE_CONFIG_DESCRIPTION,
134  &sd)) {
135  int err = GetLastError();
136  LOG_ERROR(
137  "Failed to ChangeServiceConfig2(SERVICE_CONFIG_DESCRIPTION), last err="
138  << log_space::get_win32_err_descr(err));
139  break;
140  }
141  }
142 
143  if (fail_acts) {
144  if (!::ChangeServiceConfig2A(m_service, SERVICE_CONFIG_FAILURE_ACTIONS,
145  fail_acts)) {
146  int err = GetLastError();
147  LOG_ERROR(
148  "Failed to ChangeServiceConfig2(SERVICE_CONFIG_FAILURE_ACTIONS), last err="
149  << log_space::get_win32_err_descr(err));
150  break;
151  }
152  }
153  LOG_PRINT("Installed succesfully.", LOG_LEVEL_0);
154  return true;
155  }
156  LOG_PRINT("Failed to install.", LOG_LEVEL_0);
157  return false;
158 }
virtual const char * get_description()=0
#define CHECK_AND_ASSERT(expr, fail_ret_val)
Definition: misc_log_ex.h:177
virtual const char * get_caption()=0
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
virtual const char * get_name()=0
Here is the call graph for this function:

◆ remove()

bool epee::service_impl_base::remove ( )
inlinevirtual

Definition at line 161 of file service_impl_base.h.

161  {
162  CHECK_AND_ASSERT(!m_service, false);
163 
164  while (TRUE) {
165  if (!m_manager) {
166  m_manager = ::OpenSCManager(0, 0, GENERIC_ALL);
167  if (!m_manager) {
168  int err = GetLastError();
169  LOG_ERROR(
170  "Failed to OpenSCManager(), last err="
171  << log_space::get_win32_err_descr(err));
172  break;
173  }
174  }
175 
176  if (!m_service) {
177  m_service = ::OpenServiceA(m_manager, get_name(), SERVICE_STOP | DELETE);
178  if (!m_service) {
179  int err = GetLastError();
180  LOG_ERROR(
181  "Failed to OpenService(), last err="
182  << log_space::get_win32_err_descr(err));
183  break;
184  }
185  }
186 
187  SERVICE_STATUS status = { };
188  if (!::ControlService(m_service, SERVICE_CONTROL_STOP, &status)) {
189  int err = ::GetLastError();
190  if (err == ERROR_SHUTDOWN_IN_PROGRESS)
191  continue;
192  else if (err != ERROR_SERVICE_NOT_ACTIVE) {
193  LOG_ERROR(
194  "Failed to ControlService(SERVICE_CONTROL_STOP), last err="
195  << log_space::get_win32_err_descr(err));
196  break;
197  }
198  }
199 
200  if (!::DeleteService(m_service)) {
201  int err = ::GetLastError();
202  LOG_ERROR(
203  "Failed to ControlService(SERVICE_CONTROL_STOP), last err="
204  << log_space::get_win32_err_descr(err));
205  break;
206  }
207 
208  LOG_PRINT("Removed successfully.", LOG_LEVEL_0);
209  break;
210  }
211 
212  return true;
213 }
#define CHECK_AND_ASSERT(expr, fail_ret_val)
Definition: misc_log_ex.h:177
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
virtual const char * get_name()=0
Here is the call graph for this function:

◆ run_service()

bool epee::service_impl_base::run_service ( )
inline

Definition at line 221 of file service_impl_base.h.

221  {
222  CHECK_AND_ASSERT(!m_service, false);
223 
224  long error_code = 0;
225 
226  SERVICE_TABLE_ENTRYA service_table[2];
227  ZeroMemory(&service_table, sizeof(service_table));
228 
229  service_table->lpServiceName = (char*) get_name();
230  service_table->lpServiceProc = service_entry;
231 
232  LOG_PRINT("[+] Start service control dispatcher for \"" << get_name() << "\"",
233  LOG_LEVEL_1);
234 
235  error_code = 1;
236  BOOL res = ::StartServiceCtrlDispatcherA(service_table);
237  if (!res) {
238  int err = GetLastError();
239  LOG_PRINT(
240  "[+] Error starting service control dispatcher, err="
241  << log_space::get_win32_err_descr(err), LOG_LEVEL_1);
242  return false;
243  } else {
244  LOG_PRINT("[+] End service control dispatcher for \"" << get_name() << "\"",
245  LOG_LEVEL_1);
246  }
247  return true;
248 }
const char * res
Definition: hmac_keccak.cpp:41
#define CHECK_AND_ASSERT(expr, fail_ret_val)
Definition: misc_log_ex.h:177
virtual const char * get_name()=0
Here is the call graph for this function:

◆ set_control_accepted()

void epee::service_impl_base::set_control_accepted ( unsigned  controls)
inline

Definition at line 299 of file service_impl_base.h.

299  {
300  m_accepted_control = controls;
301 }

◆ set_status()

void epee::service_impl_base::set_status ( unsigned  state,
unsigned  pending = 0 
)
inline

Definition at line 275 of file service_impl_base.h.

275  {
276  if (!m_status_handle)
277  return;
278 
279  SERVICE_STATUS status = { 0 };
280  status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
281  status.dwCurrentState = state;
282  status.dwControlsAccepted = m_accepted_control;
283  /*status.dwWin32ExitCode = NO_ERROR;
284  status.dwServiceSpecificExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
285  status.dwCheckPoint = 0;
286  status.dwWaitHint = 0;
287 
288  status.dwCurrentState = state;*/
289 
290  if (state == SERVICE_START_PENDING || state == SERVICE_STOP_PENDING
291  || state == SERVICE_CONTINUE_PENDING || state == SERVICE_PAUSE_PENDING) {
292  status.dwWaitHint = 2000;
293  status.dwCheckPoint = pending;
294  }
295  ::SetServiceStatus(m_status_handle, &status);
296 }
Definition: blake256.h:37

The documentation for this class was generated from the following file: