thorplugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jexcept.hpp"
  14. #include "jmisc.hpp"
  15. #include "jthread.hpp"
  16. #include "jsocket.hpp"
  17. #include "jprop.hpp"
  18. #include "jdebug.hpp"
  19. #include "jlzw.hpp"
  20. #include "eclrtl.hpp"
  21. #if defined(__APPLE__)
  22. #include <mach-o/getsect.h>
  23. #include <sys/mman.h>
  24. #include <sys/stat.h>
  25. #elif !defined(_WIN32)
  26. #include <sys/mman.h>
  27. #include <sys/stat.h>
  28. #include <elf.h>
  29. #endif
  30. #include "thorplugin.hpp"
  31. void * SimplePluginCtx::ctxMalloc(size_t size)
  32. {
  33. return rtlMalloc(size);
  34. }
  35. void * SimplePluginCtx::ctxRealloc(void * _ptr, size_t size)
  36. {
  37. return rtlRealloc(_ptr, size);
  38. }
  39. void SimplePluginCtx::ctxFree(void * _ptr)
  40. {
  41. rtlFree(_ptr);
  42. }
  43. char * SimplePluginCtx::ctxStrdup(char * _ptr)
  44. {
  45. return strdup(_ptr);
  46. }
  47. int SimplePluginCtx::ctxGetPropInt(const char *propName, int defaultValue) const
  48. {
  49. return defaultValue;
  50. }
  51. const char * SimplePluginCtx::ctxQueryProp(const char *propName) const
  52. {
  53. return NULL;
  54. }
  55. //-------------------------------------------------------------------------------------------------------------------
  56. static bool getResourceFromMappedFile(const char * filename, const byte * start_addr, size32_t & lenData, const void * & data, const char * type, unsigned id)
  57. {
  58. #if defined(_WIN32)
  59. throwUnexpected();
  60. #elif defined(__APPLE__)
  61. VStringBuffer sectname("%s_%u", type, id);
  62. // The first bytes are the Mach-O header
  63. const struct mach_header_64 *mh = (const struct mach_header_64 *) start_addr;
  64. if (mh->magic != MH_MAGIC_64)
  65. {
  66. DBGLOG("Failed to extract resource %s: Does not appear to be a Mach-O 64-bit binary", filename);
  67. return false;
  68. }
  69. unsigned long len = 0;
  70. data = getsectiondata(mh, "__TEXT", sectname.str(), &len);
  71. lenData = (size32_t)len;
  72. return true;
  73. #elif defined (__64BIT__)
  74. // The first bytes are the ELF header
  75. const Elf64_Ehdr * hdr = (const Elf64_Ehdr *) start_addr;
  76. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
  77. {
  78. DBGLOG("Failed to extract resource %s: Does not appear to be a ELF binary", filename);
  79. return false;
  80. }
  81. if (hdr->e_ident[EI_CLASS] != ELFCLASS64)
  82. {
  83. DBGLOG("Failed to extract resource %s: Does not appear to be a ELF 64-bit binary", filename);
  84. return false;
  85. }
  86. //Check that there is a symbol table for the sections.
  87. if (hdr->e_shstrndx == SHN_UNDEF)
  88. {
  89. DBGLOG("Failed to extract resource %s: Does not include a section symbol table", filename);
  90. return false;
  91. }
  92. //Now walk the sections comparing the section names
  93. Elf64_Half numSections = hdr->e_shnum;
  94. const Elf64_Shdr * sectionHeaders = reinterpret_cast<const Elf64_Shdr *>(start_addr + hdr->e_shoff);
  95. const Elf64_Shdr & symbolTableSection = sectionHeaders[hdr->e_shstrndx];
  96. const char * symbolTable = (const char *)start_addr + symbolTableSection.sh_offset;
  97. VStringBuffer sectname("%s_%u", type, id);
  98. for (unsigned iSect= 0; iSect < numSections; iSect++)
  99. {
  100. const Elf64_Shdr & section = sectionHeaders[iSect];
  101. const char * sectionName = symbolTable + section.sh_name;
  102. if (streq(sectionName, sectname))
  103. {
  104. lenData = (size32_t)section.sh_size;
  105. data = start_addr + section.sh_offset;
  106. return true;
  107. }
  108. }
  109. DBGLOG("Failed to extract resource %s: Does not include a matching entry", filename);
  110. return false;
  111. #else
  112. // The first bytes are the ELF header
  113. const Elf32_Ehdr * hdr = (const Elf32_Ehdr *) start_addr;
  114. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
  115. {
  116. DBGLOG("Failed to extract resource %s: Does not appear to be a ELF binary", filename);
  117. return false;
  118. }
  119. if (hdr->e_ident[EI_CLASS] != ELFCLASS32)
  120. {
  121. DBGLOG("Failed to extract resource %s: Does not appear to be a ELF 32-bit binary", filename);
  122. return false;
  123. }
  124. //Check that there is a symbol table for the sections.
  125. if (hdr->e_shstrndx == SHN_UNDEF)
  126. {
  127. DBGLOG("Failed to extract resource %s: Does not include a section symbol table", filename);
  128. return false;
  129. }
  130. //Now walk the sections comparing the section names
  131. Elf32_Half numSections = hdr->e_shnum;
  132. const Elf32_Shdr * sectionHeaders = reinterpret_cast<const Elf32_Shdr *>(start_addr + hdr->e_shoff);
  133. const Elf32_Shdr & symbolTableSection = sectionHeaders[hdr->e_shstrndx];
  134. const char * symbolTable = (const char *)start_addr + symbolTableSection.sh_offset;
  135. VStringBuffer sectname("%s_%u", type, id);
  136. for (unsigned iSect= 0; iSect < numSections; iSect++)
  137. {
  138. const Elf32_Shdr & section = sectionHeaders[iSect];
  139. const char * sectionName = symbolTable + section.sh_name;
  140. if (streq(sectionName, sectname))
  141. {
  142. lenData = (size32_t)section.sh_size;
  143. data = start_addr + section.sh_offset;
  144. return true;
  145. }
  146. }
  147. DBGLOG("Failed to extract resource %s: Does not include a matching entry", filename);
  148. return false;
  149. #endif
  150. }
  151. static bool getResourceFromMappedFile(const char * filename, const byte * start_addr, MemoryBuffer & result, const char * type, unsigned id)
  152. {
  153. size32_t len = 0;
  154. const void * data = nullptr;
  155. bool ok = getResourceFromMappedFile(filename, start_addr, len, data, type, id);
  156. if (ok)
  157. result.append(len, data);
  158. return ok;
  159. }
  160. extern bool getResourceFromFile(const char *filename, MemoryBuffer &data, const char * type, unsigned id)
  161. {
  162. #ifdef _WIN32
  163. HINSTANCE dllHandle = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE|LOAD_LIBRARY_AS_IMAGE_RESOURCE);
  164. if (dllHandle == NULL)
  165. dllHandle = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE); // the LOAD_LIBRARY_AS_IMAGE_RESOURCE flag is not supported on all versions of Windows
  166. if (dllHandle == NULL)
  167. {
  168. DBGLOG("Failed to load library %s: %d", filename, GetLastError());
  169. return false;
  170. }
  171. HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), type);
  172. if (!hrsrc)
  173. return false;
  174. size32_t len = SizeofResource(dllHandle, hrsrc);
  175. const void *rdata = (const void *) LoadResource(dllHandle, hrsrc);
  176. data.append(len, rdata);
  177. FreeLibrary(dllHandle);
  178. return true;
  179. #else
  180. struct stat stat_buf;
  181. VStringBuffer sectname("%s_%u", type, id);
  182. int fd = open(filename, O_RDONLY);
  183. if (fd == -1)
  184. {
  185. DBGLOG("Failed to load library %s: %d", filename, errno);
  186. return false;
  187. }
  188. bool ok = false;
  189. if (fstat(fd, &stat_buf) != -1)
  190. {
  191. __uint64 size = stat_buf.st_size;
  192. const byte *start_addr = (const byte *) mmap(0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
  193. if (start_addr == MAP_FAILED)
  194. {
  195. DBGLOG("Failed to load library %s: %d", filename, errno);
  196. }
  197. else
  198. {
  199. ok = getResourceFromMappedFile(filename, start_addr, data, type, id);
  200. munmap((void *)start_addr, size);
  201. }
  202. }
  203. else
  204. DBGLOG("Failed to load library %s: %d", filename, errno);
  205. close(fd);
  206. return ok;
  207. #endif
  208. }
  209. //-------------------------------------------------------------------------------------------------------------------
  210. class HelperDll : implements ILoadedDllEntry, public CInterface
  211. {
  212. SharedObject so;
  213. StringAttr name;
  214. Linked<const IFileIO> dllFile;
  215. Owned<IMemoryMappedFile> mappedDll;
  216. bool logLoad;
  217. public:
  218. IMPLEMENT_IINTERFACE;
  219. HelperDll(const char *_name, const IFileIO *dllFile);
  220. ~HelperDll();
  221. //interface ILoadedDllEntry
  222. virtual HINSTANCE getInstance() const;
  223. virtual void * getEntry(const char * name) const;
  224. virtual bool IsShared();
  225. virtual const char * queryVersion() const;
  226. virtual const char * queryName() const;
  227. virtual const byte * getResource(unsigned id) const;
  228. virtual bool getResource(size32_t & len, const void * & data, const char * type, unsigned id, bool trace) const;
  229. bool load(bool isGlobal, bool raiseOnError);
  230. bool loadCurrentExecutable();
  231. bool loadResources();
  232. virtual void logLoaded();
  233. virtual bool checkVersion(const char *expected);
  234. };
  235. class PluginDll : public HelperDll
  236. {
  237. ECLPluginDefinitionBlockEx pb;
  238. public:
  239. PluginDll(const char *_name, const IFileIO *_dllFile) : HelperDll(_name, _dllFile) {}
  240. bool init(IPluginContextEx * pluginCtx);
  241. virtual bool checkVersion(const char *expected);
  242. virtual void logLoaded();
  243. };
  244. HelperDll::HelperDll(const char *_name, const IFileIO *_dllFile)
  245. : name(_name), dllFile(_dllFile)
  246. {
  247. logLoad = false;
  248. }
  249. bool HelperDll::load(bool isGlobal, bool raiseOnError)
  250. {
  251. if (!so.load(name, isGlobal, raiseOnError))
  252. return false;
  253. return true;
  254. }
  255. bool HelperDll::loadResources()
  256. {
  257. #ifdef _WIN32
  258. return so.loadResources(name);
  259. #else
  260. Owned<IFile> file = createIFile(name);
  261. mappedDll.setown(file->openMemoryMapped());
  262. return mappedDll != nullptr;
  263. #endif
  264. }
  265. bool HelperDll::loadCurrentExecutable()
  266. {
  267. if (!so.loadCurrentExecutable())
  268. return false;
  269. return true;
  270. }
  271. HelperDll::~HelperDll()
  272. {
  273. if (logLoad)
  274. DBGLOG("Unloading dll %s", name.get());
  275. }
  276. HINSTANCE HelperDll::getInstance() const
  277. {
  278. if (!so.loaded())
  279. throw MakeStringException(0, "Dll %s only loaded for resources", name.str());
  280. return so.getInstanceHandle();
  281. }
  282. void * HelperDll::getEntry(const char * entry) const
  283. {
  284. if (!so.loaded())
  285. throw MakeStringException(0, "Dll %s only loaded for resources", name.str());
  286. return so.getEntry(entry);
  287. }
  288. bool HelperDll::IsShared()
  289. {
  290. return CInterface::IsShared();
  291. }
  292. const char * HelperDll::queryVersion() const
  293. {
  294. return "";
  295. }
  296. void HelperDll::logLoaded()
  297. {
  298. logLoad = true;
  299. DBGLOG("Loaded DLL %s", name.get());
  300. }
  301. bool HelperDll::checkVersion(const char *expected)
  302. {
  303. return true;
  304. }
  305. const char * HelperDll::queryName() const
  306. {
  307. return name.get();
  308. }
  309. const byte * HelperDll::getResource(unsigned id) const
  310. {
  311. if (so.loaded())
  312. {
  313. #ifdef _WIN32
  314. HINSTANCE dllHandle = so.getInstanceHandle();
  315. HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), "BIGSTRING");
  316. if (hrsrc)
  317. return (const byte *)LoadResource(dllHandle, hrsrc);
  318. return NULL;
  319. #else
  320. StringBuffer resourceName;
  321. resourceName.appendf("BIGSTRING_%d_txt_start", id);
  322. return (const byte *)getEntry(resourceName.str());
  323. #endif
  324. }
  325. else
  326. {
  327. size32_t len;
  328. const void * data;
  329. if (getResource(len, data, "BIGSTRING", id, false))
  330. return (const byte *)data;
  331. return nullptr;
  332. }
  333. }
  334. const byte resourceHeaderVersion=1;
  335. const size32_t resourceHeaderLength = sizeof(byte) + sizeof(byte) + sizeof(bool) + sizeof(size32_t);
  336. bool HelperDll::getResource(size32_t & len, const void * & data, const char * type, unsigned id, bool trace) const
  337. {
  338. if (so.loaded())
  339. {
  340. #ifdef _WIN32
  341. HINSTANCE dllHandle = so.getInstanceHandle();
  342. HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), type);
  343. if (!hrsrc)
  344. return false;
  345. len = SizeofResource(dllHandle, hrsrc);
  346. data = (const byte *)LoadResource(dllHandle, hrsrc);
  347. return true;
  348. #else
  349. StringBuffer symName;
  350. symName.append(type).append("_").append(id).append("_txt_start");
  351. data = (const void *)getEntry(symName.str());
  352. if (!data)
  353. {
  354. if (trace)
  355. printf("Failed to locate symbol %s\n", symName.str());
  356. return false;
  357. }
  358. byte bom;
  359. byte version;
  360. bool compressed;
  361. MemoryBuffer mb;
  362. mb.setBuffer(resourceHeaderLength, const_cast<void *>(data));
  363. mb.read(bom);
  364. if (bom != 0x80)
  365. return false;
  366. mb.read(version);
  367. if (version > resourceHeaderVersion)
  368. return false;
  369. mb.read(compressed).read(len);
  370. len += resourceHeaderLength;
  371. return true;
  372. #endif
  373. }
  374. else
  375. {
  376. #ifdef _WIN32
  377. return false;
  378. #endif
  379. if (!mappedDll)
  380. return false;
  381. return getResourceFromMappedFile(name, mappedDll->base(), len, data, type, id);
  382. }
  383. }
  384. //-------------------------------------------------------------------------------------------------------------------
  385. bool PluginDll::init(IPluginContextEx * pluginCtx)
  386. {
  387. HINSTANCE h = getInstance();
  388. assertex(h != (HINSTANCE) -1);
  389. EclPluginSetCtxEx pSetCtxEx = (EclPluginSetCtxEx) GetSharedProcedure(h,"setPluginContextEx");
  390. if (pSetCtxEx)
  391. pSetCtxEx(pluginCtx);
  392. else
  393. {
  394. // Older plugins may only support setPluginContext - fall back to that
  395. EclPluginSetCtx pSetCtx = (EclPluginSetCtx) GetSharedProcedure(h,"setPluginContext");
  396. if (pSetCtx)
  397. pSetCtx(pluginCtx);
  398. }
  399. EclPluginDefinition p= (EclPluginDefinition) GetSharedProcedure(h,"getECLPluginDefinition");
  400. if (!p)
  401. return false;
  402. pb.size = sizeof(ECLPluginDefinitionBlockEx);
  403. if (!p(&pb))
  404. {
  405. pb.compatibleVersions = NULL;
  406. pb.size = sizeof(ECLPluginDefinitionBlock);
  407. if (!p(&pb))
  408. return false;
  409. }
  410. return true;
  411. }
  412. bool PluginDll::checkVersion(const char *expected)
  413. {
  414. assertex(expected);
  415. if (stricmp(pb.version, expected) == 0)
  416. return true;
  417. if (pb.compatibleVersions)
  418. {
  419. const char **finger = pb.compatibleVersions;
  420. while (*finger)
  421. {
  422. if (stricmp(*finger, expected) == 0)
  423. return true;
  424. finger++;
  425. }
  426. }
  427. return false;
  428. }
  429. void PluginDll::logLoaded()
  430. {
  431. HelperDll::logLoaded();
  432. DBGLOG("Current reported version is %s", pb.version);
  433. if (pb.compatibleVersions)
  434. {
  435. const char **finger = pb.compatibleVersions;
  436. while (*finger)
  437. {
  438. DBGLOG("Compatible version %s", *finger);
  439. finger++;
  440. }
  441. }
  442. }
  443. extern DLLSERVER_API ILoadedDllEntry * createDllEntry(const char *path, bool isGlobal, const IFileIO *dllFile, bool resourcesOnly)
  444. {
  445. Owned<HelperDll> result = new HelperDll(path, dllFile);
  446. bool ok;
  447. if (!resourcesOnly)
  448. ok = result->load(isGlobal, true);
  449. else
  450. ok = result->loadResources();
  451. if (!ok)
  452. throw MakeStringException(0, "Failed to create ILoadedDllEntry for dll %s", path);
  453. return result.getClear();
  454. }
  455. extern DLLSERVER_API ILoadedDllEntry * createExeDllEntry(const char *path)
  456. {
  457. Owned<HelperDll> result = new HelperDll(path, NULL);
  458. if (!result->loadCurrentExecutable())
  459. throw MakeStringException(0, "Failed to create ILoadedDllEntry for current executable");
  460. return result.getClear();
  461. }
  462. extern DLLSERVER_API bool decompressResource(size32_t len, const void *data, MemoryBuffer &result)
  463. {
  464. bool hasVersion = len && (*(const byte *)data == 0x80);
  465. MemoryBuffer src;
  466. src.setBuffer(len, const_cast<void *>(data), false);
  467. byte version = 1;
  468. if (hasVersion)
  469. {
  470. src.skip(1);
  471. src.read(version);
  472. }
  473. switch (version)
  474. {
  475. case 1:
  476. decompressToBuffer(result, src);
  477. break;
  478. default:
  479. throwUnexpected();
  480. }
  481. return true;
  482. }
  483. extern DLLSERVER_API bool decompressResource(size32_t len, const void *data, StringBuffer &result)
  484. {
  485. MemoryBuffer tgt;
  486. decompressResource(len, data, tgt);
  487. tgt.append((char)0);
  488. unsigned expandedLen = tgt.length();
  489. result.setBuffer(expandedLen, reinterpret_cast<char *>(tgt.detach()), expandedLen-1);
  490. return true;
  491. }
  492. extern DLLSERVER_API void appendResource(MemoryBuffer & mb, size32_t len, const void *data, bool compress)
  493. {
  494. mb.append((byte)0x80).append(resourceHeaderVersion);
  495. if (compress)
  496. compressToBuffer(mb, len, data);
  497. else
  498. appendToBuffer(mb, len, data);
  499. }
  500. extern DLLSERVER_API void compressResource(MemoryBuffer & compressed, size32_t len, const void *data)
  501. {
  502. appendResource(compressed, len, data, true);
  503. }
  504. extern DLLSERVER_API bool getEmbeddedWorkUnitXML(ILoadedDllEntry *dll, StringBuffer &xml)
  505. {
  506. size32_t len = 0;
  507. const void * data = NULL;
  508. if (!dll->getResource(len, data, "WORKUNIT", 1000))
  509. return false;
  510. return decompressResource(len, data, xml);
  511. }
  512. extern DLLSERVER_API bool getEmbeddedManifestXML(ILoadedDllEntry *dll, StringBuffer &xml)
  513. {
  514. size32_t len = 0;
  515. const void * data = NULL;
  516. if (!dll->getResource(len, data, "MANIFEST", 1000))
  517. return false;
  518. return decompressResource(len, data, xml);
  519. }
  520. extern DLLSERVER_API bool checkEmbeddedWorkUnitXML(ILoadedDllEntry *dll)
  521. {
  522. size32_t len = 0;
  523. const void * data = NULL;
  524. return dll->getResource(len, data, "WORKUNIT", 1000, false);
  525. }
  526. extern DLLSERVER_API bool getResourceXMLFromFile(const char *filename, const char *type, unsigned id, StringBuffer &xml)
  527. {
  528. MemoryBuffer data;
  529. if (!getResourceFromFile(filename, data, type, id))
  530. return false;
  531. return decompressResource(data.length(), data.toByteArray(), xml);
  532. }
  533. extern DLLSERVER_API bool getWorkunitXMLFromFile(const char *filename, StringBuffer &xml)
  534. {
  535. return getResourceXMLFromFile(filename, "WORKUNIT", 1000, xml);
  536. }
  537. extern DLLSERVER_API bool getManifestXMLFromFile(const char *filename, StringBuffer &xml)
  538. {
  539. return getResourceXMLFromFile(filename, "MANIFEST", 1000, xml);
  540. }
  541. //-------------------------------------------------------------------------------------------------------------------
  542. //-------------------------------------------------------------------------------------------------------------------
  543. bool SafePluginMap::addPlugin(const char *path, const char *dllname)
  544. {
  545. if (!endsWithIgnoreCase(path, SharedObjectExtension))
  546. {
  547. if (trace)
  548. DBGLOG("Ecl plugin %s ignored", path);
  549. return false;
  550. }
  551. try
  552. {
  553. CriticalBlock b(crit);
  554. ILoadedDllEntry *dll = map.getValue(dllname);
  555. if (!dll)
  556. {
  557. Owned<PluginDll> n = new PluginDll(path, NULL);
  558. // Note - we used to load plugins with global=true, but that caused issues when loading
  559. // Python3 and Python2 plugins at the same time as the export similar symbols
  560. // Loading with global=false should not cause any adverse issues
  561. if (!n->load(false, false) || !n->init(pluginCtx))
  562. throw MakeStringException(0, "Failed to load plugin %s", path);
  563. if (trace)
  564. n->logLoaded();
  565. map.setValue(dllname, n); // note: setValue links arg
  566. return true;
  567. }
  568. return false;
  569. }
  570. catch (IException * e) // MORE - not sure why we don't throw exceptions back here...
  571. {
  572. EXCLOG(e, "Loading plugin");
  573. e->Release();
  574. return false;
  575. }
  576. }
  577. ILoadedDllEntry * SafePluginMap::getPluginDll(const char *id, const char *version, bool checkVersion)
  578. {
  579. CriticalBlock b(crit);
  580. Linked<PluginDll> ret = static_cast<PluginDll *>(map.getValue(id));
  581. if (ret && checkVersion)
  582. {
  583. if (!ret->checkVersion(version))
  584. return NULL;
  585. }
  586. return ret.getLink();
  587. }
  588. void SafePluginMap::loadFromList(const char * pluginsList)
  589. {
  590. const char *pluginDir = pluginsList;
  591. for (;*pluginDir;)
  592. {
  593. StringBuffer thisPlugin;
  594. while (*pluginDir && *pluginDir != ENVSEPCHAR)
  595. thisPlugin.append(*pluginDir++);
  596. if(*pluginDir)
  597. pluginDir++;
  598. if(!thisPlugin.length())
  599. continue;
  600. Owned<IFile> file = createIFile(thisPlugin.str());
  601. if (file->isDirectory() == foundYes)
  602. loadFromDirectory(thisPlugin);
  603. else
  604. {
  605. StringBuffer tail;
  606. splitFilename(thisPlugin, NULL, NULL, &tail, &tail);
  607. addPlugin(thisPlugin, tail.str());
  608. }
  609. }
  610. }
  611. void SafePluginMap::loadFromDirectory(const char * pluginDirectory)
  612. {
  613. const char * mask = "*" SharedObjectExtension;
  614. Owned<IFile> pluginDir = createIFile(pluginDirectory);
  615. Owned<IDirectoryIterator> pluginFiles = pluginDir->directoryFiles(mask,false,false);
  616. ForEach(*pluginFiles)
  617. {
  618. const char *thisPlugin = pluginFiles->query().queryFilename();
  619. StringBuffer tail;
  620. splitFilename(thisPlugin, NULL, NULL, &tail, &tail);
  621. addPlugin(thisPlugin, tail.str());
  622. }
  623. }