Match-id-ff14c3cc373447ddd47e99b03a251a5ac693c6e3

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

View File

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