diff --git a/cli/src/basic.h b/cli/src/basic.h index a5a77b3f8aa90691e88d1f326f2aa137df20bc9b..73bcd666386a00b0af4e711467a462a4ca73e639 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -12,11 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _BASIC_H -#define _BASIC_H +#ifndef BASIC_H +#define BASIC_H #include -#include #include #include @@ -40,15 +39,15 @@ #define SCREEN_NO 0 #define SCREEN_YES 1 -#define LOG_ERROR(fmt, ...) \ - do { \ - char _content[BUF_SIZE] = {0}; \ - int _ret = sprintf_s(_content, BUF_SIZE, fmt, ##__VA_ARGS__); \ - if (_ret < 0) { \ - (void)fprintf(stderr, "cannot assemble log content"); \ - } else { \ - (void)fprintf(stderr, "%s", (const char *)_content); \ - } \ +#define LOG_ERROR(fmt, ...) \ + do { \ + char content[BUF_SIZE] = {0}; \ + int ret = sprintf_s(content, BUF_SIZE, fmt, ##__VA_ARGS__); \ + if (ret < 0) { \ + (void)fprintf(stderr, "cannot assemble log content"); \ + } else { \ + (void)fprintf(stderr, "%s", (const char *)content); \ + } \ } while (0) #define ROOT_GAP 4 @@ -74,7 +73,7 @@ struct ParsedConfig { char rootfs[BUF_SIZE]; char containerNsPath[BUF_SIZE]; char cgroupPath[BUF_SIZE]; - int originNsFd; + int originNsFd; const struct MountList *files; const struct MountList *dirs; }; diff --git a/cli/src/cgrp.h b/cli/src/cgrp.h index 9df72f2c9d260810f96bf2e02d9931b482b00461..49ecaf14e332be30b70bbae7bcf35cbb6236ed13 100644 --- a/cli/src/cgrp.h +++ b/cli/src/cgrp.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _CGRP_H -#define _CGRP_H +#ifndef CGRP_H +#define CGRP_H #include "basic.h" diff --git a/cli/src/logger.h b/cli/src/logger.h index a2f45c8ca536e799561bca4615990e3b05ce4c55..3aad84f8ff155351075dba4357739985e67368a2 100644 --- a/cli/src/logger.h +++ b/cli/src/logger.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _LOGGER_H -#define _LOGGER_H +#ifndef LOGGER_H +#define LOGGER_H void Logger(const char *msg, int level, int screen); diff --git a/cli/src/main.c b/cli/src/main.c index e30513ee75febf018625994d5f6eff21fd18bb69..d9425815c8d699faf5ec85da3f105403080a3fcd 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -33,16 +33,16 @@ #include "utils.h" #include "logger.h" -#define DECIMAL 10 -#define MAX_ARGC 1024 +#define DECIMAL 10 +#define MAX_ARGC 1024 #define MAX_ARG_LEN 1024 bool g_allowLink = false; struct CmdArgs { - char rootfs[BUF_SIZE]; - long pid; - char options[BUF_SIZE]; + char rootfs[BUF_SIZE]; + long pid; + char options[BUF_SIZE]; struct MountList files; struct MountList dirs; }; @@ -87,8 +87,8 @@ static bool PidCmdArgParser(struct CmdArgs *args, const char *arg) return false; } (void)fclose(pFile); - if ((strlen(buff) > 0) && (buff[strlen(buff) -1] == '\n')) { - buff[strlen(buff) -1] = '\0'; + if ((strlen(buff) > 0) && (buff[strlen(buff) - 1] == '\n')) { + buff[strlen(buff) - 1] = '\0'; } for (size_t iLoop = 0; iLoop < strlen(buff); iLoop++) { if (isdigit(buff[iLoop]) == 0) { @@ -261,7 +261,7 @@ static bool MountDirCmdArgParser(struct CmdArgs *args, const char *arg) } char *dst = &args->dirs.list[args->dirs.count++][0]; - errno_t err = strcpy_s(dst, PATH_MAX, arg); + errno_t err = strcpy_s(dst, PATH_MAX, arg); if (err != EOK) { char* str = FormatLogMessage("error: failed to copy mount directory path: %s", arg); Logger(str, LEVEL_ERROR, SCREEN_YES); @@ -292,7 +292,7 @@ static bool LinkCheckCmdArgParser(const char *argv) g_allowLink = false; return true; } - + Logger("invalid link check value!", LEVEL_ERROR, SCREEN_YES); return false; } @@ -331,7 +331,7 @@ static int ParseOneCmdArg(struct CmdArgs *args, char indicator, const char *valu } else { isOK = g_cmdArgParsers[i].parser(args, value); } - + if (!isOK) { char* str = FormatLogMessage("failed while parsing cmd arg, indicate char: %c, value: %s.", indicator, value); Logger(str, LEVEL_ERROR, SCREEN_YES); @@ -390,7 +390,7 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config) } config->files = (const struct MountList *)&args->files; - config->dirs = (const struct MountList *)&args->dirs; + config->dirs = (const struct MountList *)&args->dirs; return 0; } diff --git a/cli/src/ns.h b/cli/src/ns.h index c9d20528e5ccb27eca055689693858ff17b1e527..ac14480c13aa003ff0078f12ebf323e1f29cb912 100644 --- a/cli/src/ns.h +++ b/cli/src/ns.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _NS_H -#define _NS_H +#ifndef NS_H +#define NS_H #include diff --git a/cli/src/options.c b/cli/src/options.c index e34a3a4cc4ff7c7d8029f7859dccfd585b75cb42..4ea3f7d6e615369e0ba44d1f02ed6970b9fe3f4d 100644 --- a/cli/src/options.c +++ b/cli/src/options.c @@ -65,12 +65,12 @@ void ParseRuntimeOptions(const char *options) free(runtimeOptions); } -bool IsOptionNoDrvSet() +bool IsOptionNoDrvSet(void) { return g_runtimeOptions.noDrv; } -bool IsVirtual() +bool IsVirtual(void) { return g_runtimeOptions.isVirtual; } diff --git a/cli/src/options.h b/cli/src/options.h index aade70ba31d9f3713fb3deb9ad29b5a332ae797c..8b7fb280885dbba12684cbc8978d0504d1aafbfc 100644 --- a/cli/src/options.h +++ b/cli/src/options.h @@ -12,13 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _OPTIONS_H -#define _OPTIONS_H +#ifndef OPTIONS_H +#define OPTIONS_H #include void ParseRuntimeOptions(const char *options); -bool IsOptionNoDrvSet(); -bool IsVirtual(); +bool IsOptionNoDrvSet(void); +bool IsVirtual(void); #endif diff --git a/cli/src/u_mount.c b/cli/src/u_mount.c index 03f444f8c944c5399f5e0e1482e3136a23d1a06f..582f9f6e8261e4df219f0669ebba41c74c2452c7 100644 --- a/cli/src/u_mount.c +++ b/cli/src/u_mount.c @@ -26,7 +26,7 @@ #include "options.h" #include "logger.h" -static bool checkSrcFile(const char *src) +static bool CheckSrcFile(const char *src) { struct stat fileStat; if (lstat(src, &fileStat) != 0) { @@ -60,7 +60,7 @@ int Mount(const char *src, const char *dst) static const unsigned long mountFlags = MS_BIND; static const unsigned long remountFlags = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID; - if (!checkSrcFile(src)) { + if (!CheckSrcFile(src)) { return -1; } int ret = mount(src, dst, NULL, mountFlags, NULL); diff --git a/cli/src/u_mount.h b/cli/src/u_mount.h index 59d90fe8b8e076e0e51df6a994e9f598e260fe0a..9d45d0d833edd0b85585871eb47dfd03aa73262a 100644 --- a/cli/src/u_mount.h +++ b/cli/src/u_mount.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _MOUNT_H -#define _MOUNT_H +#ifndef MOUNT_H +#define MOUNT_H #include #include "basic.h" diff --git a/cli/src/utils.c b/cli/src/utils.c index 64a5304f4d9c967ede9cd81b5181b3ec3b96c5e9..e8412613880784b1a699ed5f54ec9437f930756d 100644 --- a/cli/src/utils.c +++ b/cli/src/utils.c @@ -340,7 +340,7 @@ bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen) DIR *dir = NULL; struct dirent *ptr = NULL; char base[PATH_MAX] = {0}; - + if ((dir = opendir(basePath)) == NULL) { return ShowExceptionInfo("Open dir error!"); } @@ -350,24 +350,33 @@ bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen) } memset_s(base, PATH_MAX, 0, PATH_MAX); if (strcpy_s(base, PATH_MAX, basePath) != 0) { + closedir(dir); return ShowExceptionInfo("Strcpy failed!"); } - if (strcat_sp(base, PATH_MAX, "/") != 0 || - strcat_sp(base, PATH_MAX, ptr->d_name) != 0) { + if (strcat_sp(base, PATH_MAX, "/") != 0) { + closedir(dir); + return ShowExceptionInfo("Strcat failed!"); + } + if (strcat_sp(base, PATH_MAX, ptr->d_name) != 0) { + closedir(dir); return ShowExceptionInfo("Strcat failed!"); } if (ptr->d_type == DT_REG) { // 文件 const size_t maxFileSzieMb = 150; // max 150 MB if (!CheckFileSubset(base, strlen(base), maxFileSzieMb)) { + closedir(dir); return false; } } else if (!g_allowLink && ptr->d_type == DT_LNK) { // 软链接 + closedir(dir); return ShowExceptionInfo("FilePath has a soft link!"); } else if (ptr->d_type == DT_DIR) { // 目录 if (!GetFileSubsetAndCheck(base, strlen(base))) { + closedir(dir); return false; } } } + closedir(dir); return true; } \ No newline at end of file diff --git a/cli/src/utils.h b/cli/src/utils.h index 70d93d152ea08a169b762cdb3140f8f1f0917742..4a86dfaaed1f9e6fbcb9c72e496ac509365ee836 100644 --- a/cli/src/utils.h +++ b/cli/src/utils.h @@ -12,15 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _UTILS_H -#define _UTILS_H +#ifndef UTILS_H +#define UTILS_H #include #include -#include -#include -#include -#include #include "basic.h" char *FormatLogMessage(char *format, ...); diff --git a/destroy/src/main.c b/destroy/src/main.c index 16136f1a5d3d6bcad88fbf1aed130c9f4e77bc85..ed263ec4ad6e9eaf615b47e34f1568985a4856ce 100644 --- a/destroy/src/main.c +++ b/destroy/src/main.c @@ -29,15 +29,15 @@ #include "logger.h" #include "utils.h" -#define DCMI_INIT "dcmi_init" -#define DCMI_SET_DESTROY_VDEVICE "dcmi_set_destroy_vdevice" -#define ROOT_UID 0 -#define DECIMAL 10 +#define DCMI_INIT "dcmi_init" +#define DCMI_SET_DESTROY_VDEVICE "dcmi_set_destroy_vdevice" +#define ROOT_UID 0 +#define DECIMAL 10 #define DESTROY_PARAMS_NUM 4 -#define PARAMS_SECOND 1 -#define PARAMS_THIRD 2 -#define PARAMS_FOURTH 3 -#define ID_MAX 65535 +#define PARAMS_SECOND 1 +#define PARAMS_THIRD 2 +#define PARAMS_FOURTH 3 +#define ID_MAX 65535 static bool ShowExceptionInfo(const char* exceptionInfo) { @@ -164,7 +164,7 @@ static bool DeclareDcmiApiAndCheck(void **handle) Logger("dlinfo sofile failed.", LEVEL_ERROR, SCREEN_YES); return false; } - + return true; } @@ -185,9 +185,9 @@ static void DcmiDlclose(void **handle) } } -static bool CheckLimitId(const int IdValue) +static bool CheckLimitId(const int idValue) { - if (IdValue < 0 || IdValue > ID_MAX) { + if (idValue < 0 || idValue > ID_MAX) { return false; } return true; @@ -217,15 +217,15 @@ static bool DcmiInitProcess(void *handle) if (handle == NULL) { return false; } - int (*dcmi_init)(void) = NULL; - dcmi_init = dlsym(handle, DCMI_INIT); - if (dcmi_init == NULL) { + int (*dcmiInit)(void) = NULL; + dcmiInit = dlsym(handle, DCMI_INIT); + if (dcmiInit == NULL) { DcmiDlAbnormalExit(&handle, "DeclareDlApi failed"); return false; } - int ret = dcmi_init(); + int ret = dcmiInit(); if (ret != 0) { - Logger("dcmi_init faile.", LEVEL_ERROR, SCREEN_YES); + Logger("dcmiInit failed.", LEVEL_ERROR, SCREEN_YES); DcmiDlclose(&handle); return false; } @@ -238,15 +238,15 @@ static bool DcmiDestroyProcess(void *handle, const int cardId, if (handle == NULL) { return false; } - int (*dcmi_set_destroy_vdevice)(int, int, int) = NULL; - dcmi_set_destroy_vdevice = dlsym(handle, DCMI_SET_DESTROY_VDEVICE); - if (dcmi_set_destroy_vdevice == NULL) { + int (*dcmiSetDestroyVdevice)(int, int, int) = NULL; + dcmiSetDestroyVdevice = dlsym(handle, DCMI_SET_DESTROY_VDEVICE); + if (dcmiSetDestroyVdevice == NULL) { DcmiDlAbnormalExit(&handle, "DeclareDlApi failed"); return false; } - int ret = dcmi_set_destroy_vdevice(cardId, deviceId, vDeviceId); + int ret = dcmiSetDestroyVdevice(cardId, deviceId, vDeviceId); if (ret != 0) { - Logger("dcmi_set_destroy_vdevice failed.", LEVEL_ERROR, SCREEN_YES); + Logger("dcmiSetDestroyVdevice failed.", LEVEL_ERROR, SCREEN_YES); DcmiDlclose(&handle); return false; } diff --git a/hook/main.go b/hook/main.go index 503c8756f26c90201e6dea0469224b867bc47f77..ef66735bbc9dfbcb3274496db5c1fb588ace4057 100644 --- a/hook/main.go +++ b/hook/main.go @@ -71,13 +71,14 @@ type containerConfig struct { func initLogModule(ctx context.Context) error { const backups = 2 const logMaxAge = 365 + const fileMaxSize = 2 runLogConfig := hwlog.LogConfig{ LogFileName: runLogPath, LogLevel: 0, MaxBackups: backups, MaxAge: logMaxAge, OnlyToFile: true, - FileMaxSize: 2, + FileMaxSize: fileMaxSize, } if err := hwlog.InitRunLogger(&runLogConfig, ctx); err != nil { fmt.Printf("hwlog init failed, error is %v", err) @@ -208,8 +209,9 @@ var getContainerConfig = func() (*containerConfig, error) { } func getValueByKey(data []string, name string) string { + splitNumber := 2 for _, s := range data { - p := strings.SplitN(s, "=", 2) + p := strings.SplitN(s, "=", splitNumber) if len(p) != kvPairSize { log.Panicln("environment error") } diff --git a/hook/main_test.go b/hook/main_test.go index 4211c28283b4fdcfbc6f0f4b275261532d5d64df..8f52ca5db8a4b84d54f5d0a89d27a5a7eb53c2a7 100644 --- a/hook/main_test.go +++ b/hook/main_test.go @@ -23,7 +23,8 @@ import ( ) const ( - pidSample = 123 + pidSample = 123 + fileMode0600 os.FileMode = 0600 ) func TestDoPrestartHookCase1(t *testing.T) { @@ -148,6 +149,10 @@ func TestParseOciSpecFileCase2(t *testing.T) { if err != nil { t.Log("create file failed") } + err = f.Chmod(fileMode0600) + if err != nil { + t.Logf("chmod file error: %v", err) + } _, err = parseOciSpecFile(file) if err == nil { t.Fail() diff --git a/install/deb/src/main.go b/install/deb/src/main.go index 4342254f39f42b51961c470f6c2e0cf3673905b4..e4f6b1b6e2195b360de7631889d7c1f14041aed0 100644 --- a/install/deb/src/main.go +++ b/install/deb/src/main.go @@ -94,13 +94,14 @@ func main() { func initLogModule(ctx context.Context) error { const backups = 2 const logMaxAge = 365 + const fileMaxSize = 2 logConfig := hwlog.LogConfig{ LogFileName: logPath, LogLevel: 0, MaxBackups: backups, MaxAge: logMaxAge, OnlyToFile: true, - FileMaxSize: 2, + FileMaxSize: fileMaxSize, } if err := hwlog.InitRunLogger(&logConfig, ctx); err != nil { fmt.Printf("hwlog init failed, error is %v", err) diff --git a/mindxcheckutils/mindxcheckutils.go b/mindxcheckutils/mindxcheckutils.go index b812a4083903b9a49dab331459b978c03339eeaf..84fc93a43fe395b360faf6d8065ae5f9f1308dee 100644 --- a/mindxcheckutils/mindxcheckutils.go +++ b/mindxcheckutils/mindxcheckutils.go @@ -225,7 +225,6 @@ func StringChecker(text string, minLength, maxLength int, whiteList string) bool // ChangeRuntimeLogMode change log mode func ChangeRuntimeLogMode(runLog string) error { runLogDirLen := len(runLogDir) - var logMode os.FileMode counter := 0 err := filepath.Walk(runLogDir, func(fileOrPath string, fileInfo os.FileInfo, err error) error { counter += 1 @@ -240,11 +239,10 @@ func ChangeRuntimeLogMode(runLog string) error { if !hasLogPrefix { return nil } - logMode = backupLogFileMode if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink { return fmt.Errorf("the file or path is symlink") } - if errChmod := os.Chmod(fileOrPath, logMode); errChmod != nil { + if errChmod := os.Chmod(fileOrPath, backupLogFileMode); errChmod != nil { return fmt.Errorf("set file mode %s failed", fileOrPath) } return nil diff --git a/mindxcheckutils/mindxcheckutils_test.go b/mindxcheckutils/mindxcheckutils_test.go index 1d19a29c95300eb56491ee6ad0424dbdfb4ee770..15d4b0ad5ad560844f741029dfa392decfcbef45 100644 --- a/mindxcheckutils/mindxcheckutils_test.go +++ b/mindxcheckutils/mindxcheckutils_test.go @@ -21,6 +21,10 @@ import ( "testing" ) +const ( + fileMode0600 os.FileMode = 0600 +) + func TestNormalFileCheckRegularFile(t *testing.T) { tmpDir, filePath, err := createTestFile(t, "test_file.txt") defer removeTmpDir(t, tmpDir) @@ -155,10 +159,15 @@ func createTestFile(t *testing.T, fileName string) (string, string, error) { if os.MkdirAll(tmpDir+"/__test__", permission) != nil { t.Fatalf("MkdirAll failed %q", tmpDir+"/__test__") } - _, err := os.Create(tmpDir + "/__test__" + fileName) + f, err := os.Create(tmpDir + "/__test__" + fileName) if err != nil { t.Fatalf("create file failed %q: %s", tmpDir+"/__test__", err) } + defer f.Close() + err = f.Chmod(fileMode0600) + if err != nil { + t.Logf("chmod file error: %v", err) + } return tmpDir + "/__test__", tmpDir + "/__test__" + fileName, err } diff --git a/runtime/dcmi/dcmi.go b/runtime/dcmi/dcmi.go index acf8d98abe394ff19f1dad7d43b16b20c07dee25..261f970907047e6e63f65a24151d77a63cee06a2 100644 --- a/runtime/dcmi/dcmi.go +++ b/runtime/dcmi/dcmi.go @@ -90,7 +90,7 @@ func convertUCharToCharArr(cgoArr [maxChipNameLen]C.uchar) []byte { func (w *NpuWorker) Initialize() error { cDlPath := C.CString(string(make([]byte, int32(C.PATH_MAX)))) defer C.free(unsafe.Pointer(cDlPath)) - if err := C.dcmiInit_dl(cDlPath); err != C.SUCCESS { + if err := C.DcmiInitDl(cDlPath); err != C.SUCCESS { errInfo := fmt.Errorf("dcmi lib load failed, , error code: %d", int32(err)) return errInfo } @@ -98,7 +98,7 @@ func (w *NpuWorker) Initialize() error { if _, err := mindxcheckutils.RealFileChecker(dlPath, true, false, mindxcheckutils.DefaultSize); err != nil { return err } - if err := C.dcmi_init(); err != C.SUCCESS { + if err := C.DcmiInit(); err != C.SUCCESS { errInfo := fmt.Errorf("dcmi init failed, , error code: %d", int32(err)) return errInfo } @@ -107,7 +107,7 @@ func (w *NpuWorker) Initialize() error { // ShutDown shutdown dcmi lib func (w *NpuWorker) ShutDown() { - if err := C.dcmiShutDown(); err != C.SUCCESS { + if err := C.DcmiShutDown(); err != C.SUCCESS { println(fmt.Errorf("dcmi shut down failed, error code: %d", int32(err))) } } @@ -116,7 +116,7 @@ func (w *NpuWorker) ShutDown() { func GetCardList() (int32, []int32, error) { var ids [hiAIMaxCardNum]C.int var cNum C.int - if err := C.dcmi_get_card_num_list(&cNum, &ids[0], hiAIMaxCardNum); err != 0 { + if err := C.DcmiGetCardNumList(&cNum, &ids[0], hiAIMaxCardNum); err != 0 { errInfo := fmt.Errorf("get card list failed, error code: %d", int32(err)) return retError, nil, errInfo } @@ -140,7 +140,7 @@ func GetCardList() (int32, []int32, error) { // GetDeviceNumInCard get device number in the npu card func GetDeviceNumInCard(cardID int32) (int32, error) { var deviceNum C.int - if err := C.dcmi_get_device_num_in_card(C.int(cardID), &deviceNum); err != 0 { + if err := C.DcmiGetDeviceNumInCard(C.int(cardID), &deviceNum); err != 0 { errInfo := fmt.Errorf("get device count on the card failed, error code: %d", int32(err)) return retError, errInfo } @@ -154,7 +154,7 @@ func GetDeviceNumInCard(cardID int32) (int32, error) { // GetDeviceLogicID get device logicID func GetDeviceLogicID(cardID, deviceID int32) (int32, error) { var logicID C.int - if err := C.dcmi_get_device_logic_id(&logicID, C.int(cardID), C.int(deviceID)); err != 0 { + if err := C.DcmiGetDeviceLogicId(&logicID, C.int(cardID), C.int(deviceID)); err != 0 { errInfo := fmt.Errorf("get logicID failed, error code: %d", int32(err)) return retError, errInfo } @@ -169,12 +169,12 @@ func GetDeviceLogicID(cardID, deviceID int32) (int32, error) { // CreateVDevice create virtual device func (w *NpuWorker) CreateVDevice(cardID, deviceID int32, coreNum string) (int32, error) { - var createInfo C.struct_dcmi_create_vdev_out - createInfo.vdev_id = C.uint(math.MaxUint32) - var deviceCreateStr C.struct_dcmi_create_vdev_res_stru - deviceCreateStr = C.struct_dcmi_create_vdev_res_stru{ - vdev_id: C.uint(vfgID), - vfg_id: C.uint(vfgID), + var createInfo C.struct_DcmiCreateVdevOut + createInfo.vdevId = C.uint(math.MaxUint32) + var deviceCreateStr C.struct_DcmiCreateVdevResStru + deviceCreateStr = C.struct_DcmiCreateVdevResStru{ + vdevId: C.uint(vfgID), + vfgId: C.uint(vfgID), } deviceCreateStrArr := [coreNumLen]C.char{0} for i := 0; i < len(coreNum); i++ { @@ -183,16 +183,16 @@ func (w *NpuWorker) CreateVDevice(cardID, deviceID int32, coreNum string) (int32 } deviceCreateStrArr[i] = C.char(coreNum[i]) } - deviceCreateStr.template_name = deviceCreateStrArr - err := C.dcmi_create_vdevice(C.int(cardID), C.int(deviceID), &deviceCreateStr, &createInfo) + deviceCreateStr.templateName = deviceCreateStrArr + err := C.DcmiCreateVdevice(C.int(cardID), C.int(deviceID), &deviceCreateStr, &createInfo) if err != 0 { errInfo := fmt.Errorf("create virtual device failed, error code: %d", int32(err)) return math.MaxInt32, errInfo } - if createInfo.vdev_id > math.MaxInt32 { + if createInfo.vdevId > math.MaxInt32 { return math.MaxInt32, fmt.Errorf("create virtual device failed, vdeviceId too large") } - return int32(createInfo.vdev_id), nil + return int32(createInfo.vdevId), nil } // DestroyVDevice destroy virtual device @@ -200,7 +200,7 @@ func (w *NpuWorker) DestroyVDevice(cardID, deviceID int32, vDevID int32) error { if vDevID < 0 { return fmt.Errorf("param error on vDevID") } - if err := C.dcmi_set_destroy_vdevice(C.int(cardID), C.int(deviceID), C.uint(vDevID)); err != 0 { + if err := C.DcmiSetDestroyVdevice(C.int(cardID), C.int(deviceID), C.uint(vDevID)); err != 0 { errInfo := fmt.Errorf("destroy virtual device failed, error code: %d", int32(err)) return errInfo } @@ -210,7 +210,7 @@ func (w *NpuWorker) DestroyVDevice(cardID, deviceID int32, vDevID int32) error { // FindDevice find device by phyical id func (w *NpuWorker) FindDevice(visibleDevice int32) (int32, int32, error) { var dcmiLogicID C.uint - if err := C.dcmi_get_device_logicid_from_phyid(C.uint(visibleDevice), &dcmiLogicID); err != 0 { + if err := C.DcmiGetDeviceLogicidFromPhyid(C.uint(visibleDevice), &dcmiLogicID); err != 0 { return 0, 0, fmt.Errorf("phy id can not be converted to logic id : %v", err) } if int32(dcmiLogicID) < 0 || int32(dcmiLogicID) >= hiAIMaxCardNum*hiAIMaxDeviceNum { @@ -244,7 +244,7 @@ func (w *NpuWorker) FindDevice(visibleDevice int32) (int32, int32, error) { func (w *NpuWorker) GetProductType(cardID, deviceID int32) (string, error) { cProductType := C.CString(string(make([]byte, productTypeLen))) defer C.free(unsafe.Pointer(cProductType)) - if err := C.dcmi_get_product_type(C.int(cardID), C.int(deviceID), + if err := C.DcmiGetProductType(C.int(cardID), C.int(deviceID), (*C.char)(cProductType), productTypeLen); err != 0 { if err == notSupportCode { // device which does not support querying product, such as Ascend 910A/B @@ -260,15 +260,15 @@ func (w *NpuWorker) GetChipInfo(cardID, deviceID int32) (*ChipInfo, error) { if !isValidCardIDAndDeviceID(cardID, deviceID) { return nil, fmt.Errorf("cardID(%d) or deviceID(%d) is invalid", cardID, deviceID) } - var chipInfo C.struct_dcmi_chip_info - if rCode := C.dcmi_get_device_chip_info(C.int(cardID), C.int(deviceID), &chipInfo); int32(rCode) != 0 { + var chipInfo C.struct_DcmiChipInfo + if rCode := C.DcmiGetDeviceChipInfo(C.int(cardID), C.int(deviceID), &chipInfo); int32(rCode) != 0 { return nil, fmt.Errorf("get device ChipInfo information failed, cardID(%d), deviceID(%d),"+ " error code: %d", cardID, deviceID, int32(rCode)) } - name := convertUCharToCharArr(chipInfo.chip_name) - cType := convertUCharToCharArr(chipInfo.chip_type) - ver := convertUCharToCharArr(chipInfo.chip_ver) + name := convertUCharToCharArr(chipInfo.chipName) + cType := convertUCharToCharArr(chipInfo.chipType) + ver := convertUCharToCharArr(chipInfo.chipVer) chip := &ChipInfo{ Name: string(name), diff --git a/runtime/dcmi/dcmi_interface_api.h b/runtime/dcmi/dcmi_interface_api.h index 2e5425d38f36f21906c01eb6a66c3b45fa4fbc3e..78dba9f2f0efce1d403959c2cbfbd9833ab1ba51 100644 --- a/runtime/dcmi/dcmi_interface_api.h +++ b/runtime/dcmi/dcmi_interface_api.h @@ -28,143 +28,143 @@ extern "C" { #endif #endif /* __cplusplus */ -void *dcmiHandle; +void *g_dcmiHandle; #define SO_NOT_FOUND (-99999) #define FUNCTION_NOT_FOUND (-99998) #define SUCCESS (0) #define ERROR_UNKNOWN (-99997) #define SO_NOT_CORRECT (-99996) -#define CALL_FUNC(name, ...) if (name##_func == NULL) {return FUNCTION_NOT_FOUND;}return name##_func(__VA_ARGS__) +#define CALL_FUNC(name, ...) if (name == NULL) {return FUNCTION_NOT_FOUND;}return name(__VA_ARGS__) #define DCMI_VDEV_FOR_RESERVE (32) #define MAX_CHIP_NAME_LEN (32) -struct dcmi_create_vdev_out { - unsigned int vdev_id; - unsigned int pcie_bus; - unsigned int pcie_device; - unsigned int pcie_func; - unsigned int vfg_id; +struct DcmiCreateVdevOut { + unsigned int vdevId; + unsigned int pcieBus; + unsigned int pcieDevice; + unsigned int pcieFunc; + unsigned int vfgId; unsigned char reserved[DCMI_VDEV_FOR_RESERVE]; }; -struct dcmi_create_vdev_res_stru { - unsigned int vdev_id; - unsigned int vfg_id; - char template_name[32]; +struct DcmiCreateVdevResStru { + unsigned int vdevId; + unsigned int vfgId; + char templateName[32]; unsigned char reserved[64]; }; -struct dcmi_chip_info { -unsigned char chip_type[MAX_CHIP_NAME_LEN]; -unsigned char chip_name[MAX_CHIP_NAME_LEN]; -unsigned char chip_ver[MAX_CHIP_NAME_LEN]; -unsigned int aicore_cnt; +struct DcmiChipInfo { +unsigned char chipType[MAX_CHIP_NAME_LEN]; +unsigned char chipName[MAX_CHIP_NAME_LEN]; +unsigned char chipVer[MAX_CHIP_NAME_LEN]; +unsigned int aicoreCnt; }; // dcmi -int (*dcmi_init_func)(); -int dcmi_init() +int (*g_dcmiInitFunc)(void); +int DcmiInit(void) { - CALL_FUNC(dcmi_init); + CALL_FUNC(g_dcmiInitFunc); } -int (*dcmi_get_card_num_list_func)(int *card_num, int *card_list, int list_length); -int dcmi_get_card_num_list(int *card_num, int *card_list, int list_length) +int (*g_dcmiGetCardNumListFunc)(int *cardNum, int *cardList, int listLength); +int DcmiGetCardNumList(int *cardNum, int *cardList, int listLength) { - CALL_FUNC(dcmi_get_card_num_list, card_num, card_list, list_length); + CALL_FUNC(g_dcmiGetCardNumListFunc, cardNum, cardList, listLength); } -int (*dcmi_get_device_num_in_card_func)(int card_id, int *device_num); -int dcmi_get_device_num_in_card(int card_id, int *device_num) +int (*g_dcmiGetDeviceNumInCardFunc)(int cardId, int *deviceNum); +int DcmiGetDeviceNumInCard(int cardId, int *deviceNum) { - CALL_FUNC(dcmi_get_device_num_in_card, card_id, device_num); + CALL_FUNC(g_dcmiGetDeviceNumInCardFunc, cardId, deviceNum); } -int (*dcmi_get_device_logic_id_func)(int *device_logic_id, int card_id, int device_id); -int dcmi_get_device_logic_id(int *device_logic_id, int card_id, int device_id) +int (*g_dcmiGetDeviceLogicIdFunc)(int *deviceLogicId, int cardId, int deviceId); +int DcmiGetDeviceLogicId(int *deviceLogicId, int cardId, int deviceId) { - CALL_FUNC(dcmi_get_device_logic_id, device_logic_id, card_id, device_id); + CALL_FUNC(g_dcmiGetDeviceLogicIdFunc, deviceLogicId, cardId, deviceId); } -int (*dcmi_create_vdevice_func)(int card_id, int device_id, - struct dcmi_create_vdev_res_stru *vdev, - struct dcmi_create_vdev_out *out); -int dcmi_create_vdevice(int card_id, int device_id, - struct dcmi_create_vdev_res_stru *vdev, - struct dcmi_create_vdev_out *out) +int (*g_DcmiCreateVdeviceFunc)(int cardId, int deviceId, + struct DcmiCreateVdevResStru *vdev, + struct DcmiCreateVdevOut *out); +int DcmiCreateVdevice(int cardId, int deviceId, + struct DcmiCreateVdevResStru *vdev, + struct DcmiCreateVdevOut *out) { - CALL_FUNC(dcmi_create_vdevice, card_id, device_id, vdev, out); + CALL_FUNC(g_DcmiCreateVdeviceFunc, cardId, deviceId, vdev, out); } -int (*dcmi_set_destroy_vdevice_func)(int card_id, int device_id, unsigned int VDevid); -int dcmi_set_destroy_vdevice(int card_id, int device_id, unsigned int VDevid) +int (*g_dcmiSetDestroyVdeviceFunc)(int cardId, int deviceId, unsigned int vDevid); +int DcmiSetDestroyVdevice(int cardId, int deviceId, unsigned int vDevid) { - CALL_FUNC(dcmi_set_destroy_vdevice, card_id, device_id, VDevid); + CALL_FUNC(g_dcmiSetDestroyVdeviceFunc, cardId, deviceId, vDevid); } -int (*dcmi_get_device_logicid_from_phyid_func)(unsigned int phyid, unsigned int *logicid); -int dcmi_get_device_logicid_from_phyid(unsigned int phyid, unsigned int *logicid) +int (*g_dcmiGetDeviceLogicidFromPhyidFunc)(unsigned int phyid, unsigned int *logicid); +int DcmiGetDeviceLogicidFromPhyid(unsigned int phyid, unsigned int *logicid) { - CALL_FUNC(dcmi_get_device_logicid_from_phyid, phyid, logicid); + CALL_FUNC(g_dcmiGetDeviceLogicidFromPhyidFunc, phyid, logicid); } -int (*dcmi_get_product_type_func)(int card_id, int device_id, char *product_type_str, int buf_size); -int dcmi_get_product_type(int card_id, int device_id, char *product_type_str, int buf_size) +int (*g_dcmiGetProductTypeFunc)(int cardId, int deviceId, char *productTypeStr, int bufSize); +int DcmiGetProductType(int cardId, int deviceId, char *productTypeStr, int bufSize) { - CALL_FUNC(dcmi_get_product_type, card_id, device_id, product_type_str, buf_size); + CALL_FUNC(g_dcmiGetProductTypeFunc, cardId, deviceId, productTypeStr, bufSize); } -int (*dcmi_get_device_chip_info_func)(int card_id, int device_id, struct dcmi_chip_info *chip_info); -int dcmi_get_device_chip_info(int card_id, int device_id, struct dcmi_chip_info *chip_info) +int (*g_dcmiGetDeviceChipInfoFunc)(int cardId, int deviceId, struct DcmiChipInfo *chipInfo); +int DcmiGetDeviceChipInfo(int cardId, int deviceId, struct DcmiChipInfo *chipInfo) { - CALL_FUNC(dcmi_get_device_chip_info, card_id, device_id, chip_info); + CALL_FUNC(g_dcmiGetDeviceChipInfoFunc, cardId, deviceId, chipInfo); } // load .so files and functions -int dcmiInit_dl(char *dl_path) +int DcmiInitDl(char *dlPath) { - dcmiHandle = dlopen("libdcmi.so", RTLD_LAZY | RTLD_GLOBAL); - if (dcmiHandle == NULL) { + g_dcmiHandle = dlopen("libdcmi.so", RTLD_LAZY | RTLD_GLOBAL); + if (g_dcmiHandle == NULL) { fprintf (stderr, "%s\n", dlerror()); return SO_NOT_FOUND; } struct link_map *pLinkMap; - int ret = dlinfo(dcmiHandle, RTLD_DI_LINKMAP, &pLinkMap); + int ret = dlinfo(g_dcmiHandle, RTLD_DI_LINKMAP, &pLinkMap); if (ret != 0) { fprintf(stderr, "dlinfo sofile failed :%s\n", dlerror()); return SO_NOT_CORRECT; } - size_t path_size = strlen(pLinkMap->l_name); - for (int i = 0; i < path_size && i < PATH_MAX; i++) { - dl_path[i] = pLinkMap->l_name[i]; + size_t pathSize = strlen(pLinkMap->l_name); + for (int i = 0; i < pathSize && i < PATH_MAX; i++) { + dlPath[i] = pLinkMap->l_name[i]; } - dcmi_init_func = dlsym(dcmiHandle, "dcmi_init"); + g_dcmiInitFunc = dlsym(g_dcmiHandle, "dcmi_init"); - dcmi_get_card_num_list_func = dlsym(dcmiHandle, "dcmi_get_card_num_list"); + g_dcmiGetCardNumListFunc = dlsym(g_dcmiHandle, "dcmi_get_card_num_list"); - dcmi_get_device_num_in_card_func = dlsym(dcmiHandle, "dcmi_get_device_num_in_card"); + g_dcmiGetDeviceNumInCardFunc = dlsym(g_dcmiHandle, "dcmi_get_device_num_in_card"); - dcmi_get_device_logic_id_func = dlsym(dcmiHandle, "dcmi_get_device_logic_id"); + g_dcmiGetDeviceLogicIdFunc = dlsym(g_dcmiHandle, "dcmi_get_device_logic_id"); - dcmi_create_vdevice_func = dlsym(dcmiHandle, "dcmi_create_vdevice"); + g_DcmiCreateVdeviceFunc = dlsym(g_dcmiHandle, "dcmi_create_vdevice"); - dcmi_set_destroy_vdevice_func = dlsym(dcmiHandle, "dcmi_set_destroy_vdevice"); + g_dcmiSetDestroyVdeviceFunc = dlsym(g_dcmiHandle, "dcmi_set_destroy_vdevice"); - dcmi_get_device_logicid_from_phyid_func = dlsym(dcmiHandle, "dcmi_get_device_logicid_from_phyid"); + g_dcmiGetDeviceLogicidFromPhyidFunc = dlsym(g_dcmiHandle, "dcmi_get_device_logicid_from_phyid"); - dcmi_get_product_type_func = dlsym(dcmiHandle, "dcmi_get_product_type"); + g_dcmiGetProductTypeFunc = dlsym(g_dcmiHandle, "dcmi_get_product_type"); - dcmi_get_device_chip_info_func = dlsym(dcmiHandle, "dcmi_get_device_chip_info"); + g_dcmiGetDeviceChipInfoFunc = dlsym(g_dcmiHandle, "dcmi_get_device_chip_info"); return SUCCESS; } -int dcmiShutDown(void) +int DcmiShutDown(void) { - if (dcmiHandle == NULL) { + if (g_dcmiHandle == NULL) { return SUCCESS; } - return (dlclose(dcmiHandle) ? ERROR_UNKNOWN : SUCCESS); + return ((dlclose(g_dcmiHandle) != SUCCESS) ? ERROR_UNKNOWN : SUCCESS); } #ifdef __cplusplus diff --git a/runtime/main.go b/runtime/main.go index 9802adb509aee86dfbfbfba14f794dcbed5d7865..444fd7d3887380edccce8e5cfd081dd94ea7b9fc 100644 --- a/runtime/main.go +++ b/runtime/main.go @@ -149,13 +149,14 @@ func getArgs() (*args, error) { func initLogModule(ctx context.Context) error { const backups = 2 const logMaxAge = 365 + const fileMaxSize = 2 runLogConfig := hwlog.LogConfig{ LogFileName: runLogPath, LogLevel: 0, MaxBackups: backups, MaxAge: logMaxAge, OnlyToFile: true, - FileMaxSize: 2, + FileMaxSize: fileMaxSize, } if err := hwlog.InitRunLogger(&runLogConfig, ctx); err != nil { fmt.Printf("hwlog init failed, error is %v", err) @@ -403,8 +404,8 @@ func addDeviceToSpec(spec *specs.Spec, dPath string, deviceType string) error { device.Path = devicePath + davinciName + vDeviceNumber[0] case davinciManagerDocker: device.Path = devicePath + davinciManager - default: - // do nothing + default: // do nothing + } spec.Linux.Devices = append(spec.Linux.Devices, *device) diff --git a/runtime/main_test.go b/runtime/main_test.go index 64fb48508a505e888008edae4bc122b785206c02..87eb513063d5a5899eb8c9785521ff44108084be 100644 --- a/runtime/main_test.go +++ b/runtime/main_test.go @@ -34,7 +34,11 @@ const ( // strKubeDNSPort53UDPPort represents the string of the environment variable KUBE_DNS_PORT_53_UDP_PORT strKubeDNSPort53UDPPort = "KUBE_DNS_PORT_53_UDP_PORT=53" // strKubeDNSPort53UDPProto represents the string of the environment variable KUBE_DNS_PORT_53_UDP_PROTO - strKubeDNSPort53UDPProto = "KUBE_DNS_PORT_53_UDP_PROTO=udp" + strKubeDNSPort53UDPProto = "KUBE_DNS_PORT_53_UDP_PROTO=udp" + fileMode0400 os.FileMode = 0400 + fileMode0600 os.FileMode = 0600 + fileMode0655 os.FileMode = 0655 + needToMkdir = "./test" ) func TestArgsIsCreate(t *testing.T) { @@ -88,13 +92,19 @@ func TestArgsIsCreateCase2(t *testing.T) { func TestArgsIsCreateCase3(t *testing.T) { t.Log("进入测试用例") - if err := os.Mkdir("./test", 0655); err != nil { + if err := os.Mkdir(needToMkdir, fileMode0655); err != nil { + t.Fatalf("failed to create file, error: %v", err) } f, err := os.Create("./test/config.json") defer f.Close() if err != nil { + t.Logf("create file error: %v", err) } - testArgs := []string{"create", "--bundle", "./test"} + err = f.Chmod(fileMode0600) + if err != nil { + t.Logf("chmod file error: %v", err) + } + testArgs := []string{"create", "--bundle", needToMkdir} stub := gomonkey.ApplyGlobalVar(&os.Args, testArgs) defer stub.Reset() @@ -110,13 +120,19 @@ func TestArgsIsCreateCase3(t *testing.T) { func TestArgsIsCreateCase4(t *testing.T) { t.Log("进入测试用例") - if err := os.Mkdir("./test", 0655); err != nil { + if err := os.Mkdir(needToMkdir, fileMode0655); err != nil { + t.Fatalf("failed to create file, error: %v", err) } f, err := os.Create("./test/config.json") defer f.Close() if err != nil { + t.Logf("create file failed, error: %v", err) } - testArgs := []string{"spec", "--bundle", "./test"} + err = f.Chmod(fileMode0600) + if err != nil { + t.Logf("chmod file failed, error: %v", err) + } + testArgs := []string{"spec", "--bundle", needToMkdir} stub := gomonkey.ApplyGlobalVar(&os.Args, testArgs) defer stub.Reset() @@ -135,15 +151,14 @@ func TestModifySpecFile(t *testing.T) { } func TestModifySpecFileCase1(t *testing.T) { - file := "./test" - if err := os.Mkdir("./test", 0400); err != nil { - + if err := os.Mkdir(needToMkdir, fileMode0400); err != nil { + t.Logf("mkdir error: %v", err) } - err := modifySpecFile(file) + err := modifySpecFile(needToMkdir) assert.NotNil(t, err) - if err := os.Remove(file); err != nil { - + if err := os.Remove(needToMkdir); err != nil { + t.Logf("failed to remove dir, error: %v", err) } } @@ -154,7 +169,10 @@ func TestModifySpecFileCase2(t *testing.T) { if err != nil { t.Log("create file error") } - + err = f.Chmod(fileMode0600) + if err != nil { + t.Logf("chmod file error: %v", err) + } if err := modifySpecFile(file); err != nil { t.Log("run modifySpecFile failed") } @@ -365,10 +383,11 @@ func TestUpdateEnvAndPostHook(t *testing.T) { t.Logf("%s", e) } }() + vDeviceId := int32(100) vdvice := dcmi.VDeviceInfo{ CardID: 0, DeviceID: 0, - VdeviceID: 100, + VdeviceID: vDeviceId, } spec := specs.Spec{