Match-id-3fc3884a61d46bf22bd7083038de1f333f68d7d8

This commit is contained in:
BianTanggui
2021-07-08 11:33:14 +08:00
parent eec42515d8
commit 1ff5a5f6f4
8 changed files with 143 additions and 219 deletions

View File

@@ -73,17 +73,15 @@ int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const
char resolvedPath[PATH_MAX] = {0x0};
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
int iLength = 0;
char* str = FormatLogMessage("cannot canonicalize path %s.", &iLength, filepath);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("cannot canonicalize path %s.", filepath);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
fp = fopen(resolvedPath, "r");
if (fp == NULL) {
char msg[] = "cannot open file.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("cannot open file.", LEVEL_ERROR);
return -1;
}
@@ -161,18 +159,16 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to assemble dev path for %s.", &iLength, devName);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to assemble dev path for %s.", devName);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = stat((const char *)devPath, &devStat);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to get stat of %s.", &iLength, devPath);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to get stat of %s.", devPath);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -180,8 +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) {
char msg[] = "write devices failed.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("write devices failed.", LEVEL_ERROR);
return -1;
}
@@ -194,27 +189,24 @@ int SetupDriverCgroup(FILE *cgroupAllow)
ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to setup cgroup for %s.", &iLength, DAVINCI_MANAGER);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to setup cgroup for %s.", DAVINCI_MANAGER);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to setup cgroup for %s.", &iLength, DEVMM_SVM);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to setup cgroup for %s.", DEVMM_SVM);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to setup cgroup for %s.", &iLength, HISI_HDC);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to setup cgroup for %s.", HISI_HDC);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -230,17 +222,15 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("assemble mount info path failed: ppid(%d).", &iLength, getppid());
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("assemble mount info path failed: ppid(%d).", getppid());
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath);
if (ret < 0) {
char msg[] = "cat file content failed.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("cat file content failed.", LEVEL_ERROR);
return -1;
}
@@ -248,17 +238,15 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
char cgroupPath[BUF_SIZE] = {0x0};
ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("assemble cgroup path failed: pid(%d).", &iLength, pid);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("assemble cgroup path failed: pid(%d).", pid);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
if (ret < 0) {
char msg[] = "cat file content failed.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("cat file content failed.", LEVEL_ERROR);
return -1;
}
@@ -267,8 +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) {
char msg[] = "assemble cgroup device path failed.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("assemble cgroup device path failed.", LEVEL_ERROR);
return -1;
}
@@ -278,30 +265,28 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
int SetupCgroup(const struct ParsedConfig *config)
{
int ret;
int iLength = 0;
char *str = NULL;
char deviceName[BUF_SIZE] = {0};
char resolvedCgroupPath[PATH_MAX] = {0};
FILE *cgroupAllow = NULL;
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
str = FormatLogMessage("cannot canonicalize cgroup path: %s.", &iLength, config->cgroupPath);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("cannot canonicalize cgroup path: %s.", config->cgroupPath);
Logger(str, LEVEL_ERROR);
return -1;
}
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
if (cgroupAllow == NULL) {
str = FormatLogMessage("failed to open cgroup file: %s.", &iLength, resolvedCgroupPath);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to open cgroup file: %s.", resolvedCgroupPath);
Logger(str, LEVEL_ERROR);
return -1;
}
ret = SetupDriverCgroup(cgroupAllow);
if (ret < 0) {
fclose(cgroupAllow);
char msg[] = "failed to setup driver cgroup.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to setup driver cgroup.", LEVEL_ERROR);
return -1;
}
@@ -311,15 +296,15 @@ int SetupCgroup(const struct ParsedConfig *config)
config->devices[idx]);
if (ret < 0) {
fclose(cgroupAllow);
str = FormatLogMessage("failed to assemble device path for no.%u.", &iLength, config->devices[idx]);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to assemble device path for no.%u.", config->devices[idx]);
Logger(str, LEVEL_ERROR);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName);
if (ret < 0) {
fclose(cgroupAllow);
str = FormatLogMessage("failed to setup cgroup for %s.", &iLength, deviceName);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to setup cgroup for %s.", deviceName);
Logger(str, LEVEL_ERROR);
return -1;
}
}

View File

@@ -15,7 +15,7 @@
#define TEMP_BUFFER 30
#define YEAR_OFFSET 1900
#define MONTH_OFFSET 1
#define LEVEL_LENGTH 20
#define LOG_LENGTH 1024
int GetCurrentLocalTime(char* buffer, int length)
{
@@ -86,14 +86,14 @@ void WriteLogFile(char* filename, long maxSize, char* buffer, unsigned bufferSiz
}
}
void Logger(const char *msg, int level, int length)
void Logger(const char *msg, int level)
{
if (msg == NULL) {
return;
}
int iret;
char *logPath = LOG_PATH_DIR"docker-runtime-log.log";
int destMax = length + LEVEL_LENGTH;
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, int length);
void Logger(const char *msg, int level);
#endif

View File

@@ -49,8 +49,7 @@ static bool DevicesCmdArgParser(struct CmdArgs *args, const char *arg)
{
errno_t err = strcpy_s(args->devices, BUF_SIZE, arg);
if (err != EOK) {
char msg[] = "failed to get devices from cmd args.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to get devices from cmd args.", LEVEL_ERROR);
return false;
}
@@ -62,17 +61,15 @@ static bool PidCmdArgParser(struct CmdArgs *args, const char *arg)
errno = 0;
args->pid = strtol(optarg, NULL, DECIMAL);
if (errno != 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to convert pid string from cmd args, pid string: %s.", &iLength, arg);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to convert pid string from cmd args, pid string: %s.", arg);
Logger(str, LEVEL_ERROR);
free(str);
return false;
}
if (args->pid <= 0) {
int iLength = 0;
char* str = FormatLogMessage("invalid pid %d.", &iLength, args->pid);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("invalid pid %d.", args->pid);
Logger(str, LEVEL_ERROR);
free(str);
return false;
}
@@ -84,8 +81,7 @@ static bool RootfsCmdArgParser(struct CmdArgs *args, const char *arg)
{
errno_t err = strcpy_s(args->rootfs, BUF_SIZE, arg);
if (err != EOK) {
char msg[] = "failed to get rootfs path from cmd args";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to get rootfs path from cmd args", LEVEL_ERROR);
return false;
}
@@ -96,8 +92,7 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
{
errno_t err = strcpy_s(args->options, BUF_SIZE, arg);
if (err != EOK) {
char msg[] = "failed to get options string from cmd args";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to get options string from cmd args", LEVEL_ERROR);
return false;
}
@@ -107,9 +102,8 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
{
if (args->files.count == MAX_MOUNT_NR) {
int iLength = 0;
char* str = FormatLogMessage("too many files to mount, max number is %u", &iLength, MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("too many files to mount, max number is %u", MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -117,9 +111,8 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
char *dst = &args->files.list[args->files.count++][0];
errno_t err = strcpy_s(dst, PATH_MAX, arg);
if (err != EOK) {
int iLength = 0;
char* str = FormatLogMessage("failed to copy mount file path: %s", &iLength, arg);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to copy mount file path: %s", arg);
Logger(str, LEVEL_ERROR);
free(str);
return false;
}
@@ -130,9 +123,8 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
static bool MountDirCmdArgParser(struct CmdArgs *args, const char *arg)
{
if (args->dirs.count == MAX_MOUNT_NR) {
int iLength = 0;
char* str = FormatLogMessage("too many directories to mount, max number is %u", &iLength, MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("too many directories to mount, max number is %u", MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -140,9 +132,8 @@ 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);
if (err != EOK) {
int iLength = 0;
char* str = FormatLogMessage("error: failed to copy mount directory path: %s", &iLength, arg);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("error: failed to copy mount directory path: %s", arg);
Logger(str, LEVEL_ERROR);
free(str);
return false;
}
@@ -174,18 +165,15 @@ static int ParseOneCmdArg(struct CmdArgs *args, char indicator, const char *valu
}
if (i == NUM_OF_CMD_ARGS) {
int iLength = 0;
char* str = FormatLogMessage("unrecognized cmd arg: indicate char: %c, value: %s.", &iLength, indicator, value);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("unrecognized cmd arg: indicate char: %c, value: %s.", indicator, value);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
bool isOK = g_cmdArgParsers[i].parser(args, value);
if (!isOK) {
int iLength = 0;
char* str = FormatLogMessage("failed while parsing cmd arg, indicate char: %c, value: %s.",
&iLength, indicator, value);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed while parsing cmd arg, indicate char: %c, value: %s.", indicator, value);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -207,10 +195,8 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
token = strtok_s(devices, sep, &context);
while (token != NULL) {
if (idx >= *idListSize) {
int iLength = 0;
char* str = FormatLogMessage("too many devices(%u), support %u devices maximally",
&iLength, idx, *idListSize);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("too many devices(%u), support %u devices maximally", idx, *idListSize);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -218,9 +204,8 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
errno = 0;
idList[idx] = strtoul((const char *)token, NULL, DECIMAL);
if (errno != 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to convert device id (%s) from cmd args", &iLength, token);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to convert device id (%s) from cmd args", token);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -240,47 +225,41 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs);
if (err != EOK) {
char msg[] = "failed to copy rootfs path to parsed config.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to copy rootfs path to parsed config.", LEVEL_ERROR);
return -1;
}
ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices);
if (ret < 0) {
char msg[] = "failed to parse device ids from cmdline argument";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to parse device ids from cmdline argument", LEVEL_ERROR);
return -1;
}
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to get container mnt ns path: pid(%d).", &iLength, args->pid);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to get container mnt ns path: pid(%d).", args->pid);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
if (ret < 0) {
char msg[] = "failed to get cgroup path.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to get cgroup path.", LEVEL_ERROR);
return -1;
}
char originNsPath[BUF_SIZE] = {0};
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
if (ret < 0) {
char msg[] = "failed to get self ns path.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to get self ns path.", LEVEL_ERROR);
return -1;
}
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口非外部输入
if (config->originNsFd < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to get self ns fd: %s.", &iLength, originNsPath);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to get self ns fd: %s.", originNsPath);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -298,45 +277,43 @@ int SetupContainer(struct CmdArgs *args)
InitParsedConfig(&config);
Logger("prepare necessary config", LEVEL_INFO);
ret = DoPrepare(args, &config);
if (ret < 0) {
char msg[] = "failed to prepare nesessary config.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to prepare nesessary config.", LEVEL_ERROR);
return -1;
}
// enter container's mount namespace
Logger("enter container's mount namespace", LEVEL_INFO);
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to set to container ns: %s.", &iLength, config.containerNsPath);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to set to container ns: %s.", config.containerNsPath);
Logger(str, LEVEL_ERROR);
free(str);
close(config.originNsFd);
return -1;
}
Logger("do mounting", LEVEL_INFO);
ret = DoMounting(&config);
if (ret < 0) {
char msg[] = "failed to do mounting.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to do mounting.", LEVEL_ERROR);
close(config.originNsFd);
return -1;
}
Logger("setup up cgroup", LEVEL_INFO);
ret = SetupCgroup(&config);
if (ret < 0) {
char msg[] = "failed to set up cgroup.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to set up cgroup.", LEVEL_ERROR);
close(config.originNsFd);
return -1;
}
// back to original namespace
Logger("back to original namespace", LEVEL_INFO);
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
if (ret < 0) {
char msg[] = "failed to set ns back.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to set ns back.", LEVEL_ERROR);
close(config.originNsFd);
return -1;
}
@@ -352,35 +329,28 @@ int Process(int argc, char **argv)
int optionIndex;
struct CmdArgs args = {0};
char startMsg[] = "runc start prestart-hook ...";
Logger(startMsg, LEVEL_INFO, sizeof(startMsg) / sizeof(char));
Logger("runc start prestart-hook ...", LEVEL_INFO);
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) {
char msg[] = "failed to parse cmd args.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to parse cmd args.", LEVEL_ERROR);
return -1;
}
}
char parametersMsg[] = "verify parameters valid and parse runtime options";
Logger(parametersMsg, LEVEL_INFO, sizeof(parametersMsg) / sizeof(char));
Logger("verify parameters valid and parse runtime options", LEVEL_INFO);
if (!IsCmdArgsValid(&args)) {
char msg[] = "information not completed or valid.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("information not completed or valid.", LEVEL_ERROR);
return -1;
}
ParseRuntimeOptions(args.options);
char containerMsg[] = "setup container config";
Logger(containerMsg, LEVEL_INFO, sizeof(containerMsg) / sizeof(char));
Logger("setup container config ...", LEVEL_INFO);
ret = SetupContainer(&args);
if (ret < 0) {
char msg[] = "failed to setup container.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to setup container.", LEVEL_ERROR);
return ret;
}
char successMsg[] = "prestart-hook setup container successful.";
Logger(successMsg, LEVEL_INFO, sizeof(successMsg) / sizeof(char));
Logger("prestart-hook setup container successful.", LEVEL_INFO);
return 0;
}

View File

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

View File

@@ -24,15 +24,13 @@ int Mount(const char *src, const char *dst)
ret = mount(src, dst, NULL, mountFlags, NULL);
if (ret < 0) {
char msg[] = "failed to mount.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to mount.", LEVEL_ERROR);
return -1;
}
ret = mount(NULL, dst, NULL, remountFlags, NULL);
if (ret < 0) {
char msg[] = "failed to re-mount.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to re-mount.", LEVEL_ERROR);
return -1;
}
@@ -68,9 +66,8 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
}
if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) {
int iLength = 0;
char* str = FormatLogMessage("cannot canonicalize device dst: %s.", &iLength, dst);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("cannot canonicalize device dst: %s.", dst);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -83,9 +80,8 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
} else {
err = strcpy_s(dst, dstBufSize, resolvedDst);
if (err != EOK) {
int iLength = 0;
char* str = FormatLogMessage("failed to copy resolved device mnt path to dst: %s.", &iLength, resolvedDst);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to copy resolved device mnt path to dst: %s.", resolvedDst);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -97,22 +93,21 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDeviceName)
{
int ret;
int iLength = 0;
char *str = NULL;
char src[BUF_SIZE] = {0};
char dst[BUF_SIZE] = {0};
struct PathInfo pathInfo = {src, BUF_SIZE, dst, BUF_SIZE};
ret = GetDeviceMntSrcDst(rootfs, srcDeviceName, dstDeviceName, &pathInfo);
if (ret < 0) {
str = FormatLogMessage("failed to get mount src and dst path, device name: %s.", &iLength, srcDeviceName);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to get mount src and dst path, device name: %s.", srcDeviceName);
Logger(str, LEVEL_ERROR);
return -1;
}
struct stat srcStat;
ret = stat((const char *)src, &srcStat);
if (ret < 0) {
str = FormatLogMessage("failed to stat src: %s.", &iLength, src);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to stat src: %s.", src);
Logger(str, LEVEL_ERROR);
return -1;
}
errno = 0;
@@ -121,24 +116,23 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
if (ret == 0 && S_ISCHR(dstStat.st_mode)) {
return 0; // 特权容器自动挂载HOST所有设备故此处跳过
} else if (ret == 0) {
str = FormatLogMessage("%s already exists but not a char device as expected.", &iLength, dst);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("%s already exists but not a char device as expected.", dst);
Logger(str, LEVEL_ERROR);
return -1;
} else if (ret < 0 && errno != ENOENT) {
str = FormatLogMessage("failed to check dst %s stat", &iLength, dst);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to check dst %s stat", dst);
Logger(str, LEVEL_ERROR);
return -1;
}
ret = MakeMountPoints(dst, srcStat.st_mode);
if (ret < 0) {
str = FormatLogMessage("failed to create mount dst file: %s.", &iLength, dst);
Logger(str, LEVEL_ERROR, iLength);
str = FormatLogMessage("failed to create mount dst file: %s.", dst);
Logger(str, LEVEL_ERROR);
return -1;
}
ret = Mount(src, dst);
if (ret < 0) {
char msg[] = "failed to mount dev.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to mount dev.", LEVEL_ERROR);
return -1;
}
free(str);
@@ -154,17 +148,15 @@ 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 dstRet = sprintf_s(dstDeviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]);
if (srcRet < 0 || dstRet < 0) {
int iLength = 0;
char* str = FormatLogMessage("assemble device name failed, id: %u.", &iLength, ids[idx]);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("assemble device name failed, id: %u.", ids[idx]);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
int ret = MountDevice(rootfs, srcDeviceName, dstDeviceName);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to mount device %s.", &iLength, srcDeviceName);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to mount device %s.", srcDeviceName);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -180,9 +172,8 @@ int MountFile(const char *rootfs, const char *filepath)
ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to assemble file mounting path, file: %s.", &iLength, filepath);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to assemble file mounting path, file: %s.", filepath);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -195,17 +186,15 @@ int MountFile(const char *rootfs, const char *filepath)
ret = MakeMountPoints(dst, srcStat.st_mode);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to create mount dst file: %s.", &iLength, dst);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to create mount dst file: %s.", dst);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = Mount(filepath, dst);
if (ret < 0) {
char msg[] = "failed to mount dev.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to mount dev.", LEVEL_ERROR);
return -1;
}
@@ -230,18 +219,16 @@ int MountDir(const char *rootfs, const char *src)
ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to make dir: %s.", &iLength, dst);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to make dir: %s.", dst);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = Mount(src, dst);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to mount dir: %s to %s.", &iLength, src, dst);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to mount dir: %s to %s.", src, dst);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -254,27 +241,24 @@ int DoCtrlDeviceMounting(const char *rootfs)
/* device */
int ret = MountDevice(rootfs, DAVINCI_MANAGER, NULL);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to mount device %s.", &iLength, DAVINCI_MANAGER);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to mount device %s.", DAVINCI_MANAGER);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = MountDevice(rootfs, DEVMM_SVM, NULL);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to mount device %s.", &iLength, DEVMM_SVM);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to mount device %s.", DEVMM_SVM);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
ret = MountDevice(rootfs, HISI_HDC, NULL);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to mount device %s.", &iLength, HISI_HDC);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to mount device %s.", HISI_HDC);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -289,10 +273,8 @@ int DoDirectoryMounting(const char *rootfs, const struct MountList *list)
for (unsigned int i = 0; i < list->count; i++) {
ret = MountDir(rootfs, (const char *)&list->list[i][0]);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to do directory mounting for %s.", &iLength,
(const char *)&list->list[i][0]);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to do directory mounting for %s.", (const char *)&list->list[i][0]);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -308,10 +290,8 @@ int DoFileMounting(const char *rootfs, const struct MountList *list)
for (unsigned int i = 0; i < list->count; i++) {
ret = MountFile(rootfs, (const char *)&list->list[i][0]);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to do file mounting for %s.", &iLength,
(const char *)&list->list[i][0]);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to do file mounting for %s.", (const char *)&list->list[i][0]);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
@@ -327,15 +307,13 @@ int DoMounting(const struct ParsedConfig *config)
(IsVirtual() ? VDEVICE_NAME : DEVICE_NAME),
config->devices, config->devicesNr);
if (ret < 0) {
char msg[] = "failed to mount devices.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to mount devices.", LEVEL_ERROR);
return -1;
}
ret = DoCtrlDeviceMounting(config->rootfs);
if (ret < 0) {
char msg[] = "failed to mount ctrl devices.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to mount ctrl devices.", LEVEL_ERROR);
return -1;
}
@@ -345,15 +323,13 @@ int DoMounting(const struct ParsedConfig *config)
ret = DoFileMounting(config->rootfs, config->files);
if (ret < 0) {
char msg[] = "failed to mount files.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to mount files.", LEVEL_ERROR);
return -1;
}
ret = DoDirectoryMounting(config->rootfs, config->dirs);
if (ret < 0) {
char msg[] = "failed to do mount directories.";
Logger(msg, LEVEL_ERROR, sizeof(msg) / sizeof(char));
Logger("failed to do mount directories.", LEVEL_ERROR);
return -1;
}

View File

@@ -15,11 +15,11 @@
#include "securec.h"
#include "logger.h"
char *FormatLogMessage(char *format, int* iLength, ...)
char *FormatLogMessage(char *format, ...)
{
va_list list;
// 获取格式化后字符串的长度
va_start(list, iLength);
va_start(list, format);
char buff[1024] = {0};
int size = vsnprintf_s(buff, sizeof(buff), sizeof(buff), format, list);
va_end(list);
@@ -28,9 +28,8 @@ char *FormatLogMessage(char *format, int* iLength, ...)
}
size++;
// 复位va_list, 将格式化字符串写入到buf
va_start(list, iLength);
va_start(list, format);
char *buf = (char *)malloc(size);
*iLength = size;
int ret = vsnprintf_s(buf, size, size, format, list);
va_end(list);
if (ret <= 0) {
@@ -124,27 +123,24 @@ int MakeMountPoints(const char *path, mode_t mode)
int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE);
if (ret < 0) {
int iLength = 0;
char* str = FormatLogMessage("failed to make parent dir for file: %s", &iLength, path);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to make parent dir for file: %s", path);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
char resolvedPath[PATH_MAX] = {0};
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
int iLength = 0;
char* str = FormatLogMessage("failed to resolve path %s.", &iLength, path);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("failed to resolve path %s.", path);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
if (fd < 0) {
int iLength = 0;
char* str = FormatLogMessage("cannot create file: %s.", &iLength, resolvedPath);
Logger(str, LEVEL_ERROR, iLength);
char* str = FormatLogMessage("cannot create file: %s.", resolvedPath);
Logger(str, LEVEL_ERROR);
free(str);
return -1;
}

View File

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