Resulting file name changed to report-generator.jar
[public/netxms.git] / src / server / netxmsd / netxmsd.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Server startup module
1621a079 4** Copyright (C) 2003-2011 NetXMS Team
5039dede
AK
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: netxmsd.cpp
21**
22**/
23
24#include "netxmsd.h"
25
26#if HAVE_GETOPT_H
27#include <getopt.h>
28#endif
29
30#ifdef _WIN32
31#include <dbghelp.h>
32#endif
33
44cdd527
VK
34#ifdef __sun
35#include <signal.h>
36#endif
37
5039dede
AK
38
39//
40// Global data
41//
42
43BOOL g_bCheckDB = FALSE;
44
45
46//
47// Help text
48//
49
e0f99bf0
VK
50static TCHAR help_text[] = _T("NetXMS Server Version ") NETXMS_VERSION_STRING _T("\n")
51 _T("Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008 NetXMS Team\n\n")
52 _T("Usage: netxmsd [<options>]\n\n")
53 _T("Valid options are:\n")
54 _T(" -e : Run database check on startup\n")
55 _T(" -c <file> : Set non-default configuration file\n")
56 _T(" : Default is ") DEFAULT_CONFIG_FILE _T("\n")
57 _T(" -d : Run as daemon/service\n")
58 _T(" -D <level> : Set debug level (valid levels are 0..9)\n")
59 _T(" -h : Display help and exit\n")
5039dede 60#ifdef _WIN32
e0f99bf0
VK
61 _T(" -I : Install Windows service\n")
62 _T(" -L <user> : Login name for service account.\n")
63 _T(" -P <passwd> : Password for service account.\n")
5039dede 64#else
e0f99bf0 65 _T(" -p <file> : Specify pid file.\n")
5039dede
AK
66#endif
67#ifdef _WIN32
e0f99bf0
VK
68 _T(" -R : Remove Windows service\n")
69 _T(" -s : Start Windows service\n")
70 _T(" -S : Stop Windows service\n")
5039dede 71#endif
e0f99bf0
VK
72 _T(" -v : Display version and exit\n")
73 _T("\n");
5039dede
AK
74
75
76//
5039dede
AK
77// Execute command and wait
78//
79
e0f99bf0 80static BOOL ExecAndWait(char *pszCommand)
5039dede
AK
81{
82 BOOL bSuccess = TRUE;
83
84#ifdef _WIN32
e0f99bf0 85 STARTUPINFOA si;
5039dede
AK
86 PROCESS_INFORMATION pi;
87
88 // Fill in process startup info structure
89 memset(&si, 0, sizeof(STARTUPINFO));
90 si.cb = sizeof(STARTUPINFO);
91 si.dwFlags = 0;
92
93 // Create new process
e0f99bf0
VK
94 if (!CreateProcessA(NULL, pszCommand, NULL, NULL, FALSE,
95 IsStandalone() ? 0 : CREATE_NO_WINDOW | DETACHED_PROCESS,
96 NULL, NULL, &si, &pi))
5039dede
AK
97 {
98 bSuccess = FALSE;
99 }
100 else
101 {
102 WaitForSingleObject(pi.hProcess, INFINITE);
103
104 // Close all handles
105 CloseHandle(pi.hThread);
106 CloseHandle(pi.hProcess);
107 }
108#else
109 bSuccess = (system(pszCommand) != -1);
110#endif
111
112 return bSuccess;
113}
114
115
116//
117// Create minidump of given process
118//
119
120#ifdef _WIN32
121
122static void CreateMiniDump(DWORD pid)
123{
124 HANDLE hFile, hProcess;
125 TCHAR error[256];
126
e0f99bf0 127 _tprintf(_T("INFO: Starting minidump for process %d\n"), pid);
5039dede
AK
128 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
129 if (hProcess != NULL)
130 {
131 hFile = CreateFile(_T("C:\\netxmsd.mdmp"), GENERIC_WRITE, 0, NULL,
132 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
133 if (hFile != INVALID_HANDLE_VALUE)
134 {
135 MiniDumpWriteDump(hProcess, pid, hFile, MiniDumpNormal, NULL, NULL, NULL);
136 CloseHandle(hFile);
e0f99bf0 137 _tprintf(_T("INFO: Minidump created successfully\n"));
5039dede
AK
138 }
139 else
140 {
e0f99bf0 141 _tprintf(_T("ERROR: cannot create file for minidump (%s)\n"), GetSystemErrorText(GetLastError(), error, 256));
5039dede
AK
142 }
143 }
144 else
145 {
e0f99bf0 146 _tprintf(_T("ERROR: cannot open process %d (%s)\n"), pid, GetSystemErrorText(GetLastError(), error, 256));
5039dede
AK
147 }
148}
149
150#endif
151
152
153//
154// Parse command line
155// Returns TRUE on success and FALSE on failure
156//
157
158#ifdef _WIN32
159#define VALID_OPTIONS "c:CdD:ehIL:P:RsSv"
160#else
161#define VALID_OPTIONS "c:CdD:ehp:v"
162#endif
163
164static BOOL ParseCommandLine(int argc, char *argv[])
165{
166 int ch;
e0f99bf0 167 char *eptr;
5039dede 168#ifdef _WIN32
e0f99bf0
VK
169 TCHAR login[256] = _T(""), password[256] = _T("");
170 char exePath[MAX_PATH], dllPath[MAX_PATH], *ptr;
5039dede
AK
171 BOOL useLogin = FALSE;
172#endif
173#if defined(_WIN32) || HAVE_DECL_GETOPT_LONG
174 static struct option longOptions[] =
175 {
176 { "check-db", 0, NULL, 'e' },
177 { "config", 1, NULL, 'c' },
178 { "daemon", 0, NULL, 'd' },
179 { "debug", 1, NULL, 'D' },
180 { "help", 0, NULL, 'h' },
181#ifdef _WIN32
182 { "check-service", 0, NULL, '!' },
183 { "dump", 1, NULL, '~' },
184 { "install", 0, NULL, 'I' },
185 { "login", 1, NULL, 'L' },
186 { "password", 1, NULL, 'P' },
187 { "remove", 0, NULL, 'R' },
188 { "start", 0, NULL, 's' },
189 { "stop", 0, NULL, 'S' },
190#else
e0f99bf0 191 { _T("pid-file"), 1, NULL, 'p' },
5039dede
AK
192#endif
193 { NULL, 0, 0, 0 }
194 };
195#endif
196
197#if defined(_WIN32) || HAVE_DECL_GETOPT_LONG
198 while((ch = getopt_long(argc, argv, VALID_OPTIONS, longOptions, NULL)) != -1)
199#else
200 while((ch = getopt(argc, argv, VALID_OPTIONS)) != -1)
201#endif
202 {
203 switch(ch)
204 {
205 case 'h':
e0f99bf0 206 _tprintf(help_text);
5039dede
AK
207 return FALSE;
208 case 'v':
e0f99bf0 209 _tprintf(_T("NetXMS Server Version ") NETXMS_VERSION_STRING _T(" Build of %hs\n"), __DATE__);
5039dede
AK
210 return FALSE;
211 case 'c':
e0f99bf0
VK
212#ifdef UNICODE
213 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, g_szConfigFile, MAX_PATH);
214 g_szConfigFile[MAX_PATH - 1] = 0;
215#else
5039dede 216 nx_strncpy(g_szConfigFile, optarg, MAX_PATH);
e0f99bf0 217#endif
5039dede
AK
218 break;
219 case 'C': // Check config
220 g_dwFlags &= ~AF_DAEMON;
e0f99bf0 221 _tprintf(_T("Checking configuration file (%s):\n\n"), g_szConfigFile);
5039dede
AK
222 LoadConfig();
223 return FALSE;
224 case 'd':
225 g_dwFlags |= AF_DAEMON;
226 break;
227 case 'D': // Debug level
228 g_nDebugLevel = strtol(optarg, &eptr, 0);
229 if ((*eptr != 0) || (g_nDebugLevel < 0) || (g_nDebugLevel > 9))
230 {
e0f99bf0 231 _tprintf(_T("Invalid debug level \"%hs\" - should be in range 0..9\n"), optarg);
5039dede
AK
232 g_nDebugLevel = 0;
233 }
234 break;
235 case 'e':
236 g_bCheckDB = TRUE;
237 break;
238#ifdef _WIN32
239 case 'L':
e0f99bf0
VK
240#ifdef UNICODE
241 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, login, 256);
242 login[255] = 0;
243#else
5039dede 244 nx_strncpy(login, optarg, 256);
e0f99bf0 245#endif
5039dede
AK
246 useLogin = TRUE;
247 break;
248 case 'P':
e0f99bf0
VK
249#ifdef UNICODE
250 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, password, 256);
251 password[255] = 0;
252#else
5039dede 253 nx_strncpy(password, optarg, 256);
e0f99bf0 254#endif
5039dede
AK
255 break;
256 case 'I': // Install service
257 ptr = strrchr(argv[0], '\\');
258 if (ptr != NULL)
259 ptr++;
260 else
261 ptr = argv[0];
262
263 _fullpath(exePath, ptr, 255);
264
265 if (stricmp(&exePath[strlen(exePath)-4], ".exe"))
266 strcat(exePath, ".exe");
267 strcpy(dllPath, exePath);
268 ptr = strrchr(dllPath, '\\');
269 if (ptr != NULL) // Shouldn't be NULL
270 {
271 ptr++;
272 strcpy(ptr, "libnxsrv.dll");
273 }
e0f99bf0
VK
274#ifdef UNICODE
275 WCHAR wexePath[MAX_PATH], wdllPath[MAX_PATH];
276 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, exePath, -1, wexePath, MAX_PATH);
277 wexePath[MAX_PATH - 1] = 0;
278 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, dllPath, -1, wdllPath, MAX_PATH);
279 wdllPath[MAX_PATH - 1] = 0;
280 InstallService(wexePath, wdllPath, useLogin ? login : NULL, useLogin ? password : NULL);
281#else
5039dede 282 InstallService(exePath, dllPath, useLogin ? login : NULL, useLogin ? password : NULL);
e0f99bf0 283#endif
5039dede
AK
284 return FALSE;
285 case 'R': // Remove service
286 RemoveService();
287 return FALSE;
288 case 's': // Start service
289 StartCoreService();
290 return FALSE;
291 case 'S': // Stop service
292 StopCoreService();
293 return FALSE;
294 case '!': // Check service configuration (for migration from pre-0.2.20)
295 CheckServiceConfig();
296 return FALSE;
297 case '~':
e0f99bf0 298 CreateMiniDump(strtoul(optarg, NULL, 0));
5039dede
AK
299 return FALSE;
300#endif
301 default:
302 break;
303 }
304 }
305 return TRUE;
306}
307
308
309//
310// Startup code
311//
312
313int main(int argc, char *argv[])
314{
315#ifdef _WIN32
316 HKEY hKey;
317 DWORD dwSize;
318#else
319 int i;
320 FILE *fp;
321 char *pszEnv;
322#endif
323
44cdd527
VK
324#ifdef __sun
325 signal(SIGPIPE, SIG_IGN);
326 signal(SIGHUP, SIG_IGN);
327 signal(SIGINT, SIG_IGN);
328 signal(SIGQUIT, SIG_IGN);
329 signal(SIGUSR1, SIG_IGN);
330 signal(SIGUSR2, SIG_IGN);
331#endif
332
5039dede
AK
333 InitThreadLibrary();
334
335#ifdef NETXMS_MEMORY_DEBUG
336 InitMemoryDebugger();
337#endif
338
339 // Check for alternate config file location
340#ifdef _WIN32
341 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0,
342 KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
343 {
344 dwSize = MAX_PATH * sizeof(TCHAR);
345 RegQueryValueEx(hKey, _T("ConfigFile"), NULL, NULL, (BYTE *)g_szConfigFile, &dwSize);
346 RegCloseKey(hKey);
347 }
348#else
82d4fb3d 349 pszEnv = _tgetenv(_T("NETXMSD_CONFIG"));
5039dede
AK
350 if (pszEnv != NULL)
351 nx_strncpy(g_szConfigFile, pszEnv, MAX_PATH);
352#endif
353
354 if (!ParseCommandLine(argc, argv))
355 return 1;
356
357 if (!LoadConfig())
358 {
359 if (IsStandalone())
e0f99bf0 360 _tprintf(_T("Error loading configuration file\n"));
5039dede
AK
361 return 1;
362 }
363
364 // Set exception handler
365#ifdef _WIN32
366 if (g_dwFlags & AF_CATCH_EXCEPTIONS)
4262c0dc 367 SetExceptionHandler(SEHServiceExceptionHandler, SEHServiceExceptionDataWriter,
e0f99bf0 368 g_szDumpDir, _T("netxmsd"), MSG_EXCEPTION, g_dwFlags & AF_WRITE_FULL_DUMP, IsStandalone());
5039dede
AK
369 __try {
370#endif
371
372 // Check database before start if requested
373 if (g_bCheckDB)
374 {
375 char szCmd[MAX_PATH + 128], *pszSep;
376
377 strncpy(szCmd, argv[0], MAX_PATH);
378 pszSep = strrchr(szCmd, FS_PATH_SEPARATOR[0]);
379 if (pszSep != NULL)
380 pszSep++;
381 else
382 pszSep = szCmd;
e0f99bf0
VK
383#ifdef UNICODE
384 snprintf(pszSep, 128, "nxdbmgr -c \"%S\" -f check", g_szConfigFile);
385#else
386 snprintf(pszSep, 128, "nxdbmgr -c \"%s\" -f check", g_szConfigFile);
387#endif
5039dede
AK
388 if (!ExecAndWait(szCmd))
389 if (IsStandalone())
e0f99bf0 390 _tprintf(_T("ERROR: Failed to execute command \"%hs\"\n"), szCmd);
5039dede
AK
391 }
392
393#ifdef _WIN32
394 if (!IsStandalone())
395 {
396 InitService();
397 }
398 else
399 {
400 if (!Initialize())
401 {
e0f99bf0 402 _tprintf(_T("NetXMS Core initialization failed\n"));
5039dede
AK
403
404 // Remove database lock
405 if (g_dwFlags & AF_DB_LOCKED)
406 {
407 UnlockDB();
408 ShutdownDB();
409 }
410 return 3;
411 }
412 Main(NULL);
413 }
414#else /* not _WIN32 */
415 if (!IsStandalone())
416 {
417 if (daemon(0, 0) == -1)
418 {
419 perror("Call to daemon() failed");
420 return 2;
421 }
422 }
423
424 // Write PID file
e0f99bf0 425 fp = _tfopen(g_szPIDFile, _T("w"));
5039dede
AK
426 if (fp != NULL)
427 {
e0f99bf0 428 _ftprintf(fp, _T("%d"), getpid());
5039dede
AK
429 fclose(fp);
430 }
431
432 // Initialize server
433 if (!Initialize())
434 {
435 // Remove database lock
436 if (g_dwFlags & AF_DB_LOCKED)
437 {
438 UnlockDB();
439 ShutdownDB();
440 }
441 return 3;
442 }
443
444 // Everything is OK, start common main loop
445 StartMainLoop(SignalHandler, Main);
446#endif /* _WIN32 */
447
448#ifdef _WIN32
449 LIBNETXMS_EXCEPTION_HANDLER
450#endif
451 return 0;
452}