summaryrefslogtreecommitdiff
path: root/win/win32service.cpp
blob: 4c2545b1e47c722f5b38b0b756a589d2331f38e1 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
 *   Copyright (C) 2012-2013 ChrisTX <xpipe@hotmail.de>
 *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
 *   Copyright (C) 2008 Craig Edwards <brain@inspircd.org>
 *
 * This file is part of InspIRCd.  InspIRCd 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, version 2.
 *
 * 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 "config.h"
#include "inspircd.h"
#include "exitcodes.h"
#include <windows.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>

static SERVICE_STATUS_HANDLE g_ServiceStatusHandle;
static SERVICE_STATUS g_ServiceStatus;
static bool g_bRunningAsService;

struct Service_Data {
	DWORD argc;
	LPSTR *argv;
};

static Service_Data g_ServiceData;

/** The main part of inspircd runs within this thread function. This allows the service part to run
 * seperately on its own and to be able to kill the worker thread when its time to quit.
 */
DWORD WINAPI WorkerThread(LPVOID param)
{
	smain(g_ServiceData.argc, g_ServiceData.argv);
	return 0;
}

/* This is called when all startup is done */
void SetServiceRunning()
{
	if (!g_bRunningAsService)
		return;

	g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
	g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;

	if( !SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus ) )
		throw CWin32Exception();
}

/* In windows we hook this to InspIRCd::Exit() */
void SetServiceStopped(DWORD dwStatus)
{
	if (!g_bRunningAsService)
		return;

	g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
	if(dwStatus != EXIT_STATUS_NOERROR)
	{
		g_ServiceStatus.dwServiceSpecificExitCode = dwStatus;
		g_ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
	}
	else
	{
		g_ServiceStatus.dwWin32ExitCode = ERROR_SUCCESS;
	}
	SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus );
}

/** This callback is called by windows when the state of the service has been changed */
VOID ServiceCtrlHandler(DWORD controlCode)
{
	switch(controlCode)
	{
		case SERVICE_CONTROL_SHUTDOWN:
		case SERVICE_CONTROL_STOP:
			g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
			SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus );
			break;
	}
}

/** This callback is called by windows when the service is started */
VOID ServiceMain(DWORD argc, LPCSTR *argv)
{
	g_ServiceStatusHandle = RegisterServiceCtrlHandler(TEXT("InspIRCd"), (LPHANDLER_FUNCTION)ServiceCtrlHandler);
	if( !g_ServiceStatusHandle )
		return;

	g_ServiceStatus.dwCheckPoint = 1;
	g_ServiceStatus.dwControlsAccepted = 0;
	g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	g_ServiceStatus.dwWaitHint = 5000;
	g_ServiceStatus.dwWin32ExitCode = NO_ERROR;
	g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;

	if( !SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus ) )
		return;

	char szModuleName[MAX_PATH];
	if(GetModuleFileNameA(NULL, szModuleName, MAX_PATH))
	{
		if(!argc)
			argc = 1;

		g_ServiceData.argc = argc;

		// Note: since this memory is going to stay allocated for the rest of the execution,
		//		 it doesn't make sense to free it, as it's going to be "freed" on process termination
		try {
			g_ServiceData.argv = new char*[argc];

			uint32_t allocsize = strnlen_s(szModuleName, MAX_PATH) + 1;
			g_ServiceData.argv[0] = new char[allocsize];
			strcpy_s(g_ServiceData.argv[0], allocsize, szModuleName);

			for(uint32_t i = 1; i < argc; i++)
			{
				allocsize = strnlen_s(argv[i], MAX_PATH) + 1;
				g_ServiceData.argv[i] = new char[allocsize];
				strcpy_s(g_ServiceData.argv[i], allocsize, argv[i]);
			}

			*(strrchr(szModuleName, '\\') + 1) = NULL;
			SetCurrentDirectoryA(szModuleName);

			HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkerThread, NULL, 0, NULL);
			if (hThread != NULL)
			{
				WaitForSingleObject(hThread, INFINITE);
				CloseHandle(hThread);
			}
		}
		catch(...)
		{
			g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
			g_ServiceStatus.dwWin32ExitCode = ERROR_OUTOFMEMORY;
			SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus );
		}
	}
	if(g_ServiceStatus.dwCurrentState == SERVICE_STOPPED)
		return;

	g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
	g_ServiceStatus.dwWin32ExitCode = GetLastError();
	SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus );
}

/** Install the windows service. This requires administrator privileges. */
void InstallService()
{
	SC_HANDLE InspServiceHandle = 0, SCMHandle = 0;

	try {
		TCHAR tszBinaryPath[MAX_PATH];
		if(!GetModuleFileName(NULL, tszBinaryPath, _countof(tszBinaryPath)))
		{
			throw CWin32Exception();
		}

		SCMHandle = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
		if (!SCMHandle)
		{
			throw CWin32Exception();
		}

		InspServiceHandle = CreateService(SCMHandle, TEXT("InspIRCd"),TEXT("InspIRCd Daemon"), SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS,
			SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, tszBinaryPath, 0, 0, 0, TEXT("NT AUTHORITY\\NetworkService"), NULL);

		if (!InspServiceHandle)
		{
			throw CWin32Exception();
		}

		TCHAR tszDescription[] = TEXT("The InspIRCd service hosts IRC channels and conversations. If this service is stopped, the IRC server will be unavailable.");
		SERVICE_DESCRIPTION svDescription = { tszDescription };
		if(!ChangeServiceConfig2(InspServiceHandle, SERVICE_CONFIG_DESCRIPTION, &svDescription))
		{
			throw CWin32Exception();
		}

		CloseServiceHandle(InspServiceHandle);
		CloseServiceHandle(SCMHandle);
		std::cout << "Service installed." << std::endl;
	}
	catch(CWin32Exception e)
	{
		if(InspServiceHandle)
			CloseServiceHandle(InspServiceHandle);

		if(SCMHandle)
			CloseServiceHandle(SCMHandle);

		std::cout << "Service installation failed: " << e.what() << std::endl;
	}
}

/** Remove the windows service. This requires administrator privileges. */
void UninstallService()
{
	SC_HANDLE InspServiceHandle = 0, SCMHandle = 0;

	try
	{
		SCMHandle = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, DELETE);
		if (!SCMHandle)
			throw CWin32Exception();

		InspServiceHandle = OpenService(SCMHandle, TEXT("InspIRCd"), DELETE);
		if (!InspServiceHandle)
			throw CWin32Exception();

		if (!DeleteService(InspServiceHandle) && GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE)
		{
			throw CWin32Exception();
		}

		CloseServiceHandle(InspServiceHandle);
		CloseServiceHandle(SCMHandle);
		std::cout << "Service removed." << std::endl;
	}
	catch(CWin32Exception e)
	{
		if(InspServiceHandle)
			CloseServiceHandle(InspServiceHandle);

		if(SCMHandle)
			CloseServiceHandle(SCMHandle);

		std::cout << "Service deletion failed: " << e.what() << std::endl;
	}
}

/* In windows, our main() flows through here, before calling the 'real' main, smain() in inspircd.cpp */
int main(int argc, char* argv[])
{
	/* Check for parameters */
	if (argc > 1)
	{
		for (int i = 1; i < argc; i++)
		{
			if(!_stricmp(argv[i], "--installservice"))
			{
				InstallService();
				return 0;
			}
			if(!_stricmp(argv[i], "--uninstallservice") || !_stricmp(argv[i], "--removeservice"))
			{
				UninstallService();
				return 0;
			}
		}
	}

	SERVICE_TABLE_ENTRY serviceTable[] =
	{
		{ TEXT("InspIRCd"), (LPSERVICE_MAIN_FUNCTION)ServiceMain },
		{ NULL, NULL }
	};

	g_bRunningAsService = true;
	if( !StartServiceCtrlDispatcher(serviceTable) )
	{
		// This error means that the program was not started as service.
		if( GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT )
		{
			g_bRunningAsService = false;
			return smain(argc, argv);
		}
		else
		{
			return EXIT_STATUS_SERVICE;
		}
	}
	return 0;
}