diff --git a/cli/src/cgrp.c b/cli/src/cgrp.c index 62accfd..7b5eb02 100644 --- a/cli/src/cgrp.c +++ b/cli/src/cgrp.c @@ -10,6 +10,7 @@ #include #include "securec.h" #include "utils.h" +#include "logging.h" char *GetCgroupMount(char *line, const char *subsys) { @@ -68,20 +69,20 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName) ret = snprintf_s(devPath, BUF_SIZE, BUF_SIZE, "/dev/%s", devName); if (ret < 0) { - fprintf(stderr, "error: failed to assemble dev path for %s\n", devName); + logError("error: failed to assemble dev path for %s\n", devName); return -1; } ret = stat((const char *)devPath, &devStat); if (ret < 0) { - fprintf(stderr, "error: failed to get stat of %s\n", devPath); + logError("error: failed to get stat of %s\n", devPath); return -1; } bool isFailed = fprintf(cgroupAllow, "c %u:%u rw", major(devStat.st_rdev), minor(devStat.st_rdev)) < 0 || fflush(cgroupAllow) == EOF || ferror(cgroupAllow) < 0; if (isFailed) { - fprintf(stderr, "error: write devices failed\n"); + logError("error: write devices failed\n"); return -1; } @@ -94,19 +95,19 @@ int SetupDriverCgroup(FILE *cgroupAllow) ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER); if (ret < 0) { - fprintf(stderr, "error: failed to setup cgroup for %s\n", DAVINCI_MANAGER); + logError("error: failed to setup cgroup for %s\n", DAVINCI_MANAGER); return -1; } ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM); if (ret < 0) { - fprintf(stderr, "error: failed to setup cgroup for %s\n", DEVMM_SVM); + logError("error: failed to setup cgroup for %s\n", DEVMM_SVM); return -1; } ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC); if (ret < 0) { - fprintf(stderr, "error: failed to setup cgroup for %s\n", HISI_HDC); + logError("error: failed to setup cgroup for %s\n", HISI_HDC); return -1; } @@ -121,13 +122,13 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz ret = snprintf_s(mountPath, BUF_SIZE, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid()); if (ret < 0) { - fprintf(stderr, "error: assemble mount info path failed: ppid(%d)\n", getppid()); + logError("error: assemble mount info path failed: ppid(%d)\n", getppid()); return -1; } ret = CatFileContent(mount, BUF_SIZE, GetCgroupMount, mountPath); if (ret < 0) { - fprintf(stderr, "error: cat file content failed\n"); + logError("error: cat file content failed\n"); return -1; } @@ -135,13 +136,13 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz char cgroupPath[BUF_SIZE] = {0x0}; ret = snprintf_s(cgroupPath, BUF_SIZE, BUF_SIZE, "/proc/%d/cgroup", args->pid); if (ret < 0) { - fprintf(stderr, "error: assemble cgroup path failed: pid(%d)\n", args->pid); + logError("error: assemble cgroup path failed: pid(%d)\n", args->pid); return -1; } ret = CatFileContent(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath); if (ret < 0) { - fprintf(stderr, "error: cat file content failed\n"); + logError("error: cat file content failed\n"); return -1; } @@ -150,7 +151,7 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz ret = snprintf_s(effPath, BUF_SIZE, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH); if (ret < 0) { - fprintf(stderr, "error: assemble cgroup device path failed: \n"); + logError("error: assemble cgroup device path failed: \n"); return -1; } @@ -165,7 +166,7 @@ int SetupCgroup(struct CmdArgs *args, const char *cgroupPath) FILE *cgroupAllow = NULL; if (realpath(cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) { - fprintf(stderr, "error: cannot canonicalize cgroup path: %s\n", cgroupPath); + logError("error: cannot canonicalize cgroup path: %s\n", cgroupPath); return -1; } @@ -179,14 +180,14 @@ int SetupCgroup(struct CmdArgs *args, const char *cgroupPath) cgroupAllow = fopen((const char *)resolvedCgroupPath, "a"); if (cgroupAllow == NULL) { - fprintf(stderr, "error: failed to open cgroup file: %s\n", resolvedCgroupPath); + logError("error: failed to open cgroup file: %s\n", resolvedCgroupPath); return -1; } ret = SetupDriverCgroup(cgroupAllow); if (ret < 0) { fclose(cgroupAllow); - fprintf(stderr, "error: failed to setup driver cgroup\n"); + logError("error: failed to setup driver cgroup\n"); return -1; } @@ -195,14 +196,14 @@ int SetupCgroup(struct CmdArgs *args, const char *cgroupPath) ret = snprintf_s(deviceName, BUF_SIZE, BUF_SIZE, "%s%s", DEVICE_NAME, token); if (ret < 0) { fclose(cgroupAllow); - fprintf(stderr, "error: failed to assemble device path for no.%s\n", token); + logError("error: failed to assemble device path for no.%s\n", token); return -1; } ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName); if (ret < 0) { fclose(cgroupAllow); - fprintf(stderr, "error: failed to setup cgroup %s\n", token); + logError("error: failed to setup cgroup %s\n", token); return -1; } diff --git a/cli/src/logging.c b/cli/src/logging.c new file mode 100644 index 0000000..b269168 --- /dev/null +++ b/cli/src/logging.c @@ -0,0 +1,22 @@ +#include "logging.h" + +#include +#include + +void logError(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + fprintf(stderr, fmt, args); + va_end(args); +} + +void logInfo(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + fprintf(stdout, fmt, args); + va_end(args); +} \ No newline at end of file diff --git a/cli/src/logging.h b/cli/src/logging.h new file mode 100644 index 0000000..0ddef00 --- /dev/null +++ b/cli/src/logging.h @@ -0,0 +1,7 @@ +#ifndef _LOGGING_H +#define _LOGGING_H + +void logError(const char *fmt, ...); +void logInfo(const char *fmt, ...); + +#endif diff --git a/cli/src/main.c b/cli/src/main.c index baa594f..abe9f63 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -3,7 +3,6 @@ * Description: ascend-docker-cli工具,配置容器挂载Ascend NPU设备 */ #define _GNU_SOURCE -#include #include #include #include @@ -19,6 +18,7 @@ #include "ns.h" #include "mount.h" #include "cgrp.h" +#include "logging.h" struct ParsedConfig { char containerNsPath[BUF_SIZE]; @@ -44,26 +44,26 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config) ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE); if (ret < 0) { - fprintf(stderr, "error: failed to get container mnt ns path: pid(%d)\n", args->pid); + logError("error: failed to get container mnt ns path: pid(%d)\n", args->pid); return -1; } ret = GetCgroupPath(args, config->cgroupPath, BUF_SIZE); if (ret < 0) { - fprintf(stderr, "error: failed to get cgroup path\n"); + logError("error: failed to get cgroup path\n"); return -1; } char originNsPath[BUF_SIZE] = {0}; ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE); if (ret < 0) { - fprintf(stderr, "error: failed to get self ns path\n"); + logError("error: failed to get self ns path\n"); return -1; } config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口,非外部输入 if (config->originNsFd < 0) { - fprintf(stderr, "error: failed to get self ns fd: %s\n", originNsPath); + logError("error: failed to get self ns fd: %s\n", originNsPath); return -1; } @@ -77,28 +77,28 @@ int SetupContainer(struct CmdArgs *args) ret = DoPrepare(args, &config); if (ret < 0) { - fprintf(stderr, "error: failed to prepare nesessary config\n"); + logError("error: failed to prepare nesessary config\n"); return -1; } // enter container's mount namespace ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS); if (ret < 0) { - fprintf(stderr, "error: failed to set to container ns: %s\n", config.containerNsPath); + logError("error: failed to set to container ns: %s\n", config.containerNsPath); close(config.originNsFd); return -1; } ret = DoMounting(args); if (ret < 0) { - fprintf(stderr, "error: failed to do mounting\n"); + logError("error: failed to do mounting\n"); close(config.originNsFd); return -1; } ret = SetupCgroup(args, (const char *)config.cgroupPath); if (ret < 0) { - fprintf(stderr, "error: failed to set up cgroup\n"); + logError("error: failed to set up cgroup\n"); close(config.originNsFd); return -1; } @@ -106,7 +106,7 @@ int SetupContainer(struct CmdArgs *args) // back to original namespace ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS); if (ret < 0) { - fprintf(stderr, "error: failed to set ns back\n"); + logError("error: failed to set ns back\n"); close(config.originNsFd); return -1; } @@ -151,14 +151,14 @@ int Process(int argc, char **argv) } break; default: - fprintf(stderr, "unrecongnized option\n"); + logError("unrecongnized option\n"); isSucceed = false; // unrecognized option break; } } if (!isSucceed || !IsCmdArgsValid(&args)) { - fprintf(stderr, "error: information not completed or valid.\n"); + logError("error: information not completed or valid.\n"); return -1; } diff --git a/cli/src/mount.c b/cli/src/mount.c index cd581a6..53ea5ea 100644 --- a/cli/src/mount.c +++ b/cli/src/mount.c @@ -1,12 +1,12 @@ #include "mount.h" -#include #include #include #include #include #include "securec.h" #include "utils.h" +#include "logging.h" static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, struct PathInfo* pathInfo) @@ -37,13 +37,13 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, } if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) { - fprintf(stderr, "error: cannot canonicalize device dst: %s\n", dst); + logError("error: cannot canonicalize device dst: %s\n", dst); return -1; } err = strcpy_s(dst, dstBufSize, (const char *)resolvedDst); if (err != EOK) { - fprintf(stderr, "error: failed to copy resolved device mnt path to dst: %s\n", resolvedDst); + logError("error: failed to copy resolved device mnt path to dst: %s\n", resolvedDst); return -1; } @@ -59,26 +59,26 @@ int MountDevice(const char *rootfs, const char *deviceName) ret = GetDeviceMntSrcDst(rootfs, deviceName, &pathInfo); if (ret < 0) { - fprintf(stderr, "error: failed to get device mount src and(or) dst path, device name: %s\n", deviceName); + logError("error: failed to get device mount src and(or) dst path, device name: %s\n", deviceName); return -1; } struct stat srcStat; ret = stat((const char *)src, &srcStat); if (ret < 0) { - fprintf(stderr, "error: failed to stat src: %s\n", src); + logError("error: failed to stat src: %s\n", src); return -1; } ret = CreateFile(dst, srcStat.st_mode); if (ret < 0) { - fprintf(stderr, "error: failed to create mount dst file: %s\n", dst); + logError("error: failed to create mount dst file: %s\n", dst); return -1; } ret = Mount(src, dst); if (ret < 0) { - fprintf(stderr, "error: failed to mount dev\n"); + logError("error: failed to mount dev\n"); return -1; } @@ -101,13 +101,13 @@ int DoDeviceMounting(const char *rootfs, const char *devicesList) while (token != NULL) { int ret = snprintf_s(deviceName, BUF_SIZE, BUF_SIZE, "%s%s", DEVICE_NAME, token); if (ret < 0) { - fprintf(stderr, "error: assemble device name failed, id: %s\n", token); + logError("error: assemble device name failed, id: %s\n", token); return -1; } ret = MountDevice(rootfs, deviceName); if (ret < 0) { - fprintf(stderr, "error: failed to mount device no. %s\n", token); + logError("error: failed to mount device no. %s\n", token); return -1; } @@ -117,31 +117,6 @@ int DoDeviceMounting(const char *rootfs, const char *devicesList) return 0; } -int MakeParentDir(const char *path, mode_t mode) -{ - if (*path == '\0' || *path == '.') { - return 0; - } - if (CheckDirExists(path) == 0) { - return 0; - } - - char parentPath[BUF_SIZE] = {0}; - GetParentPathStr(path, parentPath, BUF_SIZE); - if (strlen(parentPath) > 0 && MakeParentDir(parentPath, mode) < 0) { - return -1; - } - - struct stat s; - int ret = stat(path, &s); - if (ret < 0) { - fprintf(stderr, "error: failed to stat path: %s\n", path); - return (MakeDir(path, mode)); - } - - return 0; -} - int MountDir(const char *rootfs, const char *src) { int ret; @@ -165,23 +140,23 @@ int MountDir(const char *rootfs, const char *src) mode_t parentMode = DEFAULT_DIR_MODE; ret = MakeParentDir(parentDir, parentMode); if (ret < 0) { - fprintf(stderr, "error: failed to make dir: %s\n", parentDir); + logError("error: failed to make dir: %s\n", parentDir); return -1; } } if (CheckDirExists(dst) < 0) { const mode_t curMode = srcStat.st_mode; - ret = MakeDir(dst, curMode); + ret = MkDir(dst, curMode); if (ret < 0) { - fprintf(stderr, "error: failed to make dir: %s\n", dst); + logError("error: failed to make dir: %s\n", dst); return -1; } } ret = Mount(src, dst); if (ret < 0) { - fprintf(stderr, "error: failed to mount dir: %s to %s\n", src, dst); + logError("error: failed to mount dir: %s to %s\n", src, dst); return -1; } @@ -193,19 +168,19 @@ int DoCtrlDeviceMounting(const char *rootfs) /* device */ int ret = MountDevice(rootfs, DAVINCI_MANAGER); if (ret < 0) { - fprintf(stderr, "error: failed to mount device %s\n", DAVINCI_MANAGER); + logError("error: failed to mount device %s\n", DAVINCI_MANAGER); return -1; } ret = MountDevice(rootfs, DEVMM_SVM); if (ret < 0) { - fprintf(stderr, "error: failed to mount device %s\n", DEVMM_SVM); + logError("error: failed to mount device %s\n", DEVMM_SVM); return -1; } ret = MountDevice(rootfs, HISI_HDC); if (ret < 0) { - fprintf(stderr, "error: failed to mount device %s\n", HISI_HDC); + logError("error: failed to mount device %s\n", HISI_HDC); return -1; } @@ -217,13 +192,13 @@ int DoDirectoryMounting(const char *rootfs) /* directory */ int ret = MountDir(rootfs, ASCEND_DRIVER_PATH); if (ret < 0) { - fprintf(stderr, "error: failed to do mount %s\n", ASCEND_DRIVER_PATH); + logError("error: failed to do mount %s\n", ASCEND_DRIVER_PATH); return -1; } ret = MountDir(rootfs, ASCEND_ADDONS_PATH); if (ret < 0) { - fprintf(stderr, "error: failed to do mount %s\n", ASCEND_ADDONS_PATH); + logError("error: failed to do mount %s\n", ASCEND_ADDONS_PATH); return -1; } @@ -236,19 +211,19 @@ int DoMounting(const struct CmdArgs *args) ret = DoDeviceMounting(args->rootfs, args->devices); if (ret < 0) { - fprintf(stderr, "error: failed to do mounts\n"); + logError("error: failed to do mounts\n"); return -1; } ret = DoCtrlDeviceMounting(args->rootfs); if (ret < 0) { - fprintf(stderr, "error: failed to do mount files\n"); + logError("error: failed to do mount files\n"); return -1; } ret = DoDirectoryMounting(args->rootfs); if (ret < 0) { - fprintf(stderr, "error: failed to do mount directory\n"); + logError("error: failed to do mount directory\n"); return -1; } diff --git a/cli/src/ns.c b/cli/src/ns.c index ff9be07..def63f0 100644 --- a/cli/src/ns.c +++ b/cli/src/ns.c @@ -4,9 +4,9 @@ #include #include #include -#include "securec.h" - #include "basic.h" +#include "securec.h" +#include "logging.h" int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize) { @@ -24,7 +24,7 @@ int EnterNsByFd(int fd, int nsType) { int ret = setns(fd, nsType); if (ret < 0) { - fprintf(stderr, "error: failed to set ns: fd(%d)\n", fd); + logError("error: failed to set ns: fd(%d)\n", fd); return -1; } @@ -38,13 +38,13 @@ int EnterNsByPath(const char *path, int nsType) fd = open(path, O_RDONLY); // proc文件接口,非外部输入 if (fd < 0) { - fprintf(stderr, "error: failed to open ns path: %s\n", path); + logError("error: failed to open ns path: %s\n", path); return -1; } ret = EnterNsByFd(fd, nsType); if (ret < 0) { - fprintf(stderr, "error: failed to set ns: %s\n", path); + logError("error: failed to set ns: %s\n", path); close(fd); return -1; } diff --git a/cli/src/utils.c b/cli/src/utils.c index 47bd293..bd20826 100644 --- a/cli/src/utils.c +++ b/cli/src/utils.c @@ -1,9 +1,9 @@ #include "utils.h" +#include #include #include #include -#include #include #include #include @@ -12,6 +12,7 @@ #include #include #include "securec.h" +#include "logging.h" int IsStrEqual(const char *s1, const char *s2) { @@ -71,13 +72,13 @@ int CatFileContent(char* buffer, int bufferSize, ParseFileLine fn, const char* f char resolvedPath[PATH_MAX] = {0x0}; if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) { - fprintf(stderr, "error: cannot canonicalize path %s\n", filepath); + logError("error: cannot canonicalize path %s\n", filepath); return -1; } fp = fopen(resolvedPath, "r"); if (fp == NULL) { - fprintf(stderr, "cannot open file.\n"); + logError("cannot open file.\n"); return -1; } @@ -101,20 +102,45 @@ int CatFileContent(char* buffer, int bufferSize, ParseFileLine fn, const char* f return 0; } -int MakeDir(const char *dir, int mode) +int MkDir(const char *dir, int mode) { return mkdir(dir, mode); } +int MakeParentDir(const char *path, mode_t mode) +{ + if (*path == '\0' || *path == '.') { + return 0; + } + if (CheckDirExists(path) == 0) { + return 0; + } + + char parentPath[BUF_SIZE] = {0}; + GetParentPathStr(path, parentPath, BUF_SIZE); + if (strlen(parentPath) > 0 && MakeParentDir(parentPath, mode) < 0) { + return -1; + } + + struct stat s; + int ret = stat(path, &s); + if (ret < 0) { + logError("error: failed to stat path: %s\n", path); + return (MkDir(path, mode)); + } + + return 0; +} + int CheckDirExists(const char *dir) { DIR *ptr = opendir(dir); if (NULL == ptr) { - fprintf(stderr, "path %s not exist\n", dir); + logError("path %s not exist\n", dir); return -1; } - fprintf(stdout, "path %s exist\n", dir); + logInfo("path %s exist\n", dir); closedir(ptr); return 0; } @@ -143,13 +169,13 @@ int CreateFile(const char *path, mode_t mode) { char resolvedPath[PATH_MAX] = {0}; if (realpath(path, resolvedPath) == NULL && errno != ENOENT) { - fprintf(stderr, "error: failed to resolve path %s\n", path); + logError("error: failed to resolve path %s\n", path); return -1; } int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode); if (fd < 0) { - fprintf(stderr, "error: cannot create file: %s\n", resolvedPath); + logError("error: cannot create file: %s\n", resolvedPath); return -1; } close(fd); @@ -171,13 +197,13 @@ int Mount(const char *src, const char *dst) ret = mount(src, dst, NULL, MS_BIND, NULL); if (ret < 0) { - fprintf(stderr, "error: failed to mount\n"); + logError("error: failed to mount\n"); return -1; } ret = mount(NULL, dst, NULL, remountFlags, NULL); if (ret < 0) { - fprintf(stderr, "error: failed to re-mount\n"); + logError("error: failed to re-mount\n"); return -1; } diff --git a/cli/src/utils.h b/cli/src/utils.h index d4a0052..7d1f8f7 100644 --- a/cli/src/utils.h +++ b/cli/src/utils.h @@ -6,18 +6,21 @@ #include #include "basic.h" +// For cgroup setup +int StrHasPrefix(const char *str, const char *prefix); typedef char *(*ParseFileLine)(char *, const char *); int CatFileContent(char* buffer, int bufferSize, ParseFileLine fn, const char* filepath); bool CheckFsType(char **pLine); bool CheckSubStr(char **pLine, const char *subsys); bool CheckRootDir(char **pLine); -int MakeDir(const char *dir, int mode); + +// For mount setup +int MkDir(const char *dir, int mode); +int MakeParentDir(const char *path, mode_t mode); int CheckDirExists(const char *dir); int GetParentPathStr(const char *path, char *parent, size_t bufSize); int CreateFile(const char *path, mode_t mode); int VerfifyPathInfo(const struct PathInfo* pathInfo); int Mount(const char *src, const char *dst); -int IsStrEqual(const char *s1, const char *s2); -int StrHasPrefix(const char *str, const char *prefix); #endif \ No newline at end of file