agent and server can use NETXMS_HOME environment variable to locate lib directory
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 21 Oct 2013 11:09:05 +0000 (11:09 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 21 Oct 2013 11:09:05 +0000 (11:09 +0000)
src/agent/core/subagent.cpp
src/server/core/config.cpp
src/server/core/modules.cpp
src/server/core/ndd.cpp

index 290bd24..2057f42 100644 (file)
@@ -165,7 +165,15 @@ BOOL LoadSubAgent(TCHAR *szModuleName)
    {
       // Assume that subagent name without path given
       // Try to load it from pkglibdir
-      _sntprintf(fullName, MAX_PATH, _T("%s/%s"), PKGLIBDIR, szModuleName);
+      const TCHAR *homeDir = _tgetenv(_T("NETXMS_HOME"));
+      if (homeDir != NULL)
+      {
+         _sntprintf(fullName, MAX_PATH, _T("%s/lib/netxms/%s"), homeDir, szModuleName);
+      }
+      else
+      {
+         _sntprintf(fullName, MAX_PATH, _T("%s/%s"), PKGLIBDIR, szModuleName);
+      }
    }
    else
    {
index a4a1823..aee4671 100644 (file)
@@ -102,6 +102,18 @@ BOOL NXCORE_EXPORTABLE LoadConfig()
        }
 #endif
 
+   // Read default values from enviroment
+   const TCHAR *homeDir = _tgetenv(_T("NETXMS_HOME"));
+   if ((homeDir != NULL) && (*homeDir != 0))
+   {
+#ifdef _WIN32
+      _sntprintf(g_szLibDir, MAX_PATH, _T("%s%slib"), homeDir, 
+         (homeDir[_tcslen(homeDir) - 1] != FS_PATH_SEPARATOR_CHAR) ? FS_PATH_SEPARATOR : _T(""));
+#else
+      _sntprintf(g_szLibDir, MAX_PATH, _T("%s/lib/netxms"), homeDir);
+#endif
+   }
+
    if (IsStandalone())
       _tprintf(_T("Using configuration file \"%s\"\n"), g_szConfigFile);
 
index 7d0a862..0d9d2d0 100644 (file)
@@ -29,15 +29,93 @@ TCHAR *g_pszModLoadList = NULL;
 UINT32 g_dwNumModules = 0;
 NXMODULE *g_pModuleList = NULL;
 
+static bool LoadNetXMSModule(const TCHAR *name)
+{
+       bool success = false;
+   TCHAR szErrorText[256];
+
+#if !defined(_WIN32) && !defined(_NETWARE)
+   TCHAR fullName[MAX_PATH];
+
+   if (_tcschr(name, _T('/')) == NULL)
+   {
+      // Assume that module name without path given
+      // Try to load it from pkglibdir
+      const TCHAR *homeDir = _tgetenv(_T("NETXMS_HOME"));
+      if (homeDir != NULL)
+      {
+         _sntprintf(fullName, MAX_PATH, _T("%s/lib/netxms/%s"), homeDir, szModuleName);
+      }
+      else
+      {
+         _sntprintf(fullName, MAX_PATH, _T("%s/%s"), PKGLIBDIR, szModuleName);
+      }
+   }
+   else
+   {
+      nx_strncpy(fullName, name, MAX_PATH);
+   }
+   HMODULE hModule = DLOpen(fullName, szErrorText);
+#else
+   HMODULE hModule = DLOpen(name, szErrorText);
+#endif
+
+   if (hModule != NULL)
+   {
+      BOOL (* ModuleInit)(NXMODULE *);
+
+      ModuleInit = (BOOL (*)(NXMODULE *))DLGetSymbolAddr(hModule, "NetXMSModuleInit", szErrorText);
+      if (ModuleInit != NULL)
+      {
+         NXMODULE module;
+         memset(&module, 0, sizeof(NXMODULE));
+         if (ModuleInit(&module))
+         {
+            if (module.dwSize == sizeof(NXMODULE))
+            {
+               // Add module to module's list
+               g_pModuleList = (NXMODULE *)realloc(g_pModuleList, 
+                                                   sizeof(NXMODULE) * (g_dwNumModules + 1));
+               memcpy(&g_pModuleList[g_dwNumModules], &module, sizeof(NXMODULE));
+               g_pModuleList[g_dwNumModules].hModule = hModule;
+
+               nxlog_write(MSG_MODULE_LOADED, EVENTLOG_INFORMATION_TYPE, "s", g_pModuleList[g_dwNumModules].szName);
+               g_dwNumModules++;
+
+               success = true;
+            }
+            else
+            {
+               nxlog_write(MSG_MODULE_BAD_MAGIC, EVENTLOG_ERROR_TYPE, "s", name);
+               DLClose(hModule);
+            }
+         }
+         else
+         {
+            nxlog_write(MSG_MODULE_INIT_FAILED, EVENTLOG_ERROR_TYPE, "s", name);
+            DLClose(hModule);
+         }
+      }
+      else
+      {
+         nxlog_write(MSG_NO_MODULE_ENTRY_POINT, EVENTLOG_ERROR_TYPE, "s", name);
+         DLClose(hModule);
+      }
+   }
+   else
+   {
+      nxlog_write(MSG_DLOPEN_FAILED, EVENTLOG_ERROR_TYPE, "ss", name, szErrorText);
+   }
+   return success;
+}
+
 /**
  * Load all registered modules
  */
 bool LoadNetXMSModules()
 {
-   TCHAR szErrorText[256], *curr, *next, *ptr;
-   NXMODULE module;
-   HMODULE hModule;
-       bool success = true;
+   TCHAR *curr, *next, *ptr;
+   bool success = true;
 
        for(curr = g_pszModLoadList; curr != NULL; curr = next)
    {
@@ -52,7 +130,6 @@ bool LoadNetXMSModules()
                        continue;
 
                bool mandatory = false;
-      memset(&module, 0, sizeof(NXMODULE));
 
                // Check for "mandatory" option
                ptr = _tcschr(curr, _T(','));
@@ -65,64 +142,8 @@ bool LoadNetXMSModules()
                        mandatory = (*ptr == _T('1')) || (*ptr == _T('Y')) || (*ptr == _T('y'));
                }
 
-      hModule = DLOpen(curr, szErrorText);
-      if (hModule != NULL)
-      {
-         BOOL (* ModuleInit)(NXMODULE *);
-
-         ModuleInit = (BOOL (*)(NXMODULE *))DLGetSymbolAddr(hModule, "NetXMSModuleInit", szErrorText);
-         if (ModuleInit != NULL)
-         {
-            memset(&module, 0, sizeof(NXMODULE));
-            if (ModuleInit(&module))
-            {
-               if (module.dwSize == sizeof(NXMODULE))
-               {
-                  // Add module to module's list
-                  g_pModuleList = (NXMODULE *)realloc(g_pModuleList, 
-                                                      sizeof(NXMODULE) * (g_dwNumModules + 1));
-                  memcpy(&g_pModuleList[g_dwNumModules], &module, sizeof(NXMODULE));
-                  g_pModuleList[g_dwNumModules].hModule = hModule;
-
-                  nxlog_write(MSG_MODULE_LOADED, EVENTLOG_INFORMATION_TYPE, "s", g_pModuleList[g_dwNumModules].szName);
-                  g_dwNumModules++;
-               }
-               else
-               {
-                  nxlog_write(MSG_MODULE_BAD_MAGIC, EVENTLOG_ERROR_TYPE, "s", curr);
-                  DLClose(hModule);
-                                               if (mandatory)
-                                               {
-                                                       success = false;
-                                                       break;
-                                               }
-               }
-            }
-            else
-            {
-               nxlog_write(MSG_MODULE_INIT_FAILED, EVENTLOG_ERROR_TYPE, "s", curr);
-               DLClose(hModule);
-                                       if (mandatory)
-                                       {
-                                               success = false;
-                                               break;
-                                       }
-            }
-         }
-         else
-         {
-            nxlog_write(MSG_NO_MODULE_ENTRY_POINT, EVENTLOG_ERROR_TYPE, "s", curr);
-            DLClose(hModule);
-                               if (mandatory)
-                               {
-                                       success = false;
-                                       break;
-                               }
-         }
-      }
-      else
+      if (!LoadNetXMSModule(curr))
       {
-         nxlog_write(MSG_DLOPEN_FAILED, EVENTLOG_ERROR_TYPE, "ss", curr, szErrorText);
                        if (mandatory)
                        {
                                success = false;
index 819b23c..13ed6b0 100644 (file)
@@ -24,7 +24,6 @@
 
 #define MAX_DEVICE_DRIVERS             1024
 
-
 /**
  * List of loaded drivers
  */