Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "plugin.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 20 | |
| 21 | #include "android-base/stringprintf.h" |
Alex Light | 3b08bcc | 2019-09-11 09:48:51 -0700 | [diff] [blame] | 22 | #include "base/locks.h" |
| 23 | #include "base/mutex.h" |
| 24 | #include "thread-current-inl.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 25 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 30 | const char* PLUGIN_INITIALIZATION_FUNCTION_NAME = "ArtPlugin_Initialize"; |
| 31 | const char* PLUGIN_DEINITIALIZATION_FUNCTION_NAME = "ArtPlugin_Deinitialize"; |
| 32 | |
| 33 | Plugin::Plugin(const Plugin& other) : library_(other.library_), dlopen_handle_(nullptr) { |
Alex Light | 443a6a5 | 2018-03-02 16:37:18 -0800 | [diff] [blame] | 34 | CHECK(!other.IsLoaded()) << "Should not copy loaded plugins."; |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool Plugin::Load(/*out*/std::string* error_msg) { |
Alex Light | 3b08bcc | 2019-09-11 09:48:51 -0700 | [diff] [blame] | 38 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 39 | DCHECK(!IsLoaded()); |
| 40 | void* res = dlopen(library_.c_str(), RTLD_LAZY); |
| 41 | if (res == nullptr) { |
| 42 | *error_msg = StringPrintf("dlopen failed: %s", dlerror()); |
| 43 | return false; |
| 44 | } |
| 45 | // Get the initializer function |
| 46 | PluginInitializationFunction init = reinterpret_cast<PluginInitializationFunction>( |
| 47 | dlsym(res, PLUGIN_INITIALIZATION_FUNCTION_NAME)); |
| 48 | if (init != nullptr) { |
| 49 | if (!init()) { |
| 50 | dlclose(res); |
| 51 | *error_msg = StringPrintf("Initialization of plugin failed"); |
| 52 | return false; |
| 53 | } |
| 54 | } else { |
| 55 | LOG(WARNING) << this << " does not include an initialization function"; |
| 56 | } |
| 57 | dlopen_handle_ = res; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool Plugin::Unload() { |
Alex Light | 3b08bcc | 2019-09-11 09:48:51 -0700 | [diff] [blame] | 62 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 63 | DCHECK(IsLoaded()); |
| 64 | bool ret = true; |
| 65 | void* handle = dlopen_handle_; |
| 66 | PluginDeinitializationFunction deinit = reinterpret_cast<PluginDeinitializationFunction>( |
| 67 | dlsym(handle, PLUGIN_DEINITIALIZATION_FUNCTION_NAME)); |
| 68 | if (deinit != nullptr) { |
| 69 | if (!deinit()) { |
| 70 | LOG(WARNING) << this << " failed deinitialization"; |
| 71 | ret = false; |
| 72 | } |
| 73 | } else { |
| 74 | LOG(WARNING) << this << " does not include a deinitialization function"; |
| 75 | } |
| 76 | dlopen_handle_ = nullptr; |
Alex Light | 5b80358 | 2017-10-16 10:59:26 -0700 | [diff] [blame] | 77 | // Don't bother to actually dlclose since we are shutting down anyway and there might be small |
| 78 | // amounts of processing still being done. |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | std::ostream& operator<<(std::ostream &os, const Plugin* m) { |
| 83 | return os << *m; |
| 84 | } |
| 85 | |
| 86 | std::ostream& operator<<(std::ostream &os, Plugin const& m) { |
| 87 | return os << "Plugin { library=\"" << m.library_ << "\", handle=" << m.dlopen_handle_ << " }"; |
| 88 | } |
| 89 | |
| 90 | } // namespace art |