Match-id-ff14c3cc373447ddd47e99b03a251a5ac693c6e3

This commit is contained in:
BianTanggui
2021-07-06 15:04:31 +08:00
parent c5e7bcb2bc
commit d7eb64aa02
8 changed files with 218 additions and 82 deletions

View File

@@ -18,6 +18,7 @@
#include "utils.h" #include "utils.h"
#include "options.h" #include "options.h"
#include "logger.h"
bool TakeNthWord(char **pLine, unsigned int n, char **word) bool TakeNthWord(char **pLine, unsigned int n, char **word)
{ {
@@ -73,13 +74,13 @@ int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const
char resolvedPath[PATH_MAX] = {0x0}; char resolvedPath[PATH_MAX] = {0x0};
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) { if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
LOG_ERROR("error: cannot canonicalize path %s.", filepath); Logger(FormatMessage("cannot canonicalize path %s.", filepath), 2);
return -1; return -1;
} }
fp = fopen(resolvedPath, "r"); fp = fopen(resolvedPath, "r");
if (fp == NULL) { if (fp == NULL) {
LOG_ERROR("cannot open file."); Logger("cannot open file.", 2);
return -1; return -1;
} }
@@ -157,20 +158,20 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName); ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to assemble dev path for %s.", devName); Logger(FormatMessage("failed to assemble dev path for %s.", devName), 2);
return -1; return -1;
} }
ret = stat((const char *)devPath, &devStat); ret = stat((const char *)devPath, &devStat);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to get stat of %s.", devPath); Logger(FormatMessage("failed to get stat of %s.", devPath), 2);
return -1; return -1;
} }
bool isFailed = fprintf(cgroupAllow, "c %u:%u rw", major(devStat.st_rdev), minor(devStat.st_rdev)) < 0 || bool isFailed = fprintf(cgroupAllow, "c %u:%u rw", major(devStat.st_rdev), minor(devStat.st_rdev)) < 0 ||
fflush(cgroupAllow) == EOF || ferror(cgroupAllow) < 0; fflush(cgroupAllow) == EOF || ferror(cgroupAllow) < 0;
if (isFailed) { if (isFailed) {
LOG_ERROR("error: write devices failed."); Logger("write devices failed.", 2);
return -1; return -1;
} }
@@ -183,19 +184,19 @@ int SetupDriverCgroup(FILE *cgroupAllow)
ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER); ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to setup cgroup for %s.", DAVINCI_MANAGER); Logger(FormatMessage("failed to setup cgroup for %s.", DAVINCI_MANAGER), 2);
return -1; return -1;
} }
ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM); ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to setup cgroup for %s.", DEVMM_SVM); Logger(FormatMessage("failed to setup cgroup for %s.", DEVMM_SVM), 2);
return -1; return -1;
} }
ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC); ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to setup cgroup for %s.", HISI_HDC); Logger(FormatMessage("failed to setup cgroup for %s.", HISI_HDC), 2);
return -1; return -1;
} }
@@ -210,13 +211,13 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid()); ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: assemble mount info path failed: ppid(%d).", getppid()); Logger(FormatMessage("assemble mount info path failed: ppid(%d).", getppid()), 2);
return -1; return -1;
} }
ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath); ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: cat file content failed."); Logger("cat file content failed.", 2);
return -1; return -1;
} }
@@ -224,13 +225,13 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
char cgroupPath[BUF_SIZE] = {0x0}; char cgroupPath[BUF_SIZE] = {0x0};
ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid); ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: assemble cgroup path failed: pid(%d).", pid); Logger(FormatMessage("assemble cgroup path failed: pid(%d).", pid), 2);
return -1; return -1;
} }
ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath); ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: cat file content failed."); Logger("cat file content failed.", 2);
return -1; return -1;
} }
@@ -239,7 +240,7 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
ret = sprintf_s(effPath, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH); ret = sprintf_s(effPath, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: assemble cgroup device path failed."); Logger("assemble cgroup device path failed.", 2);
return -1; return -1;
} }
@@ -254,20 +255,20 @@ int SetupCgroup(const struct ParsedConfig *config)
FILE *cgroupAllow = NULL; FILE *cgroupAllow = NULL;
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) { if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
LOG_ERROR("error: cannot canonicalize cgroup path: %s.", config->cgroupPath); Logger(FormatMessage("cannot canonicalize cgroup path: %s.", config->cgroupPath), 2);
return -1; return -1;
} }
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a"); cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
if (cgroupAllow == NULL) { if (cgroupAllow == NULL) {
LOG_ERROR("error: failed to open cgroup file: %s.", resolvedCgroupPath); Logger(FormatMessage("failed to open cgroup file: %s.", resolvedCgroupPath), 2);
return -1; return -1;
} }
ret = SetupDriverCgroup(cgroupAllow); ret = SetupDriverCgroup(cgroupAllow);
if (ret < 0) { if (ret < 0) {
fclose(cgroupAllow); fclose(cgroupAllow);
LOG_ERROR("error: failed to setup driver cgroup."); Logger("failed to setup driver cgroup.", 2);
return -1; return -1;
} }
@@ -277,14 +278,14 @@ int SetupCgroup(const struct ParsedConfig *config)
config->devices[idx]); config->devices[idx]);
if (ret < 0) { if (ret < 0) {
fclose(cgroupAllow); fclose(cgroupAllow);
LOG_ERROR("error: failed to assemble device path for no.%u.", config->devices[idx]); Logger(FormatMessage("failed to assemble device path for no.%u.", config->devices[idx]), 2);
return -1; return -1;
} }
ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName); ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName);
if (ret < 0) { if (ret < 0) {
fclose(cgroupAllow); fclose(cgroupAllow);
LOG_ERROR("error: failed to setup cgroup for %s.", deviceName); Logger(FormatMessage("failed to setup cgroup for %s.", deviceName), 2);
return -1; return -1;
} }
} }

101
cli/src/logger.c Normal file
View File

@@ -0,0 +1,101 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend-docker-cli日志模块
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define FILE_MAX_SIZE (1024*1024*10)
#define LOG_PATH_DIR "/var/log/"
void GetCurrentLocalTime(char* buffer)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
sprintf(buffer, "[%04d-%02d-%02d %02d:%02d:%02d]",
(timeinfo->tm_year+1900),
(timeinfo->tm_mon+1),
(timeinfo->tm_mday),
(timeinfo->tm_hour),
(timeinfo->tm_min),
(timeinfo->tm_sec));
}
long GetLogSize(char* filename)
{
long length = 0;
FILE *fp = NULL;
fp = fopen(filename, "rb");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
length = ftell(fp);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
return length;
}
void LogLoop(char* filename)
{
char* loopPath = LOG_PATH_DIR"docker-runtime-log.log.1";
int exist;
exist = access(loopPath, 0);
if (exist == 0)
{
unlink(loopPath);
}
rename(filename, loopPath);
}
void WriteLogFile(char* filename, long maxSize, char* buffer, unsigned bufferSize)
{
if (filename != NULL && buffer != NULL)
{
long length = GetLogSize(filename);
if (length > maxSize)
{
LogLoop(filename);
}
FILE *fp;
fp = fopen(filename, "a+");
if (fp != NULL)
{
char now[21];
memset(now, 0, sizeof(now));
GetCurrentLocalTime(now);
fwrite(now, strlen(now), 1, fp);
fwrite(buffer, bufferSize, 1, fp);
fclose(fp);
fp = NULL;
}
}
}
void Logger(const char *msg, int level) {
enum LEVEL { Info=0, Warn, Error, Debug};
enum LEVEL _level;
char *logPath = LOG_PATH_DIR"docker-runtime-log.log";
_level = level;
char buffer[strlen(msg)+20];
memset(buffer, 0, sizeof(buffer));
switch (_level)
{
case Debug:sprintf(buffer, "[Debug]%s\n", msg);
break;
case Error:sprintf(buffer, "[Error]%s\n", msg);
break;
case Warn:sprintf(buffer, "[Warn]%s\n", msg);
break;
default:sprintf(buffer, "[Info]%s\n", msg);
}
WriteLogFile(logPath, FILE_MAX_SIZE, buffer, strlen(buffer));
}

11
cli/src/logger.h Normal file
View File

@@ -0,0 +1,11 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend-docker-cli工具容器logger日志模块头文件
*/
#ifndef _LOGGER_H
#define _LOGGER_H
void Logger(const char *msg, int level);
#endif

View File

@@ -19,6 +19,8 @@
#include "u_mount.h" #include "u_mount.h"
#include "cgrp.h" #include "cgrp.h"
#include "options.h" #include "options.h"
#include "utils.h"
#include "logger.h"
#define DECIMAL 10 #define DECIMAL 10
@@ -47,7 +49,7 @@ static bool DevicesCmdArgParser(struct CmdArgs *args, const char *arg)
{ {
errno_t err = strcpy_s(args->devices, BUF_SIZE, arg); errno_t err = strcpy_s(args->devices, BUF_SIZE, arg);
if (err != EOK) { if (err != EOK) {
LOG_ERROR("error: failed to get devices from cmd args."); Logger("failed to get devices from cmd args.", 2);
return false; return false;
} }
@@ -59,12 +61,12 @@ static bool PidCmdArgParser(struct CmdArgs *args, const char *arg)
errno = 0; errno = 0;
args->pid = strtol(optarg, NULL, DECIMAL); args->pid = strtol(optarg, NULL, DECIMAL);
if (errno != 0) { if (errno != 0) {
LOG_ERROR("error: failed to convert pid string from cmd args, pid string: %s.", arg); Logger(FormatMessage("failed to convert pid string from cmd args, pid string: %s.", arg), 2);
return false; return false;
} }
if (args->pid <= 0) { if (args->pid <= 0) {
LOG_ERROR("error: invalid pid %d.", args->pid); Logger(FormatMessage("invalid pid %d.", args->pid), 2);
return false; return false;
} }
@@ -75,7 +77,7 @@ static bool RootfsCmdArgParser(struct CmdArgs *args, const char *arg)
{ {
errno_t err = strcpy_s(args->rootfs, BUF_SIZE, arg); errno_t err = strcpy_s(args->rootfs, BUF_SIZE, arg);
if (err != EOK) { if (err != EOK) {
LOG_ERROR("error: failed to get rootfs path from cmd args"); Logger("failed to get rootfs path from cmd args", 2);
return false; return false;
} }
@@ -86,7 +88,7 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
{ {
errno_t err = strcpy_s(args->options, BUF_SIZE, arg); errno_t err = strcpy_s(args->options, BUF_SIZE, arg);
if (err != EOK) { if (err != EOK) {
LOG_ERROR("error: failed to get options string from cmd args"); Logger("failed to get options string from cmd args", 2);
return false; return false;
} }
@@ -96,14 +98,14 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg) static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
{ {
if (args->files.count == MAX_MOUNT_NR) { if (args->files.count == MAX_MOUNT_NR) {
LOG_ERROR("error: too many files to mount, max number is %u", MAX_MOUNT_NR); Logger(FormatMessage("too many files to mount, max number is %u", MAX_MOUNT_NR), 2);
return -1; return -1;
} }
char *dst = &args->files.list[args->files.count++][0]; char *dst = &args->files.list[args->files.count++][0];
errno_t err = strcpy_s(dst, PATH_MAX, arg); errno_t err = strcpy_s(dst, PATH_MAX, arg);
if (err != EOK) { if (err != EOK) {
LOG_ERROR("error: failed to copy mount file path: %s", arg); Logger(FormatMessage("failed to copy mount file path: %s", arg), 2);
return false; return false;
} }
@@ -113,14 +115,14 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
static bool MountDirCmdArgParser(struct CmdArgs *args, const char *arg) static bool MountDirCmdArgParser(struct CmdArgs *args, const char *arg)
{ {
if (args->dirs.count == MAX_MOUNT_NR) { if (args->dirs.count == MAX_MOUNT_NR) {
LOG_ERROR("error: too many directories to mount, max number is %u", MAX_MOUNT_NR); Logger(FormatMessage("too many directories to mount, max number is %u", MAX_MOUNT_NR), 2);
return -1; return -1;
} }
char *dst = &args->dirs.list[args->dirs.count++][0]; 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) { if (err != EOK) {
LOG_ERROR("error: failed to copy mount directory path: %s", arg); Logger(FormatMessage("error: failed to copy mount directory path: %s", arg), 2);
return false; return false;
} }
@@ -151,16 +153,14 @@ static int ParseOneCmdArg(struct CmdArgs *args, char indicator, const char *valu
} }
if (i == NUM_OF_CMD_ARGS) { if (i == NUM_OF_CMD_ARGS) {
LOG_ERROR("error: unrecognized cmd arg: indicate char: %c, value: %s.", indicator, value); Logger(FormatMessage("unrecognized cmd arg: indicate char: %c, value: %s.", indicator, value), 2);
return -1; return -1;
} }
bool isOK = g_cmdArgParsers[i].parser(args, value); bool isOK = g_cmdArgParsers[i].parser(args, value);
if (!isOK) { if (!isOK) {
LOG_ERROR("error: failed while parsing cmd arg, indicate char: %c, value: %s.", indicator, value); Logger(FormatMessage("failed while parsing cmd arg, indicate char: %c, value: %s.", indicator, value), 2);
return -1; return -1;
} }
return 0; return 0;
} }
@@ -179,14 +179,14 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
token = strtok_s(devices, sep, &context); token = strtok_s(devices, sep, &context);
while (token != NULL) { while (token != NULL) {
if (idx >= *idListSize) { if (idx >= *idListSize) {
LOG_ERROR("error: too many devices(%u), support %u devices maximally", idx, *idListSize); Logger(FormatMessage("too many devices(%u), support %u devices maximally", idx, *idListSize), 2);
return -1; return -1;
} }
errno = 0; errno = 0;
idList[idx] = strtoul((const char *)token, NULL, DECIMAL); idList[idx] = strtoul((const char *)token, NULL, DECIMAL);
if (errno != 0) { if (errno != 0) {
LOG_ERROR("error: failed to convert device id (%s) from cmd args", token); Logger(FormatMessage("failed to convert device id (%s) from cmd args", token), 2);
return -1; return -1;
} }
@@ -205,38 +205,38 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs); err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs);
if (err != EOK) { if (err != EOK) {
LOG_ERROR("error: failed to copy rootfs path to parsed config."); Logger("failed to copy rootfs path to parsed config.", 2);
return -1; return -1;
} }
ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices); ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to parse device ids from cmdline argument"); Logger("failed to parse device ids from cmdline argument", 2);
return -1; return -1;
} }
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE); ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to get container mnt ns path: pid(%d).", args->pid); Logger(FormatMessage("failed to get container mnt ns path: pid(%d).", args->pid), 2);
return -1; return -1;
} }
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE); ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to get cgroup path."); Logger("failed to get cgroup path.", 2);
return -1; return -1;
} }
char originNsPath[BUF_SIZE] = {0}; char originNsPath[BUF_SIZE] = {0};
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE); ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to get self ns path."); Logger("failed to get self ns path.", 2);
return -1; return -1;
} }
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口非外部输入 config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口非外部输入
if (config->originNsFd < 0) { if (config->originNsFd < 0) {
LOG_ERROR("error: failed to get self ns fd: %s.", originNsPath); Logger(FormatMessage("failed to get self ns fd: %s.", originNsPath), 2);
return -1; return -1;
} }
@@ -255,28 +255,28 @@ int SetupContainer(struct CmdArgs *args)
ret = DoPrepare(args, &config); ret = DoPrepare(args, &config);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to prepare nesessary config."); Logger("failed to prepare nesessary config.", 2);
return -1; return -1;
} }
// enter container's mount namespace // enter container's mount namespace
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS); ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to set to container ns: %s.", config.containerNsPath); Logger(FormatMessage("failed to set to container ns: %s.", config.containerNsPath), 2);
close(config.originNsFd); close(config.originNsFd);
return -1; return -1;
} }
ret = DoMounting(&config); ret = DoMounting(&config);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to do mounting."); Logger("failed to do mounting.", 2);
close(config.originNsFd); close(config.originNsFd);
return -1; return -1;
} }
ret = SetupCgroup(&config); ret = SetupCgroup(&config);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to set up cgroup."); Logger("failed to set up cgroup.", 2);
close(config.originNsFd); close(config.originNsFd);
return -1; return -1;
} }
@@ -284,7 +284,7 @@ int SetupContainer(struct CmdArgs *args)
// back to original namespace // back to original namespace
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS); ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to set ns back."); Logger("failed to set ns back.", 2);
close(config.originNsFd); close(config.originNsFd);
return -1; return -1;
} }
@@ -300,27 +300,28 @@ int Process(int argc, char **argv)
int optionIndex; int optionIndex;
struct CmdArgs args = {0}; struct CmdArgs args = {0};
Logger("runc start prestart-hook ...", 0);
while ((c = getopt_long(argc, argv, "d:p:r:o:f:i", g_cmdOpts, &optionIndex)) != -1) { while ((c = getopt_long(argc, argv, "d:p:r:o:f:i", g_cmdOpts, &optionIndex)) != -1) {
ret = ParseOneCmdArg(&args, (char)c, optarg); ret = ParseOneCmdArg(&args, (char)c, optarg);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to parse cmd args."); Logger("failed to parse cmd args.", 2);
return -1; return -1;
} }
} }
Logger("verify parameters valid and parse runtime options", 0);
if (!IsCmdArgsValid(&args)) { if (!IsCmdArgsValid(&args)) {
LOG_ERROR("error: information not completed or valid."); Logger("information not completed or valid.", 2);
return -1; return -1;
} }
ParseRuntimeOptions(args.options); ParseRuntimeOptions(args.options);
Logger("setup container config", 0);
ret = SetupContainer(&args); ret = SetupContainer(&args);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to setup container."); Logger("failed to setup container.", 2);
return ret; return ret;
} }
Logger("prestart-hook setup container successful.", 0);
return 0; return 0;
} }

View File

@@ -10,6 +10,8 @@
#include <unistd.h> #include <unistd.h>
#include "basic.h" #include "basic.h"
#include "securec.h" #include "securec.h"
#include "utils.h"
#include "logger.h"
int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize) int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize)
{ {
@@ -27,7 +29,7 @@ int EnterNsByFd(int fd, int nsType)
{ {
int ret = setns(fd, nsType); int ret = setns(fd, nsType);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to set ns: fd(%d).", fd); Logger(FormatMessage("failed to set ns: fd(%d).", fd), 2);
return -1; return -1;
} }
@@ -41,13 +43,13 @@ int EnterNsByPath(const char *path, int nsType)
fd = open(path, O_RDONLY); // proc文件接口非外部输入 fd = open(path, O_RDONLY); // proc文件接口非外部输入
if (fd < 0) { if (fd < 0) {
LOG_ERROR("error: failed to open ns path: %s.", path); Logger(FormatMessage("failed to open ns path: %s.", path), 2);
return -1; return -1;
} }
ret = EnterNsByFd(fd, nsType); ret = EnterNsByFd(fd, nsType);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to set ns: %s.", path); Logger(FormatMessage("failed to set ns: %s.", path), 2);
close(fd); close(fd);
return -1; return -1;
} }

View File

@@ -14,6 +14,7 @@
#include "basic.h" #include "basic.h"
#include "utils.h" #include "utils.h"
#include "options.h" #include "options.h"
#include "logger.h"
int Mount(const char *src, const char *dst) int Mount(const char *src, const char *dst)
{ {
@@ -23,13 +24,13 @@ int Mount(const char *src, const char *dst)
ret = mount(src, dst, NULL, mountFlags, NULL); ret = mount(src, dst, NULL, mountFlags, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount."); Logger("failed to mount.", 2);
return -1; return -1;
} }
ret = mount(NULL, dst, NULL, remountFlags, NULL); ret = mount(NULL, dst, NULL, remountFlags, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to re-mount."); Logger("failed to re-mount.", 2);
return -1; return -1;
} }
@@ -65,7 +66,7 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
} }
if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) { if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) {
LOG_ERROR("error: cannot canonicalize device dst: %s.", dst); Logger(FormatMessage("cannot canonicalize device dst: %s.", dst), 2);
return -1; return -1;
} }
@@ -77,7 +78,7 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
} else { } else {
err = strcpy_s(dst, dstBufSize, resolvedDst); err = strcpy_s(dst, dstBufSize, resolvedDst);
if (err != EOK) { if (err != EOK) {
LOG_ERROR("error: failed to copy resolved device mnt path to dst: %s.", resolvedDst); Logger(FormatMessage("failed to copy resolved device mnt path to dst: %s.", resolvedDst), 2);
return -1; return -1;
} }
} }
@@ -94,14 +95,14 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
ret = GetDeviceMntSrcDst(rootfs, srcDeviceName, dstDeviceName, &pathInfo); ret = GetDeviceMntSrcDst(rootfs, srcDeviceName, dstDeviceName, &pathInfo);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to get device mount src and(or) dst path, device name: %s.", srcDeviceName); Logger(FormatMessage("failed to get device mount src and(or) dst path, device name: %s.", srcDeviceName), 2);
return -1; return -1;
} }
struct stat srcStat; struct stat srcStat;
ret = stat((const char *)src, &srcStat); ret = stat((const char *)src, &srcStat);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to stat src: %s.", src); Logger(FormatMessage("failed to stat src: %s.", src), 2);
return -1; return -1;
} }
@@ -111,21 +112,21 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
if (ret == 0 && S_ISCHR(dstStat.st_mode)) { if (ret == 0 && S_ISCHR(dstStat.st_mode)) {
return 0; // 特权容器自动挂载HOST所有设备故此处跳过 return 0; // 特权容器自动挂载HOST所有设备故此处跳过
} else if (ret == 0) { } else if (ret == 0) {
LOG_ERROR("error: %s already exists but not a char device as expected.", dst); Logger(FormatMessage("%s already exists but not a char device as expected.", dst), 2);
return -1; return -1;
} else if (ret < 0 && errno != ENOENT) { } else if (ret < 0 && errno != ENOENT) {
LOG_ERROR("error: failed to check dst %s stat", dst); Logger(FormatMessage("failed to check dst %s stat", dst), 2);
return -1; return -1;
} }
ret = MakeMountPoints(dst, srcStat.st_mode); ret = MakeMountPoints(dst, srcStat.st_mode);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to create mount dst file: %s.", dst); Logger(FormatMessage("failed to create mount dst file: %s.", dst), 2);
return -1; return -1;
} }
ret = Mount(src, dst); ret = Mount(src, dst);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount dev."); Logger("failed to mount dev.", 2);
return -1; return -1;
} }
@@ -141,12 +142,12 @@ int DoDeviceMounting(const char *rootfs, const char *device_name, const unsigned
int srcRet = sprintf_s(srcDeviceName, BUF_SIZE, "%s%u", device_name, ids[idx]); int srcRet = sprintf_s(srcDeviceName, BUF_SIZE, "%s%u", device_name, ids[idx]);
int dstRet = sprintf_s(dstDeviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]); int dstRet = sprintf_s(dstDeviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]);
if (srcRet < 0 || dstRet < 0) { if (srcRet < 0 || dstRet < 0) {
LOG_ERROR("error: assemble device name failed, id: %u.", ids[idx]); Logger(FormatMessage("assemble device name failed, id: %u.", ids[idx]), 2);
return -1; return -1;
} }
int ret = MountDevice(rootfs, srcDeviceName, dstDeviceName); int ret = MountDevice(rootfs, srcDeviceName, dstDeviceName);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", srcDeviceName); Logger(FormatMessage("failed to mount device %s.", srcDeviceName), 2);
return -1; return -1;
} }
} }
@@ -161,7 +162,7 @@ int MountFile(const char *rootfs, const char *filepath)
ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath); ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to assemble file mounting path, file: %s.", filepath); Logger(FormatMessage("failed to assemble file mounting path, file: %s.", filepath), 2);
return -1; return -1;
} }
@@ -173,13 +174,13 @@ int MountFile(const char *rootfs, const char *filepath)
ret = MakeMountPoints(dst, srcStat.st_mode); ret = MakeMountPoints(dst, srcStat.st_mode);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to create mount dst file: %s.", dst); Logger(FormatMessage("failed to create mount dst file: %s.", dst), 2);
return -1; return -1;
} }
ret = Mount(filepath, dst); ret = Mount(filepath, dst);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount dev."); Logger("failed to mount dev.", 2);
return -1; return -1;
} }
@@ -204,13 +205,13 @@ int MountDir(const char *rootfs, const char *src)
ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE); ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to make dir: %s.", dst); Logger(FormatMessage("failed to make dir: %s.", dst), 2);
return -1; return -1;
} }
ret = Mount(src, dst); ret = Mount(src, dst);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount dir: %s to %s.", src, dst); Logger(FormatMessage("error: failed to mount dir: %s to %s.", src, dst), 2);
return -1; return -1;
} }
@@ -222,19 +223,19 @@ int DoCtrlDeviceMounting(const char *rootfs)
/* device */ /* device */
int ret = MountDevice(rootfs, DAVINCI_MANAGER, NULL); int ret = MountDevice(rootfs, DAVINCI_MANAGER, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", DAVINCI_MANAGER); Logger(FormatMessage("failed to mount device %s.", DAVINCI_MANAGER), 2);
return -1; return -1;
} }
ret = MountDevice(rootfs, DEVMM_SVM, NULL); ret = MountDevice(rootfs, DEVMM_SVM, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", DEVMM_SVM); Logger(FormatMessage("failed to mount device %s.", DEVMM_SVM), 2);
return -1; return -1;
} }
ret = MountDevice(rootfs, HISI_HDC, NULL); ret = MountDevice(rootfs, HISI_HDC, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", HISI_HDC); Logger(FormatMessage("failed to mount device %s.", HISI_HDC), 2);
return -1; return -1;
} }
@@ -248,7 +249,7 @@ int DoDirectoryMounting(const char *rootfs, const struct MountList *list)
for (unsigned int i = 0; i < list->count; i++) { for (unsigned int i = 0; i < list->count; i++) {
ret = MountDir(rootfs, (const char *)&list->list[i][0]); ret = MountDir(rootfs, (const char *)&list->list[i][0]);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to do directory mounting for %s.", (const char *)&list->list[i][0]); Logger(FormatMessage("failed to do directory mounting for %s.", (const char *)&list->list[i][0]), 2);
return -1; return -1;
} }
} }
@@ -263,7 +264,7 @@ int DoFileMounting(const char *rootfs, const struct MountList *list)
for (unsigned int i = 0; i < list->count; i++) { for (unsigned int i = 0; i < list->count; i++) {
ret = MountFile(rootfs, (const char *)&list->list[i][0]); ret = MountFile(rootfs, (const char *)&list->list[i][0]);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to do file mounting for %s.", (const char *)&list->list[i][0]); Logger(FormatMessage("failed to do file mounting for %s.", (const char *)&list->list[i][0]), 2);
return -1; return -1;
} }
} }
@@ -278,13 +279,13 @@ int DoMounting(const struct ParsedConfig *config)
(IsVirtual() ? VDEVICE_NAME : DEVICE_NAME), (IsVirtual() ? VDEVICE_NAME : DEVICE_NAME),
config->devices, config->devicesNr); config->devices, config->devicesNr);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount devices."); Logger("failed to mount devices.", 2);
return -1; return -1;
} }
ret = DoCtrlDeviceMounting(config->rootfs); ret = DoCtrlDeviceMounting(config->rootfs);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount ctrl devices."); Logger("failed to mount ctrl devices.", 2);
return -1; return -1;
} }
@@ -294,13 +295,13 @@ int DoMounting(const struct ParsedConfig *config)
ret = DoFileMounting(config->rootfs, config->files); ret = DoFileMounting(config->rootfs, config->files);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount files."); Logger("failed to mount files.", 2);
return -1; return -1;
} }
ret = DoDirectoryMounting(config->rootfs, config->dirs); ret = DoDirectoryMounting(config->rootfs, config->dirs);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to do mount directories."); Logger("failed to do mount directories.", 2);
return -1; return -1;
} }

View File

@@ -13,6 +13,24 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "securec.h" #include "securec.h"
#include "logger.h"
char *FormatMessage(char *format, ...){
va_list list;
// 获取格式化后字符串的长度
va_start(list, format);
int size = vsnprintf(NULL, 0, format, list);
va_end(list);
if(size <= 0){
return NULL;
}
size++;
// 复位va_list, 将格式化字符串写入到buf
va_start(list, format);
char *buf = (char *)malloc(size);
vsnprintf(buf, size, format, list);
va_end(list);
return buf;
}
int IsStrEqual(const char *s1, const char *s2) int IsStrEqual(const char *s1, const char *s2)
{ {
@@ -99,19 +117,19 @@ int MakeMountPoints(const char *path, mode_t mode)
int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE); int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to make parent dir for file: %s", path); Logger(FormatMessage("failed to make parent dir for file: %s", path), 2);
return -1; return -1;
} }
char resolvedPath[PATH_MAX] = {0}; char resolvedPath[PATH_MAX] = {0};
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) { if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
LOG_ERROR("error: failed to resolve path %s.", path); Logger(FormatMessage("failed to resolve path %s.", path), 2);
return -1; return -1;
} }
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode); int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
if (fd < 0) { if (fd < 0) {
LOG_ERROR("error: cannot create file: %s.", resolvedPath); Logger(FormatMessage("cannot create file: %s.", resolvedPath), 2);
return -1; return -1;
} }
close(fd); close(fd);

View File

@@ -9,6 +9,7 @@
#include <sys/types.h> #include <sys/types.h>
#include "basic.h" #include "basic.h"
char *FormatMessage(char *format, ...);
int IsStrEqual(const char *s1, const char *s2); int IsStrEqual(const char *s1, const char *s2);
int StrHasPrefix(const char *str, const char *prefix); int StrHasPrefix(const char *str, const char *prefix);
int MkDir(const char *dir, int mode); int MkDir(const char *dir, int mode);