Match-id-07c112cdd8cc885546cc9ab91bfc4de70eaafc66

This commit is contained in:
BianTanggui
2022-06-24 19:18:03 +08:00
committed by BianTanggui
parent 2795903944
commit 2bb9bfa00b
2 changed files with 25 additions and 18 deletions

View File

@@ -8,8 +8,9 @@ add_compile_options(-fstack-protector-all -D _GNU_SOURCE -Wl,--no-undefined)
message(STATUS "CMAKE_SHARED_LIBRARY_LINK_C_FLAGS = " ${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS})
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
include_directories("${PROJECT_SOURCE_DIR}/../../platform/HuaweiSecureC/include")
include_directories("${PROJECT_SOURCE_DIR}/../../cli/src")
aux_source_directory(. SRC)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../build/HuaweiSecureC ${CMAKE_CURRENT_SOURCE_DIR}/../../build/HuaweiSecureC)
add_executable(ascend-docker-destroy ${SRC})
add_executable(ascend-docker-destroy ../../cli/src/logger.c ../../cli/src/utils.c ../../cli/src/basic.c ${SRC} )
target_compile_options(ascend-docker-destroy PRIVATE -fstack-protector-all -fpie -ldl -D_FORTIFY_SOURCE=2 -O2)
target_link_libraries(ascend-docker-destroy -ldl -pie -Wl,-s,-z,now HuaweiSecureC)

View File

@@ -15,6 +15,9 @@
#include <link.h>
#include <dlfcn.h>
#include "securec.h"
#include "basic.h"
#include "logger.h"
#include "utils.h"
#define DCMI_INIT "dcmi_init"
#define DCMI_SET_DESTROY_VDEVICE "dcmi_set_destroy_vdevice"
@@ -28,8 +31,7 @@
static bool ShowExceptionInfo(const char* exceptionInfo)
{
(void)fprintf(stderr, exceptionInfo);
(void)fprintf(stderr, "\n");
Logger(exceptionInfo, LEVEL_ERROR, SCREEN_YES);
return false;
}
@@ -79,7 +81,6 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
struct stat fileStat;
if ((stat(buf, &fileStat) != 0) ||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
(void)fprintf(stderr, "[2: %s]\n", buf);
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
}
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
@@ -88,7 +89,7 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
return CheckParentDir(buf, PATH_MAX, fileStat, checkOwner);
}
static bool IsValidChar(const char c)
static bool IsAValidChar(const char c)
{
if (isalnum(c) != 0) {
return true;
@@ -108,14 +109,14 @@ static bool CheckFileName(const char* filePath, const size_t filePathLen)
return ShowExceptionInfo("filePathLen out of bounds!");
}
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
if (!IsValidChar(filePath[iLoop])) { // 非法字符
if (!IsAValidChar(filePath[iLoop])) { // 非法字符
return ShowExceptionInfo("filePath has an illegal character!");
}
}
return true;
}
static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
static bool CheckAExternalFile(const char* filePath, const size_t filePathLen,
const size_t maxFileSzieMb, const bool checkOwner)
{
if (filePath == NULL) {
@@ -138,7 +139,7 @@ static bool DeclareDcmiApiAndCheck(void **handle)
{
*handle = dlopen("libdcmi.so", RTLD_LAZY);
if (*handle == NULL) {
(void)fprintf(stderr, "dlopen failed.\n");
Logger("dlopen failed.\n", LEVEL_ERROR, SCREEN_YES);
return false;
}
char pLinkMap[sizeof(struct link_map)] = {0};
@@ -146,12 +147,12 @@ static bool DeclareDcmiApiAndCheck(void **handle)
if (ret == 0) {
struct link_map* pLink = *(struct link_map**)pLinkMap;
const size_t maxFileSzieMb = 10; // max 10 mb
if (!CheckExternalFile(pLink->l_name, strlen(pLink->l_name), maxFileSzieMb, true)) {
(void)fprintf(stderr, "check sofile failed.\n");
if (!CheckAExternalFile(pLink->l_name, strlen(pLink->l_name), maxFileSzieMb, true)) {
Logger("check sofile failed.\n", LEVEL_ERROR, SCREEN_YES);
return false;
}
} else {
(void)fprintf(stderr, "dlinfo sofile failed.\n");
Logger("dlinfo sofile failed.\n", LEVEL_ERROR, SCREEN_YES);
return false;
}
@@ -160,8 +161,7 @@ static bool DeclareDcmiApiAndCheck(void **handle)
static void DcmiDlAbnormalExit(void **handle, const char* errorInfo)
{
(void)fprintf(stderr, errorInfo);
(void)fprintf(stderr, "\n");
Logger(errorInfo, LEVEL_INFO, SCREEN_YES);
if (*handle != NULL) {
dlclose(*handle);
*handle = NULL;
@@ -216,7 +216,7 @@ static bool DcmiInitProcess(void *handle)
}
int ret = dcmi_init();
if (ret != 0) {
(void)fprintf(stderr, "dcmi_init failed, ret = %d\n", ret);
Logger("dcmi_init faile.\n", LEVEL_ERROR, SCREEN_YES);
DcmiDlclose(&handle);
return false;
}
@@ -237,7 +237,7 @@ static bool DcmiDestroyProcess(void *handle, const int cardId,
}
int ret = dcmi_set_destroy_vdevice(cardId, deviceId, vDeviceId);
if (ret != 0) {
(void)fprintf(stderr, "dcmi_set_destroy_vdevice failed, ret = %d\n", ret);
Logger("dcmi_set_destroy_vdevice failed.\n", LEVEL_ERROR, SCREEN_YES);
DcmiDlclose(&handle);
return false;
}
@@ -252,13 +252,16 @@ static int DestroyEntrance(const char *argv[])
int cardId = 0;
int deviceId = 0;
int vDeviceId = 0;
char *str = FormatLogMessage("start to destroy v-device %d start...\n", vDeviceId);
Logger(str, LEVEL_INFO, SCREEN_YES);
free(str);
if (!GetAndCheckID(argv, &cardId, &deviceId, &vDeviceId)) {
return -1;
}
void *handle = NULL;
if (!DeclareDcmiApiAndCheck(&handle)) {
(void)fprintf(stderr, "Declare dcmi failed.\n");
Logger("Declare dcmi failed.\n", LEVEL_ERROR, SCREEN_YES);
return -1;
}
if (!DcmiInitProcess(handle)) {
@@ -268,13 +271,16 @@ static int DestroyEntrance(const char *argv[])
return -1;
}
DcmiDlclose(&handle);
char *strEnd = FormatLogMessage("destroy v-device %d successfully.\n", vDeviceId);
Logger(strEnd, LEVEL_INFO, SCREEN_YES);
free(strEnd);
return 0;
}
static bool EntryCheck(const int argc, const char *argv[])
{
if (argc != DESTROY_PARAMS_NUM) {
(void)fprintf(stderr, "destroy params namber error.\n");
Logger("destroy params namber error.\n", LEVEL_ERROR, SCREEN_YES);
return false;
}
for (int iLoop = 1; iLoop < argc; iLoop++) {
@@ -290,7 +296,7 @@ static bool EntryCheck(const int argc, const char *argv[])
int main(const int argc, const char *argv[])
{
if (!EntryCheck(argc, argv)) {
(void)fprintf(stderr, "destroy params value error.\n");
Logger("destroy params value error.\n", LEVEL_ERROR, SCREEN_YES);
return -1;
}