LCOV - code coverage report
Current view: top level - basic_component/package_manager/src - aicpu_package_process.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.4 % 130 124
Test Date: 2026-07-28 10:52:48 Functions: 100.0 % 11 11

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "aicpu_package_process.h"
      12              : 
      13              : #include <regex>
      14              : #include <memory>
      15              : #include <fstream>
      16              : #include <sstream>
      17              : #include "securec.h"
      18              : #include "tsd_log.h"
      19              : #include "tsd_scope_guard.h"
      20              : #include "tsd_path_mgr.h"
      21              : #include "tsd_util_func.h"
      22              : #include "package_worker_utils.h"
      23              : 
      24              : namespace tsd {
      25              : namespace {
      26              : const std::string SAND_BOX_DIR_NAME = "sand_box";
      27              : const std::string SAND_BOX_SO_ITEM_NAME = "SandBoxSo=";
      28              : } // namespace
      29              : 
      30            5 : TSD_StatusT AicpuPackageProcess::CheckPackageName(const std::string& soInstallPath, const std::string& packageName)
      31              : {
      32            5 :     std::string srcPkgName("");
      33            5 :     TSD_StatusT ret = GetSrcPackageName(packageName, srcPkgName);
      34            5 :     if (ret != TSD_OK) {
      35            1 :         TSD_ERROR("Get src package name failed");
      36            1 :         return ret;
      37              :     }
      38              : 
      39            4 :     std::string innerPkgName("");
      40            4 :     ret = GetInnerPkgName(soInstallPath, innerPkgName);
      41            4 :     if (ret != TSD_OK) {
      42            2 :         TSD_ERROR("Get inner package name failed, soInstallPath=%s", soInstallPath.c_str());
      43            2 :         return ret;
      44              :     }
      45              : 
      46            2 :     if (srcPkgName != innerPkgName) {
      47            1 :         TSD_ERROR(
      48              :             "Package source name is not same as package inner name, srcPkgName=%s, innerPkgName=%s", srcPkgName.c_str(),
      49              :             innerPkgName.c_str());
      50            1 :         return TSD_INTERNAL_ERROR;
      51              :     }
      52              : 
      53            1 :     return TSD_OK;
      54            5 : }
      55              : 
      56            5 : TSD_StatusT AicpuPackageProcess::GetSrcPackageName(const std::string& packageName, std::string& srcPkgName)
      57              : {
      58            5 :     std::smatch ret;
      59            5 :     const std::regex rule("Ascend.*\\.tar\\.gz");
      60            5 :     const bool searchRet = std::regex_search(packageName, ret, rule);
      61            5 :     if (!searchRet) {
      62            1 :         TSD_ERROR("Package name is invalid, name=%s", packageName.c_str());
      63            1 :         return TSD_START_FAIL;
      64              :     }
      65              : 
      66            4 :     srcPkgName = ret.str();
      67              : 
      68            4 :     return TSD_OK;
      69            5 : }
      70              : 
      71            4 : TSD_StatusT AicpuPackageProcess::GetInnerPkgName(const std::string& soInstallPath, std::string& innerPkgName)
      72              : {
      73            6 :     const auto handler = [&innerPkgName](const std::string& line) -> bool {
      74            6 :         const auto idx = line.find("=");
      75            6 :         if ((idx != std::string::npos) && (line.find("Name") != std::string::npos)) {
      76            2 :             innerPkgName = line.substr(idx + 1UL);
      77            2 :             TSD_INFO("Get inner package name success, name=%s", innerPkgName.c_str());
      78            2 :             return true;
      79              :         }
      80              : 
      81            4 :         return false;
      82            4 :     };
      83              : 
      84            4 :     const TSD_StatusT ret = WalkInVersionFile(soInstallPath, handler);
      85            4 :     if (ret != TSD_OK) {
      86            2 :         TSD_ERROR("Walk in version file to get inner package name failed, path=%s", soInstallPath.c_str());
      87              :     }
      88              : 
      89            4 :     return ret;
      90              : }
      91              : 
      92            8 : TSD_StatusT AicpuPackageProcess::WalkInVersionFile(const std::string& soInstallPath, const VersionLineHandler& handler)
      93              : {
      94            8 :     std::unique_ptr<char_t[]> path(new (std::nothrow) char_t[PATH_MAX]);
      95            8 :     if (path == nullptr) {
      96            0 :         TSD_ERROR("Alloc memory for path failed.");
      97            0 :         return TSD_INTERNAL_ERROR;
      98              :     }
      99              : 
     100            8 :     const int32_t eRet = memset_s(path.get(), PATH_MAX, 0, PATH_MAX);
     101            8 :     if (eRet != EOK) {
     102            1 :         TSD_ERROR("Memset path failed, ret=%d", eRet);
     103            1 :         return TSD_INTERNAL_ERROR;
     104              :     }
     105              : 
     106            7 :     const std::string versionInfoFilePath = TsdPathMgr::AddVersionInfoName(soInstallPath);
     107            7 :     if (realpath(versionInfoFilePath.data(), path.get()) == nullptr) {
     108            2 :         TSD_ERROR(
     109              :             "Get real path of version file failed, file=%s, reason=%s", versionInfoFilePath.c_str(),
     110              :             SafeStrerror().c_str());
     111            2 :         return TSD_INTERNAL_ERROR;
     112              :     }
     113              : 
     114            5 :     TSD_INFO("Start walk in version file, file=%s", path.get());
     115              : 
     116            5 :     std::fstream f;
     117            5 :     f.open(path.get(), std::fstream::in);
     118            5 :     if (!f.is_open()) {
     119            1 :         TSD_ERROR("Open version file failed, file=%s", path.get());
     120            1 :         return TSD_INTERNAL_ERROR;
     121              :     }
     122            8 :     ScopeGuard fGuard([&f]() { f.close(); });
     123              : 
     124            4 :     std::string line("");
     125           14 :     while (getline(f, line)) {
     126           14 :         if (handler(line)) {
     127            4 :             break;
     128              :         };
     129              :     }
     130              : 
     131            4 :     return TSD_OK;
     132            8 : }
     133              : 
     134            3 : TSD_StatusT AicpuPackageProcess::MoveSoToSandBox(const std::string& soInstallPath)
     135              : {
     136            3 :     std::string soList("");
     137            3 :     TSD_StatusT ret = GetSandBoxSoListInVersionFile(soInstallPath, soList);
     138            3 :     if ((ret != TSD_OK) || (soList.empty())) {
     139            1 :         TSD_ERROR("Sandbox so item value is empty, ret=%u.", ret);
     140            1 :         return TSD_INTERNAL_ERROR;
     141              :     }
     142              : 
     143              :     // make sand box dir
     144            2 :     const std::string sandBoxPath = soInstallPath + SAND_BOX_DIR_NAME + "/";
     145            2 :     ret = PackageWorkerUtils::MakeDirectory(sandBoxPath);
     146            2 :     if (ret != TSD_OK) {
     147            1 :         TSD_ERROR("Check or make sandbox dir failed, ret=%u, path=%s", ret, sandBoxPath.c_str());
     148            1 :         return TSD_INTERNAL_ERROR;
     149              :     }
     150              : 
     151              :     // move so to sand box
     152            1 :     std::istringstream iss(soList);
     153            1 :     std::string soName("");
     154            2 :     while (getline(iss, soName, ',')) {
     155            1 :         Trim(soName);
     156            1 :         if (soName.empty()) {
     157            0 :             continue;
     158              :         }
     159            1 :         const std::string orgFile = soInstallPath + soName;
     160            1 :         const std::string dstFile = sandBoxPath + soName;
     161            1 :         const int32_t status = rename(orgFile.c_str(), dstFile.c_str());
     162            1 :         if (status != 0) {
     163            0 :             TSD_RUN_WARN(
     164              :                 "Rename sandbox so failed, status=%d, orgFile=%s, dstFile=%s, reason=%s,", status, orgFile.c_str(),
     165              :                 dstFile.c_str(), SafeStrerror().c_str());
     166              :         } else {
     167            1 :             TSD_RUN_INFO("Rename sandbox so success, orgFile=%s, dstFile=%s", orgFile.c_str(), dstFile.c_str());
     168              :         }
     169            1 :     }
     170              : 
     171            1 :     return TSD_OK;
     172            3 : }
     173              : 
     174            3 : TSD_StatusT AicpuPackageProcess::GetSandBoxSoListInVersionFile(const std::string& soInstallPath, std::string& soList)
     175              : {
     176            8 :     const auto handler = [&soList](const std::string& line) -> bool {
     177            8 :         const auto idx = line.find("=");
     178            8 :         if ((idx != std::string::npos) && (line.find(SAND_BOX_SO_ITEM_NAME) != std::string::npos)) {
     179            2 :             soList = line.substr(idx + 1UL);
     180            2 :             TSD_INFO("Get sand box items success, soList=%s", soList.c_str());
     181            2 :             return true;
     182              :         }
     183              : 
     184            6 :         return false;
     185            3 :     };
     186              : 
     187            3 :     const TSD_StatusT ret = WalkInVersionFile(soInstallPath, handler);
     188            3 :     if (ret != TSD_OK) {
     189            1 :         TSD_ERROR("Walk in version file to get sandbox so list failed, path=%s", soInstallPath.c_str());
     190              :     }
     191              : 
     192            3 :     return ret;
     193              : }
     194              : 
     195            2 : bool AicpuPackageProcess::IsSoExist(const uint32_t uniqueVfId)
     196              : {
     197            2 :     const std::string path = TsdPathMgr::BuildKernelSoPath(uniqueVfId);
     198            2 :     if (access(path.c_str(), F_OK) != 0) {
     199            1 :         TSD_INFO("Aicpu so does not exist, path=%s", path.c_str());
     200            1 :         return false;
     201              :     }
     202            1 :     return true;
     203            2 : }
     204              : 
     205            4 : TSD_StatusT AicpuPackageProcess::CopyExtendSoToCommonSoPath(const std::string& soInstallRootPath, const bool isAsan)
     206              : {
     207            4 :     const std::string aicpuSoPath = TsdPathMgr::BuildKernelSoPath(soInstallRootPath);
     208            4 :     const std::string extendSoPath = TsdPathMgr::BuildExtendKernelSoPath(soInstallRootPath);
     209            4 :     const std::string extendHashCfgPath = TsdPathMgr::BuildExtendKernelHashCfgPath(soInstallRootPath);
     210            4 :     const std::string hashCfgFile = extendSoPath + BASE_HASH_CFG_FILE;
     211            4 :     std::string cmd;
     212            4 :     if (access(hashCfgFile.c_str(), F_OK) == 0) {
     213            0 :         cmd = "mkdir -p " + aicpuSoPath + " ; mv " + extendSoPath + "*.so* " + aicpuSoPath + " ; mv " + hashCfgFile +
     214            0 :               " " + extendHashCfgPath + " && rm -rf " + extendSoPath;
     215              :     } else {
     216              :         cmd =
     217            4 :             "mkdir -p " + aicpuSoPath + " ; mv " + extendSoPath + "*.so* " + aicpuSoPath + " && rm -rf " + extendSoPath;
     218              :     }
     219            4 :     const int32_t ret = PackSystem(cmd.c_str());
     220            4 :     if ((isAsan) && (ret != 0)) {
     221            1 :         TSD_INFO(
     222              :             "Move extend so to common so path end. cmd=%s, ret=%d, reason=%s.", cmd.c_str(), ret,
     223              :             SafeStrerror().c_str());
     224            3 :     } else if (ret != 0) {
     225            1 :         TSD_ERROR(
     226              :             "Move extend so to common so path failed. cmd=%s, ret=%d, reason=%s.", cmd.c_str(), ret,
     227              :             SafeStrerror().c_str());
     228            1 :         return TSD_INTERNAL_ERROR;
     229              :     }
     230              : 
     231            3 :     TSD_RUN_INFO("Move extend so to common so path success, cmd=%s", cmd.c_str());
     232            3 :     return TSD_OK;
     233            4 : }
     234              : } // namespace tsd
        

Generated by: LCOV version 2.0-1