-
Notifications
You must be signed in to change notification settings - Fork 6
/
MainWin.cpp
430 lines (346 loc) · 13.4 KB
/
MainWin.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* XMail by Davide Libenzi (Intranet and Internet mail server)
* Copyright (C) 1999,..,2010 Davide Libenzi
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <[email protected]>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "SList.h"
#include "ShBlocks.h"
#include "UsrUtils.h"
#include "SvrUtils.h"
#include "MessQueue.h"
#include "SMAILUtils.h"
#include "QueueUtils.h"
#include "AppDefines.h"
#include "MailSvr.h"
/* Comment this statement if You want a normal startup ( not as a service ) */
#ifndef NO_SERVICE /* [i_a] */
#define SERVICE
#endif
#define NULFILE "nul"
#ifndef SERVICE
/* Normal startup */
int main(int iArgCount, char *pszArgs[])
{
return SvrMain(iArgCount, pszArgs);
}
#else // #ifndef SERVICE
/* Service startup */
#define SZDEPENDENCIES _T("Tcpip\0")
#define SERVER_START_WAIT 8000
#define SERVER_STOP_WAIT 4000
static int MnSetupStdHandles(void);
static VOID WINAPI ServiceCtrl(DWORD dwCtrlCode);
static VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR lpszArgv[]);
static BOOL CmdInstallService(DWORD dwStartType);
static BOOL CmdRemoveService(void);
static int CmdDebugService(int argc, TCHAR * argv[]);
static BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);
static LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize);
static VOID AddToMessageLog(LPCTSTR lpszMsg);
static int GetServiceNameFromModule(LPCTSTR pszModule, LPTSTR pszName, int iSize);
static SERVICE_STATUS ssStatus;
static SERVICE_STATUS_HANDLE sshStatusHandle = NULL;
static DWORD dwErr = 0;
static BOOL bDebug = FALSE;
static TCHAR szErr[2048] = _T("");
static TCHAR szServicePath[MAX_PATH] = _T("");
static TCHAR szServiceName[128] = _T("");
static TCHAR szServiceDispName[256] = _T("");
static int MnSetupStdHandles(void)
{
HANDLE hInFile = CreateFile(NULFILE, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hInFile == INVALID_HANDLE_VALUE) {
AddToMessageLog(_T("CreateFile"));
return -1;
}
HANDLE hOutFile = CreateFile(NULFILE, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hOutFile == INVALID_HANDLE_VALUE) {
AddToMessageLog(_T("CreateFile"));
CloseHandle(hInFile);
return -1;
}
HANDLE hErrFile = CreateFile(NULFILE, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hErrFile == INVALID_HANDLE_VALUE) {
AddToMessageLog(_T("CreateFile"));
CloseHandle(hOutFile);
CloseHandle(hInFile);
return -1;
}
if (!SetStdHandle(STD_INPUT_HANDLE, hInFile) ||
!SetStdHandle(STD_OUTPUT_HANDLE, hOutFile) ||
!SetStdHandle(STD_ERROR_HANDLE, hErrFile)) {
AddToMessageLog(_T("SetStdHandle"));
CloseHandle(hErrFile);
CloseHandle(hOutFile);
CloseHandle(hInFile);
return -1;
}
return 0;
}
int _tmain(int argc, TCHAR * argv[])
{
if (GetModuleFileName(NULL, szServicePath, CountOf(szServicePath)) == 0) {
_tprintf(_T("Unable to get module name - %s\n"),
GetLastErrorText(szErr, CountOf(szErr)));
return 1;
}
GetServiceNameFromModule(szServicePath, szServiceName, CountOf(szServiceName));
_stprintf(szServiceDispName, _T("%s Server"), szServiceName);
SERVICE_TABLE_ENTRY DispTable[] = {
{ szServiceName, (LPSERVICE_MAIN_FUNCTION) ServiceMain },
{ NULL, NULL }
};
if (argc > 1) {
if (_tcsicmp(_T("--install"), argv[1]) == 0) {
CmdInstallService(SERVICE_DEMAND_START);
return 0;
}
if (_tcsicmp(_T("--install-auto"), argv[1]) == 0) {
CmdInstallService(SERVICE_AUTO_START);
return 0;
} else if (_tcsicmp(_T("--remove"), argv[1]) == 0) {
CmdRemoveService();
return 0;
} else if (_tcsicmp(_T("--debug"), argv[1]) == 0) {
bDebug = TRUE;
CmdDebugService(argc, argv);
return 0;
}
}
_tprintf(_T("%s --install = Install the service\n"), argv[0]);
_tprintf(_T("%s --remove = Remove the service\n"), argv[0]);
_tprintf(_T("%s --debug [params] = Run as a console app for debugging\n"), argv[0]);
_tprintf(_T("\nStartServiceCtrlDispatcher being called.\n"));
_tprintf(_T("This may take several seconds. Please wait.\n"));
/* Setup std handles */
if (MnSetupStdHandles() < 0)
return 1;
/* Service loop */
if (!StartServiceCtrlDispatcher(DispTable))
AddToMessageLog(_T("StartServiceCtrlDispatcher"));
return 0;
}
static void WINAPI ServiceMain(DWORD dwArgc, LPTSTR lpszArgv[])
{
if ((sshStatusHandle = RegisterServiceCtrlHandler(szServiceName, ServiceCtrl)) != NULL) {
ZeroData(ssStatus);
ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode = 0;
if (ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, SERVER_START_WAIT)) {
ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0);
/* Run server */
int iSvrResult = SvrMain((int) dwArgc, lpszArgv);
if (iSvrResult < 0) {
AddToMessageLog(ErrGetErrorString(iSvrResult));
}
}
ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0);
} else
AddToMessageLog(_T("RegisterServiceCtrlHandler"));
}
static VOID WINAPI ServiceCtrl(DWORD dwCtrlCode)
{
switch (dwCtrlCode) {
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
{
ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, SERVER_STOP_WAIT);
/* Signal the server to stop and wait for completion */
SvrStopServer(false);
while (SvrInShutdown()) {
Sleep(SERVER_STOP_WAIT / 2);
ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR,
SERVER_STOP_WAIT);
}
ReportStatusToSCMgr(SERVICE_STOPPED, 0, 0);
}
break;
default:
ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
}
}
static BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
BOOL bResult = TRUE;
if (!bDebug) {
if (dwCurrentState == SERVICE_START_PENDING)
ssStatus.dwControlsAccepted = 0;
else
ssStatus.dwControlsAccepted =
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ssStatus.dwCurrentState = dwCurrentState;
ssStatus.dwWin32ExitCode = dwWin32ExitCode;
ssStatus.dwWaitHint = dwWaitHint;
if ((dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED))
ssStatus.dwCheckPoint = 0;
else
ssStatus.dwCheckPoint = dwCheckPoint++;
if (!(bResult = SetServiceStatus(sshStatusHandle, &ssStatus)))
AddToMessageLog(_T("SetServiceStatus"));
}
return bResult;
}
static VOID AddToMessageLog(LPCTSTR lpszMsg)
{
HANDLE hEventSource = NULL;
LPTSTR lpszStrings[2];
TCHAR szMsg[512] = _T("");
TCHAR szErrMsg[2048] = _T("");
if (!bDebug) {
dwErr = GetLastError();
GetLastErrorText(szErr, CountOf(szErr));
_stprintf(szMsg, _T("%s error: %d"), szServiceName, dwErr);
_stprintf(szErrMsg, _T("{%s}: %s"), lpszMsg, szErr);
lpszStrings[0] = szMsg;
lpszStrings[1] = szErrMsg;
if ((hEventSource = RegisterEventSource(NULL, szServiceName)) != NULL) {
ReportEvent(hEventSource, // handle of event source
EVENTLOG_ERROR_TYPE, // event type
0, // event category
0, // event ID
NULL, // current user's SID
2, // strings in lpszStrings
0, // no bytes of raw data
(const char **) lpszStrings, // array of error strings
NULL); // no raw data
DeregisterEventSource(hEventSource);
}
}
}
static BOOL CmdInstallService(DWORD dwStartType)
{
SC_HANDLE schService = NULL;
SC_HANDLE schSCManager = NULL;
if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL) {
schService = CreateService(schSCManager, // SCManager database
szServiceName, // name of service
szServiceDispName, // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
dwStartType, // start type
SERVICE_ERROR_NORMAL, // error control type
szServicePath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
SZDEPENDENCIES, // dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService != NULL) {
_tprintf(_T("%s installed.\n"), szServiceDispName);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return TRUE;
} else
_tprintf(_T("CreateService failed - %s\n"),
GetLastErrorText(szErr, CountOf(szErr)));
CloseServiceHandle(schSCManager);
} else
_tprintf(_T("OpenSCManager failed - %s\n"),
GetLastErrorText(szErr, CountOf(szErr)));
return FALSE;
}
static BOOL CmdRemoveService(void)
{
SC_HANDLE schService = NULL;
SC_HANDLE schSCManager = NULL;
if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL) {
schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
if (schService != NULL) {
if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)) {
_tprintf(_T("Stopping %s."), szServiceDispName);
Sleep(1000);
while (QueryServiceStatus(schService, &ssStatus)) {
if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) {
_tprintf(_T("."));
Sleep(1000);
} else
break;
}
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
_tprintf(_T("\n%s stopped.\n"), szServiceDispName);
else
_tprintf(_T("\n%s failed to stop.\n"), szServiceDispName);
}
if (DeleteService(schService)) {
_tprintf(_T("%s removed.\n"), szServiceDispName);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return TRUE;
} else
_tprintf(_T("DeleteService failed - %s\n"),
GetLastErrorText(szErr, CountOf(szErr)));
CloseServiceHandle(schService);
} else
_tprintf(_T("OpenService failed - %s\n"),
GetLastErrorText(szErr, CountOf(szErr)));
CloseServiceHandle(schSCManager);
} else
_tprintf(_T("OpenSCManager failed - %s\n"),
GetLastErrorText(szErr, CountOf(szErr)));
return FALSE;
}
static int CmdDebugService(int argc, LPTSTR argv[])
{
_tprintf(_T("Debugging %s.\n"), szServiceDispName);
/* Run server */
return SvrMain(argc, argv);
}
static LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize)
{
DWORD dwRet;
DWORD dwError = GetLastError();
LPTSTR lpszTemp = NULL;
dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, dwError, LANG_NEUTRAL,
(LPTSTR) &lpszTemp, 0, NULL);
if (dwRet == 0 || (long) dwSize < (long) (dwRet + 14))
lpszBuf[0] = TCHAR('\0');
else {
lpszTemp[lstrlen(lpszTemp) - 2] = TCHAR('\0');
_stprintf(lpszBuf, _T("%s (0x%x)"), lpszTemp, dwError);
}
if (lpszTemp != NULL)
LocalFree((HLOCAL) lpszTemp);
return lpszBuf;
}
static int GetServiceNameFromModule(LPCTSTR pszModule, LPTSTR pszName, int iSize)
{
LPCTSTR pszSlash;
LPCTSTR pszDot;
if ((pszSlash = _tcsrchr(pszModule, (TCHAR) '\\')) == NULL)
pszSlash = pszModule;
else
pszSlash++;
if ((pszDot = _tcschr(pszSlash, (TCHAR) '.')) == NULL)
pszDot = pszSlash + _tcslen(pszSlash);
iSize = Min(iSize - 1, (int) (pszDot - pszSlash));
Cpy2Sz(pszName, pszSlash, iSize);
return 0;
}
#endif // #ifndef SERVICE