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 | |
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
-rw-r--r-- | cpp/src/Makefile.am | 11 | ||||
-rw-r--r-- | cpp/src/qpid/Plugin.cpp | 59 | ||||
-rw-r--r-- | cpp/src/qpid/Plugin.h | 36 | ||||
-rw-r--r-- | cpp/src/qpid/broker/Broker.cpp | 14 | ||||
-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 | ||||
-rw-r--r-- | cpp/src/tests/Cluster.cpp | 10 | ||||
-rw-r--r-- | cpp/src/tests/Makefile.am | 41 | ||||
-rw-r--r-- | cpp/src/tests/Shlib.cpp | 56 | ||||
-rw-r--r-- | cpp/src/tests/shlibtest.cpp | 28 |
15 files changed, 416 insertions, 346 deletions
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index d898a25562..28216649cc 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -48,11 +48,12 @@ apr_netio_hdr = \ apr_plat_src = \ qpid/sys/apr/Socket.cpp \ qpid/sys/apr/Time.cpp \ - qpid/sys/apr/Thread.cpp + qpid/sys/apr/Thread.cpp \ + qpid/sys/apr/Shlib.cpp + apr_plat_hdr = \ qpid/sys/apr/Condition.h \ - qpid/sys/apr/Module.h \ qpid/sys/apr/Mutex.h \ qpid/sys/apr/Socket.h \ qpid/sys/apr/Thread.h @@ -74,13 +75,13 @@ posix_plat_src = \ qpid/sys/posix/Socket.cpp \ qpid/sys/posix/AsynchIO.cpp \ qpid/sys/posix/Time.cpp \ - qpid/sys/posix/Thread.cpp + qpid/sys/posix/Thread.cpp \ + qpid/sys/posix/Shlib.cpp posix_plat_hdr = \ qpid/sys/posix/check.h \ qpid/sys/posix/Condition.h \ qpid/sys/posix/PrivatePosix.h \ - qpid/sys/posix/Module.h \ qpid/sys/posix/Mutex.h \ qpid/sys/posix/Socket.h \ qpid/sys/posix/Thread.h @@ -167,6 +168,8 @@ libqpidcommon_la_SOURCES = \ qpid/Url.cpp \ qpid/QpidError.cpp \ qpid/sys/Runnable.cpp \ + qpid/sys/Shlib.h \ + qpid/sys/Shlib.cpp \ qpid/sys/ProducerConsumer.cpp \ qpid/Options.cpp \ qpid/Options.h \ diff --git a/cpp/src/qpid/Plugin.cpp b/cpp/src/qpid/Plugin.cpp index 31fab65995..c6513d33e8 100644 --- a/cpp/src/qpid/Plugin.cpp +++ b/cpp/src/qpid/Plugin.cpp @@ -1,18 +1,20 @@ /* + * 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 * - * Copyright (c) 2006 The Apache Software Foundation + * http://www.apache.org/licenses/LICENSE-2.0 * - * Licensed 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. + * 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. * */ @@ -34,38 +36,5 @@ Options* PluginProvider::getOptions() { return 0; } const std::vector<PluginProvider*>& PluginProvider::getProviders() { return providers; } -} // namespace qpid - -// TODO aconway 2007-06-28: GNU lib has portable dlopen if we go that way. -#ifdef USE_APR_PLATFORM - -#include "qpid/sys/apr/APRBase.h" -#include "qpid/sys/apr/APRPool.h" -#include <apr_dso.h> - -namespace qpid { -void dlopen(const char* name) { - apr_dso_handle_t* handle; - CHECK_APR_SUCCESS( - apr_dso_load(&handle, name, sys::APRPool::get())); -} } // namespace qpid - -#else // Posix - -#include "qpid/sys/posix/check.h" -#include <dlfcn.h> - -namespace qpid { -void dlopen(const char* name) { - ::dlerror(); - ::dlopen(name, RTLD_NOW); - const char* error = ::dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } -} -} // namespace qpidpp - -#endif // USE_APR_PLATFORM diff --git a/cpp/src/qpid/Plugin.h b/cpp/src/qpid/Plugin.h index 48fb16a541..f50e97eb00 100644 --- a/cpp/src/qpid/Plugin.h +++ b/cpp/src/qpid/Plugin.h @@ -2,20 +2,22 @@ #define QPID_PLUGIN_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 * - * Copyright (c) 2006 The Apache Software Foundation + * http://www.apache.org/licenses/LICENSE-2.0 * - * Licensed 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. + * 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. * */ @@ -53,7 +55,6 @@ struct PluginUser : boost::noncopyable { virtual void use(const shared_ptr<Plugin>& plugin) = 0; }; - /** * Base for classes that provide plug-ins. */ @@ -97,15 +98,6 @@ class PluginProvider : boost::noncopyable static std::vector<PluginProvider*> providers; }; -/** Load a shared library, registering any PluginProvider it contains. - * - * This is just a convenient portable wrapper for normal shared - * library loading. A global PluginProvider instance loaded or - * linked in any way will get registered. - */ -void dlopen(const char* libname); - - } // namespace qpid #endif /*!QPID_PLUGIN_H*/ diff --git a/cpp/src/qpid/broker/Broker.cpp b/cpp/src/qpid/broker/Broker.cpp index 43a5567b3d..9c8e98ec9a 100644 --- a/cpp/src/qpid/broker/Broker.cpp +++ b/cpp/src/qpid/broker/Broker.cpp @@ -21,22 +21,24 @@ #include "Broker.h" -#include "qpid/framing/AMQFrame.h" -#include "qpid/framing/HandlerUpdater.h" +#include "Connection.h" #include "DirectExchange.h" -#include "TopicExchange.h" #include "FanOutExchange.h" #include "HeadersExchange.h" #include "MessageStoreModule.h" #include "NullMessageStore.h" -#include "qpid/framing/ProtocolInitiation.h" #include "RecoveryManagerImpl.h" -#include "Connection.h" +#include "TopicExchange.h" + +#include "qpid/log/Statement.h" +#include "qpid/Url.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/HandlerUpdater.h" +#include "qpid/framing/ProtocolInitiation.h" #include "qpid/sys/Acceptor.h" #include "qpid/sys/ConnectionInputHandler.h" #include "qpid/sys/ConnectionInputHandlerFactory.h" #include "qpid/sys/TimeoutHandler.h" -#include "qpid/Url.h" #include <iostream> #include <memory> 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 diff --git a/cpp/src/tests/Cluster.cpp b/cpp/src/tests/Cluster.cpp index 95cd607979..ccc1b52249 100644 --- a/cpp/src/tests/Cluster.cpp +++ b/cpp/src/tests/Cluster.cpp @@ -16,14 +16,18 @@ * */ -#define BOOST_AUTO_TEST_MAIN // Must come before #include<boost/test/*> -#include <boost/test/auto_unit_test.hpp> -#include "test_tools.h" #include "Cluster.h" +#include "test_tools.h" + #include "qpid/framing/ChannelPingBody.h" #include "qpid/framing/ChannelOkBody.h" #include "qpid/cluster/ClassifierHandler.h" +#define BOOST_AUTO_TEST_MAIN // Must come before #include<boost/test/*> +#include <boost/test/auto_unit_test.hpp> + +#include <sys/wait.h> + static const ProtocolVersion VER; using namespace qpid::log; diff --git a/cpp/src/tests/Makefile.am b/cpp/src/tests/Makefile.am index 3adf8818db..76f821798d 100644 --- a/cpp/src/tests/Makefile.am +++ b/cpp/src/tests/Makefile.am @@ -11,6 +11,7 @@ lib_broker = $(abs_builddir)/../libqpidbroker.la # Initialize variables that are incremented with += # check_PROGRAMS= +check_LTLIBRARIES= TESTS= EXTRA_DIST= @@ -32,6 +33,15 @@ check_PROGRAMS+=Uuid Uuid_SOURCES=Uuid.cpp test_tools.h Uuid_LDADD=-lboost_unit_test_framework $(lib_common) +check_LTLIBRARIES += libshlibtest.la +libshlibtest_la_LDFLAGS = -module -rpath $(abs_builddir) +libshlibtest_la_SOURCES = shlibtest.cpp + +TESTS+=Shlib +check_PROGRAMS+=Shlib +Shlib_SOURCES=Shlib.cpp +Shlib_LDADD=-lboost_unit_test_framework $(lib_common) + include cluster.mk # NB: CppUnit test libraries below will be migrated to boost test programs. @@ -112,24 +122,23 @@ EXTRA_DIST += \ include gen.mk check_LTLIBRARIES += libdlclose_noop.la -libdlclose_noop_la_LDFLAGS = -module -rpath /home/aconway/svn/qpid/cpp/tests +libdlclose_noop_la_LDFLAGS = -module -rpath $(abs_builddir) libdlclose_noop_la_SOURCES = dlclose_noop.c gen.mk: Makefile.am - ( \ - for i in $(testprogs); do \ - echo $${i}_SOURCES = $$i.cpp; \ - echo $${i}_LDADD = '$$(lib_client) $$(lib_common) $$(extra_libs)'; \ - done; \ - libs=; \ - for i in $(unit_tests); do \ - libs="$$libs $${i}.la"; \ - echo $${i}_la_SOURCES = $$i.cpp; \ - echo $${i}_la_LIBADD = '$$(lib_common) $$(lib_client)'; \ - echo $${i}_la_LIBADD += '$$(lib_broker) $$(extra_libs)'; \ - echo $${i}_la_LDFLAGS = "-module -rpath `pwd`"; \ - done; \ - echo "check_LTLIBRARIES =$$libs"; \ + ( \ + for i in $(testprogs); do \ + echo $${i}_SOURCES = $$i.cpp; \ + echo $${i}_LDADD = '$$(lib_client) $$(lib_common) $$(extra_libs)'; \ + done; \ + libs=; \ + for i in $(unit_tests); do \ + echo "check_LTLIBRARIES +=$${i}.la"; \ + echo $${i}_la_SOURCES = $$i.cpp; \ + echo $${i}_la_LIBADD = '$$(lib_common) $$(lib_client)'; \ + echo $${i}_la_LIBADD += '$$(lib_broker) $$(extra_libs)'; \ + echo $${i}_la_LDFLAGS = "-module -rpath `pwd`"; \ + done; \ ) \ > $@-t mv $@-t $@ @@ -138,7 +147,7 @@ check-unit: $(MAKE) check TESTS=$(UNIT_TESTS) run-unit-tests # Make sure valgrind files are generated. -all: .valgrind.supp .valgrindrc +all-am: .valgrind.supp .valgrindrc # Create a copy so that can be modified without risk of committing the changes. .valgrindrc: .valgrindrc-default diff --git a/cpp/src/tests/Shlib.cpp b/cpp/src/tests/Shlib.cpp new file mode 100644 index 0000000000..87136425ab --- /dev/null +++ b/cpp/src/tests/Shlib.cpp @@ -0,0 +1,56 @@ +/* + * 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 "test_tools.h" +#include "qpid/sys/Shlib.h" + +#define BOOST_AUTO_TEST_MAIN // Must come before #include<boost/test/*> +#include <boost/test/auto_unit_test.hpp> + +using namespace qpid::sys; +typedef void (*CallMe)(int*); + +BOOST_AUTO_TEST_CASE(testShlib) { + Shlib sh(".libs/libshlibtest.so"); + // Double cast to avoid ISO warning. + CallMe callMe=sh.getSymbol<CallMe>("callMe"); + BOOST_REQUIRE(callMe != 0); + int unloaded=0; + callMe(&unloaded); + sh.unload(); + BOOST_CHECK_EQUAL(42, unloaded); + try { + sh.getSymbol("callMe"); + BOOST_FAIL("Expected exception"); + } + catch (...) {} +} + +BOOST_AUTO_TEST_CASE(testAutoShlib) { + int unloaded = 0; + { + AutoShlib sh(".libs/libshlibtest.so"); + CallMe callMe=sh.getSymbol<CallMe>("callMe"); + BOOST_REQUIRE(callMe != 0); + callMe(&unloaded); + } + BOOST_CHECK_EQUAL(42, unloaded); +} + diff --git a/cpp/src/tests/shlibtest.cpp b/cpp/src/tests/shlibtest.cpp new file mode 100644 index 0000000000..80320ea7be --- /dev/null +++ b/cpp/src/tests/shlibtest.cpp @@ -0,0 +1,28 @@ +/* + * 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. + * + */ + +int* loaderData = 0; +extern "C" void callMe(int *i) { loaderData=i; } + +struct OnUnload { ~OnUnload() { *loaderData=42; } }; +OnUnload unloader; // For destructor. + + + |