aboutsummaryrefslogtreecommitdiff
path: root/src/common/Platform/Windows/ServiceWin32.cpp
blob: f536d4b6e4abc01d5a15741bb7de43d155d7f502 (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
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
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ServiceWin32.h"
#include <array> // for std::size
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <Windows.h>
#include <winsvc.h>

namespace
{
_TCHAR* ServiceLongName;
_TCHAR* ServiceName;
_TCHAR* ServiceDescription;
int(*ServiceEntryPoint)(int argc, char** argv);
int* ServiceStatusPtr;

SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE ServiceStatusHandle = nullptr;
}

typedef BOOL (WINAPI *CSD_T)(SC_HANDLE, DWORD, LPCVOID);

void Trinity::Service::Init(_TCHAR* serviceLongName, _TCHAR* serviceName, _TCHAR* serviceDescription, int(* entryPoint)(int argc, char** argv), int* status)
{
    ServiceLongName = serviceLongName;
    ServiceName = serviceName;
    ServiceDescription = serviceDescription;
    ServiceEntryPoint = entryPoint;
    ServiceStatusPtr = status;
}

int32 Trinity::Service::Install()
{
    SC_HANDLE serviceControlManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);

    if (serviceControlManager)
    {
        TCHAR path[_MAX_PATH + 10];
        if (GetModuleFileName(nullptr, path, std::size(path)) > 0)
        {
            _tcscat(path, _T(" --service run"));
            SC_HANDLE service = CreateService(serviceControlManager,
                ServiceName,                                // name of service
                ServiceLongName,                            // service name to display
                SERVICE_ALL_ACCESS,                         // desired access
                                                            // service type
                SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
                SERVICE_AUTO_START,                         // start type
                SERVICE_ERROR_IGNORE,                       // error control type
                path,                                       // service's binary
                nullptr,                                    // no load ordering group
                nullptr,                                    // no tag identifier
                nullptr,                                    // no dependencies
                nullptr,                                    // LocalSystem account
                nullptr);                                   // no password
            if (service)
            {
                SERVICE_DESCRIPTION sdBuf;
                sdBuf.lpDescription = ServiceDescription;
                ChangeServiceConfig2(
                    service,                                // handle to service
                    SERVICE_CONFIG_DESCRIPTION,             // change: description
                    &sdBuf);                                // new data

                SC_ACTION _action[1];
                _action[0].Type = SC_ACTION_RESTART;
                _action[0].Delay = 10000;
                SERVICE_FAILURE_ACTIONS sfa;
                ZeroMemory(&sfa, sizeof(SERVICE_FAILURE_ACTIONS));
                sfa.lpsaActions = _action;
                sfa.cActions = 1;
                sfa.dwResetPeriod = INFINITE;
                ChangeServiceConfig2(
                    service,                                // handle to service
                    SERVICE_CONFIG_FAILURE_ACTIONS,         // information level
                    &sfa);                                  // new data

                CloseServiceHandle(service);

            }
        }
        CloseServiceHandle(serviceControlManager);
    }

    printf("Service installed\n");
    return 0;
}

int32 Trinity::Service::Uninstall()
{
    SC_HANDLE serviceControlManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);

    if (serviceControlManager)
    {
        SC_HANDLE service = OpenService(serviceControlManager,
            ServiceName, SERVICE_QUERY_STATUS | DELETE);
        if (service)
        {
            SERVICE_STATUS serviceStatus2;
            if (QueryServiceStatus(service, &serviceStatus2))
            {
                if (serviceStatus2.dwCurrentState == SERVICE_STOPPED)
                    DeleteService(service);
            }
            CloseServiceHandle(service);
        }

        CloseServiceHandle(serviceControlManager);
    }

    printf("Service uninstalled\n");
    return 0;
}

void WINAPI ServiceControlHandler(DWORD controlCode)
{
    switch (controlCode)
    {
        case SERVICE_CONTROL_INTERROGATE:
            break;

        case SERVICE_CONTROL_SHUTDOWN:
        case SERVICE_CONTROL_STOP:
            ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
            SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

            *ServiceStatusPtr = 0;
            return;

        case SERVICE_CONTROL_PAUSE:
            *ServiceStatusPtr = 2;
            ServiceStatus.dwCurrentState = SERVICE_PAUSED;
            SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
            break;

        case SERVICE_CONTROL_CONTINUE:
            ServiceStatus.dwCurrentState = SERVICE_RUNNING;
            SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
            *ServiceStatusPtr = 1;
            break;

        default:
            if (controlCode >= 128 && controlCode <= 255)
                // user defined control code
                break;
            else
                // unrecognized control code
                break;
    }

    SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
}

template<size_t size>
void TCharToChar(TCHAR const* src, char(&dst)[size])
{
    if constexpr (std::is_same_v<TCHAR, char>)
        ::strcpy_s(dst, src);
    else
        ::wcstombs_s(nullptr, dst, src, _TRUNCATE);
}

void WINAPI ServiceMain(DWORD /*argc*/, TCHAR *argv[])
{
    // initialise service status
    ServiceStatus.dwServiceType = SERVICE_WIN32;
    ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
    ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
    ServiceStatus.dwWin32ExitCode = NO_ERROR;
    ServiceStatus.dwServiceSpecificExitCode = NO_ERROR;
    ServiceStatus.dwCheckPoint = 0;
    ServiceStatus.dwWaitHint = 0;

    ServiceStatusHandle = RegisterServiceCtrlHandler(ServiceName, ServiceControlHandler);

    if (ServiceStatusHandle)
    {
        TCHAR path[_MAX_PATH + 1];
        size_t last_slash = 0;

        size_t pathLen = GetModuleFileName(nullptr, path, std::size(path));
        for (size_t i = 0; i < pathLen; i++)
            if (path[i] == '\\')
                last_slash = i;

        path[last_slash] = 0;

        // service is starting
        ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

        // do initialisation here
        SetCurrentDirectory(path);

        // running
        ServiceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
        ServiceStatus.dwCurrentState = SERVICE_RUNNING;
        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

        ////////////////////////
        // service main cycle //
        ////////////////////////

        *ServiceStatusPtr = 1;

        char cArg[_MAX_PATH + 1];
        TCharToChar(argv[0], cArg);
        char* cArgv[] = { cArg };

        ServiceEntryPoint(1, cArgv);

        // service was stopped
        ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

        // do cleanup here

        // service is now stopped
        ServiceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
    }
}

int32 Trinity::Service::Run()
{
    SERVICE_TABLE_ENTRY serviceTable[] =
    {
        { ServiceName, ServiceMain },
        { nullptr, nullptr }
    };

    if (!StartServiceCtrlDispatcher(serviceTable))
    {
        printf("StartService Failed. Error [%u]", uint32(::GetLastError()));
        return 1;
    }
    return 0;
}