1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
/*=========================================================================
Program: ParaView
Module: vtkPVPluginLoader.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPVPluginLoader.h"
#include "vtkDynamicLoader.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPDirectory.h"
#include "vtkProcessModule.h"
#include "vtkPVConfig.h"
#include "vtkPVOptions.h"
#include "vtkPVPlugin.h"
#include "vtkPVPluginTracker.h"
#include "vtkPVPythonPluginInterface.h"
#include "vtkPVServerManagerPluginInterface.h"
#include "vtkPVXMLParser.h"
#include <string>
#include <vtksys/SystemTools.hxx>
#include <sstream>
#include <cstdlib>
#define vtkPVPluginLoaderDebugMacro(x)\
{ if (this->DebugPlugin) {\
std::ostringstream vtkerror;\
vtkerror << x;\
vtkOutputWindowDisplayText(vtkerror.str().c_str());} }
#define vtkPVPluginLoaderErrorMacro(x)\
if (!no_errors) {vtkErrorMacro(<< x);} this->SetErrorString(x);
#if defined(_WIN32) && !defined(__CYGWIN__)
const char ENV_PATH_SEP=';';
#else
const char ENV_PATH_SEP=':';
#endif
namespace
{
// This is an helper class used for plugins constructed from XMLs.
class vtkPVXMLOnlyPlugin : public vtkPVPlugin,
public vtkPVServerManagerPluginInterface
{
std::string PluginName;
std::string XML;
vtkPVXMLOnlyPlugin(){};
vtkPVXMLOnlyPlugin(const vtkPVXMLOnlyPlugin& other);
void operator=(const vtkPVXMLOnlyPlugin& other);
public:
static vtkPVXMLOnlyPlugin* Create(const char* xmlfile)
{
vtkNew<vtkPVXMLParser> parser;
parser->SetFileName(xmlfile);
if (!parser->Parse())
{
return NULL;
}
vtkPVXMLOnlyPlugin* instance = new vtkPVXMLOnlyPlugin();
instance->PluginName =
vtksys::SystemTools::GetFilenameWithoutExtension(xmlfile);
ifstream is;
is.open(xmlfile, ios::binary);
// get length of file:
is.seekg (0, ios::end);
size_t length = is.tellg();
is.seekg (0, ios::beg);
// allocate memory:
char* buffer = new char [length + 1];
// read data as a block:
is.read (buffer,length);
is.close();
buffer[length] = 0;
instance->XML = buffer;
delete [] buffer;
return instance;
}
// Description:
// Returns the name for this plugin.
virtual const char* GetPluginName()
{ return this->PluginName.c_str(); }
// Description:
// Returns the version for this plugin.
virtual const char* GetPluginVersionString()
{ return "1.0"; }
// Description:
// Returns true if this plugin is required on the server.
virtual bool GetRequiredOnServer()
{ return true; }
// Description:
// Returns true if this plugin is required on the client.
virtual bool GetRequiredOnClient()
{ return false; }
// Description:
// Returns a ';' separated list of plugin names required by this plugin.
virtual const char* GetRequiredPlugins()
{ return ""; }
// Description:
// Obtain the server-manager configuration xmls, if any.
virtual void GetXMLs(std::vector<std::string>& xmls)
{
xmls.push_back(this->XML);
}
// Description:
// Returns the callback function to call to initialize the interpretor for the
// new vtk/server-manager classes added by this plugin. Returning NULL is
// perfectly valid.
virtual vtkClientServerInterpreterInitializer::InterpreterInitializationCallback
GetInitializeInterpreterCallback()
{ return NULL; }
};
// Cleans successfully opened libs when the application quits.
// BUG # 10293
class vtkPVPluginLoaderCleaner
{
typedef std::map<std::string, vtkLibHandle> HandlesType;
HandlesType Handles;
std::vector<vtkPVXMLOnlyPlugin*> XMLPlugins;
public:
void Register(const char* pname, vtkLibHandle &handle)
{
this->Handles[pname] = handle;
}
void Register(vtkPVXMLOnlyPlugin* plugin)
{
this->XMLPlugins.push_back(plugin);
}
~vtkPVPluginLoaderCleaner()
{
for (HandlesType::const_iterator iter = this->Handles.begin();
iter != this->Handles.end(); ++iter)
{
vtkDynamicLoader::CloseLibrary(iter->second);
}
for (std::vector<vtkPVXMLOnlyPlugin*>::iterator iter =
this->XMLPlugins.begin();
iter != this->XMLPlugins.end(); ++iter)
{
delete *iter;
}
}
static vtkPVPluginLoaderCleaner* GetInstance()
{
if (!vtkPVPluginLoaderCleaner::LibCleaner)
{
vtkPVPluginLoaderCleaner::LibCleaner = new vtkPVPluginLoaderCleaner();
}
return vtkPVPluginLoaderCleaner::LibCleaner;
}
static void FinalizeInstance()
{
if (vtkPVPluginLoaderCleaner::LibCleaner)
{
vtkPVPluginLoaderCleaner* cleaner = vtkPVPluginLoaderCleaner::LibCleaner;
vtkPVPluginLoaderCleaner::LibCleaner = NULL;
delete cleaner;
}
}
static void PluginLibraryUnloaded(const char* pname)
{
if (vtkPVPluginLoaderCleaner::LibCleaner && pname)
{
vtkPVPluginLoaderCleaner::LibCleaner->Handles.erase(pname);
}
}
private:
static vtkPVPluginLoaderCleaner *LibCleaner;
};
vtkPVPluginLoaderCleaner* vtkPVPluginLoaderCleaner::LibCleaner = NULL;
};
//=============================================================================
static int nifty_counter = 0;
vtkPVPluginLoaderCleanerInitializer::vtkPVPluginLoaderCleanerInitializer()
{
nifty_counter++;
}
vtkPVPluginLoaderCleanerInitializer::~vtkPVPluginLoaderCleanerInitializer()
{
nifty_counter--;
if (nifty_counter == 0)
{
vtkPVPluginLoaderCleaner::FinalizeInstance();
}
}
//=============================================================================
vtkPluginLoadFunction vtkPVPluginLoader::StaticPluginLoadFunction = 0;
vtkStandardNewMacro(vtkPVPluginLoader);
//-----------------------------------------------------------------------------
vtkPVPluginLoader::vtkPVPluginLoader()
{
this->DebugPlugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
this->ErrorString = NULL;
this->PluginName = NULL;
this->PluginVersion = NULL;
this->FileName = NULL;
this->SearchPaths = NULL;
this->Loaded = false;
this->SetErrorString("No plugin loaded yet.");
vtksys::String paths;
const char* env = vtksys::SystemTools::GetEnv("PV_PLUGIN_PATH");
if(env)
{
paths += env;
vtkPVPluginLoaderDebugMacro("PV_PLUGIN_PATH: " << env);
}
#ifdef PARAVIEW_PLUGIN_LOADER_PATHS
if(!paths.empty())
{
paths += ENV_PATH_SEP;
}
paths += PARAVIEW_PLUGIN_LOADER_PATHS;
#endif
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
vtkPVOptions* opt = pm ? pm->GetOptions() : NULL;
if(opt)
{
std::string appDir = vtkProcessModule::GetProcessModule()->GetSelfDir();
if(appDir.size())
{
appDir += "/plugins";
if(paths.size())
{
paths += ENV_PATH_SEP;
}
paths += appDir;
}
// pqPluginManager::pluginPaths() used to automatically load plugins a host
// of locations. We no longer support that. It becomes less useful since we
// now list plugins in the plugin manager dialog.
}
this->SetSearchPaths(paths.c_str());
}
//-----------------------------------------------------------------------------
vtkPVPluginLoader::~vtkPVPluginLoader()
{
this->SetErrorString(0);
this->SetPluginName(0);
this->SetPluginVersion(0);
this->SetFileName(0);
this->SetSearchPaths(0);
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::LoadPluginsFromPluginSearchPath()
{
#ifdef BUILD_SHARED_LIBS
vtkPVPluginLoaderDebugMacro(
"Loading Plugins from standard PLUGIN_PATHS \n"
<< this->SearchPaths);
std::vector<std::string> paths;
vtksys::SystemTools::Split(this->SearchPaths, paths, ENV_PATH_SEP);
for (size_t cc = 0; cc < paths.size(); cc++)
{
std::vector<std::string> subpaths;
vtksys::SystemTools::Split(paths[cc].c_str(), subpaths, ';');
for (size_t scc = 0; scc < subpaths.size(); scc++)
{
this->LoadPluginsFromPath(subpaths[scc].c_str());
}
}
#else
vtkPVPluginLoaderDebugMacro(
"Static build. Skipping PLUGIN_PATHS.");
#endif
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::LoadPluginsFromPluginConfigFile()
{
#ifdef BUILD_SHARED_LIBS
const char* configFiles = vtksys::SystemTools::GetEnv("PV_PLUGIN_CONFIG_FILE");
if (configFiles != NULL)
{
vtkPVPluginLoaderDebugMacro(
"Loading Plugins from standard PV_PLUGIN_CONFIG_FILE \n"
<< configFiles);
std::vector<std::string> paths;
vtksys::SystemTools::Split(configFiles, paths, ENV_PATH_SEP);
for (size_t cc = 0; cc < paths.size(); cc++)
{
std::vector<std::string> subpaths;
vtksys::SystemTools::Split(paths[cc].c_str(), subpaths, ';');
for (size_t scc = 0; scc < subpaths.size(); scc++)
{
vtkPVPluginTracker::GetInstance()->
LoadPluginConfigurationXML(subpaths[scc].c_str(), true);
}
}
}
#else
vtkPVPluginLoaderDebugMacro(
"Static build. Skipping PV_PLUGIN_CONFIG_FILE.");
#endif
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::LoadPluginsFromPath(const char* path)
{
vtkPVPluginLoaderDebugMacro("Loading plugins in Path: " << path);
vtkNew<vtkPDirectory> dir;
if (dir->Load(path) == false)
{
vtkPVPluginLoaderDebugMacro("Invalid directory: " << path);
return;
}
for (vtkIdType cc=0; cc < dir->GetNumberOfFiles(); cc++)
{
std::string ext =
vtksys::SystemTools::GetFilenameLastExtension(dir->GetFile(cc));
if (ext == ".so" || ext == ".dll" || ext == ".xml" || ext == ".dylib" ||
ext == ".xml" || ext == ".sl")
{
std::string file = dir->GetPath();
file += "/";
file += dir->GetFile(cc);
this->LoadPluginSilently(file.c_str());
}
}
}
//-----------------------------------------------------------------------------
bool vtkPVPluginLoader::LoadPluginInternal(const char* file, bool no_errors)
{
this->Loaded = false;
vtkPVPluginLoaderDebugMacro(
"\n***************************************************\n"
"Attempting to load " << file);
if (!file || file[0] == '\0')
{
vtkPVPluginLoaderErrorMacro("Invalid filename");
return false;
}
this->SetFileName(file);
std::string defaultname = vtksys::SystemTools::GetFilenameWithoutExtension(file);
this->SetPluginName(defaultname.c_str());
if (vtksys::SystemTools::GetFilenameLastExtension(file) == ".xml")
{
vtkPVPluginLoaderDebugMacro("Loading XML plugin");
vtkPVXMLOnlyPlugin* plugin = vtkPVXMLOnlyPlugin::Create(file);
if (plugin)
{
vtkPVPluginLoaderCleaner::GetInstance()->Register(plugin);
return this->LoadPlugin(file, plugin);
}
vtkPVPluginLoaderErrorMacro(
"Failed to load XML plugin. Not a valid XML or file could not be read.");
return false;
}
#ifndef BUILD_SHARED_LIBS
if (StaticPluginLoadFunction &&
StaticPluginLoadFunction(file))
{
this->Loaded = true;
return true;
}
vtkPVPluginLoaderErrorMacro(
"Could not find the plugin statically linked in, and "
"cannot load dynamic plugins in static builds.");
return false;
#else // ifndef BUILD_SHARED_LIBS
vtkLibHandle lib = vtkDynamicLoader::OpenLibrary(file);
if (!lib)
{
vtkPVPluginLoaderErrorMacro(vtkDynamicLoader::LastError());
vtkPVPluginLoaderDebugMacro("Failed to load the shared library.");
vtkPVPluginLoaderDebugMacro(this->ErrorString);
return false;
}
vtkPVPluginLoaderDebugMacro("Loaded shared library successfully. "
"Now trying to validate that it's a ParaView plugin.");
// A plugin shared library has two global functions:
// * pv_plugin_query_verification_data -- to obtain version
// * pv_plugin_instance -- to obtain the plugin instance.
pv_plugin_query_verification_data_fptr pv_plugin_query_verification_data =
(pv_plugin_query_verification_data_fptr)(
vtkDynamicLoader::GetSymbolAddress(lib,
"pv_plugin_query_verification_data"));
if (!pv_plugin_query_verification_data)
{
vtkPVPluginLoaderDebugMacro("Failed to locate the global function "
"\"pv_plugin_query_verification_data\" which is required to test the "
"plugin signature. This may not be a ParaView plugin dll or maybe "
"from a older version of ParaView when this function was not required.");
vtkPVPluginLoaderErrorMacro(
"Not a ParaView Plugin since could not locate the plugin-verification function");
vtkDynamicLoader::CloseLibrary(lib);
return false;
}
std::string pv_verfication_data = pv_plugin_query_verification_data();
vtkPVPluginLoaderDebugMacro("Plugin's signature: " <<
pv_verfication_data.c_str());
// Validate the signature. If the signature is invalid, then this plugin is
// totally bogus (even for the GUI layer).
if (pv_verfication_data != _PV_PLUGIN_VERIFICATION_STRING)
{
std::ostringstream error;
error << "Mismatch in versions: \n" <<
"ParaView Signature: " << _PV_PLUGIN_VERIFICATION_STRING << "\n"
"Plugin Signature: " << pv_verfication_data.c_str();
vtkPVPluginLoaderErrorMacro(error.str().c_str());
vtkDynamicLoader::CloseLibrary(lib);
vtkPVPluginLoaderDebugMacro(
"Mismatch in versions signifies that the plugin was built for "
"a different version of ParaView or with a different compilter. "
"Look at the signatures to determine what caused the mismatch.");
return false;
}
// If we succeeded so far, then obtain the instace of vtkPVPlugin for this
// plugin and load it.
pv_plugin_query_instance_fptr pv_plugin_query_instance =
(pv_plugin_query_instance_fptr)(
vtkDynamicLoader::GetSymbolAddress(lib,
"pv_plugin_instance"));
if (!pv_plugin_query_instance)
{
vtkPVPluginLoaderDebugMacro("We've encountered an error locating the other "
"global function \"pv_plugin_instance\" which is required to locate the "
"instance of the vtkPVPlugin class. Possibly the plugin shared library was "
"not compiled properly.");
vtkPVPluginLoaderErrorMacro(
"Not a ParaView Plugin since could not locate the plugin-instance "
"function.");
vtkDynamicLoader::CloseLibrary(lib);
return false;
}
vtkPVPluginLoaderDebugMacro("Plugin signature verification successful. "
"This is definitely a ParaView plugin compiled with correct compiler for "
"correct ParaView version.");
// BUG # 0008673
// Tell the platform to look in the plugin's directory for
// its dependencies. This isn't the right thing to do. A better
// solution would be to let the plugin tell us where to look so
// that a list of locations could be added.
std::string ldLibPath;
#if defined(_WIN32) && !defined(__CYGWIN__)
const char LIB_PATH_SEP=';';
const char PATH_SEP='\\';
const char *LIB_PATH_NAME="PATH";
ldLibPath=LIB_PATH_NAME;
ldLibPath+='=';
#elif defined (__APPLE__)
const char LIB_PATH_SEP=':';
const char PATH_SEP='/';
const char *LIB_PATH_NAME="DYLD_LIBRARY_PATH";
#else
const char LIB_PATH_SEP=':';
const char PATH_SEP='/';
const char *LIB_PATH_NAME="LD_LIBRARY_PATH";
#endif
// Trim the plugin name from the end of its path.
std::string thisPluginsPath(file);
size_t eop=thisPluginsPath.rfind(PATH_SEP);
thisPluginsPath=thisPluginsPath.substr(0,eop);
// Load the shared library search path.
const char *pLdLibPath=getenv(LIB_PATH_NAME);
bool pluginPathPresent
= pLdLibPath==NULL?false:strstr(pLdLibPath,thisPluginsPath.c_str())!=NULL;
// Update it.
if (!pluginPathPresent)
{
// Make sure we are only adding it once, because there can
// be multiple plugins in the same folder.
if (pLdLibPath)
{
ldLibPath+=pLdLibPath;
ldLibPath+=LIB_PATH_SEP;
}
ldLibPath+=thisPluginsPath;
vtksys::SystemTools::PutEnv(ldLibPath.c_str());
vtkPVPluginLoaderDebugMacro("Updating Shared Library Paths: " <<
ldLibPath.c_str());
}
vtkPVPlugin* plugin = pv_plugin_query_instance();
// if (plugin->UnloadOnExit())
{
// So that the lib is closed when the application quits.
// BUGS #10293, #15608.
vtkPVPluginLoaderCleaner::GetInstance()->Register(plugin->GetPluginName(), lib);
}
return this->LoadPlugin(file, plugin);
#endif // ifndef BUILD_SHARED_LIBS else
}
//-----------------------------------------------------------------------------
bool vtkPVPluginLoader::LoadPlugin(const char* file, vtkPVPlugin* plugin)
{
#ifndef BUILD_SHARED_LIBS
if (StaticPluginLoadFunction &&
StaticPluginLoadFunction(plugin->GetPluginName()))
{
this->Loaded = true;
return true;
}
else
{
this->SetErrorString("Failed to load static plugin.");
}
#endif
this->SetPluginName(plugin->GetPluginName());
this->SetPluginVersion(plugin->GetPluginVersionString());
// Print some debug information about the plugin, if needed.
vtkPVPluginLoaderDebugMacro("Plugin instance located successfully. "
"Now loading components from the plugin instance based on the interfaces it "
"implements.");
vtkPVPluginLoaderDebugMacro(
"----------------------------------------------------------------\n"
"Plugin Information: \n"
" Name : " << plugin->GetPluginName() << "\n"
" Version : " << plugin->GetPluginVersionString() << "\n"
" ReqOnServer : " << plugin->GetRequiredOnServer() << "\n"
" ReqOnClient : " << plugin->GetRequiredOnClient() << "\n"
" ReqPlugins : " << plugin->GetRequiredPlugins());
vtkPVServerManagerPluginInterface* smplugin =
dynamic_cast<vtkPVServerManagerPluginInterface*>(plugin);
if (smplugin)
{
vtkPVPluginLoaderDebugMacro(" ServerManager Plugin : Yes");
}
else
{
vtkPVPluginLoaderDebugMacro(" ServerManager Plugin : No");
}
vtkPVPythonPluginInterface *pyplugin =
dynamic_cast<vtkPVPythonPluginInterface*>(plugin);
if (pyplugin)
{
vtkPVPluginLoaderDebugMacro(" Python Plugin : Yes");
}
else
{
vtkPVPluginLoaderDebugMacro(" Python Plugin : No");
}
// Set the filename so the vtkPVPluginTracker knows what file this plugin was
// loaded from.
plugin->SetFileName(file);
// From this point onwards the vtkPVPlugin travels the same path as a
// statically imported plugin.
vtkPVPlugin::ImportPlugin(plugin);
this->Loaded = true;
return true;
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::LoadPluginConfigurationXMLFromString(const char*
xmlcontents)
{
vtkPVPluginTracker::GetInstance()->LoadPluginConfigurationXMLFromString(
xmlcontents);
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "DebugPlugin: " << this->DebugPlugin << endl;
os << indent << "PluginName: " <<
(this->PluginName? this->PluginName : "(none)") << endl;
os << indent << "PluginVersion: " <<
(this->PluginVersion? this->PluginVersion : "(none)") << endl;
os << indent << "FileName: " <<
(this->FileName ? this->FileName : "(none)") << endl;
os << indent << "SearchPaths: " <<
(this->SearchPaths ? this->SearchPaths : "(none)") << endl;
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::SetStaticPluginLoadFunction(vtkPluginLoadFunction function)
{
if (!StaticPluginLoadFunction)
{
StaticPluginLoadFunction = function;
}
}
//-----------------------------------------------------------------------------
void vtkPVPluginLoader::PluginLibraryUnloaded(const char* pluginname)
{
vtkPVPluginLoaderCleaner::PluginLibraryUnloaded(pluginname);
}
|