blob: 0a942cddeb40991efa90034483531dc897b944af [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
Jiyong Parkacfcb302017-06-19 22:06:41 +090017
18#include "SharedLibrary.h"
Jeff Tinker0ea40692017-01-11 19:42:05 -080019
20#include <dlfcn.h>
Jeff Tinker0ea40692017-01-11 19:42:05 -080021
Jiyong Parkacfcb302017-06-19 22:06:41 +090022namespace android {
23namespace hardware {
24namespace drm {
25namespace V1_0 {
26namespace helper {
Jeff Tinker0ea40692017-01-11 19:42:05 -080027
Jiyong Parkacfcb302017-06-19 22:06:41 +090028SharedLibrary::SharedLibrary(const String8& path) {
29 mLibHandle = dlopen(path.string(), RTLD_NOW);
Jeff Tinker0ea40692017-01-11 19:42:05 -080030}
31
32SharedLibrary::~SharedLibrary() {
33 if (mLibHandle != NULL) {
34 dlclose(mLibHandle);
35 mLibHandle = NULL;
36 }
37}
38
39bool SharedLibrary::operator!() const {
40 return mLibHandle == NULL;
41}
42
43void* SharedLibrary::lookup(const char* symbol) const {
44 if (!mLibHandle) {
45 return NULL;
46 }
47
48 // Clear last error before we load the symbol again,
49 // in case the caller didn't retrieve it.
50 (void)dlerror();
51 return dlsym(mLibHandle, symbol);
52}
53
54const char* SharedLibrary::lastError() const {
55 const char* error = dlerror();
56 return error ? error : "No errors or unknown error";
57}
Jiyong Parkacfcb302017-06-19 22:06:41 +090058
59}
60}
61}
62}
63} // namespace android