]> granicus.if.org Git - icinga2/commitdiff
Fixed search path problems.
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 2 Apr 2012 11:09:33 +0000 (13:09 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 2 Apr 2012 11:09:33 +0000 (13:09 +0200)
base/application.cpp
base/application.h
base/base.vcxproj
base/unix.h
base/win32.h
icinga/icingaapplication.cpp
icinga/icingaapplication.h

index 9c1c5e3643bd59cce8e54e94acec9b94a8c750ee..37c226728b07ff5bc909ac61ec2808373998f78d 100644 (file)
@@ -188,7 +188,7 @@ Component::RefType Application::LoadComponent(string path, ConfigObject::RefType
        lt_dlhandle hModule = 0;
        lt_dladvise advise;
 
-       if (!lt_dladvise_init(&advise) && !lt_dladvise_global(&advise)) {
+       if (!lt_dladvise_init(&advise) && !lt_dladvise_local(&advise)) {
                hModule = lt_dlopenadvise(path.c_str(), advise);
        }
 
@@ -199,7 +199,7 @@ Component::RefType Application::LoadComponent(string path, ConfigObject::RefType
                throw exception(/*"Could not load module"*/);
 
 #ifdef _WIN32
-       pCreateComponent = (Component *(*)())GetProcAddress(hModule, );
+       pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
 #else /* _WIN32 */
        pCreateComponent = (Component *(*)())lt_dlsym(hModule, "CreateComponent");
 #endif /* _WIN32 */
@@ -256,3 +256,92 @@ void Application::Log(const char *format, ...)
        fprintf(stderr, "%s\n", message);
 }
 
+vector<string>& Application::GetArguments(void)
+{
+       return m_Arguments;
+}
+
+string Application::GetExeDirectory(void)
+{
+       static string ExePath;
+
+       if (ExePath.length() != 0)
+               return ExePath;
+
+#ifndef _WIN32
+       char Buf[MAXPATHLEN], Cwd[MAXPATHLEN];
+       char *PathEnv, *Directory, PathTest[MAXPATHLEN], FullExePath[MAXPATHLEN];
+       bool FoundPath;
+
+       const char *argv0 = m_Arguments[0].c_str();
+
+       if (getcwd(Cwd, sizeof(Cwd)) == NULL)
+               throw exception(/*"getcwd() failed"*/);
+
+       if (argv0[0] != '/')
+               snprintf(FullExePath, sizeof(FullExePath), "%s/%s", Cwd, argv0);
+       else
+               strncpy(FullExePath, argv0, sizeof(FullExePath));
+
+       if (strchr(argv0, '/') == NULL) {
+               PathEnv = getenv("PATH");
+
+               if (PathEnv != NULL) {
+                       PathEnv = strdup(PathEnv);
+
+                       if (PathEnv == NULL)
+                               throw exception(/*"strdup() failed"*/);
+
+                       FoundPath = false;
+
+                       for (Directory = strtok(PathEnv, ":"); Directory != NULL; Directory = strtok(NULL, ":")) {
+                               if (snprintf(PathTest, sizeof(PathTest), "%s/%s", Directory, argv0) < 0)
+                                       throw exception(/*"snprintf() failed"*/);
+
+                               if (access(PathTest, X_OK) == 0) {
+                                       strncpy(FullExePath, PathTest, sizeof(FullExePath));
+
+                                       FoundPath = true;
+
+                                       break;
+                               }
+                       }
+
+                       free(PathEnv);
+
+                       if (!FoundPath)
+                               throw exception(/*"Could not determine executable path."*/);
+               }
+       }
+
+       if (realpath(FullExePath, Buf) == NULL)
+               throw exception(/*"realpath() failed"*/);
+
+       // remove filename
+       char *LastSlash = strrchr(Buf, '/');
+
+       if (LastSlash != NULL)
+               *LastSlash = '\0';
+
+       ExePath = string(Buf);
+#else /* _WIN32 */
+       char FullExePath[MAXPATHLEN];
+
+       GetModuleFileName(NULL, FullExePath, MAXPATHLEN);
+
+       PathRemoveFileSpec(FullExePath);
+
+       ExePath = string(FullExePath);
+#endif /* _WIN32 */
+
+       return ExePath;
+}
+
+void Application::AddComponentSearchDir(string componentDirectory)
+{
+#ifdef _WIN32
+       SetDllDirectory(componentDirectory.c_str());
+#else /* _WIN32 */
+       lt_dladdsearchdir(componentDirectory.c_str());
+#endif /* _WIN32 */
+}
index 59dfda905865673ac4cdcf7dc82ea8139fbb5365..212730b65b0f228ed2a4fc24a2a4bc318d8b0208 100644 (file)
@@ -10,6 +10,7 @@ private:
        bool m_ShuttingDown;
        ConfigHive::RefType m_ConfigHive;
        map< string, shared_ptr<Component> > m_Components;
+       vector<string> m_Arguments;
 
 public:
        typedef shared_ptr<Application> RefType;
@@ -22,6 +23,8 @@ public:
 
        virtual int Main(const vector<string>& args) = 0;
 
+       vector<string>& GetArguments(void);
+
        void RunEventLoop(void);
        bool Daemonize(void);
        void Shutdown(void);
@@ -33,6 +36,9 @@ public:
        shared_ptr<Component> LoadComponent(string path, ConfigObject::RefType componentConfig);
        void UnloadComponent(string name);
        shared_ptr<Component> GetComponent(string name);
+       void AddComponentSearchDir(string componentDirectory);
+
+       string GetExeDirectory(void);
 };
 
 template<class T>
index 389883f4576b2d8550aa07d6e3b044a2ddfc3a26..3802c4fa3ce858223d4f23666a697b8a004d258e 100644 (file)
@@ -94,7 +94,7 @@
       <AdditionalDependencies>ws2_32.lib;imagehlp.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Lib>
-      <AdditionalDependencies>ws2_32.lib</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;shlwapi.lib</AdditionalDependencies>
     </Lib>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
       <AdditionalDependencies>ws2_32.lib;imagehlp.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Lib>
-      <AdditionalDependencies>ws2_32.lib</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;shlwapi.lib</AdditionalDependencies>
     </Lib>
   </ItemDefinitionGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
index de9892be31b13871966cfe0b5c7047a84ac6de5c..9876ab5cffba09ab7bd7da853817758de0d224ff 100644 (file)
@@ -19,6 +19,12 @@ void closesocket(SOCKET fd);
 
 #define ioctlsocket ioctl
 
+#ifndef PATH_MAX
+#      define PATH_MAX 1024
+#endif /* PATH_MAX */
+
+#define MAXPATHLEN PATH_MAX
+
 /* default visibility takes care of exported symbols */
 #define I2_EXPORT
 #define I2_IMPORT
index 44b9fc86fae201e113d36e35383985bc4b8c2c6d..fe6d5ba6f8e2acbff2609197becd345f66521bb2 100644 (file)
@@ -4,9 +4,12 @@
 #define NOGDI
 #include <windows.h>
 #include <imagehlp.h>
+#include <shlwapi.h>
 
 typedef int socklen_t;
 
+#define MAXPATHLEN MAX_PATH
+
 #define I2_EXPORT __declspec(dllexport)
 #define I2_IMPORT __declspec(dllimport)
 
index b7144ca10ed8e850cf02e53ebca1bdb32e9c2754..cdeefa9304f95869d1d7eb8b46174bbaf09ef6eb 100644 (file)
@@ -22,6 +22,16 @@ int IcingaApplication::Main(const vector<string>& args)
        cout << "Icinga component loader (version: " << ICINGA_VERSION << ")" << endl;
 #endif  /* _WIN32 */
 
+       if (args.size() < 2) {
+               PrintUsage(args[0]);
+               return EXIT_FAILURE;
+       }
+
+#ifndef _WIN32
+       string componentDirectory = GetExeDirectory() + "../lib/icinga";
+       AddComponentSearchDir(componentDirectory);
+#endif /* _WIN32 */
+
        GetConfigHive()->OnObjectCreated.bind(bind_weak(&IcingaApplication::ConfigObjectCreatedHandler, shared_from_this()));
        GetConfigHive()->OnObjectRemoved.bind(bind_weak(&IcingaApplication::ConfigObjectRemovedHandler, shared_from_this()));
 
@@ -33,7 +43,12 @@ int IcingaApplication::Main(const vector<string>& args)
 
        RunEventLoop();
 
-       return 0;
+       return EXIT_SUCCESS;
+}
+
+void IcingaApplication::PrintUsage(const string& programPath)
+{
+       cout << "Syntax: " << programPath << " <config-file>" << endl;
 }
 
 ConnectionManager::RefType IcingaApplication::GetConnectionManager(void)
index f7f92d0a4467905cb1045796271d5e0658e34988..3c51bcfee0bad45a47fe7fce5c380742334b5ff8 100644 (file)
@@ -20,6 +20,8 @@ public:
 
        virtual int Main(const vector<string>& args);
 
+       void PrintUsage(const string& programPath);
+
        virtual ConnectionManager::RefType GetConnectionManager(void);
 };