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 #include "base/locks.h"
23 #include "base/mutex.h"
24 #include "thread-current-inl.h"
25 
26 namespace art {
27 
28 using android::base::StringPrintf;
29 
30 const char* PLUGIN_INITIALIZATION_FUNCTION_NAME = "ArtPlugin_Initialize";
31 const char* PLUGIN_DEINITIALIZATION_FUNCTION_NAME = "ArtPlugin_Deinitialize";
32 
Plugin(const Plugin & other)33 Plugin::Plugin(const Plugin& other) : library_(other.library_), dlopen_handle_(nullptr) {
34   CHECK(!other.IsLoaded()) << "Should not copy loaded plugins.";
35 }
36 
Load(std::string * error_msg)37 bool Plugin::Load(/*out*/std::string* error_msg) {
38   Locks::mutator_lock_->AssertNotHeld(Thread::Current());
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 
Unload()61 bool Plugin::Unload() {
62   Locks::mutator_lock_->AssertNotHeld(Thread::Current());
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   // Don't bother to actually dlclose since we are shutting down anyway and there might be small
78   // amounts of processing still being done.
79   return ret;
80 }
81 
operator <<(std::ostream & os,const Plugin * m)82 std::ostream& operator<<(std::ostream &os, const Plugin* m) {
83   return os << *m;
84 }
85 
operator <<(std::ostream & os,Plugin const & m)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
91