blob: 6658150fd6c0186b664d371f0738243f9fbcf367 [file] [log] [blame]
Jeff Tinker0ea40692017-01-11 19:42:05 -08001/*
2 * Copyright (C) 2017 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#define LOG_TAG "drm-vts-shared-library"
18
19#include <dlfcn.h>
20#include <shared_library.h>
21
22using std::string;
23
24namespace drm_vts {
25
26SharedLibrary::SharedLibrary(const string& path) {
27 mLibHandle = dlopen(path.c_str(), RTLD_NOW);
28}
29
30SharedLibrary::~SharedLibrary() {
31 if (mLibHandle != NULL) {
32 dlclose(mLibHandle);
33 mLibHandle = NULL;
34 }
35}
36
37bool SharedLibrary::operator!() const {
38 return mLibHandle == NULL;
39}
40
41void* SharedLibrary::lookup(const char* symbol) const {
42 if (!mLibHandle) {
43 return NULL;
44 }
45
46 // Clear last error before we load the symbol again,
47 // in case the caller didn't retrieve it.
48 (void)dlerror();
49 return dlsym(mLibHandle, symbol);
50}
51
52const char* SharedLibrary::lastError() const {
53 const char* error = dlerror();
54 return error ? error : "No errors or unknown error";
55}
56};