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 #include "base/logging.h"
24
25 namespace art {
26
27 using android::base::StringPrintf;
28
29 const char* PLUGIN_INITIALIZATION_FUNCTION_NAME = "ArtPlugin_Initialize";
30 const char* PLUGIN_DEINITIALIZATION_FUNCTION_NAME = "ArtPlugin_Deinitialize";
31
Plugin(const Plugin & other)32 Plugin::Plugin(const Plugin& other) : library_(other.library_), dlopen_handle_(nullptr) {
33 if (other.IsLoaded()) {
34 std::string err;
35 Load(&err);
36 }
37 }
38
Load(std::string * error_msg)39 bool Plugin::Load(/*out*/std::string* error_msg) {
40 DCHECK(!IsLoaded());
41 void* res = dlopen(library_.c_str(), RTLD_LAZY);
42 if (res == nullptr) {
43 *error_msg = StringPrintf("dlopen failed: %s", dlerror());
44 return false;
45 }
46 // Get the initializer function
47 PluginInitializationFunction init = reinterpret_cast<PluginInitializationFunction>(
48 dlsym(res, PLUGIN_INITIALIZATION_FUNCTION_NAME));
49 if (init != nullptr) {
50 if (!init()) {
51 dlclose(res);
52 *error_msg = StringPrintf("Initialization of plugin failed");
53 return false;
54 }
55 } else {
56 LOG(WARNING) << this << " does not include an initialization function";
57 }
58 dlopen_handle_ = res;
59 return true;
60 }
61
Unload()62 bool Plugin::Unload() {
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;
77 if (dlclose(handle) != 0) {
78 LOG(ERROR) << this << " failed to dlclose: " << dlerror();
79 ret = false;
80 }
81 return ret;
82 }
83
operator <<(std::ostream & os,const Plugin * m)84 std::ostream& operator<<(std::ostream &os, const Plugin* m) {
85 return os << *m;
86 }
87
operator <<(std::ostream & os,Plugin const & m)88 std::ostream& operator<<(std::ostream &os, Plugin const& m) {
89 return os << "Plugin { library=\"" << m.library_ << "\", handle=" << m.dlopen_handle_ << " }";
90 }
91
92 } // namespace art
93