Match-id-96556915147637860db042d07e874ab9d2ea0b4f

This commit is contained in:
BianTanggui
2021-07-13 10:40:41 +08:00
parent ed39ae1bc8
commit bc45b64469
8 changed files with 163 additions and 100 deletions

View File

@@ -15,6 +15,7 @@
#define DEVMM_SVM "devmm_svm"
#define HISI_HDC "hisi_hdc"
#define DEFAULT_DIR_MODE 0755
#define DEFAULT_LOG_MODE 0666
#define BUF_SIZE 1024
#define MAX_DEVICE_NR 1024
#define MAX_MOUNT_NR 512
@@ -23,7 +24,8 @@
#define LEVEL_WARN 1
#define LEVEL_ERROR 2
#define LEVEL_DEBUG 3
#define SCREEN_NO 0
#define SCREEN_YES 1
#define LOG_ERROR(fmt, ...) \
do { \

View File

@@ -74,14 +74,14 @@ int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
char* str = FormatLogMessage("cannot canonicalize path %s.", filepath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
fp = fopen(resolvedPath, "r");
if (fp == NULL) {
Logger("cannot open file.", LEVEL_ERROR);
Logger("cannot open file.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -160,7 +160,7 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName);
if (ret < 0) {
char* str = FormatLogMessage("failed to assemble dev path for %s.", devName);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -168,7 +168,7 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
ret = stat((const char *)devPath, &devStat);
if (ret < 0) {
char* str = FormatLogMessage("failed to get stat of %s.", devPath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -176,7 +176,7 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
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) {
Logger("write devices failed.", LEVEL_ERROR);
Logger("write devices failed.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -190,7 +190,7 @@ int SetupDriverCgroup(FILE *cgroupAllow)
ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER);
if (ret < 0) {
char* str = FormatLogMessage("failed to setup cgroup for %s.", DAVINCI_MANAGER);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -198,7 +198,7 @@ int SetupDriverCgroup(FILE *cgroupAllow)
ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM);
if (ret < 0) {
char* str = FormatLogMessage("failed to setup cgroup for %s.", DEVMM_SVM);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -206,7 +206,7 @@ int SetupDriverCgroup(FILE *cgroupAllow)
ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC);
if (ret < 0) {
char* str = FormatLogMessage("failed to setup cgroup for %s.", HISI_HDC);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -223,14 +223,14 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
if (ret < 0) {
char* str = FormatLogMessage("assemble mount info path failed: ppid(%d).", getppid());
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath);
if (ret < 0) {
Logger("cat file content failed.", LEVEL_ERROR);
Logger("cat file content failed.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -239,14 +239,14 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid);
if (ret < 0) {
char* str = FormatLogMessage("assemble cgroup path failed: pid(%d).", pid);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
if (ret < 0) {
Logger("cat file content failed.", LEVEL_ERROR);
Logger("cat file content failed.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -255,7 +255,7 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
ret = sprintf_s(effPath, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH);
if (ret < 0) {
Logger("assemble cgroup device path failed.", LEVEL_ERROR);
Logger("assemble cgroup device path failed.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -272,21 +272,23 @@ int SetupCgroup(const struct ParsedConfig *config)
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
str = FormatLogMessage("cannot canonicalize cgroup path: %s.", config->cgroupPath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
if (cgroupAllow == NULL) {
str = FormatLogMessage("failed to open cgroup file: %s.", resolvedCgroupPath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = SetupDriverCgroup(cgroupAllow);
if (ret < 0) {
fclose(cgroupAllow);
Logger("failed to setup driver cgroup.", LEVEL_ERROR);
Logger("failed to setup driver cgroup.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -297,14 +299,16 @@ int SetupCgroup(const struct ParsedConfig *config)
if (ret < 0) {
fclose(cgroupAllow);
str = FormatLogMessage("failed to assemble device path for no.%u.", config->devices[idx]);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName);
if (ret < 0) {
fclose(cgroupAllow);
str = FormatLogMessage("failed to setup cgroup for %s.", deviceName);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
}

View File

@@ -5,24 +5,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include "securec.h"
#include "basic.h"
#include "utils.h"
#define FILE_MAX_SIZE (1024 * 1024 * 10)
#define LOG_PATH_DIR "/var/log/"
#define LOG_PATH_DIR "/var/log/ascend-docker-runtime/"
#define TEMP_BUFFER 30
#define YEAR_OFFSET 1900
#define MONTH_OFFSET 1
#define LOG_LENGTH 1024
int GetCurrentLocalTime(char* buffer, int length)
{
time_t rawtime;
struct tm* timeinfo = NULL;
time(&rawtime);
timeinfo = localtime(&rawtime);
if (timeinfo == NULL) {
return -1;
}
return sprintf_s(buffer,
TEMP_BUFFER,
"[%04d-%02d-%02d %02d:%02d:%02d]",
@@ -34,10 +41,29 @@ int GetCurrentLocalTime(char* buffer, int length)
(timeinfo->tm_sec));
}
long GetLogSize(char* filename)
int CreateLog(const char* filename)
{
int exist;
exist = access(filename, 0);
if (exist != 0) {
return creat(filename, DEFAULT_LOG_MODE);
}
return 0;
}
long GetLogSize(const char* filename)
{
int iret;
iret = CreateLog(filename);
if (iret < 0) {
return -1;
}
FILE *fp = NULL;
fp = fopen(filename, "rb");
char path[PATH_MAX + 1] = {0x00};
if (strlen(filename) > PATH_MAX || NULL == realpath(filename, path)) {
return -1;
}
fp = fopen(path, "rb");
long length = 0;
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
@@ -50,8 +76,10 @@ long GetLogSize(char* filename)
return length;
}
void LogLoop(char* filename)
int LogLoop(const char* filename)
{
int iret;
char* loopPath = LOG_PATH_DIR"docker-runtime-log.log.1";
int exist;
exist = access(loopPath, 0);
@@ -59,17 +87,33 @@ void LogLoop(char* filename)
unlink(loopPath);
}
rename(filename, loopPath);
iret = CreateLog(filename);
if (iret < 0) {
return -1;
}
return iret;
}
void WriteLogFile(char* filename, long maxSize, char* buffer, unsigned bufferSize)
void WriteLogFile(char* filename, long maxSize, const char* buffer, unsigned bufferSize)
{
if (filename != NULL && buffer != NULL) {
long length = GetLogSize(filename);
if (length > maxSize) {
LogLoop(filename);
}
char path[PATH_MAX + 1] = {0x00};
FILE *fp;
fp = fopen(filename, "a+");
int iret;
long length = GetLogSize(filename);
if (length < 0) {
return;
}
if (length > maxSize) {
iret = LogLoop(filename);
if (iret < 0) {
return;
}
}
if (strlen(filename) > PATH_MAX || NULL == realpath(filename, path)) {
return;
}
fp = fopen(path, "a+");
if (fp != NULL) {
int ret;
char now[TEMP_BUFFER] = {0};
@@ -86,13 +130,19 @@ void WriteLogFile(char* filename, long maxSize, char* buffer, unsigned bufferSiz
}
}
void Logger(const char *msg, int level)
void Logger(const char *msg, int level, int screen)
{
if (msg == NULL) {
return;
}
if (screen == SCREEN_YES) {
LOG_ERROR(msg);
}
int iret;
char *logPath = LOG_PATH_DIR"docker-runtime-log.log";
if (MakeDirWithParent(LOG_PATH_DIR, DEFAULT_DIR_MODE) < 0) {
return;
}
int destMax = LOG_LENGTH;
if (destMax <= 0) {
return;

View File

@@ -6,6 +6,6 @@
#ifndef _LOGGER_H
#define _LOGGER_H
void Logger(const char *msg, int level);
void Logger(const char *msg, int level, int screen);
#endif

View File

@@ -49,7 +49,7 @@ static bool DevicesCmdArgParser(struct CmdArgs *args, const char *arg)
{
errno_t err = strcpy_s(args->devices, BUF_SIZE, arg);
if (err != EOK) {
Logger("failed to get devices from cmd args.", LEVEL_ERROR);
Logger("failed to get devices from cmd args.", LEVEL_ERROR, SCREEN_YES);
return false;
}
@@ -62,14 +62,14 @@ static bool PidCmdArgParser(struct CmdArgs *args, const char *arg)
args->pid = strtol(optarg, NULL, DECIMAL);
if (errno != 0) {
char* str = FormatLogMessage("failed to convert pid string from cmd args, pid string: %s.", arg);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return false;
}
if (args->pid <= 0) {
char* str = FormatLogMessage("invalid pid %d.", args->pid);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return false;
}
@@ -81,7 +81,7 @@ static bool RootfsCmdArgParser(struct CmdArgs *args, const char *arg)
{
errno_t err = strcpy_s(args->rootfs, BUF_SIZE, arg);
if (err != EOK) {
Logger("failed to get rootfs path from cmd args", LEVEL_ERROR);
Logger("failed to get rootfs path from cmd args", LEVEL_ERROR, SCREEN_YES);
return false;
}
@@ -92,7 +92,7 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
{
errno_t err = strcpy_s(args->options, BUF_SIZE, arg);
if (err != EOK) {
Logger("failed to get options string from cmd args", LEVEL_ERROR);
Logger("failed to get options string from cmd args", LEVEL_ERROR, SCREEN_YES);
return false;
}
@@ -103,7 +103,7 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
{
if (args->files.count == MAX_MOUNT_NR) {
char* str = FormatLogMessage("too many files to mount, max number is %u", MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -112,7 +112,7 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
errno_t err = strcpy_s(dst, PATH_MAX, arg);
if (err != EOK) {
char* str = FormatLogMessage("failed to copy mount file path: %s", arg);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return false;
}
@@ -124,7 +124,7 @@ static bool MountDirCmdArgParser(struct CmdArgs *args, const char *arg)
{
if (args->dirs.count == MAX_MOUNT_NR) {
char* str = FormatLogMessage("too many directories to mount, max number is %u", MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -133,7 +133,7 @@ static bool MountDirCmdArgParser(struct CmdArgs *args, const char *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);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return false;
}
@@ -166,14 +166,14 @@ static int ParseOneCmdArg(struct CmdArgs *args, char indicator, const char *valu
if (i == NUM_OF_CMD_ARGS) {
char* str = FormatLogMessage("unrecognized cmd arg: indicate char: %c, value: %s.", indicator, value);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
bool 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);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -196,7 +196,7 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
while (token != NULL) {
if (idx >= *idListSize) {
char* str = FormatLogMessage("too many devices(%u), support %u devices maximally", idx, *idListSize);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -205,7 +205,7 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
idList[idx] = strtoul((const char *)token, NULL, DECIMAL);
if (errno != 0) {
char* str = FormatLogMessage("failed to convert device id (%s) from cmd args", token);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -225,41 +225,41 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs);
if (err != EOK) {
Logger("failed to copy rootfs path to parsed config.", LEVEL_ERROR);
Logger("failed to copy rootfs path to parsed config.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices);
if (ret < 0) {
Logger("failed to parse device ids from cmdline argument", LEVEL_ERROR);
Logger("failed to parse device ids from cmdline argument", LEVEL_ERROR, SCREEN_YES);
return -1;
}
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
if (ret < 0) {
char* str = FormatLogMessage("failed to get container mnt ns path: pid(%d).", args->pid);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
if (ret < 0) {
Logger("failed to get cgroup path.", LEVEL_ERROR);
Logger("failed to get cgroup path.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
char originNsPath[BUF_SIZE] = {0};
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
if (ret < 0) {
Logger("failed to get self ns path.", LEVEL_ERROR);
Logger("failed to get self ns path.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口非外部输入
if (config->originNsFd < 0) {
char* str = FormatLogMessage("failed to get self ns fd: %s.", originNsPath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -277,43 +277,43 @@ int SetupContainer(struct CmdArgs *args)
InitParsedConfig(&config);
Logger("prepare necessary config", LEVEL_INFO);
Logger("prepare necessary config", LEVEL_INFO, SCREEN_YES);
ret = DoPrepare(args, &config);
if (ret < 0) {
Logger("failed to prepare nesessary config.", LEVEL_ERROR);
Logger("failed to prepare nesessary config.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
// enter container's mount namespace
Logger("enter container's mount namespace", LEVEL_INFO);
Logger("enter container's mount namespace", LEVEL_INFO, SCREEN_YES);
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
if (ret < 0) {
char* str = FormatLogMessage("failed to set to container ns: %s.", config.containerNsPath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
close(config.originNsFd);
return -1;
}
Logger("do mounting", LEVEL_INFO);
Logger("do mounting", LEVEL_INFO, SCREEN_YES);
ret = DoMounting(&config);
if (ret < 0) {
Logger("failed to do mounting.", LEVEL_ERROR);
Logger("failed to do mounting.", LEVEL_ERROR, SCREEN_YES);
close(config.originNsFd);
return -1;
}
Logger("setup up cgroup", LEVEL_INFO);
Logger("setup up cgroup", LEVEL_INFO, SCREEN_YES);
ret = SetupCgroup(&config);
if (ret < 0) {
Logger("failed to set up cgroup.", LEVEL_ERROR);
Logger("failed to set up cgroup.", LEVEL_ERROR, SCREEN_YES);
close(config.originNsFd);
return -1;
}
// back to original namespace
Logger("back to original namespace", LEVEL_INFO);
Logger("back to original namespace", LEVEL_INFO, SCREEN_YES);
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
if (ret < 0) {
Logger("failed to set ns back.", LEVEL_ERROR);
Logger("failed to set ns back.", LEVEL_ERROR, SCREEN_YES);
close(config.originNsFd);
return -1;
}
@@ -329,28 +329,28 @@ int Process(int argc, char **argv)
int optionIndex;
struct CmdArgs args = {0};
Logger("runc start prestart-hook ...", LEVEL_INFO);
Logger("runc start prestart-hook ...", LEVEL_INFO, SCREEN_YES);
while ((c = getopt_long(argc, argv, "d:p:r:o:f:i", g_cmdOpts, &optionIndex)) != -1) {
ret = ParseOneCmdArg(&args, (char)c, optarg);
if (ret < 0) {
Logger("failed to parse cmd args.", LEVEL_ERROR);
Logger("failed to parse cmd args.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
}
Logger("verify parameters valid and parse runtime options", LEVEL_INFO);
Logger("verify parameters valid and parse runtime options", LEVEL_INFO, SCREEN_YES);
if (!IsCmdArgsValid(&args)) {
Logger("information not completed or valid.", LEVEL_ERROR);
Logger("information not completed or valid.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
ParseRuntimeOptions(args.options);
Logger("setup container config ...", LEVEL_INFO);
Logger("setup container config ...", LEVEL_INFO, SCREEN_YES);
ret = SetupContainer(&args);
if (ret < 0) {
Logger("failed to setup container.", LEVEL_ERROR);
Logger("failed to setup container.", LEVEL_ERROR, SCREEN_YES);
return ret;
}
Logger("prestart-hook setup container successful.", LEVEL_INFO);
Logger("prestart-hook setup container successful.", LEVEL_INFO, SCREEN_YES);
return 0;
}

View File

@@ -30,7 +30,7 @@ int EnterNsByFd(int fd, int nsType)
int ret = setns(fd, nsType);
if (ret < 0) {
char* str = FormatLogMessage("failed to set ns: fd(%d).", fd);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -46,7 +46,7 @@ int EnterNsByPath(const char *path, int nsType)
fd = open(path, O_RDONLY); // proc文件接口非外部输入
if (fd < 0) {
char* str = FormatLogMessage("failed to open ns path: %s.", path);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -54,7 +54,7 @@ int EnterNsByPath(const char *path, int nsType)
ret = EnterNsByFd(fd, nsType);
if (ret < 0) {
char* str = FormatLogMessage("failed to set ns: %s.", path);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
close(fd);
return -1;

View File

@@ -25,7 +25,7 @@ int Mount(const char *src, const char *dst)
ret = mount(src, dst, NULL, mountFlags, NULL);
if (ret < 0) {
char* str = FormatLogMessage("failed to mount src:%s.", src);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -33,7 +33,7 @@ int Mount(const char *src, const char *dst)
ret = mount(NULL, dst, NULL, remountFlags, NULL);
if (ret < 0) {
char* str = FormatLogMessage("failed to re-mount. dst:%s.", dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -71,7 +71,7 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) {
char* str = FormatLogMessage("cannot canonicalize device dst: %s.", dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -85,7 +85,7 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
err = strcpy_s(dst, dstBufSize, resolvedDst);
if (err != EOK) {
char* str = FormatLogMessage("failed to copy resolved device mnt path to dst: %s.", resolvedDst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -104,14 +104,16 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
ret = GetDeviceMntSrcDst(rootfs, srcDeviceName, dstDeviceName, &pathInfo);
if (ret < 0) {
str = FormatLogMessage("failed to get mount src and dst path, device name: %s.", srcDeviceName);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
struct stat srcStat;
ret = stat((const char *)src, &srcStat);
if (ret < 0) {
str = FormatLogMessage("failed to stat src: %s.", src);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
errno = 0;
@@ -121,22 +123,23 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
return 0; // 特权容器自动挂载HOST所有设备故此处跳过
} else if (ret == 0) {
str = FormatLogMessage("%s already exists but not a char device as expected.", dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
} else if (ret < 0 && errno != ENOENT) {
str = FormatLogMessage("failed to check dst %s stat", dst);
Logger(str, LEVEL_ERROR);
Logger("failed to check dst stat", LEVEL_ERROR, SCREEN_YES);
return -1;
}
ret = MakeMountPoints(dst, srcStat.st_mode);
if (ret < 0) {
str = FormatLogMessage("failed to create mount dst file: %s.", dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = Mount(src, dst);
if (ret < 0) {
Logger("failed to mount dev.", LEVEL_ERROR);
Logger("failed to mount dev.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
free(str);
@@ -153,14 +156,14 @@ int DoDeviceMounting(const char *rootfs, const char *device_name, const unsigned
int dstRet = sprintf_s(dstDeviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]);
if (srcRet < 0 || dstRet < 0) {
char* str = FormatLogMessage("assemble device name failed, id: %u.", ids[idx]);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
int ret = MountDevice(rootfs, srcDeviceName, dstDeviceName);
if (ret < 0) {
char* str = FormatLogMessage("failed to mount device %s.", srcDeviceName);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -177,7 +180,7 @@ int MountFile(const char *rootfs, const char *filepath)
ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath);
if (ret < 0) {
char* str = FormatLogMessage("failed to assemble file mounting path, file: %s.", filepath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -191,14 +194,14 @@ int MountFile(const char *rootfs, const char *filepath)
ret = MakeMountPoints(dst, srcStat.st_mode);
if (ret < 0) {
char* str = FormatLogMessage("failed to create mount dst file: %s.", dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
ret = Mount(filepath, dst);
if (ret < 0) {
Logger("failed to mount dev.", LEVEL_ERROR);
Logger("failed to mount dev.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -224,7 +227,7 @@ int MountDir(const char *rootfs, const char *src)
ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE);
if (ret < 0) {
char* str = FormatLogMessage("failed to make dir: %s.", dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -232,7 +235,7 @@ int MountDir(const char *rootfs, const char *src)
ret = Mount(src, dst);
if (ret < 0) {
char* str = FormatLogMessage("failed to mount dir: %s to %s.", src, dst);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -246,7 +249,7 @@ int DoCtrlDeviceMounting(const char *rootfs)
int ret = MountDevice(rootfs, DAVINCI_MANAGER, NULL);
if (ret < 0) {
char* str = FormatLogMessage("failed to mount device %s.", DAVINCI_MANAGER);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -254,7 +257,7 @@ int DoCtrlDeviceMounting(const char *rootfs)
ret = MountDevice(rootfs, DEVMM_SVM, NULL);
if (ret < 0) {
char* str = FormatLogMessage("failed to mount device %s.", DEVMM_SVM);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -262,7 +265,7 @@ int DoCtrlDeviceMounting(const char *rootfs)
ret = MountDevice(rootfs, HISI_HDC, NULL);
if (ret < 0) {
char* str = FormatLogMessage("failed to mount device %s.", HISI_HDC);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -278,7 +281,7 @@ int DoDirectoryMounting(const char *rootfs, const struct MountList *list)
ret = MountDir(rootfs, (const char *)&list->list[i][0]);
if (ret < 0) {
char* str = FormatLogMessage("failed to do directory mounting for %s.", (const char *)&list->list[i][0]);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -295,7 +298,7 @@ int DoFileMounting(const char *rootfs, const struct MountList *list)
ret = MountFile(rootfs, (const char *)&list->list[i][0]);
if (ret < 0) {
char* str = FormatLogMessage("failed to do file mounting for %s.", (const char *)&list->list[i][0]);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -311,13 +314,13 @@ int DoMounting(const struct ParsedConfig *config)
(IsVirtual() ? VDEVICE_NAME : DEVICE_NAME),
config->devices, config->devicesNr);
if (ret < 0) {
Logger("failed to mount devices.", LEVEL_ERROR);
Logger("failed to mount devices.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
ret = DoCtrlDeviceMounting(config->rootfs);
if (ret < 0) {
Logger("failed to mount ctrl devices.", LEVEL_ERROR);
Logger("failed to mount ctrl devices.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
@@ -327,13 +330,13 @@ int DoMounting(const struct ParsedConfig *config)
ret = DoFileMounting(config->rootfs, config->files);
if (ret < 0) {
Logger("failed to mount files.", LEVEL_ERROR);
Logger("failed to mount files.", LEVEL_ERROR, SCREEN_YES);
return -1;
}
ret = DoDirectoryMounting(config->rootfs, config->dirs);
if (ret < 0) {
Logger("failed to do mount directories.", LEVEL_ERROR);
Logger("failed to do mount directories.", LEVEL_ERROR, SCREEN_YES);
return -1;
}

View File

@@ -30,9 +30,13 @@ char *FormatLogMessage(char *format, ...)
// 复位va_list, 将格式化字符串写入到buf
va_start(list, format);
char *buf = (char *)malloc(size);
if (buf == NULL) {
return NULL;
}
int ret = vsnprintf_s(buf, size, size, format, list);
va_end(list);
if (ret <= 0) {
free(buf);
return NULL;
}
return buf;
@@ -124,7 +128,7 @@ int MakeMountPoints(const char *path, mode_t mode)
int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE);
if (ret < 0) {
char* str = FormatLogMessage("failed to make parent dir for file: %s", path);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -132,7 +136,7 @@ int MakeMountPoints(const char *path, mode_t mode)
char resolvedPath[PATH_MAX] = {0};
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
char* str = FormatLogMessage("failed to resolve path %s.", path);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}
@@ -140,7 +144,7 @@ int MakeMountPoints(const char *path, mode_t mode)
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
if (fd < 0) {
char* str = FormatLogMessage("cannot create file: %s.", resolvedPath);
Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str);
return -1;
}