/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "utils_common.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "log/log.h" namespace Updater { namespace Utils { constexpr int USECONDS_PER_SECONDS = 1000000; // 1s = 1000000us constexpr int NANOSECS_PER_USECONDS = 1000; // 1us = 1000ns bool PathToRealPath(const std::string &path, std::string &realPath) { if (path.empty()) { LOG(ERROR) << "path is empty!"; return false; } if ((path.length() >= PATH_MAX)) { LOG(ERROR) << "path len is error, the len is: " << path.length(); return false; } char tmpPath[PATH_MAX] = {0}; if (realpath(path.c_str(), tmpPath) == nullptr) { LOG(ERROR) << "path to realpath error " << path; return false; } realPath = tmpPath; return true; } void UsSleep(int usec) { auto seconds = usec / USECONDS_PER_SECONDS; long nanoSeconds = static_cast(usec) % USECONDS_PER_SECONDS * NANOSECS_PER_USECONDS; struct timespec ts = { static_cast(seconds), nanoSeconds }; while (nanosleep(&ts, &ts) < 0 && errno == EINTR) { } } bool IsUpdaterMode() { struct stat st {}; if (stat("/bin/updater", &st) == 0 && S_ISREG(st.st_mode)) { LOG(INFO) << "updater mode"; return true; } LOG(INFO) << "normal mode"; return false; } void* LoadLibrary(const std::string &libName, const std::string &libPath) { if (libName.empty()) { LOG(ERROR) << "lib name is empty"; return nullptr; } if (libPath != "/system/lib64/" && libPath != "/vendor/lib64/") { LOG(ERROR) << "lib path invalid"; return nullptr; } std::string libAbsPath = libPath + libName; char realPath[PATH_MAX + 1] = {0}; if (realpath(libAbsPath.c_str(), realPath) == nullptr) { LOG(ERROR) << "realpath error"; return nullptr; } void* handle = dlopen(libAbsPath.c_str(), RTLD_LAZY); if (handle == nullptr) { LOG(ERROR) << "dlopen fail, lib name = " << libName << "; dlerror = " << dlerror(); return nullptr; } return handle; } void CloseLibrary(void* handle) { if (handle == nullptr) { LOG(ERROR) << "handle is nulptr"; return; } dlclose(handle); handle = nullptr; } void* GetFunction(void* handle, const std::string &funcName) { if (handle == nullptr) { LOG(ERROR) << "handle is nullptr"; return nullptr; } if (funcName.empty()) { LOG(ERROR) << "func name is empty"; return nullptr; } return dlsym(handle, funcName.c_str()); } } // Utils } // updater