1fc0b0055Sopenharmony_ci/* 2fc0b0055Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License. 5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at 6fc0b0055Sopenharmony_ci * 7fc0b0055Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fc0b0055Sopenharmony_ci * 9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and 13fc0b0055Sopenharmony_ci * limitations under the License. 14fc0b0055Sopenharmony_ci */ 15fc0b0055Sopenharmony_ci 16fc0b0055Sopenharmony_ci#include "libraryloader.h" 17fc0b0055Sopenharmony_ci 18fc0b0055Sopenharmony_ci#include <dlfcn.h> 19fc0b0055Sopenharmony_ci#include <string> 20fc0b0055Sopenharmony_ci 21fc0b0055Sopenharmony_ci#include "accesstoken_log.h" 22fc0b0055Sopenharmony_ci 23fc0b0055Sopenharmony_cinamespace OHOS { 24fc0b0055Sopenharmony_cinamespace Security { 25fc0b0055Sopenharmony_cinamespace AccessToken { 26fc0b0055Sopenharmony_cinamespace { 27fc0b0055Sopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 28fc0b0055Sopenharmony_ci SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenLibLoader"}; 29fc0b0055Sopenharmony_citypedef void* (*FUNC_CREATE) (void); 30fc0b0055Sopenharmony_citypedef void (*FUNC_DESTROY) (void*); 31fc0b0055Sopenharmony_ci} 32fc0b0055Sopenharmony_ci 33fc0b0055Sopenharmony_ciLibraryLoader::LibraryLoader(const std::string& path) 34fc0b0055Sopenharmony_ci{ 35fc0b0055Sopenharmony_ci handle_ = dlopen(path.c_str(), RTLD_LAZY); 36fc0b0055Sopenharmony_ci if (handle_ == nullptr) { 37fc0b0055Sopenharmony_ci PrintErrorLog(path); 38fc0b0055Sopenharmony_ci return; 39fc0b0055Sopenharmony_ci } 40fc0b0055Sopenharmony_ci Create(); 41fc0b0055Sopenharmony_ci} 42fc0b0055Sopenharmony_ci 43fc0b0055Sopenharmony_ciLibraryLoader::~LibraryLoader() 44fc0b0055Sopenharmony_ci{ 45fc0b0055Sopenharmony_ci if (instance_ != nullptr) { 46fc0b0055Sopenharmony_ci Destroy(); 47fc0b0055Sopenharmony_ci } 48fc0b0055Sopenharmony_ci#ifndef FUZZ_ENABLE 49fc0b0055Sopenharmony_ci if (handle_ != nullptr) { 50fc0b0055Sopenharmony_ci dlclose(handle_); 51fc0b0055Sopenharmony_ci handle_ = nullptr; 52fc0b0055Sopenharmony_ci } 53fc0b0055Sopenharmony_ci#endif // FUZZ_ENABLE 54fc0b0055Sopenharmony_ci} 55fc0b0055Sopenharmony_ci 56fc0b0055Sopenharmony_cibool LibraryLoader::PrintErrorLog(const std::string& targetName) 57fc0b0055Sopenharmony_ci{ 58fc0b0055Sopenharmony_ci char* error; 59fc0b0055Sopenharmony_ci if ((error = dlerror()) != nullptr) { 60fc0b0055Sopenharmony_ci ACCESSTOKEN_LOG_ERROR(LABEL, "Get %{public}s failed, errMsg=%{public}s.", 61fc0b0055Sopenharmony_ci targetName.c_str(), error); 62fc0b0055Sopenharmony_ci return false; 63fc0b0055Sopenharmony_ci } 64fc0b0055Sopenharmony_ci return true; 65fc0b0055Sopenharmony_ci} 66fc0b0055Sopenharmony_ci 67fc0b0055Sopenharmony_civoid LibraryLoader::Create() 68fc0b0055Sopenharmony_ci{ 69fc0b0055Sopenharmony_ci void* (*create)(void) = reinterpret_cast<FUNC_CREATE>(dlsym(handle_, "Create")); 70fc0b0055Sopenharmony_ci if (!PrintErrorLog("Create")) { 71fc0b0055Sopenharmony_ci return; 72fc0b0055Sopenharmony_ci } 73fc0b0055Sopenharmony_ci instance_ = create(); 74fc0b0055Sopenharmony_ci} 75fc0b0055Sopenharmony_ci 76fc0b0055Sopenharmony_civoid LibraryLoader::Destroy() 77fc0b0055Sopenharmony_ci{ 78fc0b0055Sopenharmony_ci void (*destroy)(void*) = reinterpret_cast<FUNC_DESTROY>(dlsym(handle_, "Destroy")); 79fc0b0055Sopenharmony_ci if (!PrintErrorLog("Destroy")) { 80fc0b0055Sopenharmony_ci return; 81fc0b0055Sopenharmony_ci } 82fc0b0055Sopenharmony_ci destroy(instance_); 83fc0b0055Sopenharmony_ci instance_ = nullptr; 84fc0b0055Sopenharmony_ci} 85fc0b0055Sopenharmony_ci} // AccessToken 86fc0b0055Sopenharmony_ci} // Security 87fc0b0055Sopenharmony_ci} // OHOS