blob: c12d17d0cdf4fa2bfac710a0e854bfde38355d8d [file] [log] [blame]
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +05301/*
2 * Copyright (C) 2022 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 "android.hardware.cas-SharedLibrary"
18
19#include "SharedLibrary.h"
20#include <dlfcn.h>
21#include <utils/Log.h>
22
23namespace aidl {
24namespace android {
25namespace hardware {
26namespace cas {
27
28SharedLibrary::SharedLibrary(const String8& path) {
Sebastian Pickl8b192862023-09-14 08:42:44 +000029 mLibHandle = dlopen(path.c_str(), RTLD_NOW);
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +053030}
31
32SharedLibrary::~SharedLibrary() {
33 if (mLibHandle != NULL) {
Sebastian Pickl8b192862023-09-14 08:42:44 +000034 dlclose(mLibHandle);
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +053035 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 // Clear last error before we load the symbol again,
48 // in case the caller didn't retrieve it.
49 (void)dlerror();
50 return dlsym(mLibHandle, symbol);
51}
52
53const char* SharedLibrary::lastError() const {
54 const char* error = dlerror();
55 return error ? error : "No errors or unknown error";
56}
57
58} // namespace cas
59} // namespace hardware
60} // namespace android
61} // namespace aidl