From 27561ec1ce44617dd5e1dbcb4fb82d26df79803c Mon Sep 17 00:00:00 2001 From: BianTanggui Date: Mon, 27 Jul 2020 17:44:05 +0800 Subject: [PATCH] Match-id-3b1b67c3bf449ccb084588665428f4aef723d2b2 --- cli/src/basic.c | 10 +++++++++ cli/src/basic.h | 19 ++++++++++------ cli/src/cgrp.c | 33 +++++++++------------------ cli/src/cgrp.h | 4 ++-- cli/src/main.c | 59 +++++++++++++++++++++++++++++++++++++++++++------ cli/src/mount.c | 43 ++++++++++++----------------------- cli/src/mount.h | 2 +- 7 files changed, 102 insertions(+), 68 deletions(-) create mode 100644 cli/src/basic.c diff --git a/cli/src/basic.c b/cli/src/basic.c new file mode 100644 index 0000000..643cc9f --- /dev/null +++ b/cli/src/basic.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: ascend-docker-cli工具公共宏和结构体定义 +*/ +#include "basic.h" + +void InitParsedConfig(struct ParsedConfig *parsedConfig) +{ + parsedConfig->devicesNr = MAX_DEVICE_NR; +} \ No newline at end of file diff --git a/cli/src/basic.h b/cli/src/basic.h index 98743de..8f188dc 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -16,6 +16,7 @@ #define ASCEND_ADDONS_PATH "/usr/local/Ascend/add-ons" #define DEFAULT_DIR_MODE 0755 #define BUF_SIZE 1024 +#define MAX_DEVICE_NR 1024 #define ALLOW_PATH "/devices.allow" #define ROOT_GAP 4 @@ -23,13 +24,6 @@ #define MOUNT_SUBSTR_GAP 2 #define ROOT_SUBSTR_GAP 2 -struct CmdArgs { - char devices[BUF_SIZE]; - char rootfs[BUF_SIZE]; - int pid; - char options[BUF_SIZE]; -}; - struct PathInfo { char* src; size_t srcLen; @@ -37,4 +31,15 @@ struct PathInfo { size_t dstLen; }; +struct ParsedConfig { + char rootfs[BUF_SIZE]; + unsigned int devices[MAX_DEVICE_NR]; + size_t devicesNr; + char containerNsPath[BUF_SIZE]; + char cgroupPath[BUF_SIZE]; + int originNsFd; +}; + +void InitParsedConfig(struct ParsedConfig *parsedConfig); + #endif \ No newline at end of file diff --git a/cli/src/cgrp.c b/cli/src/cgrp.c index b664042..c799bb9 100644 --- a/cli/src/cgrp.c +++ b/cli/src/cgrp.c @@ -199,7 +199,7 @@ int SetupDriverCgroup(FILE *cgroupAllow) return 0; } -int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSize) +int GetCgroupPath(int pid, char *effPath, size_t maxSize) { int ret; char mountPath[BUF_SIZE] = {0x0}; @@ -219,9 +219,9 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz char cgroup[BUF_SIZE] = {0x0}; char cgroupPath[BUF_SIZE] = {0x0}; - ret = snprintf_s(cgroupPath, BUF_SIZE, BUF_SIZE, "/proc/%d/cgroup", args->pid); + ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid); if (ret < 0) { - LogError("error: assemble cgroup path failed: pid(%d)\n", args->pid); + LogError("error: assemble cgroup path failed: pid(%d).", pid); return -1; } @@ -243,26 +243,18 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz return 0; } -int SetupCgroup(struct CmdArgs *args, const char *cgroupPath) +int SetupCgroup(const struct ParsedConfig *config) { int ret; char deviceName[BUF_SIZE] = {0}; char resolvedCgroupPath[PATH_MAX] = {0}; FILE *cgroupAllow = NULL; - if (realpath(cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) { - LogError("error: cannot canonicalize cgroup path: %s\n", cgroupPath); + if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) { + LogError("error: cannot canonicalize cgroup path: %s.", config->cgroupPath); return -1; } - static const char *sep = ","; - char list[BUF_SIZE] = {0}; - errno_t err = strncpy_s(list, BUF_SIZE, args->devices, strlen(args->devices)); - if (err != EOK) { - return -1; - } - char *token = NULL; - cgroupAllow = fopen((const char *)resolvedCgroupPath, "a"); if (cgroupAllow == NULL) { LogError("error: failed to open cgroup file: %s.", resolvedCgroupPath); @@ -275,24 +267,21 @@ int SetupCgroup(struct CmdArgs *args, const char *cgroupPath) LogError("error: failed to setup driver cgroup."); return -1; } - - token = strtok(list, sep); - while (token != NULL) { - ret = snprintf_s(deviceName, BUF_SIZE, BUF_SIZE, "%s%s", DEVICE_NAME, token); + + for (size_t idx = 0; idx < config->devicesNr; idx++) { + ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, config->devices[idx]); if (ret < 0) { fclose(cgroupAllow); - LogError("error: failed to assemble device path for no.%s\n", token); + LogError("error: failed to assemble device path for no.%u.", config->devices[idx]); return -1; } ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName); if (ret < 0) { fclose(cgroupAllow); - LogError("error: failed to setup cgroup %s\n", token); + LogError("error: failed to setup cgroup for %s.", deviceName); return -1; } - - token = strtok(NULL, sep); } fclose(cgroupAllow); diff --git a/cli/src/cgrp.h b/cli/src/cgrp.h index f4fa949..e66dd1e 100644 --- a/cli/src/cgrp.h +++ b/cli/src/cgrp.h @@ -7,7 +7,7 @@ #include "basic.h" -int GetCgroupPath(const struct CmdArgs *args, char *effPath, size_t maxSize); -int SetupCgroup(struct CmdArgs *args, const char *cgroupPath); +int GetCgroupPath(int pid, char *effPath, size_t maxSize); +int SetupCgroup(const struct ParsedConfig *config); #endif \ No newline at end of file diff --git a/cli/src/main.c b/cli/src/main.c index 8556e6e..6cdf36b 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -23,10 +23,11 @@ #define DECIMAL 10 -struct ParsedConfig { - char containerNsPath[BUF_SIZE]; - char cgroupPath[BUF_SIZE]; - int originNsFd; +struct CmdArgs { + char devices[BUF_SIZE]; + char rootfs[BUF_SIZE]; + int pid; + char options[BUF_SIZE]; }; static struct option g_cmdOpts[] = { @@ -129,9 +130,51 @@ static inline bool IsCmdArgsValid(struct CmdArgs *args) return (strlen(args->devices) > 0) && (strlen(args->rootfs) > 0) && (args->pid > 0); } +static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *devices) +{ + static const char *sep = ","; + char *token = NULL; + char *context = NULL; + size_t idx = 0; + + token = strtok_s(devices, sep, &context); + while (token != NULL) { + if (idx >= *idListSize) { + LogError("error: too many devices(%u), support %u devices maximally", idx, *idListSize); + return -1; + } + + errno = 0; + idList[idx] = strtoul((const char *)token, NULL, DECIMAL); + if (errno != 0) { + LogError("error: failed to convert device id (%s) from cmd args, caused by: %s.", token, strerror(errno)); + return -1; + } + + idx++; + token = strtok_s(NULL, sep, &context); + } + + *idListSize = idx; + return 0; +} + int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config) { int ret; + errno_t err; + + err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs); + if (err != EOK) { + LogError("error: failed to copy rootfs path to parsed config."); + return -1; + } + + ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices); + if (ret < 0) { + LogError("error: failed to parse device ids from cmdline argument"); + return -1; + } ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE); if (ret < 0) { @@ -139,7 +182,7 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config) return -1; } - ret = GetCgroupPath(args, config->cgroupPath, BUF_SIZE); + ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE); if (ret < 0) { LogError("error: failed to get cgroup path."); return -1; @@ -172,6 +215,8 @@ int SetupContainer(struct CmdArgs *args) int ret; struct ParsedConfig config; + InitParsedConfig(&config); + ret = DoPrepare(args, &config); if (ret < 0) { LogError("error: failed to prepare nesessary config."); @@ -186,14 +231,14 @@ int SetupContainer(struct CmdArgs *args) return -1; } - ret = DoMounting(args); + ret = DoMounting(&config); if (ret < 0) { LogError("error: failed to do mounting."); close(config.originNsFd); return -1; } - ret = SetupCgroup(args, (const char *)config.cgroupPath); + ret = SetupCgroup(&config); if (ret < 0) { LogError("error: failed to set up cgroup."); close(config.originNsFd); diff --git a/cli/src/mount.c b/cli/src/mount.c index 9d23190..8ea900a 100644 --- a/cli/src/mount.c +++ b/cli/src/mount.c @@ -112,34 +112,22 @@ int MountDevice(const char *rootfs, const char *deviceName) return 0; } -int DoDeviceMounting(const char *rootfs, const char *devicesList) +int DoDeviceMounting(const char *rootfs, const unsigned int ids[], size_t idsNr) { - static const char *sep = ","; - char list[BUF_SIZE] = {0}; char deviceName[BUF_SIZE] = {0}; - errno_t err = strncpy_s(list, BUF_SIZE, devicesList, strlen(devicesList)); - if (err != EOK) { - return -1; - } - char *token = NULL; - char *context = NULL; - - token = strtok_s(list, sep, &context); - while (token != NULL) { - int ret = snprintf_s(deviceName, BUF_SIZE, BUF_SIZE, "%s%s", DEVICE_NAME, token); + for (size_t idx = 0; idx < idsNr; idx++) { + int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]); if (ret < 0) { - LogError("error: assemble device name failed, id: %s\n", token); + LogError("error: assemble device name failed, id: %u.", ids[idx]); return -1; } ret = MountDevice(rootfs, deviceName); if (ret < 0) { - LogError("error: failed to mount device no. %s\n", token); + LogError("error: failed to mount device %s.", deviceName); return -1; } - - token = strtok_s(NULL, sep, &context); } return 0; @@ -233,31 +221,28 @@ int DoDirectoryMounting(const char *rootfs) return 0; } -int DoMounting(const struct CmdArgs *args) +int DoMounting(const struct ParsedConfig *config) { int ret; - ret = DoDeviceMounting(args->rootfs, args->devices); + ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr); if (ret < 0) { LogError("error: failed to do mounts."); return -1; } - ret = DoCtrlDeviceMounting(args->rootfs); + ret = DoCtrlDeviceMounting(config->rootfs); if (ret < 0) { LogError("error: failed to do mount files."); return -1; } - if (IsOptionNoDrvSet()) { - // 不挂载DRIVER - return 0; - } - - ret = DoDirectoryMounting(args->rootfs); - if (ret < 0) { - LogError("error: failed to do mount directory\n"); - return -1; + if (!IsOptionNoDrvSet()) { + ret = DoDirectoryMounting(config->rootfs); + if (ret < 0) { + LogError("error: failed to do mount directory."); + return -1; + } } return 0; diff --git a/cli/src/mount.h b/cli/src/mount.h index ddefced..f1bb6ff 100644 --- a/cli/src/mount.h +++ b/cli/src/mount.h @@ -7,6 +7,6 @@ #include "basic.h" -int DoMounting(const struct CmdArgs *args); +int DoMounting(const struct ParsedConfig *config); #endif \ No newline at end of file