diff options
| author | Alan Conway <aconway@apache.org> | 2007-07-04 03:24:48 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2007-07-04 03:24:48 +0000 |
| commit | ae896d73d4914fa96f053a0443f6b05003c12b35 (patch) | |
| tree | 0aac1abf592704282ab5860a0f69dcfd9610f2cd /cpp/src/qpid/sys | |
| parent | 7865fcf6689f96219365995e8ad09b3332205176 (diff) | |
| download | qpid-python-ae896d73d4914fa96f053a0443f6b05003c12b35.tar.gz | |
2007-07-02 <aconway@redhat.com
* qpid/sys/Shlib.h, .cpp: Portable shared library abstraction.
- Shlib: load, unload, getSymbol
- AutoShlib: unload in destructor.
* qpid/sys/apr/Shlib.cpp sys/posix/Shlib.cpp: APR/Posix impls
* qpid/sys/Module.h: Reimplemented Module in terms of AutoShlib.
* qpid/Plugin.cpp, .h: Removed dlopen - use Shlib instead.
* qpid/broker/Broker.cpp: Added missing #include "qpid/log/Statement.h"
* tests/Shlib.cpp, shlibtest.cpp: Unit test
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@553056 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/sys')
| -rw-r--r-- | cpp/src/qpid/sys/Module.h | 37 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/Shlib.cpp | 38 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/Shlib.h | 82 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/apr/Module.h | 114 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/apr/Shlib.cpp | 49 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/posix/Module.h | 127 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/posix/Shlib.cpp | 60 |
7 files changed, 257 insertions, 250 deletions
diff --git a/cpp/src/qpid/sys/Module.h b/cpp/src/qpid/sys/Module.h index e812f57d7a..79793ed0ca 100644 --- a/cpp/src/qpid/sys/Module.h +++ b/cpp/src/qpid/sys/Module.h @@ -1,5 +1,5 @@ -#ifndef _sys_Module_h -#define _sys_Module_h +#ifndef QPID_SYS_MODULE_H +#define QPID_SYS_MODULE_H /* * @@ -21,11 +21,30 @@ * under the License. * */ - -#ifdef USE_APR_PLATFORM -#include "apr/Module.h" -#else -#include "posix/Module.h" -#endif -#endif //ifndef _sys_Module_h +#include "qpid/sys/Shlib.h" +#include <boost/noncopyable.hpp> + +namespace qpid { +namespace sys { + +template <class T> class Module : public AutoShlib, private boost::noncopyable +{ + public: + Module(const std::string& name) : + AutoShlib(name), + ptr(getSymbol<T*(*)()>("create")()) {} + + T* get() { return ptr; } + T* operator->() { return ptr; } + ~Module() throw() { + getSymbol<void (*)(T*)>("destroy")(ptr); + } + + private: + T* ptr; +}; + +}} + +#endif /*!QPID_SYS_MODULE_H*/ diff --git a/cpp/src/qpid/sys/Shlib.cpp b/cpp/src/qpid/sys/Shlib.cpp new file mode 100644 index 0000000000..8fd3f42cc6 --- /dev/null +++ b/cpp/src/qpid/sys/Shlib.cpp @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "Shlib.h" + +#include "qpid/log/Statement.h" + +namespace qpid { +namespace sys { + +AutoShlib::~AutoShlib() throw() { + try { + unload(); + } catch(const std::exception& e) { + QPID_LOG(error, "Unloading shared library: " << e.what()); + } +} + +// Note: Other functions are defined in apr/Shlib.cpp or posix/Shlib.cpp. + +}} // namespace qpid::sys diff --git a/cpp/src/qpid/sys/Shlib.h b/cpp/src/qpid/sys/Shlib.h new file mode 100644 index 0000000000..debf231125 --- /dev/null +++ b/cpp/src/qpid/sys/Shlib.h @@ -0,0 +1,82 @@ +#ifndef QPID_SYS_SHLIB_H +#define QPID_SYS_SHLIB_H + +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include <boost/noncopyable.hpp> +#include <iostream> +#include <dlfcn.h> + +namespace qpid { +namespace sys { + +/** Encapsulates a shared library handle. + *@see AutoShlib + */ +class Shlib { + public: + /** Load a shared library */ + Shlib(const char* libname) { load(libname); } + + /** Load a shared library */ + Shlib(const std::string& libname) { load(libname.c_str()); } + + /** Unload shared library. */ + void unload(); + + /** Look up symbol. */ + void* getSymbol(const char* symbol); + + /** Look up symbol in shared library, cast it to the desired + * pointer type, void* by default. + */ + template <class T> + T getSymbol(const char* symbol) { + // Double cast avoids warning about casting object to function pointer + return reinterpret_cast<T>(reinterpret_cast<intptr_t>( + this->getSymbol(symbol))); + } + + /** Look up symbol in shared library. */ + template <class T*> + void* getSymbol(const std::string& symbol) { + return getSymbol<T>(symbol.c_str()); + } + + private: + void* handle; + void load(const char* libname); +}; + +/** A shared library handle that unloads the shlib in it's dtor */ +class AutoShlib : public Shlib { + public: + /** Load shared library */ + AutoShlib(const std::string& libname) : Shlib(libname) {} + /** Calls unload() */ + ~AutoShlib() throw(); +}; + + +}} // namespace qpid::sys + +#endif /*!QPID_SYS_SHLIB_H*/ diff --git a/cpp/src/qpid/sys/apr/Module.h b/cpp/src/qpid/sys/apr/Module.h deleted file mode 100644 index ff93a0eaf0..0000000000 --- a/cpp/src/qpid/sys/apr/Module.h +++ /dev/null @@ -1,114 +0,0 @@ -#ifndef _sys_apr_Module_h -#define _sys_apr_Module_h - -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -#include "qpid/log/Statement.h" -#include "qpid/QpidError.h" -#include "APRBase.h" -#include "APRPool.h" - -#include <boost/noncopyable.hpp> -#include <iostream> -#include <apr_dso.h> - -namespace qpid { -namespace sys { - -typedef apr_dso_handle_t* dso_handle_t; - -template <class T> class Module : private boost::noncopyable -{ - typedef T* create_t(); - typedef void destroy_t(T*); - - dso_handle_t handle; - destroy_t* destroy; - T* ptr; - - void load(const std::string& name); - void unload(); - void* getSymbol(const std::string& name); - -public: - Module(const std::string& name); - T* operator->(); - T* get(); - ~Module() throw(); -}; - -template <class T> Module<T>::Module(const std::string& module) : destroy(0), ptr(0) -{ - load(module); - //TODO: need a better strategy for symbol names to allow multiple - //modules to be loaded without clashes... - - //Note: need the double cast to avoid errors in casting from void* to function pointer with -pedantic - create_t* create = reinterpret_cast<create_t*>(reinterpret_cast<intptr_t>(getSymbol("create"))); - destroy = reinterpret_cast<destroy_t*>(reinterpret_cast<intptr_t>(getSymbol("destroy"))); - ptr = create(); -} - -template <class T> T* Module<T>::operator->() -{ - return ptr; -} - -template <class T> T* Module<T>::get() -{ - return ptr; -} - -template <class T> Module<T>::~Module() throw() -{ - try { - if (handle && ptr) { - destroy(ptr); - } - if (handle) unload(); - } catch (std::exception& e) { - QPID_LOG(error, "Error while destroying module: " << e.what()); - } - destroy = 0; - handle = 0; - ptr = 0; -} - -template <class T> void Module<T>::load(const std::string& name) -{ - CHECK_APR_SUCCESS(apr_dso_load(&handle, name.c_str(), APRPool::get())); -} - -template <class T> void Module<T>::unload() -{ - CHECK_APR_SUCCESS(apr_dso_unload(handle)); -} - -template <class T> void* Module<T>::getSymbol(const std::string& name) -{ - apr_dso_handle_sym_t symbol; - CHECK_APR_SUCCESS(apr_dso_sym(&symbol, handle, name.c_str())); - return (void*) symbol; -} - -}} -#endif //ifndef _sys_apr_Module_h - diff --git a/cpp/src/qpid/sys/apr/Shlib.cpp b/cpp/src/qpid/sys/apr/Shlib.cpp new file mode 100644 index 0000000000..b0ba706713 --- /dev/null +++ b/cpp/src/qpid/sys/apr/Shlib.cpp @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "qpid/sys/Shlib.h" +#include "APRBase.h" +#include "APRPool.h" +#include <apr_dso.h> + +namespace qpid { +namespace sys { + +void Shlib::load(const char* libname) { + apr_dso_handle_t* aprHandle; + CHECK_APR_SUCCESS( + apr_dso_load(&aprHandle, libname, APRPool::get())); + handle=aprHandle; +} + +void Shlib::unload() { + CHECK_APR_SUCCESS( + apr_dso_unload(static_cast<apr_dso_handle_t*>(handle))); +} + +void* Shlib::getSymbol(const char* name) { + apr_dso_handle_sym_t symbol; + CHECK_APR_SUCCESS(apr_dso_sym(&symbol, + static_cast<apr_dso_handle_t*>(handle), + name)); + return (void*) symbol; +} + +}} // namespace qpid::sys diff --git a/cpp/src/qpid/sys/posix/Module.h b/cpp/src/qpid/sys/posix/Module.h deleted file mode 100644 index 5a0358a748..0000000000 --- a/cpp/src/qpid/sys/posix/Module.h +++ /dev/null @@ -1,127 +0,0 @@ -#ifndef _sys_posix_Module_h -#define _sys_posix_Module_h - -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "qpid/QpidError.h" -#include "qpid/log/Statement.h" - -#include <boost/noncopyable.hpp> -#include <iostream> -#include <dlfcn.h> - -namespace qpid { -namespace sys { - -typedef void* dso_handle_t; - -template <class T> class Module : private boost::noncopyable -{ - typedef T* create_t(); - typedef void destroy_t(T*); - - dso_handle_t handle; - destroy_t* destroy; - T* ptr; - - void load(const std::string& name); - void unload(); - void* getSymbol(const std::string& name); - -public: - Module(const std::string& name); - T* operator->(); - T* get(); - ~Module() throw(); -}; - -template <class T> Module<T>::Module(const std::string& module) : destroy(0), ptr(0) -{ - load(module); - //TODO: need a better strategy for symbol names to allow multiple - //modules to be loaded without clashes... - - //Note: need the double cast to avoid errors in casting from void* to function pointer with -pedantic - create_t* create = reinterpret_cast<create_t*>(reinterpret_cast<intptr_t>(getSymbol("create"))); - destroy = reinterpret_cast<destroy_t*>(reinterpret_cast<intptr_t>(getSymbol("destroy"))); - ptr = create(); -} - -template <class T> T* Module<T>::operator->() -{ - return ptr; -} - -template <class T> T* Module<T>::get() -{ - return ptr; -} - -template <class T> Module<T>::~Module() throw() -{ - try { - if (handle && ptr) { - destroy(ptr); - } - if (handle) unload(); - } catch (std::exception& e) { - QPID_LOG(error, "Error while destroying module: " << e.what()); - } - destroy = 0; - handle = 0; - ptr = 0; -} - -template <class T> void Module<T>::load(const std::string& name) -{ - ::dlerror(); - handle = ::dlopen(name.c_str(), RTLD_NOW); - const char* error = ::dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } -} - -template <class T> void Module<T>::unload() -{ - dlerror(); - dlclose(handle); - const char* error = dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } -} - -template <class T> void* Module<T>::getSymbol(const std::string& name) -{ - dlerror(); - void* sym = dlsym(handle, name.c_str()); - const char* error = dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } - return sym; -} - -}} -#endif //ifndef _sys_posix_Module_h - diff --git a/cpp/src/qpid/sys/posix/Shlib.cpp b/cpp/src/qpid/sys/posix/Shlib.cpp new file mode 100644 index 0000000000..2630337408 --- /dev/null +++ b/cpp/src/qpid/sys/posix/Shlib.cpp @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "qpid/sys/Shlib.h" + +#include <qpid/QpidError.h> +#include <dlfcn.h> + + +namespace qpid { +namespace sys { + +void Shlib::load(const char* name) { + dlerror(); + handle = ::dlopen(name, RTLD_NOW); + const char* error = ::dlerror(); + if (error) { + THROW_QPID_ERROR(INTERNAL_ERROR, error); + } +} + +void Shlib::unload() { + if (handle) { + ::dlerror(); + ::dlclose(handle); + const char* error = ::dlerror(); + if (error) { + THROW_QPID_ERROR(INTERNAL_ERROR, error); + } + handle = 0; + } +} + +void* Shlib::getSymbol(const char* name) { + ::dlerror(); + void* sym = ::dlsym(handle, name); + const char* error = ::dlerror(); + if (error) + THROW_QPID_ERROR(INTERNAL_ERROR, error); + return sym; +} + +}} // namespace qpid::sys |
