SharedLibrary_UNIX.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // SharedLibrary_UNIX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/SharedLibrary_UNIX.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: SharedLibrary
  8. // Module: SharedLibrary
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/SharedLibrary_UNIX.h"
  16. #include "Poco/Exception.h"
  17. #include <dlfcn.h>
  18. // Note: cygwin is missing RTLD_LOCAL, set it to 0
  19. #if defined(__CYGWIN__) && !defined(RTLD_LOCAL)
  20. #define RTLD_LOCAL 0
  21. #endif
  22. namespace Poco {
  23. FastMutex SharedLibraryImpl::_mutex;
  24. SharedLibraryImpl::SharedLibraryImpl()
  25. {
  26. _handle = 0;
  27. }
  28. SharedLibraryImpl::~SharedLibraryImpl()
  29. {
  30. }
  31. void SharedLibraryImpl::loadImpl(const std::string& path, int flags)
  32. {
  33. FastMutex::ScopedLock lock(_mutex);
  34. if (_handle) throw LibraryAlreadyLoadedException(path);
  35. int realFlags = RTLD_LAZY;
  36. if (flags & SHLIB_LOCAL_IMPL)
  37. realFlags |= RTLD_LOCAL;
  38. else
  39. realFlags |= RTLD_GLOBAL;
  40. _handle = dlopen(path.c_str(), realFlags);
  41. if (!_handle)
  42. {
  43. const char* err = dlerror();
  44. throw LibraryLoadException(err ? std::string(err) : path);
  45. }
  46. _path = path;
  47. }
  48. void SharedLibraryImpl::unloadImpl()
  49. {
  50. FastMutex::ScopedLock lock(_mutex);
  51. if (_handle)
  52. {
  53. dlclose(_handle);
  54. _handle = 0;
  55. }
  56. }
  57. bool SharedLibraryImpl::isLoadedImpl() const
  58. {
  59. return _handle != 0;
  60. }
  61. void* SharedLibraryImpl::findSymbolImpl(const std::string& name)
  62. {
  63. FastMutex::ScopedLock lock(_mutex);
  64. void* result = 0;
  65. if (_handle)
  66. {
  67. result = dlsym(_handle, name.c_str());
  68. }
  69. return result;
  70. }
  71. const std::string& SharedLibraryImpl::getPathImpl() const
  72. {
  73. return _path;
  74. }
  75. std::string SharedLibraryImpl::suffixImpl()
  76. {
  77. #if defined(__APPLE__)
  78. #if defined(_DEBUG)
  79. return "d.dylib";
  80. #else
  81. return ".dylib";
  82. #endif
  83. #elif defined(hpux) || defined(_hpux)
  84. #if defined(_DEBUG)
  85. return "d.sl";
  86. #else
  87. return ".sl";
  88. #endif
  89. #elif defined(__CYGWIN__)
  90. #if defined(_DEBUG)
  91. return "d.dll";
  92. #else
  93. return ".dll";
  94. #endif
  95. #else
  96. #if defined(_DEBUG)
  97. return "d.so";
  98. #else
  99. return ".so";
  100. #endif
  101. #endif
  102. }
  103. } // namespace Poco