Implement injectLibrary() function
authorAlexey Vereschagin <avereschagin@dev.rtsoft.ru>
Wed, 12 Sep 2018 23:35:27 +0000 (02:35 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Tue, 25 Sep 2018 17:16:14 +0000 (20:16 +0300)
NativeLauncher/launcher/injection.cc

index 6aeaa61..31134f6 100644 (file)
 #include <cstdlib>
 #include <cstring>
 
+#include <dlfcn.h>
+
 static int injectLibrary(const char path[])
 {
-       return 0;
+       typedef int inject_func();
+
+       int res = -1;
+       void *lib = nullptr;
+       inject_func *inject = nullptr;
+       const char *inject_sym = "dotnet_launcher_inject";
+
+       _INFO("Inject %s library", path);
+
+       // FIXME: remove RTLD_GLOBAL?
+       lib = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+       if (lib == nullptr) {
+               _ERR("%s", dlerror());
+               goto ret;
+       }
+
+       inject = reinterpret_cast<inject_func*>(dlsym(lib, inject_sym));
+       if (inject == nullptr) {
+               _ERR("%s is not found in the %s", inject_sym, path);
+               goto ret;
+       }
+
+       res = inject();
+
+ret:
+       return res;
 }
 
 int doInjection()