blocxx

dlSharedLibrary.cpp

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