Jeff Tinker | 441a78d | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_NDEBUG 0 |
| 18 | #define LOG_TAG "Drm" |
Jeff Tinker | 441a78d | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 19 | |
| 20 | #include <dlfcn.h> |
Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 21 | #include <media/stagefright/foundation/ADebug.h> |
| 22 | #include <media/SharedLibrary.h> |
| 23 | #include <utils/Log.h> |
Jeff Tinker | 441a78d | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | SharedLibrary::SharedLibrary(const String8 &path) { |
| 28 | mLibHandle = dlopen(path.string(), RTLD_NOW); |
| 29 | } |
| 30 | |
| 31 | SharedLibrary::~SharedLibrary() { |
| 32 | if (mLibHandle != NULL) { |
| 33 | dlclose(mLibHandle); |
| 34 | mLibHandle = NULL; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | bool SharedLibrary::operator!() const { |
| 39 | return mLibHandle == NULL; |
| 40 | } |
| 41 | |
| 42 | void *SharedLibrary::lookup(const char *symbol) const { |
| 43 | if (!mLibHandle) { |
| 44 | return NULL; |
| 45 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 46 | // Clear last error before we load the symbol again, |
| 47 | // in case the caller didn't retrieve it. |
| 48 | (void)dlerror(); |
Jeff Tinker | 441a78d | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 49 | return dlsym(mLibHandle, symbol); |
| 50 | } |
leozwang@google.com | d1eff57 | 2013-07-13 21:52:50 -0700 | [diff] [blame] | 51 | |
| 52 | const char *SharedLibrary::lastError() const { |
| 53 | const char *error = dlerror(); |
| 54 | return error ? error : "No errors or unknown error"; |
| 55 | } |
| 56 | |
Jeff Tinker | 441a78d | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 57 | }; |