thorplugin.cpp 22 KB

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