Match-id-bb59ee5d1a8d5afaa75ac2bb07db32c8a3a07b1d

This commit is contained in:
BianTanggui
2020-10-26 11:04:50 +08:00
parent 9d49338ed6
commit 62c2d9a004
11 changed files with 23 additions and 143 deletions

View File

@@ -16,9 +16,21 @@
#define DEFAULT_DIR_MODE 0755 #define DEFAULT_DIR_MODE 0755
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define MAX_DEVICE_NR 1024 #define MAX_DEVICE_NR 1024
#define DEFAULT_LOG_FILE "/var/log/ascend-docker-runtime.log"
#define MAX_MOUNT_NR 512 #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 ALLOW_PATH "/devices.allow"
#define ROOT_GAP 4 #define ROOT_GAP 4
#define FSTYPE_GAP 2 #define FSTYPE_GAP 2

View File

@@ -14,7 +14,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "securec.h" #include "securec.h"
#include "utils.h" #include "utils.h"
#include "logging.h"
bool TakeNthWord(char **pLine, unsigned int n, char **word) bool TakeNthWord(char **pLine, unsigned int n, char **word)
{ {

View File

@@ -1,74 +0,0 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend-docker-cli工具日志模块
*/
#include "logging.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#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);
}

View File

@@ -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 <stdio.h>
#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

View File

@@ -18,7 +18,6 @@
#include "ns.h" #include "ns.h"
#include "mount.h" #include "mount.h"
#include "cgrp.h" #include "cgrp.h"
#include "logging.h"
#include "options.h" #include "options.h"
#define DECIMAL 10 #define DECIMAL 10
@@ -315,22 +314,13 @@ int Process(int argc, char **argv)
} }
ParseRuntimeOptions(args.options); 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); ret = SetupContainer(&args);
if (ret < 0) { if (ret < 0) {
CloseLog(); LOG_ERROR("error: failed to setup container.");
return ret; return ret;
} }
CloseLog();
return 0; return 0;
} }

View File

@@ -11,7 +11,6 @@
#include <sys/mount.h> #include <sys/mount.h>
#include "securec.h" #include "securec.h"
#include "utils.h" #include "utils.h"
#include "logging.h"
#include "options.h" #include "options.h"
int Mount(const char *src, const char *dst) int Mount(const char *src, const char *dst)
@@ -147,7 +146,6 @@ int MountFile(const char *rootfs, const char *filepath)
struct stat srcStat; struct stat srcStat;
ret = stat(filepath, &srcStat); ret = stat(filepath, &srcStat);
if (ret < 0) { if (ret < 0) {
LOG_WARNING("warning: failed to find file %s on host, skipping", filepath);
return 0; return 0;
} }
@@ -179,7 +177,6 @@ int MountDir(const char *rootfs, const char *src)
struct stat srcStat; struct stat srcStat;
ret = stat(src, &srcStat); ret = stat(src, &srcStat);
if (ret < 0) { if (ret < 0) {
LOG_WARNING("warning: failed to find dir %s on host, skipping", src);
return 0; return 0;
} }

View File

@@ -8,8 +8,8 @@
#include <sched.h> #include <sched.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include "basic.h"
#include "securec.h" #include "securec.h"
#include "logging.h"
int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize) int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize)
{ {

View File

@@ -10,7 +10,6 @@
static struct { static struct {
bool noDrv; bool noDrv;
bool verbose;
} g_runtimeOptions; } g_runtimeOptions;
static struct { static struct {
@@ -18,7 +17,6 @@ static struct {
bool *flag; bool *flag;
} g_optionNameFlagTable[] = { } g_optionNameFlagTable[] = {
{"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver {"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver
{"VERBOSE", &g_runtimeOptions.verbose}, // 输出日志
{NULL, NULL} {NULL, NULL}
}; };
@@ -26,7 +24,6 @@ void ParseRuntimeOptions(const char *options)
{ {
// set defaults value // set defaults value
g_runtimeOptions.noDrv = false; g_runtimeOptions.noDrv = false;
g_runtimeOptions.verbose = false;
static const char *seperator = ","; static const char *seperator = ",";
char *runtimeOptions = strdup(options); char *runtimeOptions = strdup(options);
@@ -53,8 +50,3 @@ bool IsOptionNoDrvSet()
{ {
return g_runtimeOptions.noDrv; return g_runtimeOptions.noDrv;
} }
bool IsOptionVerboseSet()
{
return g_runtimeOptions.verbose;
}

View File

@@ -9,6 +9,5 @@
void ParseRuntimeOptions(const char *options); void ParseRuntimeOptions(const char *options);
bool IsOptionNoDrvSet(); bool IsOptionNoDrvSet();
bool IsOptionVerboseSet();
#endif #endif

View File

@@ -13,7 +13,6 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "securec.h" #include "securec.h"
#include "logging.h"
int IsStrEqual(const char *s1, const char *s2) int IsStrEqual(const char *s1, const char *s2)
{ {

View File

@@ -45,7 +45,6 @@ var (
var validRuntimeOptions = [...]string{ var validRuntimeOptions = [...]string{
"NODRV", "NODRV",
"VERBOSE",
} }
type containerConfig struct { type containerConfig struct {