thorplugin.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. return false;
  112. #else
  113. // The first bytes are the ELF header
  114. const Elf32_Ehdr * hdr = (const Elf32_Ehdr *) start_addr;
  115. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
  116. {
  117. DBGLOG("Failed to extract resource %s: Does not appear to be a ELF binary", filename);
  118. return false;
  119. }
  120. if (hdr->e_ident[EI_CLASS] != ELFCLASS32)
  121. {
  122. DBGLOG("Failed to extract resource %s: Does not appear to be a ELF 32-bit binary", filename);
  123. return false;
  124. }
  125. //Check that there is a symbol table for the sections.
  126. if (hdr->e_shstrndx == SHN_UNDEF)
  127. {
  128. DBGLOG("Failed to extract resource %s: Does not include a section symbol table", filename);
  129. return false;
  130. }
  131. //Now walk the sections comparing the section names
  132. Elf32_Half numSections = hdr->e_shnum;
  133. const Elf32_Shdr * sectionHeaders = reinterpret_cast<const Elf32_Shdr *>(start_addr + hdr->e_shoff);
  134. const Elf32_Shdr & symbolTableSection = sectionHeaders[hdr->e_shstrndx];
  135. const char * symbolTable = (const char *)start_addr + symbolTableSection.sh_offset;
  136. VStringBuffer sectname("%s_%u", type, id);
  137. for (unsigned iSect= 0; iSect < numSections; iSect++)
  138. {
  139. const Elf32_Shdr & section = sectionHeaders[iSect];
  140. const char * sectionName = symbolTable + section.sh_name;
  141. if (streq(sectionName, sectname))
  142. {
  143. lenData = (size32_t)section.sh_size;
  144. data = start_addr + section.sh_offset;
  145. return true;
  146. }
  147. }
  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 ManifestFileList : public MappingBase
  211. {
  212. StringArray filenames;
  213. StringAttr type;
  214. StringAttr dir;
  215. void recursiveRemoveDirectory(const char *fullPath)
  216. {
  217. if (rmdir(fullPath) == 0 && !streq(fullPath, dir))
  218. {
  219. StringBuffer head;
  220. splitFilename(fullPath, &head, &head, NULL, NULL);
  221. if (head.length() > 1)
  222. {
  223. head.setLength(head.length()-1);
  224. recursiveRemoveDirectory(head);
  225. }
  226. }
  227. }
  228. void removeFileAndEmptyParents(const char *fullFileName)
  229. {
  230. remove(fullFileName);
  231. StringBuffer path;
  232. splitFilename(fullFileName, &path, &path, NULL, NULL);
  233. if (path.length() > 1)
  234. {
  235. path.setLength(path.length()-1);
  236. recursiveRemoveDirectory(path.str());
  237. }
  238. }
  239. public:
  240. ManifestFileList(const char *_type, const char *_dir) : type(_type), dir(_dir) {}
  241. ~ManifestFileList()
  242. {
  243. ForEachItemIn(idx, filenames)
  244. {
  245. removeFileAndEmptyParents(filenames.item(idx));
  246. }
  247. rmdir(dir); // If the specified temporary directory is now empty, remove it.
  248. }
  249. void append(const char *filename)
  250. {
  251. assertex(strncmp(filename, dir, strlen(dir))==0);
  252. filenames.append(filename);
  253. }
  254. inline const StringArray &queryFileNames() { return filenames; }
  255. virtual const void * getKey() const { return type; }
  256. };
  257. class HelperDll : implements ILoadedDllEntry, public CInterface
  258. {
  259. SharedObject so;
  260. Linked<const IFileIO> dllFile;
  261. Owned<IMemoryMappedFile> mappedDll;
  262. mutable std::atomic<IPropertyTree *> manifest {nullptr};
  263. mutable CriticalSection manifestLock;
  264. mutable StringMapOf<ManifestFileList> manifestFiles;
  265. protected:
  266. StringAttr name;
  267. bool logLoad;
  268. public:
  269. IMPLEMENT_IINTERFACE;
  270. HelperDll(const char *_name, const IFileIO *dllFile);
  271. ~HelperDll();
  272. //interface ILoadedDllEntry
  273. virtual HINSTANCE getInstance() const;
  274. virtual void * getEntry(const char * name) const;
  275. virtual bool IsShared();
  276. virtual const char * queryVersion() const;
  277. virtual const char * queryName() const;
  278. virtual const byte * getResource(unsigned id) const;
  279. virtual bool getResource(size32_t & len, const void * & data, const char * type, unsigned id, bool trace) const;
  280. virtual IPropertyTree &queryManifest() const override;
  281. virtual const StringArray &queryManifestFiles(const char *type, const char *wuid) const override;
  282. bool load(bool isGlobal, bool raiseOnError);
  283. bool loadCurrentExecutable();
  284. bool loadResources();
  285. virtual void logLoaded();
  286. virtual bool checkVersion(const char *expected);
  287. };
  288. class PluginDll : public HelperDll
  289. {
  290. ECLPluginDefinitionBlockEx pb;
  291. public:
  292. PluginDll(const char *_name, const IFileIO *_dllFile) : HelperDll(_name, _dllFile) {}
  293. bool init(IPluginContextEx * pluginCtx);
  294. virtual bool checkVersion(const char *expected) override;
  295. virtual void logLoaded() override;
  296. };
  297. HelperDll::HelperDll(const char *_name, const IFileIO *_dllFile)
  298. : name(_name), dllFile(_dllFile), manifestFiles(false)
  299. {
  300. logLoad = false;
  301. }
  302. bool HelperDll::load(bool isGlobal, bool raiseOnError)
  303. {
  304. if (!so.load(name, isGlobal, raiseOnError))
  305. return false;
  306. return true;
  307. }
  308. bool HelperDll::loadResources()
  309. {
  310. #ifdef _WIN32
  311. return so.loadResources(name);
  312. #else
  313. Owned<IFile> file = createIFile(name);
  314. mappedDll.setown(file->openMemoryMapped());
  315. return mappedDll != nullptr;
  316. #endif
  317. }
  318. bool HelperDll::loadCurrentExecutable()
  319. {
  320. if (!so.loadCurrentExecutable())
  321. return false;
  322. return true;
  323. }
  324. HelperDll::~HelperDll()
  325. {
  326. if (logLoad)
  327. DBGLOG("Unloading dll %s", name.get());
  328. ::Release(manifest.load(std::memory_order_relaxed));
  329. }
  330. HINSTANCE HelperDll::getInstance() const
  331. {
  332. if (!so.loaded())
  333. throw MakeStringException(0, "Dll %s only loaded for resources", name.str());
  334. return so.getInstanceHandle();
  335. }
  336. void * HelperDll::getEntry(const char * entry) const
  337. {
  338. if (!so.loaded())
  339. throw MakeStringException(0, "Dll %s only loaded for resources", name.str());
  340. return so.getEntry(entry);
  341. }
  342. bool HelperDll::IsShared()
  343. {
  344. return CInterface::IsShared();
  345. }
  346. const char * HelperDll::queryVersion() const
  347. {
  348. return "";
  349. }
  350. void HelperDll::logLoaded()
  351. {
  352. logLoad = true;
  353. DBGLOG("Loaded DLL %s", name.get());
  354. }
  355. bool HelperDll::checkVersion(const char *expected)
  356. {
  357. return true;
  358. }
  359. const char * HelperDll::queryName() const
  360. {
  361. return name.get();
  362. }
  363. const byte * HelperDll::getResource(unsigned id) const
  364. {
  365. if (so.loaded())
  366. {
  367. #ifdef _WIN32
  368. HINSTANCE dllHandle = so.getInstanceHandle();
  369. HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), "BIGSTRING");
  370. if (hrsrc)
  371. return (const byte *)LoadResource(dllHandle, hrsrc);
  372. return NULL;
  373. #else
  374. StringBuffer resourceName;
  375. resourceName.appendf("BIGSTRING_%d_txt_start", id);
  376. return (const byte *)getEntry(resourceName.str());
  377. #endif
  378. }
  379. else
  380. {
  381. size32_t len;
  382. const void * data;
  383. if (getResource(len, data, "BIGSTRING", id, false))
  384. return (const byte *)data;
  385. return nullptr;
  386. }
  387. }
  388. const byte resourceHeaderVersion=1;
  389. const size32_t resourceHeaderLength = sizeof(byte) + sizeof(byte) + sizeof(bool) + sizeof(size32_t);
  390. bool HelperDll::getResource(size32_t & len, const void * & data, const char * type, unsigned id, bool trace) const
  391. {
  392. if (so.loaded())
  393. {
  394. #ifdef _WIN32
  395. HINSTANCE dllHandle = so.getInstanceHandle();
  396. HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), type);
  397. if (!hrsrc)
  398. return false;
  399. len = SizeofResource(dllHandle, hrsrc);
  400. data = (const byte *)LoadResource(dllHandle, hrsrc);
  401. return true;
  402. #else
  403. StringBuffer symName;
  404. symName.append(type).append("_").append(id).append("_txt_start");
  405. data = (const void *)getEntry(symName.str());
  406. if (!data)
  407. {
  408. if (trace)
  409. printf("Failed to locate symbol %s\n", symName.str());
  410. return false;
  411. }
  412. byte bom;
  413. byte version;
  414. bool compressed;
  415. MemoryBuffer mb;
  416. mb.setBuffer(resourceHeaderLength, const_cast<void *>(data));
  417. mb.read(bom);
  418. if (bom != 0x80)
  419. return false;
  420. mb.read(version);
  421. if (version > resourceHeaderVersion)
  422. return false;
  423. mb.read(compressed).read(len);
  424. len += resourceHeaderLength;
  425. return true;
  426. #endif
  427. }
  428. else
  429. {
  430. #ifdef _WIN32
  431. return false;
  432. #endif
  433. if (!mappedDll)
  434. return false;
  435. return getResourceFromMappedFile(name, mappedDll->base(), len, data, type, id);
  436. }
  437. }
  438. IPropertyTree &HelperDll::queryManifest() const
  439. {
  440. return *querySingleton(manifest, manifestLock, [this]{ return getEmbeddedManifestPTree(this); });
  441. }
  442. const StringArray &HelperDll::queryManifestFiles(const char *type, const char *wuid) const
  443. {
  444. CriticalBlock b(manifestLock);
  445. Linked<ManifestFileList> list = manifestFiles.find(type);
  446. if (!list)
  447. {
  448. // The temporary path we unpack to is based on so file's current location and workunit
  449. // MORE - this is good for deployed cases, may not be so good for standalone executables.
  450. StringBuffer tempDir;
  451. splitFilename(name, &tempDir, &tempDir, &tempDir, nullptr);
  452. list.setown(new ManifestFileList(type, tempDir));
  453. tempDir.append(".tmp").append(PATHSEPCHAR).append(wuid);
  454. VStringBuffer xpath("Resource[@type='%s']", type);
  455. Owned<IPropertyTreeIterator> resourceFiles = queryManifest().getElements(xpath.str());
  456. ForEach(*resourceFiles)
  457. {
  458. IPropertyTree &resourceFile = resourceFiles->query();
  459. unsigned id = resourceFile.getPropInt("@id", 0);
  460. size32_t len = 0;
  461. const void *data = nullptr;
  462. if (!getResource(len, data, type, id, false))
  463. throwUnexpected();
  464. MemoryBuffer decompressed;
  465. if (resourceFile.getPropBool("@compressed"))
  466. {
  467. // MORE - would be better to try to spot files that are not worth recompressing (like jar files)?
  468. decompressResource(len, data, decompressed);
  469. data = decompressed.toByteArray();
  470. len = decompressed.length();
  471. }
  472. else
  473. {
  474. // Data is preceded by the resource header
  475. // MORE - does this depend on whether @header is set? is that what @header means?
  476. data = ((const byte *) data) + resourceHeaderLength;
  477. len -= resourceHeaderLength;
  478. }
  479. StringBuffer extractName(tempDir);
  480. extractName.append(PATHSEPCHAR);
  481. if (resourceFile.hasProp("@filename"))
  482. resourceFile.getProp("@filename", extractName);
  483. else
  484. extractName.append(id).append('.').append(type);
  485. recursiveCreateDirectoryForFile(extractName);
  486. OwnedIFile f = createIFile(extractName);
  487. OwnedIFileIO o = f->open(IFOcreaterw);
  488. assertex(o.get() != nullptr);
  489. o->write(0, len, data);
  490. list->append(extractName);
  491. }
  492. manifestFiles.replaceOwn(*list.getLink());
  493. }
  494. return list->queryFileNames();
  495. }
  496. //-------------------------------------------------------------------------------------------------------------------
  497. bool PluginDll::init(IPluginContextEx * pluginCtx)
  498. {
  499. HINSTANCE h = getInstance();
  500. assertex(h != (HINSTANCE) -1);
  501. EclPluginSetCtxEx pSetCtxEx = (EclPluginSetCtxEx) GetSharedProcedure(h,"setPluginContextEx");
  502. if (pSetCtxEx)
  503. pSetCtxEx(pluginCtx);
  504. else
  505. {
  506. // Older plugins may only support setPluginContext - fall back to that
  507. EclPluginSetCtx pSetCtx = (EclPluginSetCtx) GetSharedProcedure(h,"setPluginContext");
  508. if (pSetCtx)
  509. pSetCtx(pluginCtx);
  510. }
  511. EclPluginDefinition p= (EclPluginDefinition) GetSharedProcedure(h,"getECLPluginDefinition");
  512. if (!p)
  513. return false;
  514. pb.size = sizeof(ECLPluginDefinitionBlockEx);
  515. if (!p(&pb))
  516. {
  517. pb.compatibleVersions = NULL;
  518. pb.size = sizeof(ECLPluginDefinitionBlock);
  519. if (!p(&pb))
  520. return false;
  521. }
  522. return true;
  523. }
  524. bool PluginDll::checkVersion(const char *expected)
  525. {
  526. assertex(expected);
  527. if (stricmp(pb.version, expected) == 0)
  528. return true;
  529. if (pb.compatibleVersions)
  530. {
  531. const char **finger = pb.compatibleVersions;
  532. while (*finger)
  533. {
  534. if (stricmp(*finger, expected) == 0)
  535. return true;
  536. finger++;
  537. }
  538. }
  539. return false;
  540. }
  541. void PluginDll::logLoaded()
  542. {
  543. logLoad = true;
  544. DBGLOG("Loaded DLL %s [%s]", name.get(), pb.version);
  545. }
  546. extern DLLSERVER_API ILoadedDllEntry * createDllEntry(const char *path, bool isGlobal, const IFileIO *dllFile, bool resourcesOnly)
  547. {
  548. Owned<HelperDll> result = new HelperDll(path, dllFile);
  549. bool ok;
  550. if (!resourcesOnly)
  551. ok = result->load(isGlobal, true);
  552. else
  553. ok = result->loadResources();
  554. if (!ok)
  555. throw MakeStringException(0, "Failed to create ILoadedDllEntry for dll %s", path);
  556. return result.getClear();
  557. }
  558. extern DLLSERVER_API ILoadedDllEntry * createExeDllEntry(const char *path)
  559. {
  560. Owned<HelperDll> result = new HelperDll(path, NULL);
  561. if (!result->loadCurrentExecutable())
  562. throw MakeStringException(0, "Failed to create ILoadedDllEntry for current executable");
  563. return result.getClear();
  564. }
  565. extern DLLSERVER_API bool decompressResource(size32_t len, const void *data, MemoryBuffer &result)
  566. {
  567. bool hasVersion = len && (*(const byte *)data == 0x80);
  568. MemoryBuffer src;
  569. src.setBuffer(len, const_cast<void *>(data), false);
  570. byte version = 1;
  571. if (hasVersion)
  572. {
  573. src.skip(1);
  574. src.read(version);
  575. }
  576. switch (version)
  577. {
  578. case 1:
  579. decompressToBuffer(result, src);
  580. break;
  581. default:
  582. throwUnexpected();
  583. }
  584. return true;
  585. }
  586. extern DLLSERVER_API bool decompressResource(size32_t len, const void *data, StringBuffer &result)
  587. {
  588. MemoryBuffer tgt;
  589. if (len)
  590. decompressResource(len, data, tgt);
  591. tgt.append((char)0);
  592. unsigned expandedLen = tgt.length();
  593. result.setBuffer(expandedLen, reinterpret_cast<char *>(tgt.detach()), expandedLen-1);
  594. return true;
  595. }
  596. extern DLLSERVER_API void appendResource(MemoryBuffer & mb, size32_t len, const void *data, bool compress)
  597. {
  598. mb.append((byte)0x80).append(resourceHeaderVersion);
  599. if (compress)
  600. compressToBuffer(mb, len, data);
  601. else
  602. appendToBuffer(mb, len, data);
  603. }
  604. extern DLLSERVER_API void compressResource(MemoryBuffer & compressed, size32_t len, const void *data)
  605. {
  606. appendResource(compressed, len, data, true);
  607. }
  608. extern DLLSERVER_API bool getEmbeddedWorkUnitXML(ILoadedDllEntry *dll, StringBuffer &xml)
  609. {
  610. size32_t len = 0;
  611. const void * data = NULL;
  612. if (!dll->getResource(len, data, "WORKUNIT", 1000))
  613. return false;
  614. return decompressResource(len, data, xml);
  615. }
  616. extern DLLSERVER_API bool getEmbeddedManifestXML(const ILoadedDllEntry *dll, StringBuffer &xml)
  617. {
  618. size32_t len = 0;
  619. const void * data = NULL;
  620. if (!dll->getResource(len, data, "MANIFEST", 1000))
  621. return false;
  622. return decompressResource(len, data, xml);
  623. }
  624. extern DLLSERVER_API IPropertyTree *getEmbeddedManifestPTree(const ILoadedDllEntry *dll)
  625. {
  626. StringBuffer xml;
  627. return getEmbeddedManifestXML(dll, xml) ? createPTreeFromXMLString(xml.str()) : createPTree();
  628. }
  629. extern DLLSERVER_API bool checkEmbeddedWorkUnitXML(ILoadedDllEntry *dll)
  630. {
  631. size32_t len = 0;
  632. const void * data = NULL;
  633. return dll->getResource(len, data, "WORKUNIT", 1000, false);
  634. }
  635. extern DLLSERVER_API bool getResourceXMLFromFile(const char *filename, const char *type, unsigned id, StringBuffer &xml)
  636. {
  637. MemoryBuffer data;
  638. if (!getResourceFromFile(filename, data, type, id))
  639. return false;
  640. return decompressResource(data.length(), data.toByteArray(), xml);
  641. }
  642. extern DLLSERVER_API bool getWorkunitXMLFromFile(const char *filename, StringBuffer &xml)
  643. {
  644. return getResourceXMLFromFile(filename, "WORKUNIT", 1000, xml);
  645. }
  646. extern DLLSERVER_API bool getArchiveXMLFromFile(const char *filename, StringBuffer &xml)
  647. {
  648. return getResourceXMLFromFile(filename, "ARCHIVE", 1000, xml);
  649. }
  650. extern DLLSERVER_API bool getManifestXMLFromFile(const char *filename, StringBuffer &xml)
  651. {
  652. return getResourceXMLFromFile(filename, "MANIFEST", 1000, xml);
  653. }
  654. //-------------------------------------------------------------------------------------------------------------------
  655. extern DLLSERVER_API void getAdditionalPluginsPath(StringBuffer &pluginsPath, const char *_base)
  656. {
  657. // We only add the additional plugins if the plugins path already includes the default plugins location
  658. StringBuffer base(_base);
  659. removeTrailingPathSepChar(base);
  660. removeTrailingPathSepChar(pluginsPath);
  661. StringBuffer defaultLocation(base);
  662. defaultLocation.append(PATHSEPSTR "plugins");
  663. StringArray paths;
  664. paths.appendList(pluginsPath, ENVSEPSTR);
  665. if (paths.contains(defaultLocation))
  666. {
  667. const char *additional = queryEnvironmentConf().queryProp("additionalPlugins");
  668. if (additional)
  669. {
  670. StringArray additionalPaths;
  671. additionalPaths.appendList(additional, ENVSEPSTR);
  672. ForEachItemIn(idx, additionalPaths)
  673. {
  674. const char *additionalPath = additionalPaths.item(idx);
  675. pluginsPath.append(ENVSEPCHAR);
  676. if (!isAbsolutePath(additionalPath))
  677. pluginsPath.append(base).append(PATHSEPSTR "versioned" PATHSEPSTR);
  678. pluginsPath.append(additionalPath);
  679. }
  680. }
  681. }
  682. }
  683. bool SafePluginMap::addPlugin(const char *path, const char *dllname)
  684. {
  685. if (!endsWithIgnoreCase(path, SharedObjectExtension))
  686. {
  687. if (trace)
  688. DBGLOG("Ecl plugin %s ignored", path);
  689. return false;
  690. }
  691. try
  692. {
  693. CriticalBlock b(crit);
  694. ILoadedDllEntry *dll = map.getValue(dllname);
  695. if (!dll)
  696. {
  697. Owned<PluginDll> n = new PluginDll(path, NULL);
  698. // Note - we used to load plugins with global=true, but that caused issues when loading
  699. // Python3 and Python2 plugins at the same time as the export similar symbols
  700. // Loading with global=false should not cause any adverse issues
  701. if (!n->load(false, false) || !n->init(pluginCtx))
  702. throw MakeStringException(0, "Failed to load plugin %s", path);
  703. if (trace)
  704. n->logLoaded();
  705. map.setValue(dllname, n); // note: setValue links arg
  706. return true;
  707. }
  708. return false;
  709. }
  710. catch (IException * e) // MORE - not sure why we don't throw exceptions back here...
  711. {
  712. EXCLOG(e, "Loading plugin");
  713. e->Release();
  714. return false;
  715. }
  716. }
  717. ILoadedDllEntry * SafePluginMap::getPluginDll(const char *id, const char *version, bool checkVersion)
  718. {
  719. CriticalBlock b(crit);
  720. Linked<PluginDll> ret = static_cast<PluginDll *>(map.getValue(id));
  721. if (ret && checkVersion)
  722. {
  723. if (!ret->checkVersion(version))
  724. return NULL;
  725. }
  726. return ret.getLink();
  727. }
  728. void SafePluginMap::loadFromList(const char * pluginsList)
  729. {
  730. const char *pluginDir = pluginsList;
  731. for (;*pluginDir;)
  732. {
  733. StringBuffer thisPlugin;
  734. while (*pluginDir && *pluginDir != ENVSEPCHAR)
  735. thisPlugin.append(*pluginDir++);
  736. if(*pluginDir)
  737. pluginDir++;
  738. if(!thisPlugin.length())
  739. continue;
  740. Owned<IFile> file = createIFile(thisPlugin.str());
  741. if (file->isDirectory() == fileBool::foundYes)
  742. loadFromDirectory(thisPlugin);
  743. else
  744. {
  745. StringBuffer tail;
  746. splitFilename(thisPlugin, NULL, NULL, &tail, &tail);
  747. addPlugin(thisPlugin, tail.str());
  748. }
  749. }
  750. }
  751. void SafePluginMap::loadFromDirectory(const char * pluginDirectory)
  752. {
  753. const char * mask = "*" SharedObjectExtension;
  754. Owned<IFile> pluginDir = createIFile(pluginDirectory);
  755. Owned<IDirectoryIterator> pluginFiles = pluginDir->directoryFiles(mask,false,false);
  756. ForEach(*pluginFiles)
  757. {
  758. const char *thisPlugin = pluginFiles->query().queryFilename();
  759. StringBuffer tail;
  760. splitFilename(thisPlugin, NULL, NULL, &tail, &tail);
  761. addPlugin(thisPlugin, tail.str());
  762. }
  763. }