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

View File

@@ -15,7 +15,7 @@
#define TEMP_BUFFER 30 #define TEMP_BUFFER 30
#define YEAR_OFFSET 1900 #define YEAR_OFFSET 1900
#define MONTH_OFFSET 1 #define MONTH_OFFSET 1
#define LEVEL_LENGTH 20 #define LOG_LENGTH 1024
int GetCurrentLocalTime(char* buffer, int length) 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) { if (msg == NULL) {
return; return;
} }
int iret; int iret;
char *logPath = LOG_PATH_DIR"docker-runtime-log.log"; char *logPath = LOG_PATH_DIR"docker-runtime-log.log";
int destMax = length + LEVEL_LENGTH; int destMax = LOG_LENGTH;
if (destMax <= 0) { if (destMax <= 0) {
return; return;
} }

View File

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

View File

@@ -29,9 +29,8 @@ int EnterNsByFd(int fd, int nsType)
{ {
int ret = setns(fd, nsType); int ret = setns(fd, nsType);
if (ret < 0) { if (ret < 0) {
int iLength = 0; char* str = FormatLogMessage("failed to set ns: fd(%d).", fd);
char* str = FormatLogMessage("failed to set ns: fd(%d).", &iLength, fd); Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, iLength);
free(str); free(str);
return -1; return -1;
} }
@@ -46,18 +45,16 @@ 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) {
int iLength = 0; char* str = FormatLogMessage("failed to open ns path: %s.", path);
char* str = FormatLogMessage("failed to open ns path: %s.", &iLength, path); Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, iLength);
free(str); free(str);
return -1; return -1;
} }
ret = EnterNsByFd(fd, nsType); ret = EnterNsByFd(fd, nsType);
if (ret < 0) { if (ret < 0) {
int iLength = 0; char* str = FormatLogMessage("failed to set ns: %s.", path);
char* str = FormatLogMessage("failed to set ns: %s.", &iLength, path); Logger(str, LEVEL_ERROR);
Logger(str, LEVEL_ERROR, iLength);
free(str); free(str);
close(fd); close(fd);
return -1; return -1;

View File

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

View File

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

View File

@@ -9,7 +9,7 @@
#include <sys/types.h> #include <sys/types.h>
#include "basic.h" #include "basic.h"
char *FormatLogMessage(char *format, int* iLength, ...); char *FormatLogMessage(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);