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);
}
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 */
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 */
+}
bool m_ShuttingDown;
ConfigHive::RefType m_ConfigHive;
map< string, shared_ptr<Component> > m_Components;
+ vector<string> m_Arguments;
public:
typedef shared_ptr<Application> RefType;
virtual int Main(const vector<string>& args) = 0;
+ vector<string>& GetArguments(void);
+
void RunEventLoop(void);
bool Daemonize(void);
void Shutdown(void);
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>
<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" />
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()));
RunEventLoop();
- return 0;
+ return EXIT_SUCCESS;
+}
+
+void IcingaApplication::PrintUsage(const string& programPath)
+{
+ cout << "Syntax: " << programPath << " <config-file>" << endl;
}
ConnectionManager::RefType IcingaApplication::GetConnectionManager(void)