blob: 6b9e0081cf8f98efa500893675ff1d22bdaa5c55 [file] [log] [blame]
Alex Light185d1342016-08-11 10:48:03 -07001/*
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 Gampe46ee31b2016-12-14 10:11:49 -080020
21#include "android-base/stringprintf.h"
Alex Light3b08bcc2019-09-11 09:48:51 -070022#include "base/locks.h"
23#include "base/mutex.h"
24#include "thread-current-inl.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080025
Alex Light185d1342016-08-11 10:48:03 -070026namespace art {
27
Andreas Gampe46ee31b2016-12-14 10:11:49 -080028using android::base::StringPrintf;
29
Alex Light185d1342016-08-11 10:48:03 -070030const char* PLUGIN_INITIALIZATION_FUNCTION_NAME = "ArtPlugin_Initialize";
31const char* PLUGIN_DEINITIALIZATION_FUNCTION_NAME = "ArtPlugin_Deinitialize";
32
33Plugin::Plugin(const Plugin& other) : library_(other.library_), dlopen_handle_(nullptr) {
Alex Light443a6a52018-03-02 16:37:18 -080034 CHECK(!other.IsLoaded()) << "Should not copy loaded plugins.";
Alex Light185d1342016-08-11 10:48:03 -070035}
36
37bool Plugin::Load(/*out*/std::string* error_msg) {
Alex Light3b08bcc2019-09-11 09:48:51 -070038 Locks::mutator_lock_->AssertNotHeld(Thread::Current());
Alex Light185d1342016-08-11 10:48:03 -070039 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
61bool Plugin::Unload() {
Alex Light3b08bcc2019-09-11 09:48:51 -070062 Locks::mutator_lock_->AssertNotHeld(Thread::Current());
Alex Light185d1342016-08-11 10:48:03 -070063 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 Light5b803582017-10-16 10:59:26 -070077 // 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 Light185d1342016-08-11 10:48:03 -070079 return ret;
80}
81
82std::ostream& operator<<(std::ostream &os, const Plugin* m) {
83 return os << *m;
84}
85
86std::ostream& operator<<(std::ostream &os, Plugin const& m) {
87 return os << "Plugin { library=\"" << m.library_ << "\", handle=" << m.dlopen_handle_ << " }";
88}
89
90} // namespace art