1//
2// SharedLibrary_UNIX.cpp
3//
4// Library: Foundation
5// Package: SharedLibrary
6// Module: SharedLibrary
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/SharedLibrary_UNIX.h"
16#include "Poco/Exception.h"
17#include <dlfcn.h>
18
19
20// Note: cygwin is missing RTLD_LOCAL, set it to 0
21#if POCO_OS == POCO_OS_CYGWIN && !defined(RTLD_LOCAL)
22#define RTLD_LOCAL 0
23#endif
24
25
26namespace Poco {
27
28
29FastMutex SharedLibraryImpl::_mutex;
30
31
32SharedLibraryImpl::SharedLibraryImpl()
33{
34 _handle = 0;
35}
36
37
38SharedLibraryImpl::~SharedLibraryImpl()
39{
40}
41
42
43void SharedLibraryImpl::loadImpl(const std::string& path, int flags)
44{
45 FastMutex::ScopedLock lock(_mutex);
46
47 if (_handle) throw LibraryAlreadyLoadedException(path);
48 int realFlags = RTLD_LAZY;
49 if (flags & SHLIB_LOCAL_IMPL)
50 realFlags |= RTLD_LOCAL;
51 else
52 realFlags |= RTLD_GLOBAL;
53 _handle = dlopen(path.c_str(), realFlags);
54 if (!_handle)
55 {
56 const char* err = dlerror();
57 throw LibraryLoadException(err ? std::string(err) : path);
58 }
59 _path = path;
60}
61
62
63void SharedLibraryImpl::unloadImpl()
64{
65 FastMutex::ScopedLock lock(_mutex);
66
67 if (_handle)
68 {
69 dlclose(_handle);
70 _handle = 0;
71 }
72}
73
74
75bool SharedLibraryImpl::isLoadedImpl() const
76{
77 return _handle != 0;
78}
79
80
81void* SharedLibraryImpl::findSymbolImpl(const std::string& name)
82{
83 FastMutex::ScopedLock lock(_mutex);
84
85 void* result = 0;
86 if (_handle)
87 {
88 result = dlsym(_handle, name.c_str());
89 }
90 return result;
91}
92
93
94const std::string& SharedLibraryImpl::getPathImpl() const
95{
96 return _path;
97}
98
99
100std::string SharedLibraryImpl::prefixImpl()
101{
102#if POCO_OS == POCO_OS_CYGWIN
103 return "cyg";
104#else
105 return "lib";
106#endif
107}
108
109
110std::string SharedLibraryImpl::suffixImpl()
111{
112#if POCO_OS == POCO_OS_MAC_OS_X
113 #if defined(_DEBUG) && !defined(POCO_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
114 return "d.dylib";
115 #else
116 return ".dylib";
117 #endif
118#elif POCO_OS == POCO_OS_HPUX
119 #if defined(_DEBUG) && !defined(POCO_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
120 return "d.sl";
121 #else
122 return ".sl";
123 #endif
124#elif POCO_OS == POCO_OS_CYGWIN
125 #if defined(_DEBUG) && !defined(POCO_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
126 return "d.dll";
127 #else
128 return ".dll";
129 #endif
130#else
131 #if defined(_DEBUG) && !defined(POCO_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
132 return "d.so";
133 #else
134 return ".so";
135 #endif
136#endif
137}
138
139
140} // namespace Poco
141