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" |
| 22 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 25 | using android::base::StringPrintf; |
| 26 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 27 | const char* PLUGIN_INITIALIZATION_FUNCTION_NAME = "ArtPlugin_Initialize"; |
| 28 | const char* PLUGIN_DEINITIALIZATION_FUNCTION_NAME = "ArtPlugin_Deinitialize"; |
| 29 | |
| 30 | Plugin::Plugin(const Plugin& other) : library_(other.library_), dlopen_handle_(nullptr) { |
| 31 | if (other.IsLoaded()) { |
| 32 | std::string err; |
| 33 | Load(&err); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | bool Plugin::Load(/*out*/std::string* error_msg) { |
| 38 | DCHECK(!IsLoaded()); |
| 39 | void* res = dlopen(library_.c_str(), RTLD_LAZY); |
| 40 | if (res == nullptr) { |
| 41 | *error_msg = StringPrintf("dlopen failed: %s", dlerror()); |
| 42 | return false; |
| 43 | } |
| 44 | // Get the initializer function |
| 45 | PluginInitializationFunction init = reinterpret_cast<PluginInitializationFunction>( |
| 46 | dlsym(res, PLUGIN_INITIALIZATION_FUNCTION_NAME)); |
| 47 | if (init != nullptr) { |
| 48 | if (!init()) { |
| 49 | dlclose(res); |
| 50 | *error_msg = StringPrintf("Initialization of plugin failed"); |
| 51 | return false; |
| 52 | } |
| 53 | } else { |
| 54 | LOG(WARNING) << this << " does not include an initialization function"; |
| 55 | } |
| 56 | dlopen_handle_ = res; |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool Plugin::Unload() { |
| 61 | DCHECK(IsLoaded()); |
| 62 | bool ret = true; |
| 63 | void* handle = dlopen_handle_; |
| 64 | PluginDeinitializationFunction deinit = reinterpret_cast<PluginDeinitializationFunction>( |
| 65 | dlsym(handle, PLUGIN_DEINITIALIZATION_FUNCTION_NAME)); |
| 66 | if (deinit != nullptr) { |
| 67 | if (!deinit()) { |
| 68 | LOG(WARNING) << this << " failed deinitialization"; |
| 69 | ret = false; |
| 70 | } |
| 71 | } else { |
| 72 | LOG(WARNING) << this << " does not include a deinitialization function"; |
| 73 | } |
| 74 | dlopen_handle_ = nullptr; |
Alex Light | 5b80358 | 2017-10-16 10:59:26 -0700 | [diff] [blame] | 75 | // Don't bother to actually dlclose since we are shutting down anyway and there might be small |
| 76 | // amounts of processing still being done. |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | std::ostream& operator<<(std::ostream &os, const Plugin* m) { |
| 81 | return os << *m; |
| 82 | } |
| 83 | |
| 84 | std::ostream& operator<<(std::ostream &os, Plugin const& m) { |
| 85 | return os << "Plugin { library=\"" << m.library_ << "\", handle=" << m.dlopen_handle_ << " }"; |
| 86 | } |
| 87 | |
| 88 | } // namespace art |