summaryrefslogtreecommitdiffstats
path: root/src/common/win32/service.cc
blob: 846c5d09dfaa8859e1a0c6fd586042418a0c5c8c (plain)
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
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2019 SUSE LINUX GmbH
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#define dout_context cct
#define dout_subsys ceph_subsys_

#include "common/debug.h"
#include "common/errno.h"
#include "common/win32/service.h"

// Initialize the singleton service instance.
ServiceBase *ServiceBase::s_service = NULL;

ServiceBase::ServiceBase(CephContext *cct_): cct(cct_)
{
  status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  status.dwCurrentState = SERVICE_START_PENDING;
  status.dwWin32ExitCode = NO_ERROR;
  status.dwCheckPoint = 0;
  /* The estimated time required for the stop operation in ms. */
  status.dwWaitHint = 0;
}

/* Register service action callbacks */
int ServiceBase::initialize(ServiceBase *service)
{
  s_service = service;

  SERVICE_TABLE_ENTRY service_table[] = {
    {"", (LPSERVICE_MAIN_FUNCTION)run},
    {NULL, NULL}
  };

  /* StartServiceCtrlDispatcher blocks until the service is stopped. */
  if (!StartServiceCtrlDispatcher(service_table)) {
    int err = GetLastError();
    lderr(service->cct) << "StartServiceCtrlDispatcher error: "
                        << err << dendl;
    return -EINVAL;
  }
  return 0;
}

void WINAPI ServiceBase::run()
{
  assert(s_service != NULL);

  /* Register the control handler. This function is called by the service
   * manager to stop the service. The service name that we're passing here
   * doesn't have to be valid as we're using SERVICE_WIN32_OWN_PROCESS. */
  s_service->hstatus = RegisterServiceCtrlHandler(
    "", (LPHANDLER_FUNCTION)control_handler);
  if (!s_service->hstatus) {
    lderr(s_service->cct) << "Could not initialize service control handler. "
                          << "Error: " << GetLastError() << dendl;
    return;
  }

  s_service->set_status(SERVICE_START_PENDING);

  // TODO: should we expect exceptions?
  ldout(s_service->cct, 5) << "Starting service." << dendl;
  int err = s_service->run_hook();
  if (err) {
    lderr(s_service->cct) << "Failed to start service. Error code: "
                          << err << dendl;
    s_service->shutdown(true);
  } else {
    ldout(s_service->cct, 5) << "Successfully started service." << dendl;
    s_service->set_status(SERVICE_RUNNING);
  }
}

void ServiceBase::shutdown(bool ignore_errors)
{
  DWORD original_state = status.dwCurrentState;
  set_status(SERVICE_STOP_PENDING);

  int err = shutdown_hook();
  if (err) {
    derr << "Shutdown service hook failed. Error code: " << err << dendl;
    if (ignore_errors) {
      derr << "Ignoring shutdown hook failure, marking the service as stopped."
           << dendl;
      set_status(SERVICE_STOPPED);
    } else {
      derr << "Reverting to original service state." << dendl;
      set_status(original_state);
    }
  } else {
    dout(5) << "Shutdown hook completed." << dendl;
    set_status(SERVICE_STOPPED);
  }
}

void ServiceBase::stop()
{
  DWORD original_state = status.dwCurrentState;
  set_status(SERVICE_STOP_PENDING);

  int err = stop_hook();
  if (err) {
    derr << "Service stop hook failed. Error code: " << err << dendl;
    set_status(original_state);
  } else {
    dout(5) << "Successfully stopped service." << dendl;
    set_status(SERVICE_STOPPED);
  }
}

/* This function is registered with the Windows services manager through
 * a call to RegisterServiceCtrlHandler() and will be called by the Windows
 * service manager asynchronously to stop the service. */
void ServiceBase::control_handler(DWORD request)
{
  switch (request) {
  case SERVICE_CONTROL_STOP:
    s_service->stop();
    break;
  case SERVICE_CONTROL_SHUTDOWN:
    s_service->shutdown();
    break;
  default:
    break;
  }
}

void ServiceBase::set_status(DWORD current_state, DWORD exit_code) {
  static DWORD dwCheckPoint = 1;
  if (current_state == SERVICE_RUNNING || current_state == SERVICE_STOPPED) {
    status.dwCheckPoint = dwCheckPoint++;
  }

  status.dwCurrentState = current_state;
  status.dwWin32ExitCode = exit_code;

  if (hstatus) {
    dout(5) << "Updating service service status (" << current_state
             << ") and exit code(" << exit_code << ")." << dendl;
    ::SetServiceStatus(hstatus, &status);
  } else {
    derr << "Service control handler not initialized. Cannot "
         << "update service status (" << current_state
         << ") and exit code(" << exit_code << ")." << dendl;
  }
}