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> 20 21 #include "android-base/stringprintf.h" 22 23 namespace art { 24 25 using android::base::StringPrintf; 26 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 CHECK(!other.IsLoaded()) << "Should not copy loaded plugins."; 32 } 33 34 bool Plugin::Load(/*out*/std::string* error_msg) { 35 DCHECK(!IsLoaded()); 36 void* res = dlopen(library_.c_str(), RTLD_LAZY); 37 if (res == nullptr) { 38 *error_msg = StringPrintf("dlopen failed: %s", dlerror()); 39 return false; 40 } 41 // Get the initializer function 42 PluginInitializationFunction init = reinterpret_cast<PluginInitializationFunction>( 43 dlsym(res, PLUGIN_INITIALIZATION_FUNCTION_NAME)); 44 if (init != nullptr) { 45 if (!init()) { 46 dlclose(res); 47 *error_msg = StringPrintf("Initialization of plugin failed"); 48 return false; 49 } 50 } else { 51 LOG(WARNING) << this << " does not include an initialization function"; 52 } 53 dlopen_handle_ = res; 54 return true; 55 } 56 57 bool Plugin::Unload() { 58 DCHECK(IsLoaded()); 59 bool ret = true; 60 void* handle = dlopen_handle_; 61 PluginDeinitializationFunction deinit = reinterpret_cast<PluginDeinitializationFunction>( 62 dlsym(handle, PLUGIN_DEINITIALIZATION_FUNCTION_NAME)); 63 if (deinit != nullptr) { 64 if (!deinit()) { 65 LOG(WARNING) << this << " failed deinitialization"; 66 ret = false; 67 } 68 } else { 69 LOG(WARNING) << this << " does not include a deinitialization function"; 70 } 71 dlopen_handle_ = nullptr; 72 // Don't bother to actually dlclose since we are shutting down anyway and there might be small 73 // amounts of processing still being done. 74 return ret; 75 } 76 77 std::ostream& operator<<(std::ostream &os, const Plugin* m) { 78 return os << *m; 79 } 80 81 std::ostream& operator<<(std::ostream &os, Plugin const& m) { 82 return os << "Plugin { library=\"" << m.library_ << "\", handle=" << m.dlopen_handle_ << " }"; 83 } 84 85 } // namespace art 86