Fixed search path problems.

This commit is contained in:
Gunnar Beutner 2012-04-02 13:09:33 +02:00
parent d386a2cc4c
commit 0e215f112a
7 changed files with 126 additions and 5 deletions

View File

@ -188,7 +188,7 @@ Component::RefType Application::LoadComponent(string path, ConfigObject::RefType
lt_dlhandle hModule = 0; lt_dlhandle hModule = 0;
lt_dladvise advise; 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); 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"*/); throw exception(/*"Could not load module"*/);
#ifdef _WIN32 #ifdef _WIN32
pCreateComponent = (Component *(*)())GetProcAddress(hModule, ); pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
#else /* _WIN32 */ #else /* _WIN32 */
pCreateComponent = (Component *(*)())lt_dlsym(hModule, "CreateComponent"); pCreateComponent = (Component *(*)())lt_dlsym(hModule, "CreateComponent");
#endif /* _WIN32 */ #endif /* _WIN32 */
@ -256,3 +256,92 @@ void Application::Log(const char *format, ...)
fprintf(stderr, "%s\n", message); 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 */
}

View File

@ -10,6 +10,7 @@ private:
bool m_ShuttingDown; bool m_ShuttingDown;
ConfigHive::RefType m_ConfigHive; ConfigHive::RefType m_ConfigHive;
map< string, shared_ptr<Component> > m_Components; map< string, shared_ptr<Component> > m_Components;
vector<string> m_Arguments;
public: public:
typedef shared_ptr<Application> RefType; typedef shared_ptr<Application> RefType;
@ -22,6 +23,8 @@ public:
virtual int Main(const vector<string>& args) = 0; virtual int Main(const vector<string>& args) = 0;
vector<string>& GetArguments(void);
void RunEventLoop(void); void RunEventLoop(void);
bool Daemonize(void); bool Daemonize(void);
void Shutdown(void); void Shutdown(void);
@ -33,6 +36,9 @@ public:
shared_ptr<Component> LoadComponent(string path, ConfigObject::RefType componentConfig); shared_ptr<Component> LoadComponent(string path, ConfigObject::RefType componentConfig);
void UnloadComponent(string name); void UnloadComponent(string name);
shared_ptr<Component> GetComponent(string name); shared_ptr<Component> GetComponent(string name);
void AddComponentSearchDir(string componentDirectory);
string GetExeDirectory(void);
}; };
template<class T> template<class T>

View File

@ -94,7 +94,7 @@
<AdditionalDependencies>ws2_32.lib;imagehlp.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>ws2_32.lib;imagehlp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
<Lib> <Lib>
<AdditionalDependencies>ws2_32.lib</AdditionalDependencies> <AdditionalDependencies>ws2_32.lib;shlwapi.lib</AdditionalDependencies>
</Lib> </Lib>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -114,7 +114,7 @@
<AdditionalDependencies>ws2_32.lib;imagehlp.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>ws2_32.lib;imagehlp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
<Lib> <Lib>
<AdditionalDependencies>ws2_32.lib</AdditionalDependencies> <AdditionalDependencies>ws2_32.lib;shlwapi.lib</AdditionalDependencies>
</Lib> </Lib>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -19,6 +19,12 @@ void closesocket(SOCKET fd);
#define ioctlsocket ioctl #define ioctlsocket ioctl
#ifndef PATH_MAX
# define PATH_MAX 1024
#endif /* PATH_MAX */
#define MAXPATHLEN PATH_MAX
/* default visibility takes care of exported symbols */ /* default visibility takes care of exported symbols */
#define I2_EXPORT #define I2_EXPORT
#define I2_IMPORT #define I2_IMPORT

View File

@ -4,9 +4,12 @@
#define NOGDI #define NOGDI
#include <windows.h> #include <windows.h>
#include <imagehlp.h> #include <imagehlp.h>
#include <shlwapi.h>
typedef int socklen_t; typedef int socklen_t;
#define MAXPATHLEN MAX_PATH
#define I2_EXPORT __declspec(dllexport) #define I2_EXPORT __declspec(dllexport)
#define I2_IMPORT __declspec(dllimport) #define I2_IMPORT __declspec(dllimport)

View File

@ -22,6 +22,16 @@ int IcingaApplication::Main(const vector<string>& args)
cout << "Icinga component loader (version: " << ICINGA_VERSION << ")" << endl; cout << "Icinga component loader (version: " << ICINGA_VERSION << ")" << endl;
#endif /* _WIN32 */ #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()->OnObjectCreated.bind(bind_weak(&IcingaApplication::ConfigObjectCreatedHandler, shared_from_this()));
GetConfigHive()->OnObjectRemoved.bind(bind_weak(&IcingaApplication::ConfigObjectRemovedHandler, 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(); RunEventLoop();
return 0; return EXIT_SUCCESS;
}
void IcingaApplication::PrintUsage(const string& programPath)
{
cout << "Syntax: " << programPath << " <config-file>" << endl;
} }
ConnectionManager::RefType IcingaApplication::GetConnectionManager(void) ConnectionManager::RefType IcingaApplication::GetConnectionManager(void)

View File

@ -20,6 +20,8 @@ public:
virtual int Main(const vector<string>& args); virtual int Main(const vector<string>& args);
void PrintUsage(const string& programPath);
virtual ConnectionManager::RefType GetConnectionManager(void); virtual ConnectionManager::RefType GetConnectionManager(void);
}; };