Match-id-f7cdfc7a411976f9681dbea6a0d794378ec36d97

This commit is contained in:
BianTanggui
2020-08-11 12:00:52 +08:00
parent fe9f73054c
commit 12c4bbd54a
9 changed files with 130 additions and 11 deletions

View File

@@ -22,6 +22,7 @@
#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 ALLOW_PATH "/devices.allow"
#define ROOT_GAP 4

View File

@@ -5,22 +5,109 @@
#include "logging.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include "securec.h"
#include "basic.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 (g_logFile != NULL) {
return 0; // 防重入
}
if (realpath(logFile, realPath) == NULL && errno != ENOENT) {
LogError("error: cannot canonicalize log file path %s.", logFile);
return -1;
}
g_logFile = fopen((const char *)realPath, "ae");
if (g_logFile == NULL) {
LogError("error: failed to open log file %s.", realPath);
return -1;
}
return 0;
}
void CloseLog()
{
if (g_logFile != NULL) {
(void)fclose(g_logFile);
g_logFile = NULL;
}
}
static void WriteLog(char level, const char *fmt, va_list args)
{
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);
vfprintf(g_logFile, fmt, args);
fputc('\n', g_logFile);
}
void LogError(const char *fmt, ...)
{
va_list args;
va_list logArgs;
va_start(args, fmt);
va_copy(logArgs, args);
vfprintf(stderr, fmt, args);
WriteLog('E', fmt, logArgs);
va_end(logArgs);
va_end(args);
}
void LogInfo(const char *fmt, ...)
{
va_list args;
va_list logArgs;
va_start(args, fmt);
va_copy(logArgs, args);
vfprintf(stdout, fmt, args);
WriteLog('I', fmt, logArgs);
va_end(logArgs);
va_end(args);
}
void LogWarning(const char *fmt, ...)
{
va_list args;
va_list logArgs;
va_start(args, fmt);
va_copy(logArgs, args);
vfprintf(stderr, fmt, args);
WriteLog('W', fmt, logArgs);
va_end(logArgs);
va_end(args);
}

View File

@@ -5,7 +5,12 @@
#ifndef _LOGGING_H
#define _LOGGING_H
void SetPidForLog(int pid);
int OpenLog(const char *logFile);
void CloseLog();
void LogError(const char *fmt, ...);
void LogInfo(const char *fmt, ...);
void LogWarning(const char *fmt, ...);
#endif

View File

@@ -201,12 +201,6 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
return -1;
}
ret = ParseRuntimeOptions(args->options);
if (ret < 0) {
LogError("error: failed to parse runtime options.");
return -1;
}
return 0;
}
@@ -277,11 +271,34 @@ int Process(int argc, char **argv)
return -1;
}
ret = ParseRuntimeOptions(args.options);
if (ret < 0) {
LogError("error: failed to parse runtime options.");
return -1;
}
SetPidForLog(args.pid);
if (IsOptionVerboseSet()) {
ret = OpenLog(DEFAULT_LOG_FILE);
if (ret < 0) {
LogError("error: failed to open log file %s.", DEFAULT_LOG_FILE);
return -1;
}
}
ret = SetupContainer(&args);
if (ret < 0) {
if (IsOptionVerboseSet()) {
CloseLog();
}
return ret;
}
if (IsOptionVerboseSet()) {
CloseLog();
}
return 0;
}

View File

@@ -147,8 +147,8 @@ int MountFile(const char *rootfs, const char *filepath)
struct stat srcStat;
ret = stat(filepath, &srcStat);
if (ret < 0) {
LogError("error: failed to stat src: %s.", filepath);
return -1;
LogWarning("warning: failed to find file %s on host, skipping", filepath);
return 0;
}
ret = CreateFile(dst, srcStat.st_mode);
@@ -179,7 +179,8 @@ int MountDir(const char *rootfs, const char *src)
struct stat srcStat;
ret = stat(src, &srcStat);
if (ret < 0) {
return -1;
LogWarning("warning: failed to find dir %s on host, skipping", src);
return 0;
}
ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE);

View File

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

View File

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

View File

@@ -42,11 +42,9 @@ int CheckDirExists(const char *dir)
{
DIR *ptr = opendir(dir);
if (NULL == ptr) {
LogError("path %s not exist.", dir);
return -1;
}
LogInfo("path %s exist.", dir);
closedir(ptr);
return 0;
}

View File

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