Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProcessorLoader.cc
Go to the documentation of this file.
2 
3 #include <dlfcn.h>
4 #include <iostream>
5 #include <cstdlib>
6 #include <set>
7 
8 
9 namespace marlin{
10 
11 
13  lcio::StringVec::const_iterator first,
14  lcio::StringVec::const_iterator last ) {
15 
16 
17  _loadError=false;
18  lcio::StringVec::const_iterator current = first ;
19 
20  std::set<std::string> _checkDuplicateLibs;
21 
22  while( current != last ){
23 
24  std::string libName( *current ) ;
25  size_t idx;
26  idx = libName.find_last_of("/");
27  // the library basename, i.e. /path/to/libBlah.so --> libBlah.so
28  std::string libBaseName( libName.substr( idx + 1 ) );
29 
30  char *real_path = realpath(libName.c_str(), NULL);
31 
32  if( real_path != NULL ){
33  std::cout << "<!-- Loading shared library : " << real_path << " ("<< libBaseName << ")-->" << std::endl ;
34 
35  // use real_path
36  free(real_path);
37  }
38  else{
39  std::cout << "<!-- Loading shared library : " << libName << " ("<< libBaseName << ")-->" << std::endl ;
40  }
41 
42 
43  if( _checkDuplicateLibs.find( libBaseName ) == _checkDuplicateLibs.end() ){
44  _checkDuplicateLibs.insert( libBaseName ) ;
45  }
46  else{
47  std::cout << std::endl << "<!-- ERROR loading shared library : " << libName << std::endl
48  << " -> Trying to load DUPLICATE library -->" << std::endl << std::endl ;
49  _loadError=true;
50  }
51 
52 
53  if( ! _loadError ){
54 
55  //void* libPointer = dlopen( libName.c_str() , RTLD_NOW) ;
56  //void* libPointer = dlopen( libName.c_str() , RTLD_LAZY ) ;
57  //void* libPointer = dlopen( libName.c_str() , RTLD_NOW | RTLD_GLOBAL) ;
58  void* libPointer = dlopen( libName.c_str() , RTLD_LAZY | RTLD_GLOBAL) ;
59 
60  if( libPointer == 0 ){
61  std::cout << std::endl << "<!-- ERROR loading shared library : " << libName << std::endl
62  << " -> " << dlerror() << " -->" << std::endl << std::endl ;
63  _loadError=true;
64  }
65  else{
66  _libs.push_back( libPointer ) ;
67  }
68  }
69 
70  ++current ;
71 
72  }
73 }
74 
75 
77 
78 
79  //fg: calling of dlclose causes a ROOT segmentation fault at the end of the program
80  // - as the dlclose is not strictly needed, we do not call it anymore
81  // so also MARLIN_DEBUG is no longer needed
82  // (thanks to A.Sailer for pointing this out)
83  //
84  //
85  // char * s = std::getenv("MARLIN_DEBUG" ) ;
86  //
87  // // do not unload processors if $MARLIN_DEBUG is set to 1
88  // // useful for debugging with valgrind (https://jira.slac.stanford.edu/browse/MAR-45)
89  // if( std::string("1").compare( (s?s:"0") ) == 0 ) {
90  // std::cout << std::endl << "<!-- MARLIN_DEBUG=1 set in your environment - skip unloading processors --> " << std::endl ;
91  // } else {
93  // std::cout << std::endl << "################ ProcessorLoader: dlclose libraries in reverse order !!!!!! " << std::endl ;
94  //
95  // for( LibVec::reverse_iterator it = _libs.rbegin() ; it != _libs.rend() ; ++it ) {
96  // dlclose( *it ) ;
97  // }
98  // }
99 
100 }
101 
102 
103 } // namespace marlin
ProcessorLoader(lcio::StringVec::const_iterator first, lcio::StringVec::const_iterator last)
T endl(T...args)
T end(T...args)
STL class.
T find_last_of(T...args)
T push_back(T...args)
T insert(T...args)
T find(T...args)
T c_str(T...args)
T substr(T...args)