From 62c2d9a004ac6368409383e6d209482dd67c18ff Mon Sep 17 00:00:00 2001 From: BianTanggui Date: Mon, 26 Oct 2020 11:04:50 +0800 Subject: [PATCH 1/2] Match-id-bb59ee5d1a8d5afaa75ac2bb07db32c8a3a07b1d --- cli/src/basic.h | 30 +++++++++++++------ cli/src/cgrp.c | 1 - cli/src/logging.c | 74 ----------------------------------------------- cli/src/logging.h | 33 --------------------- cli/src/main.c | 12 +------- cli/src/mount.c | 3 -- cli/src/ns.c | 2 +- cli/src/options.c | 8 ----- cli/src/options.h | 1 - cli/src/utils.c | 1 - hook/main.go | 1 - 11 files changed, 23 insertions(+), 143 deletions(-) delete mode 100644 cli/src/logging.c delete mode 100644 cli/src/logging.h diff --git a/cli/src/basic.h b/cli/src/basic.h index 3a628ac..c15a3ff 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -9,15 +9,27 @@ #include #include -#define DEVICE_NAME "davinci" -#define DAVINCI_MANAGER "davinci_manager" -#define DEVMM_SVM "devmm_svm" -#define HISI_HDC "hisi_hdc" -#define DEFAULT_DIR_MODE 0755 -#define BUF_SIZE 1024 -#define MAX_DEVICE_NR 1024 -#define DEFAULT_LOG_FILE "/var/log/ascend-docker-runtime.log" -#define MAX_MOUNT_NR 512 +#define DEVICE_NAME "davinci" +#define DAVINCI_MANAGER "davinci_manager" +#define DEVMM_SVM "devmm_svm" +#define HISI_HDC "hisi_hdc" +#define DEFAULT_DIR_MODE 0755 +#define BUF_SIZE 1024 +#define MAX_DEVICE_NR 1024 +#define MAX_MOUNT_NR 512 + +#define LOG(level, fmt, ...) \ + do { \ + char _content[BUF_SIZE] = {0}; \ + int _ret = sprintf_s(_content, BUF_SIZE, fmt, ##__VA_ARGS__); \ + if (_ret < 0) { \ + fprintf(stderr, "cannot assemble log content"); \ + } else { \ + fprintf(stderr, "%s", (const char *)_content); \ + } \ + } while (0) + +#define LOG_ERROR(fmt, ...) LOG('E', fmt, ##__VA_ARGS__) #define ALLOW_PATH "/devices.allow" #define ROOT_GAP 4 diff --git a/cli/src/cgrp.c b/cli/src/cgrp.c index f647e09..dc60394 100644 --- a/cli/src/cgrp.c +++ b/cli/src/cgrp.c @@ -14,7 +14,6 @@ #include #include "securec.h" #include "utils.h" -#include "logging.h" bool TakeNthWord(char **pLine, unsigned int n, char **word) { diff --git a/cli/src/logging.c b/cli/src/logging.c deleted file mode 100644 index 8d2b779..0000000 --- a/cli/src/logging.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. - * Description: ascend-docker-cli工具日志模块 -*/ -#include "logging.h" - -#include -#include -#include -#include -#include -#include -#include "securec.h" -#include "basic.h" -#include "options.h" - -static int g_pid = -1; -static FILE *g_logFile = NULL; - -void SetPidForLog(int pid) -{ - g_pid = pid; -} - -int OpenLog(const char *logFile) -{ - char realPath[PATH_MAX] = {0}; - - if (!IsOptionVerboseSet()) { // 日志开关 - return 0; - } - - if (realpath(logFile, realPath) == NULL && errno != ENOENT) { - LOG_ERROR("error: cannot canonicalize log file path %s.", logFile); - return -1; - } - - g_logFile = fopen((const char *)realPath, "ae"); - if (g_logFile == NULL) { - LOG_ERROR("error: failed to open log file %s.", realPath); - return -1; - } - - return 0; -} - -void CloseLog() -{ - if (IsOptionVerboseSet() && g_logFile != NULL) { - (void)fclose(g_logFile); - g_logFile = NULL; - } -} - -void WriteLog(char level, const char *content) -{ - struct timeval tv = {0}; - struct tm *tm = NULL; - char buf[BUF_SIZE] = {0}; - - if (g_logFile == NULL) { - return; - } - - if (gettimeofday(&tv, NULL) < 0 || - (tm = gmtime(&tv.tv_sec)) == NULL || - strftime(buf, sizeof(buf), "%m%d %T", tm) == 0) { - strcpy_s(buf, sizeof(buf), "0000 00:00:00"); - } - - fprintf(g_logFile, "[%c %s.%06ld %d] ", level, buf, tv.tv_usec, g_pid); - fprintf(g_logFile, "%s", content); - fputc('\n', g_logFile); -} diff --git a/cli/src/logging.h b/cli/src/logging.h deleted file mode 100644 index 1361e2f..0000000 --- a/cli/src/logging.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. - * Description: ascend-docker-cli工具日志模块头文件 -*/ -#ifndef _LOGGING_H -#define _LOGGING_H - -#include -#include "securec.h" -#include "basic.h" - -void SetPidForLog(int pid); -int OpenLog(const char *logFile); -void CloseLog(); -void WriteLog(char level, const char *content); - -#define LOG(level, fmt, ...) \ - do { \ - char _content[BUF_SIZE] = {0}; \ - int _ret = sprintf_s(_content, BUF_SIZE, fmt, ##__VA_ARGS__); \ - if (_ret < 0) { \ - fprintf(stderr, "cannot assemble log content"); \ - } else { \ - WriteLog(level, (const char *)_content); \ - fprintf(stderr, "%s", (const char *)_content); \ - } \ - } while (0) - -#define LOG_ERROR(fmt, ...) LOG('E', fmt, ##__VA_ARGS__) - -#define LOG_WARNING(fmt, ...) LOG('W', fmt, ##__VA_ARGS__) - -#endif diff --git a/cli/src/main.c b/cli/src/main.c index 88898bf..a793184 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -18,7 +18,6 @@ #include "ns.h" #include "mount.h" #include "cgrp.h" -#include "logging.h" #include "options.h" #define DECIMAL 10 @@ -315,22 +314,13 @@ int Process(int argc, char **argv) } ParseRuntimeOptions(args.options); - SetPidForLog(args.pid); - - ret = OpenLog(DEFAULT_LOG_FILE); - if (ret < 0) { - LOG_ERROR("error: failed to open log file %s.", DEFAULT_LOG_FILE); - return -1; - } ret = SetupContainer(&args); if (ret < 0) { - CloseLog(); + LOG_ERROR("error: failed to setup container."); return ret; } - CloseLog(); - return 0; } diff --git a/cli/src/mount.c b/cli/src/mount.c index c3e8f79..b527aef 100644 --- a/cli/src/mount.c +++ b/cli/src/mount.c @@ -11,7 +11,6 @@ #include #include "securec.h" #include "utils.h" -#include "logging.h" #include "options.h" int Mount(const char *src, const char *dst) @@ -147,7 +146,6 @@ int MountFile(const char *rootfs, const char *filepath) struct stat srcStat; ret = stat(filepath, &srcStat); if (ret < 0) { - LOG_WARNING("warning: failed to find file %s on host, skipping", filepath); return 0; } @@ -179,7 +177,6 @@ int MountDir(const char *rootfs, const char *src) struct stat srcStat; ret = stat(src, &srcStat); if (ret < 0) { - LOG_WARNING("warning: failed to find dir %s on host, skipping", src); return 0; } diff --git a/cli/src/ns.c b/cli/src/ns.c index 4e06333..57ec208 100644 --- a/cli/src/ns.c +++ b/cli/src/ns.c @@ -8,8 +8,8 @@ #include #include #include +#include "basic.h" #include "securec.h" -#include "logging.h" int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize) { diff --git a/cli/src/options.c b/cli/src/options.c index c3ddcaf..7ab75d9 100644 --- a/cli/src/options.c +++ b/cli/src/options.c @@ -10,7 +10,6 @@ static struct { bool noDrv; - bool verbose; } g_runtimeOptions; static struct { @@ -18,7 +17,6 @@ static struct { bool *flag; } g_optionNameFlagTable[] = { {"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver - {"VERBOSE", &g_runtimeOptions.verbose}, // 输出日志 {NULL, NULL} }; @@ -26,7 +24,6 @@ void ParseRuntimeOptions(const char *options) { // set defaults value g_runtimeOptions.noDrv = false; - g_runtimeOptions.verbose = false; static const char *seperator = ","; char *runtimeOptions = strdup(options); @@ -53,8 +50,3 @@ bool IsOptionNoDrvSet() { return g_runtimeOptions.noDrv; } - -bool IsOptionVerboseSet() -{ - return g_runtimeOptions.verbose; -} \ No newline at end of file diff --git a/cli/src/options.h b/cli/src/options.h index 6c8c0b7..ab7ddda 100644 --- a/cli/src/options.h +++ b/cli/src/options.h @@ -9,6 +9,5 @@ void ParseRuntimeOptions(const char *options); bool IsOptionNoDrvSet(); -bool IsOptionVerboseSet(); #endif diff --git a/cli/src/utils.c b/cli/src/utils.c index 7764b15..ab504b3 100644 --- a/cli/src/utils.c +++ b/cli/src/utils.c @@ -13,7 +13,6 @@ #include #include #include "securec.h" -#include "logging.h" int IsStrEqual(const char *s1, const char *s2) { diff --git a/hook/main.go b/hook/main.go index 69a0063..1b94f97 100644 --- a/hook/main.go +++ b/hook/main.go @@ -45,7 +45,6 @@ var ( var validRuntimeOptions = [...]string{ "NODRV", - "VERBOSE", } type containerConfig struct { From 230b2759a69e0c8f7b3adff86543d0b22befad66 Mon Sep 17 00:00:00 2001 From: BianTanggui Date: Tue, 27 Oct 2020 15:33:37 +0800 Subject: [PATCH 2/2] Match-id-e09f4bfabe86ff28154841143128cab741c4e76a --- cli/src/basic.h | 4 +--- cli/src/mount.c | 13 +++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cli/src/basic.h b/cli/src/basic.h index c15a3ff..3335cb1 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -18,7 +18,7 @@ #define MAX_DEVICE_NR 1024 #define MAX_MOUNT_NR 512 -#define LOG(level, fmt, ...) \ +#define LOG_ERROR(fmt, ...) \ do { \ char _content[BUF_SIZE] = {0}; \ int _ret = sprintf_s(_content, BUF_SIZE, fmt, ##__VA_ARGS__); \ @@ -29,8 +29,6 @@ } \ } while (0) -#define LOG_ERROR(fmt, ...) LOG('E', fmt, ##__VA_ARGS__) - #define ALLOW_PATH "/devices.allow" #define ROOT_GAP 4 #define FSTYPE_GAP 2 diff --git a/cli/src/mount.c b/cli/src/mount.c index b527aef..2488d3b 100644 --- a/cli/src/mount.c +++ b/cli/src/mount.c @@ -96,6 +96,19 @@ int MountDevice(const char *rootfs, const char *deviceName) return -1; } + errno = 0; + struct stat dstStat; + ret = stat((const char *)dst, &dstStat); + if (ret == 0 && S_ISCHR(dstStat.st_mode)) { + return 0; // 特权容器自动挂载HOST所有设备,故此处跳过 + } else if (ret == 0) { + LOG_ERROR("error: %s already exists but not a char device as expected.", dst); + return -1; + } else if (ret < 0 && errno != ENOENT) { + LOG_ERROR("error: failed to check dst %s stat: %s.", dst, strerror(errno)); + return -1; + } + ret = CreateFile(dst, srcStat.st_mode); if (ret < 0) { LOG_ERROR("error: failed to create mount dst file: %s.", dst);