7d0a86258cc2f4b36679ccb35d16bbb731f5db44
[public/netxms.git] / src / server / core / modules.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: modules.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * List of loaded modules
27 */
28 TCHAR *g_pszModLoadList = NULL;
29 UINT32 g_dwNumModules = 0;
30 NXMODULE *g_pModuleList = NULL;
31
32 /**
33 * Load all registered modules
34 */
35 bool LoadNetXMSModules()
36 {
37 TCHAR szErrorText[256], *curr, *next, *ptr;
38 NXMODULE module;
39 HMODULE hModule;
40 bool success = true;
41
42 for(curr = g_pszModLoadList; curr != NULL; curr = next)
43 {
44 next = _tcschr(curr, _T('\n'));
45 if (next != NULL)
46 {
47 *next = 0;
48 next++;
49 }
50 StrStrip(curr);
51 if (*curr == 0)
52 continue;
53
54 bool mandatory = false;
55 memset(&module, 0, sizeof(NXMODULE));
56
57 // Check for "mandatory" option
58 ptr = _tcschr(curr, _T(','));
59 if (ptr != NULL)
60 {
61 *ptr = 0;
62 ptr++;
63 StrStrip(curr);
64 StrStrip(ptr);
65 mandatory = (*ptr == _T('1')) || (*ptr == _T('Y')) || (*ptr == _T('y'));
66 }
67
68 hModule = DLOpen(curr, szErrorText);
69 if (hModule != NULL)
70 {
71 BOOL (* ModuleInit)(NXMODULE *);
72
73 ModuleInit = (BOOL (*)(NXMODULE *))DLGetSymbolAddr(hModule, "NetXMSModuleInit", szErrorText);
74 if (ModuleInit != NULL)
75 {
76 memset(&module, 0, sizeof(NXMODULE));
77 if (ModuleInit(&module))
78 {
79 if (module.dwSize == sizeof(NXMODULE))
80 {
81 // Add module to module's list
82 g_pModuleList = (NXMODULE *)realloc(g_pModuleList,
83 sizeof(NXMODULE) * (g_dwNumModules + 1));
84 memcpy(&g_pModuleList[g_dwNumModules], &module, sizeof(NXMODULE));
85 g_pModuleList[g_dwNumModules].hModule = hModule;
86
87 nxlog_write(MSG_MODULE_LOADED, EVENTLOG_INFORMATION_TYPE, "s", g_pModuleList[g_dwNumModules].szName);
88 g_dwNumModules++;
89 }
90 else
91 {
92 nxlog_write(MSG_MODULE_BAD_MAGIC, EVENTLOG_ERROR_TYPE, "s", curr);
93 DLClose(hModule);
94 if (mandatory)
95 {
96 success = false;
97 break;
98 }
99 }
100 }
101 else
102 {
103 nxlog_write(MSG_MODULE_INIT_FAILED, EVENTLOG_ERROR_TYPE, "s", curr);
104 DLClose(hModule);
105 if (mandatory)
106 {
107 success = false;
108 break;
109 }
110 }
111 }
112 else
113 {
114 nxlog_write(MSG_NO_MODULE_ENTRY_POINT, EVENTLOG_ERROR_TYPE, "s", curr);
115 DLClose(hModule);
116 if (mandatory)
117 {
118 success = false;
119 break;
120 }
121 }
122 }
123 else
124 {
125 nxlog_write(MSG_DLOPEN_FAILED, EVENTLOG_ERROR_TYPE, "ss", curr, szErrorText);
126 if (mandatory)
127 {
128 success = false;
129 break;
130 }
131 }
132 }
133 return success;
134 }