Match-id-245bc76da887818fe1357759f0f0645039ea7ff1

This commit is contained in:
BianTanggui
2020-07-25 10:40:27 +08:00
parent d89d13214f
commit af7ffd82c0
6 changed files with 56 additions and 56 deletions

View File

@@ -71,22 +71,22 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
struct stat devStat;
char devPath[BUF_SIZE];
ret = snprintf_s(devPath, BUF_SIZE, BUF_SIZE, "/dev/%s", devName);
ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName);
if (ret < 0) {
LogError("error: failed to assemble dev path for %s\n", devName);
LogError("error: failed to assemble dev path for %s.", devName);
return -1;
}
ret = stat((const char *)devPath, &devStat);
if (ret < 0) {
LogError("error: failed to get stat of %s\n", devPath);
LogError("error: failed to get stat of %s.", 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) {
LogError("error: write devices failed\n");
LogError("error: write devices failed.");
return -1;
}
@@ -99,19 +99,19 @@ int SetupDriverCgroup(FILE *cgroupAllow)
ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER);
if (ret < 0) {
LogError("error: failed to setup cgroup for %s\n", DAVINCI_MANAGER);
LogError("error: failed to setup cgroup for %s.", DAVINCI_MANAGER);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM);
if (ret < 0) {
LogError("error: failed to setup cgroup for %s\n", DEVMM_SVM);
LogError("error: failed to setup cgroup for %s.", DEVMM_SVM);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC);
if (ret < 0) {
LogError("error: failed to setup cgroup for %s\n", HISI_HDC);
LogError("error: failed to setup cgroup for %s.", HISI_HDC);
return -1;
}
@@ -124,15 +124,15 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz
char mountPath[BUF_SIZE] = {0x0};
char mount[BUF_SIZE] = {0x0};
ret = snprintf_s(mountPath, BUF_SIZE, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
if (ret < 0) {
LogError("error: assemble mount info path failed: ppid(%d)\n", getppid());
LogError("error: assemble mount info path failed: ppid(%d).", getppid());
return -1;
}
ret = CatFileContent(mount, BUF_SIZE, GetCgroupMount, mountPath);
if (ret < 0) {
LogError("error: cat file content failed\n");
LogError("error: cat file content failed.");
return -1;
}
@@ -146,16 +146,16 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz
ret = CatFileContent(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
if (ret < 0) {
LogError("error: cat file content failed\n");
LogError("error: cat file content failed.");
return -1;
}
// cut last '\n' off
cgroup[strcspn(cgroup, "\n")] = '\0';
ret = snprintf_s(effPath, BUF_SIZE, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH);
ret = sprintf_s(effPath, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH);
if (ret < 0) {
LogError("error: assemble cgroup device path failed: \n");
LogError("error: assemble cgroup device path failed.");
return -1;
}
@@ -184,14 +184,14 @@ int SetupCgroup(struct CmdArgs *args, const char *cgroupPath)
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
if (cgroupAllow == NULL) {
LogError("error: failed to open cgroup file: %s\n", resolvedCgroupPath);
LogError("error: failed to open cgroup file: %s.", resolvedCgroupPath);
return -1;
}
ret = SetupDriverCgroup(cgroupAllow);
if (ret < 0) {
fclose(cgroupAllow);
LogError("error: failed to setup driver cgroup\n");
LogError("error: failed to setup driver cgroup.");
return -1;
}

View File

@@ -12,7 +12,7 @@ void LogError(const char *fmt, ...)
va_list args;
va_start(args, fmt);
fprintf(stderr, fmt, args);
vfprintf(stderr, fmt, args);
va_end(args);
}
@@ -21,6 +21,6 @@ void LogInfo(const char *fmt, ...)
va_list args;
va_start(args, fmt);
fprintf(stdout, fmt, args);
vfprintf(stdout, fmt, args);
va_end(args);
}

View File

@@ -135,26 +135,26 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
if (ret < 0) {
LogError("error: failed to get container mnt ns path: pid(%d)\n", args->pid);
LogError("error: failed to get container mnt ns path: pid(%d).", args->pid);
return -1;
}
ret = GetCgroupPath(args, config->cgroupPath, BUF_SIZE);
if (ret < 0) {
LogError("error: failed to get cgroup path\n");
LogError("error: failed to get cgroup path.");
return -1;
}
char originNsPath[BUF_SIZE] = {0};
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
if (ret < 0) {
LogError("error: failed to get self ns path\n");
LogError("error: failed to get self ns path.");
return -1;
}
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口非外部输入
if (config->originNsFd < 0) {
LogError("error: failed to get self ns fd: %s\n", originNsPath);
LogError("error: failed to get self ns fd: %s.", originNsPath);
return -1;
}
@@ -174,28 +174,28 @@ int SetupContainer(struct CmdArgs *args)
ret = DoPrepare(args, &config);
if (ret < 0) {
LogError("error: failed to prepare nesessary config\n");
LogError("error: failed to prepare nesessary config.");
return -1;
}
// enter container's mount namespace
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
if (ret < 0) {
LogError("error: failed to set to container ns: %s\n", config.containerNsPath);
LogError("error: failed to set to container ns: %s.", config.containerNsPath);
close(config.originNsFd);
return -1;
}
ret = DoMounting(args);
if (ret < 0) {
LogError("error: failed to do mounting\n");
LogError("error: failed to do mounting.");
close(config.originNsFd);
return -1;
}
ret = SetupCgroup(args, (const char *)config.cgroupPath);
if (ret < 0) {
LogError("error: failed to set up cgroup\n");
LogError("error: failed to set up cgroup.");
close(config.originNsFd);
return -1;
}
@@ -203,7 +203,7 @@ int SetupContainer(struct CmdArgs *args)
// back to original namespace
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
if (ret < 0) {
LogError("error: failed to set ns back\n");
LogError("error: failed to set ns back.");
close(config.originNsFd);
return -1;
}
@@ -228,7 +228,7 @@ int Process(int argc, char **argv)
}
if (!IsCmdArgsValid(&args)) {
LogError("error: information not completed or valid.\n");
LogError("error: information not completed or valid.");
return -1;
}

View File

@@ -31,24 +31,24 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName,
char *src = pathInfo->src;
char *dst = pathInfo->dst;
ret = snprintf_s(src, srcBufSize, srcBufSize, "/dev/%s", deviceName);
ret = sprintf_s(src, srcBufSize, "/dev/%s", deviceName);
if (ret < 0) {
return -1;
}
ret = snprintf_s(unresolvedDst, BUF_SIZE, BUF_SIZE, "%s%s", rootfs, src);
ret = sprintf_s(unresolvedDst, BUF_SIZE, "%s%s", rootfs, src);
if (ret < 0) {
return -1;
}
if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) {
LogError("error: cannot canonicalize device dst: %s\n", dst);
LogError("error: cannot canonicalize device dst: %s.", dst);
return -1;
}
err = strcpy_s(dst, dstBufSize, (const char *)resolvedDst);
if (err != EOK) {
LogError("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.", resolvedDst);
return -1;
}
@@ -64,26 +64,26 @@ int MountDevice(const char *rootfs, const char *deviceName)
ret = GetDeviceMntSrcDst(rootfs, deviceName, &pathInfo);
if (ret < 0) {
LogError("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.", deviceName);
return -1;
}
struct stat srcStat;
ret = stat((const char *)src, &srcStat);
if (ret < 0) {
LogError("error: failed to stat src: %s\n", src);
LogError("error: failed to stat src: %s.", src);
return -1;
}
ret = CreateFile(dst, srcStat.st_mode);
if (ret < 0) {
LogError("error: failed to create mount dst file: %s\n", dst);
LogError("error: failed to create mount dst file: %s.", dst);
return -1;
}
ret = Mount(src, dst);
if (ret < 0) {
LogError("error: failed to mount dev\n");
LogError("error: failed to mount dev.");
return -1;
}
@@ -128,7 +128,7 @@ int MountDir(const char *rootfs, const char *src)
int ret;
char dst[BUF_SIZE] = {0};
ret = snprintf_s(dst, BUF_SIZE, BUF_SIZE, "%s%s", rootfs, src);
ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, src);
if (ret < 0) {
return -1;
}
@@ -146,7 +146,7 @@ int MountDir(const char *rootfs, const char *src)
mode_t parentMode = DEFAULT_DIR_MODE;
ret = MakeParentDir(parentDir, parentMode);
if (ret < 0) {
LogError("error: failed to make dir: %s\n", parentDir);
LogError("error: failed to make dir: %s.", parentDir);
return -1;
}
}
@@ -155,14 +155,14 @@ int MountDir(const char *rootfs, const char *src)
const mode_t curMode = srcStat.st_mode;
ret = MkDir(dst, curMode);
if (ret < 0) {
LogError("error: failed to make dir: %s\n", dst);
LogError("error: failed to make dir: %s.", dst);
return -1;
}
}
ret = Mount(src, dst);
if (ret < 0) {
LogError("error: failed to mount dir: %s to %s\n", src, dst);
LogError("error: failed to mount dir: %s to %s.", src, dst);
return -1;
}
@@ -174,19 +174,19 @@ int DoCtrlDeviceMounting(const char *rootfs)
/* device */
int ret = MountDevice(rootfs, DAVINCI_MANAGER);
if (ret < 0) {
LogError("error: failed to mount device %s\n", DAVINCI_MANAGER);
LogError("error: failed to mount device %s.", DAVINCI_MANAGER);
return -1;
}
ret = MountDevice(rootfs, DEVMM_SVM);
if (ret < 0) {
LogError("error: failed to mount device %s\n", DEVMM_SVM);
LogError("error: failed to mount device %s.", DEVMM_SVM);
return -1;
}
ret = MountDevice(rootfs, HISI_HDC);
if (ret < 0) {
LogError("error: failed to mount device %s\n", HISI_HDC);
LogError("error: failed to mount device %s.", HISI_HDC);
return -1;
}
@@ -198,13 +198,13 @@ int DoDirectoryMounting(const char *rootfs)
/* directory */
int ret = MountDir(rootfs, ASCEND_DRIVER_PATH);
if (ret < 0) {
LogError("error: failed to do mount %s\n", ASCEND_DRIVER_PATH);
LogError("error: failed to do mount %s.", ASCEND_DRIVER_PATH);
return -1;
}
ret = MountDir(rootfs, ASCEND_ADDONS_PATH);
if (ret < 0) {
LogError("error: failed to do mount %s\n", ASCEND_ADDONS_PATH);
LogError("error: failed to do mount %s.", ASCEND_ADDONS_PATH);
return -1;
}
@@ -217,13 +217,13 @@ int DoMounting(const struct CmdArgs *args)
ret = DoDeviceMounting(args->rootfs, args->devices);
if (ret < 0) {
LogError("error: failed to do mounts\n");
LogError("error: failed to do mounts.");
return -1;
}
ret = DoCtrlDeviceMounting(args->rootfs);
if (ret < 0) {
LogError("error: failed to do mount files\n");
LogError("error: failed to do mount files.");
return -1;
}

View File

@@ -15,20 +15,20 @@
int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize)
{
static const char *fmtStr = "/proc/%d/ns/%s";
return snprintf_s(buf, BUF_SIZE, bufSize, fmtStr, pid, nsType);
return sprintf_s(buf, bufSize, fmtStr, pid, nsType);
}
int GetSelfNsPath(const char *nsType, char *buf, size_t bufSize)
{
static const char *fmtStr = "/proc/self/ns/%s";
return snprintf_s(buf, BUF_SIZE, bufSize, fmtStr, nsType);
return sprintf_s(buf, bufSize, fmtStr, nsType);
}
int EnterNsByFd(int fd, int nsType)
{
int ret = setns(fd, nsType);
if (ret < 0) {
LogError("error: failed to set ns: fd(%d)\n", fd);
LogError("error: failed to set ns: fd(%d).", fd);
return -1;
}
@@ -42,13 +42,13 @@ int EnterNsByPath(const char *path, int nsType)
fd = open(path, O_RDONLY); // proc文件接口非外部输入
if (fd < 0) {
LogError("error: failed to open ns path: %s\n", path);
LogError("error: failed to open ns path: %s.", path);
return -1;
}
ret = EnterNsByFd(fd, nsType);
if (ret < 0) {
LogError("error: failed to set ns: %s\n", path);
LogError("error: failed to set ns: %s.", path);
close(fd);
return -1;
}

View File

@@ -122,11 +122,11 @@ int CheckDirExists(const char *dir)
{
DIR *ptr = opendir(dir);
if (NULL == ptr) {
LogError("path %s not exist\n", dir);
LogError("path %s not exist.", dir);
return -1;
}
LogInfo("path %s exist\n", dir);
LogInfo("path %s exist.", dir);
closedir(ptr);
return 0;
}
@@ -169,7 +169,7 @@ int MakeParentDir(const char *path, mode_t mode)
struct stat s;
int ret = stat(path, &s);
if (ret < 0) {
LogError("error: failed to stat path: %s\n", path);
LogError("error: failed to stat path: %s.", path);
return (MkDir(path, mode));
}
@@ -180,13 +180,13 @@ int CreateFile(const char *path, mode_t mode)
{
char resolvedPath[PATH_MAX] = {0};
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
LogError("error: failed to resolve path %s\n", path);
LogError("error: failed to resolve path %s.", path);
return -1;
}
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
if (fd < 0) {
LogError("error: cannot create file: %s\n", resolvedPath);
LogError("error: cannot create file: %s.", resolvedPath);
return -1;
}
close(fd);