dlSharedLibrary.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00037 #include "blocxx/BLOCXX_config.h"
00038 #if defined(BLOCXX_USE_DL)
00039 #include "blocxx/dlSharedLibrary.hpp"
00040 #include "blocxx/Format.hpp"
00041 #include "blocxx/Mutex.hpp"
00042 #include "blocxx/MutexLock.hpp"
00043 #include <dlfcn.h>
00044 
00045 #include "blocxx/Types.hpp"
00046 
00047 #if defined(BLOCXX_USE_FAKE_LIBS)
00048 
00049 #include "blocxx/FileSystem.hpp"
00050 #include "blocxx/File.hpp"
00051 #include "blocxx/Array.hpp"
00052 
00053 #define BLOCXX_FAKELIB_HEADING "FAKE"
00054 #define BLOCXX_FAKELIB_HEADING_LENGTH 4
00055 
00056 // NOTE: To make a fake library, create a text file with all functions that
00057 // should be contained, in a list like:
00058 // FAKE=true
00059 // library_function1=some_unique_global_function_name
00060 // library_function2=some_other_unique_global_function_name
00061 // library_function3=some_final_unique_global_function_name
00062 // ...
00063 //
00064 // The first entry MUST begin with FAKE, and there can be NO characters before
00065 // this entry.
00066 //
00068 #endif
00069 
00070 #include <iostream>
00071 
00072 namespace BLOCXX_NAMESPACE
00073 {
00074 
00075 // static
00076 bool dlSharedLibrary::s_call_dlclose = true;
00077 
00078 Mutex dlSharedLibrary_guard;
00079 
00080 dlSharedLibrary::dlSharedLibrary(void * libhandle, const String& libName)
00081    : SharedLibrary(), m_libhandle( libhandle ), m_libName(libName)
00082 {
00083 #if defined(BLOCXX_USE_FAKE_LIBS)
00084    // Find out if it is a fake library.
00085    m_fakeLibrary = dlSharedLibrary::isFakeLibrary(libName);
00086 
00087    if ( m_fakeLibrary )
00088    {
00089       initializeSymbolMap();
00090    }
00091 #endif /* defined(BLOCXX_USE_FAKE_LIBS) */
00092 }
00093   
00094 dlSharedLibrary::~dlSharedLibrary()
00095 {
00096 #if !defined(BLOCXX_VALGRIND_SUPPORT) // dlclose()ing shared libs make it impossible to see where memory leaks occurred with valgrind.
00097    if (s_call_dlclose)
00098    {
00099       dlclose( m_libhandle );
00100    }
00101 #endif
00102 }
00103 bool dlSharedLibrary::doGetFunctionPointer(const String& functionName,
00104       void** fp) const
00105 {
00106    MutexLock l(dlSharedLibrary_guard);
00107 #if defined(BLOCXX_USE_FAKE_LIBS)
00108    String realFunctionName = functionName;
00109    // If this is a fake library, extract convert the requested function
00110    // name into the proper function name for the main executable.
00111    if ( m_fakeLibrary )
00112    {
00113       Map<String,String>::const_iterator symIter = m_symbolMap.find(functionName);
00114       if ( symIter == m_symbolMap.end() )
00115       {
00116          return false;
00117       }
00118       realFunctionName = symIter->second;
00119    }
00120    *fp = dlsym( m_libhandle, realFunctionName.c_str() );
00121 #else
00122    *fp = dlsym( m_libhandle, functionName.c_str() );
00123 #endif /* defined(BLOCXX_USE_FAKE_LIBS) */
00124      
00125    if (!*fp)
00126    {
00127       return false;
00128    }
00129    return true;
00130 }
00131 
00132 bool dlSharedLibrary::isFakeLibrary(const String& library_path)
00133 {
00134 #if defined(BLOCXX_USE_FAKE_LIBS)
00135   if ( FileSystem::canRead(library_path) )
00136   {
00137     // Read the beginning of the file and see if it
00138     // contains the fake library heading.
00139     File libfile = FileSystem::openFile(library_path);
00140 
00141     if ( libfile )
00142     {
00143       char buffer[(BLOCXX_FAKELIB_HEADING_LENGTH) + 1];
00144       size_t num_read = libfile.read(buffer,(BLOCXX_FAKELIB_HEADING_LENGTH));
00145       if ( num_read == (BLOCXX_FAKELIB_HEADING_LENGTH) )
00146       {
00147    // Null terminate it.
00148    buffer[BLOCXX_FAKELIB_HEADING_LENGTH] = '\0';
00149    if ( String(BLOCXX_FAKELIB_HEADING) == buffer )
00150    {
00151      // Yes, it's a fake library.
00152      return true;
00153    }
00154       }
00155     }
00156   }  
00157 #endif
00158   return false;
00159 }
00160 
00161 #if defined(BLOCXX_USE_FAKE_LIBS)
00162 void dlSharedLibrary::initializeSymbolMap()
00163 {
00164    if ( ! m_fakeLibrary )
00165    {
00166       return;
00167    }
00168    // Read the contents of the file to find out what the function names
00169    // (normally available from dlsym) are mapped to functions in the main
00170    // program. 
00171    StringArray lines = FileSystem::getFileLines(m_libName);
00172 
00173    for ( StringArray::const_iterator iter = lines.begin();
00174       iter != lines.end();
00175       ++iter )
00176    {
00177       // Skip commented lines.  
00178       if ( iter->startsWith('#') )
00179       {
00180          continue;
00181       }
00182       StringArray current_line = iter->tokenize("=");
00183       // Skip invalid lines.
00184       if ( current_line.size() != 2 )
00185       {
00186          continue;
00187       }
00188       // Add the data into the map.
00189       String option = String(current_line[0]).trim();
00190       String value = String(current_line[1]).trim();
00191       m_symbolMap[option] = value;
00192    }      
00193 }
00194 #endif /* defined(BLOCXX_USE_FAKE_LIBS) */
00195 
00196 } // end namespace BLOCXX_NAMESPACE
00197 
00198 #endif
00199 

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6