mirror of
https://github.com/Ascend/ascend-docker-runtime.git
synced 2025-10-14 17:33:46 +08:00
Match-id-d23b01861e8b27062a1d27792272a26cc174c10a
This commit is contained in:
@@ -70,13 +70,13 @@ int ParseFileByLine(char* buffer, int bufferSize, ParseFileLine fn, const char*
|
|||||||
char resolvedPath[PATH_MAX] = {0x0};
|
char resolvedPath[PATH_MAX] = {0x0};
|
||||||
|
|
||||||
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
|
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
|
||||||
LogError("error: cannot canonicalize path %s.", filepath);
|
LOG_ERROR("error: cannot canonicalize path %s.", filepath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = fopen(resolvedPath, "r");
|
fp = fopen(resolvedPath, "r");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
LogError("cannot open file.");
|
LOG_ERROR("cannot open file.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,20 +154,20 @@ int SetupDeviceCgroup(FILE *cgroupAllow, const char *devName)
|
|||||||
|
|
||||||
ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName);
|
ret = sprintf_s(devPath, BUF_SIZE, "/dev/%s", devName);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to assemble dev path for %s.", devName);
|
LOG_ERROR("error: failed to assemble dev path for %s.", devName);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = stat((const char *)devPath, &devStat);
|
ret = stat((const char *)devPath, &devStat);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to get stat of %s.", devPath);
|
LOG_ERROR("error: failed to get stat of %s.", devPath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isFailed = fprintf(cgroupAllow, "c %u:%u rw", major(devStat.st_rdev), minor(devStat.st_rdev)) < 0 ||
|
bool isFailed = fprintf(cgroupAllow, "c %u:%u rw", major(devStat.st_rdev), minor(devStat.st_rdev)) < 0 ||
|
||||||
fflush(cgroupAllow) == EOF || ferror(cgroupAllow) < 0;
|
fflush(cgroupAllow) == EOF || ferror(cgroupAllow) < 0;
|
||||||
if (isFailed) {
|
if (isFailed) {
|
||||||
LogError("error: write devices failed.");
|
LOG_ERROR("error: write devices failed.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,19 +180,19 @@ int SetupDriverCgroup(FILE *cgroupAllow)
|
|||||||
|
|
||||||
ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER);
|
ret = SetupDeviceCgroup(cgroupAllow, DAVINCI_MANAGER);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to setup cgroup for %s.", DAVINCI_MANAGER);
|
LOG_ERROR("error: failed to setup cgroup for %s.", DAVINCI_MANAGER);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM);
|
ret = SetupDeviceCgroup(cgroupAllow, DEVMM_SVM);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to setup cgroup for %s.", DEVMM_SVM);
|
LOG_ERROR("error: failed to setup cgroup for %s.", DEVMM_SVM);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC);
|
ret = SetupDeviceCgroup(cgroupAllow, HISI_HDC);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to setup cgroup for %s.", HISI_HDC);
|
LOG_ERROR("error: failed to setup cgroup for %s.", HISI_HDC);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,13 +207,13 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
|
|||||||
|
|
||||||
ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
|
ret = sprintf_s(mountPath, BUF_SIZE, "/proc/%d/mountinfo", (int)getppid());
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: assemble mount info path failed: ppid(%d).", getppid());
|
LOG_ERROR("error: assemble mount info path failed: ppid(%d).", getppid());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath);
|
ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: cat file content failed.");
|
LOG_ERROR("error: cat file content failed.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,13 +221,13 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
|
|||||||
char cgroupPath[BUF_SIZE] = {0x0};
|
char cgroupPath[BUF_SIZE] = {0x0};
|
||||||
ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid);
|
ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: assemble cgroup path failed: pid(%d).", pid);
|
LOG_ERROR("error: assemble cgroup path failed: pid(%d).", pid);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
|
ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: cat file content failed.");
|
LOG_ERROR("error: cat file content failed.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,7 +236,7 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
|
|||||||
|
|
||||||
ret = sprintf_s(effPath, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH);
|
ret = sprintf_s(effPath, maxSize, "%s%s%s", mount, cgroup, ALLOW_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: assemble cgroup device path failed.");
|
LOG_ERROR("error: assemble cgroup device path failed.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,20 +251,20 @@ int SetupCgroup(const struct ParsedConfig *config)
|
|||||||
FILE *cgroupAllow = NULL;
|
FILE *cgroupAllow = NULL;
|
||||||
|
|
||||||
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
|
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
|
||||||
LogError("error: cannot canonicalize cgroup path: %s.", config->cgroupPath);
|
LOG_ERROR("error: cannot canonicalize cgroup path: %s.", config->cgroupPath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
|
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
|
||||||
if (cgroupAllow == NULL) {
|
if (cgroupAllow == NULL) {
|
||||||
LogError("error: failed to open cgroup file: %s.", resolvedCgroupPath);
|
LOG_ERROR("error: failed to open cgroup file: %s.", resolvedCgroupPath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = SetupDriverCgroup(cgroupAllow);
|
ret = SetupDriverCgroup(cgroupAllow);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fclose(cgroupAllow);
|
fclose(cgroupAllow);
|
||||||
LogError("error: failed to setup driver cgroup.");
|
LOG_ERROR("error: failed to setup driver cgroup.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,14 +272,14 @@ int SetupCgroup(const struct ParsedConfig *config)
|
|||||||
ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, config->devices[idx]);
|
ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, config->devices[idx]);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fclose(cgroupAllow);
|
fclose(cgroupAllow);
|
||||||
LogError("error: failed to assemble device path for no.%u.", config->devices[idx]);
|
LOG_ERROR("error: failed to assemble device path for no.%u.", config->devices[idx]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName);
|
ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fclose(cgroupAllow);
|
fclose(cgroupAllow);
|
||||||
LogError("error: failed to setup cgroup for %s.", deviceName);
|
LOG_ERROR("error: failed to setup cgroup for %s.", deviceName);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
@@ -32,13 +31,13 @@ int OpenLog(const char *logFile)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (realpath(logFile, realPath) == NULL && errno != ENOENT) {
|
if (realpath(logFile, realPath) == NULL && errno != ENOENT) {
|
||||||
LogError("error: cannot canonicalize log file path %s.", logFile);
|
LOG_ERROR("error: cannot canonicalize log file path %s.", logFile);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_logFile = fopen((const char *)realPath, "ae");
|
g_logFile = fopen((const char *)realPath, "ae");
|
||||||
if (g_logFile == NULL) {
|
if (g_logFile == NULL) {
|
||||||
LogError("error: failed to open log file %s.", realPath);
|
LOG_ERROR("error: failed to open log file %s.", realPath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +52,7 @@ void CloseLog()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLog(char level, const char *fmt, va_list args)
|
void WriteLog(char level, const char *content)
|
||||||
{
|
{
|
||||||
struct timeval tv = {0};
|
struct timeval tv = {0};
|
||||||
struct tm *tm = NULL;
|
struct tm *tm = NULL;
|
||||||
@@ -70,51 +69,6 @@ static void WriteLog(char level, const char *fmt, va_list args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fprintf(g_logFile, "[%c %s.%06ld %d] ", level, buf, tv.tv_usec, g_pid);
|
fprintf(g_logFile, "[%c %s.%06ld %d] ", level, buf, tv.tv_usec, g_pid);
|
||||||
vfprintf(g_logFile, fmt, args);
|
fprintf(g_logFile, "%s", content);
|
||||||
fputc('\n', g_logFile);
|
fputc('\n', g_logFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogError(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
|
|
||||||
va_start(args, fmt);
|
|
||||||
|
|
||||||
va_list argsCopy;
|
|
||||||
va_copy(argsCopy, args);
|
|
||||||
WriteLog('E', fmt, argsCopy);
|
|
||||||
va_end(argsCopy);
|
|
||||||
|
|
||||||
vfprintf(stderr, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogInfo(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
|
|
||||||
va_start(args, fmt);
|
|
||||||
|
|
||||||
va_list argsCopy;
|
|
||||||
va_copy(argsCopy, args);
|
|
||||||
WriteLog('I', fmt, argsCopy);
|
|
||||||
va_end(argsCopy);
|
|
||||||
|
|
||||||
vfprintf(stdout, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogWarning(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
|
|
||||||
va_start(args, fmt);
|
|
||||||
|
|
||||||
va_list argsCopy;
|
|
||||||
va_copy(argsCopy, args);
|
|
||||||
WriteLog('W', fmt, argsCopy);
|
|
||||||
va_end(argsCopy);
|
|
||||||
|
|
||||||
vfprintf(stderr, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
@@ -5,12 +5,35 @@
|
|||||||
#ifndef _LOGGING_H
|
#ifndef _LOGGING_H
|
||||||
#define _LOGGING_H
|
#define _LOGGING_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include "securec.h"
|
||||||
|
#include "basic.h"
|
||||||
|
|
||||||
void SetPidForLog(int pid);
|
void SetPidForLog(int pid);
|
||||||
int OpenLog(const char *logFile);
|
int OpenLog(const char *logFile);
|
||||||
void CloseLog();
|
void CloseLog();
|
||||||
|
void WriteLog(char level, const char *content);
|
||||||
|
|
||||||
void LogError(const char *fmt, ...);
|
#define LOG(level, fmt, ...) \
|
||||||
void LogInfo(const char *fmt, ...);
|
do { \
|
||||||
void LogWarning(const char *fmt, ...);
|
char content[BUF_SIZE] = {0}; \
|
||||||
|
int ret = sprintf_s(content, BUF_SIZE, fmt, ##__VA_ARGS__); \
|
||||||
|
if (ret < 0) { \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
WriteLog(level, (const char *)content); \
|
||||||
|
fprintf(stderr, "%s", (const char *)content); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LOG_ERROR(fmt, ...) \
|
||||||
|
do { \
|
||||||
|
LOG('E', fmt, ##__VA_ARGS__); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LOG_WARNING(fmt, ...) \
|
||||||
|
do { \
|
||||||
|
LOG('W', fmt, ##__VA_ARGS__); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -44,7 +44,7 @@ static bool DevicesCmdArgParser(struct CmdArgs *args, const char *arg)
|
|||||||
{
|
{
|
||||||
errno_t err = strcpy_s(args->devices, BUF_SIZE, arg);
|
errno_t err = strcpy_s(args->devices, BUF_SIZE, arg);
|
||||||
if (err != EOK) {
|
if (err != EOK) {
|
||||||
LogError("error: failed to get devices from cmd args.");
|
LOG_ERROR("error: failed to get devices from cmd args.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,12 +56,12 @@ static bool PidCmdArgParser(struct CmdArgs *args, const char *arg)
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
args->pid = strtol(optarg, NULL, DECIMAL);
|
args->pid = strtol(optarg, NULL, DECIMAL);
|
||||||
if (errno != 0) {
|
if (errno != 0) {
|
||||||
LogError("error: failed to convert pid string from cmd args, pid string: %s.", arg);
|
LOG_ERROR("error: failed to convert pid string from cmd args, pid string: %s.", arg);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args->pid <= 0) {
|
if (args->pid <= 0) {
|
||||||
LogError("error: invalid pid %d.", args->pid);
|
LOG_ERROR("error: invalid pid %d.", args->pid);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ static bool RootfsCmdArgParser(struct CmdArgs *args, const char *arg)
|
|||||||
{
|
{
|
||||||
errno_t err = strcpy_s(args->rootfs, BUF_SIZE, arg);
|
errno_t err = strcpy_s(args->rootfs, BUF_SIZE, arg);
|
||||||
if (err != EOK) {
|
if (err != EOK) {
|
||||||
LogError("error: failed to get rootfs path from cmd args");
|
LOG_ERROR("error: failed to get rootfs path from cmd args");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
|
|||||||
{
|
{
|
||||||
errno_t err = strcpy_s(args->options, BUF_SIZE, arg);
|
errno_t err = strcpy_s(args->options, BUF_SIZE, arg);
|
||||||
if (err != EOK) {
|
if (err != EOK) {
|
||||||
LogError("error: failed to get options string from cmd args");
|
LOG_ERROR("error: failed to get options string from cmd args");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ static bool OptionsCmdArgParser(struct CmdArgs *args, const char *arg)
|
|||||||
#define NUM_OF_CMD_ARGS 4
|
#define NUM_OF_CMD_ARGS 4
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
const int c;
|
const char c;
|
||||||
CmdArgParser parser;
|
CmdArgParser parser;
|
||||||
} g_cmdArgParsers[NUM_OF_CMD_ARGS] = {
|
} g_cmdArgParsers[NUM_OF_CMD_ARGS] = {
|
||||||
{'d', DevicesCmdArgParser},
|
{'d', DevicesCmdArgParser},
|
||||||
@@ -102,7 +102,7 @@ static struct {
|
|||||||
{'o', OptionsCmdArgParser}
|
{'o', OptionsCmdArgParser}
|
||||||
};
|
};
|
||||||
|
|
||||||
static int ParseOneCmdArg(struct CmdArgs *args, int indicator, const char *value)
|
static int ParseOneCmdArg(struct CmdArgs *args, char indicator, const char *value)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < NUM_OF_CMD_ARGS; i++) {
|
for (i = 0; i < NUM_OF_CMD_ARGS; i++) {
|
||||||
@@ -112,13 +112,13 @@ static int ParseOneCmdArg(struct CmdArgs *args, int indicator, const char *value
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (i == NUM_OF_CMD_ARGS) {
|
if (i == NUM_OF_CMD_ARGS) {
|
||||||
LogError("error: unrecognized cmd arg: indicate char: %c, value: %s.", indicator, value);
|
LOG_ERROR("error: unrecognized cmd arg: indicate char: %c, value: %s.", indicator, value);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isOK = g_cmdArgParsers[i].parser(args, value);
|
bool isOK = g_cmdArgParsers[i].parser(args, value);
|
||||||
if (!isOK) {
|
if (!isOK) {
|
||||||
LogError("error: failed while parsing cmd arg, indicate char: %c, value: %s.", indicator, value);
|
LOG_ERROR("error: failed while parsing cmd arg, indicate char: %c, value: %s.", indicator, value);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,14 +140,14 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
|
|||||||
token = strtok_s(devices, sep, &context);
|
token = strtok_s(devices, sep, &context);
|
||||||
while (token != NULL) {
|
while (token != NULL) {
|
||||||
if (idx >= *idListSize) {
|
if (idx >= *idListSize) {
|
||||||
LogError("error: too many devices(%u), support %u devices maximally", idx, *idListSize);
|
LOG_ERROR("error: too many devices(%u), support %u devices maximally", idx, *idListSize);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
idList[idx] = strtoul((const char *)token, NULL, DECIMAL);
|
idList[idx] = strtoul((const char *)token, NULL, DECIMAL);
|
||||||
if (errno != 0) {
|
if (errno != 0) {
|
||||||
LogError("error: failed to convert device id (%s) from cmd args, caused by: %s.", token, strerror(errno));
|
LOG_ERROR("error: failed to convert device id (%s) from cmd args, caused by: %s.", token, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,38 +166,38 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
|
|||||||
|
|
||||||
err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs);
|
err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs);
|
||||||
if (err != EOK) {
|
if (err != EOK) {
|
||||||
LogError("error: failed to copy rootfs path to parsed config.");
|
LOG_ERROR("error: failed to copy rootfs path to parsed config.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices);
|
ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to parse device ids from cmdline argument");
|
LOG_ERROR("error: failed to parse device ids from cmdline argument");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
|
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to get container mnt ns path: pid(%d).", args->pid);
|
LOG_ERROR("error: failed to get container mnt ns path: pid(%d).", args->pid);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
|
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to get cgroup path.");
|
LOG_ERROR("error: failed to get cgroup path.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char originNsPath[BUF_SIZE] = {0};
|
char originNsPath[BUF_SIZE] = {0};
|
||||||
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
|
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to get self ns path.");
|
LOG_ERROR("error: failed to get self ns path.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口,非外部输入
|
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口,非外部输入
|
||||||
if (config->originNsFd < 0) {
|
if (config->originNsFd < 0) {
|
||||||
LogError("error: failed to get self ns fd: %s.", originNsPath);
|
LOG_ERROR("error: failed to get self ns fd: %s.", originNsPath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,28 +213,28 @@ int SetupContainer(struct CmdArgs *args)
|
|||||||
|
|
||||||
ret = DoPrepare(args, &config);
|
ret = DoPrepare(args, &config);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to prepare nesessary config.");
|
LOG_ERROR("error: failed to prepare nesessary config.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// enter container's mount namespace
|
// enter container's mount namespace
|
||||||
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
|
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to set to container ns: %s.", config.containerNsPath);
|
LOG_ERROR("error: failed to set to container ns: %s.", config.containerNsPath);
|
||||||
close(config.originNsFd);
|
close(config.originNsFd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DoMounting(&config);
|
ret = DoMounting(&config);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mounting.");
|
LOG_ERROR("error: failed to do mounting.");
|
||||||
close(config.originNsFd);
|
close(config.originNsFd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = SetupCgroup(&config);
|
ret = SetupCgroup(&config);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to set up cgroup.");
|
LOG_ERROR("error: failed to set up cgroup.");
|
||||||
close(config.originNsFd);
|
close(config.originNsFd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -242,7 +242,7 @@ int SetupContainer(struct CmdArgs *args)
|
|||||||
// back to original namespace
|
// back to original namespace
|
||||||
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
|
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to set ns back.");
|
LOG_ERROR("error: failed to set ns back.");
|
||||||
close(config.originNsFd);
|
close(config.originNsFd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -259,29 +259,24 @@ int Process(int argc, char **argv)
|
|||||||
struct CmdArgs args = {0};
|
struct CmdArgs args = {0};
|
||||||
|
|
||||||
while ((c = getopt_long(argc, argv, "d:p:r:o", g_cmdOpts, &optionIndex)) != -1) {
|
while ((c = getopt_long(argc, argv, "d:p:r:o", g_cmdOpts, &optionIndex)) != -1) {
|
||||||
ret = ParseOneCmdArg(&args, c, optarg);
|
ret = ParseOneCmdArg(&args, (char)c, optarg);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to parse cmd args.");
|
LOG_ERROR("error: failed to parse cmd args.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsCmdArgsValid(&args)) {
|
if (!IsCmdArgsValid(&args)) {
|
||||||
LogError("error: information not completed or valid.");
|
LOG_ERROR("error: information not completed or valid.");
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ParseRuntimeOptions(args.options);
|
|
||||||
if (ret < 0) {
|
|
||||||
LogError("error: failed to parse runtime options.");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ParseRuntimeOptions(args.options);
|
||||||
SetPidForLog(args.pid);
|
SetPidForLog(args.pid);
|
||||||
|
|
||||||
ret = OpenLog(DEFAULT_LOG_FILE);
|
ret = OpenLog(DEFAULT_LOG_FILE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to open log file %s.", DEFAULT_LOG_FILE);
|
LOG_ERROR("error: failed to open log file %s.", DEFAULT_LOG_FILE);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -22,13 +22,13 @@ int Mount(const char *src, const char *dst)
|
|||||||
|
|
||||||
ret = mount(src, dst, NULL, mountFlags, NULL);
|
ret = mount(src, dst, NULL, mountFlags, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount.");
|
LOG_ERROR("error: failed to mount.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mount(NULL, dst, NULL, remountFlags, NULL);
|
ret = mount(NULL, dst, NULL, remountFlags, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to re-mount.");
|
LOG_ERROR("error: failed to re-mount.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,13 +64,13 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) {
|
if (realpath(unresolvedDst, resolvedDst) == NULL && errno != ENOENT) {
|
||||||
LogError("error: cannot canonicalize device dst: %s.", dst);
|
LOG_ERROR("error: cannot canonicalize device dst: %s.", dst);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = strcpy_s(dst, dstBufSize, (const char *)resolvedDst);
|
err = strcpy_s(dst, dstBufSize, (const char *)resolvedDst);
|
||||||
if (err != EOK) {
|
if (err != EOK) {
|
||||||
LogError("error: failed to copy resolved device mnt path to dst: %s.", resolvedDst);
|
LOG_ERROR("error: failed to copy resolved device mnt path to dst: %s.", resolvedDst);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,26 +86,26 @@ int MountDevice(const char *rootfs, const char *deviceName)
|
|||||||
|
|
||||||
ret = GetDeviceMntSrcDst(rootfs, deviceName, &pathInfo);
|
ret = GetDeviceMntSrcDst(rootfs, deviceName, &pathInfo);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to get device mount src and(or) dst path, device name: %s.", deviceName);
|
LOG_ERROR("error: failed to get device mount src and(or) dst path, device name: %s.", deviceName);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat srcStat;
|
struct stat srcStat;
|
||||||
ret = stat((const char *)src, &srcStat);
|
ret = stat((const char *)src, &srcStat);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to stat src: %s.", src);
|
LOG_ERROR("error: failed to stat src: %s.", src);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = CreateFile(dst, srcStat.st_mode);
|
ret = CreateFile(dst, srcStat.st_mode);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to create mount dst file: %s.", dst);
|
LOG_ERROR("error: failed to create mount dst file: %s.", dst);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = Mount(src, dst);
|
ret = Mount(src, dst);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount dev.");
|
LOG_ERROR("error: failed to mount dev.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,13 +119,13 @@ int DoDeviceMounting(const char *rootfs, const unsigned int ids[], size_t idsNr)
|
|||||||
for (size_t idx = 0; idx < idsNr; idx++) {
|
for (size_t idx = 0; idx < idsNr; idx++) {
|
||||||
int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]);
|
int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: assemble device name failed, id: %u.", ids[idx]);
|
LOG_ERROR("error: assemble device name failed, id: %u.", ids[idx]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDevice(rootfs, deviceName);
|
ret = MountDevice(rootfs, deviceName);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount device %s.", deviceName);
|
LOG_ERROR("error: failed to mount device %s.", deviceName);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,26 +140,26 @@ int MountFile(const char *rootfs, const char *filepath)
|
|||||||
|
|
||||||
ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath);
|
ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to assemble file mounting path, file: %s.", filepath);
|
LOG_ERROR("error: failed to assemble file mounting path, file: %s.", filepath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat srcStat;
|
struct stat srcStat;
|
||||||
ret = stat(filepath, &srcStat);
|
ret = stat(filepath, &srcStat);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogWarning("warning: failed to find file %s on host, skipping", filepath);
|
LOG_WARNING("warning: failed to find file %s on host, skipping", filepath);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = CreateFile(dst, srcStat.st_mode);
|
ret = CreateFile(dst, srcStat.st_mode);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to create mount dst file: %s.", dst);
|
LOG_ERROR("error: failed to create mount dst file: %s.", dst);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = Mount(filepath, dst);
|
ret = Mount(filepath, dst);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount dev.");
|
LOG_ERROR("error: failed to mount dev.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,19 +179,19 @@ 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) {
|
||||||
LogWarning("warning: failed to find dir %s on host, skipping", src);
|
LOG_WARNING("warning: failed to find dir %s on host, skipping", src);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE);
|
ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to make dir: %s.", dst);
|
LOG_ERROR("error: failed to make dir: %s.", dst);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = Mount(src, dst);
|
ret = Mount(src, dst);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount dir: %s to %s.", src, dst);
|
LOG_ERROR("error: failed to mount dir: %s to %s.", src, dst);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,19 +203,19 @@ int DoCtrlDeviceMounting(const char *rootfs)
|
|||||||
/* device */
|
/* device */
|
||||||
int ret = MountDevice(rootfs, DAVINCI_MANAGER);
|
int ret = MountDevice(rootfs, DAVINCI_MANAGER);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount device %s.", DAVINCI_MANAGER);
|
LOG_ERROR("error: failed to mount device %s.", DAVINCI_MANAGER);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDevice(rootfs, DEVMM_SVM);
|
ret = MountDevice(rootfs, DEVMM_SVM);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount device %s.", DEVMM_SVM);
|
LOG_ERROR("error: failed to mount device %s.", DEVMM_SVM);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDevice(rootfs, HISI_HDC);
|
ret = MountDevice(rootfs, HISI_HDC);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount device %s.", HISI_HDC);
|
LOG_ERROR("error: failed to mount device %s.", HISI_HDC);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,31 +227,31 @@ int DoDirectoryMounting(const char *rootfs)
|
|||||||
/* directory */
|
/* directory */
|
||||||
int ret = MountDir(rootfs, ASCEND_DRIVER_LIB64_PATH);
|
int ret = MountDir(rootfs, ASCEND_DRIVER_LIB64_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_DRIVER_LIB64_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_DRIVER_LIB64_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDir(rootfs, ASCEND_DRIVER_TOOLS_PATH);
|
ret = MountDir(rootfs, ASCEND_DRIVER_TOOLS_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_DRIVER_TOOLS_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_DRIVER_TOOLS_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDir(rootfs, ASCEND_DRIVER_INC_PATH);
|
ret = MountDir(rootfs, ASCEND_DRIVER_INC_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_DRIVER_INC_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_DRIVER_INC_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDir(rootfs, ASCEND_ADDONS_PATH);
|
ret = MountDir(rootfs, ASCEND_ADDONS_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_ADDONS_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_ADDONS_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MountDir(rootfs, ASCEND_DCMI_PATH);
|
ret = MountDir(rootfs, ASCEND_DCMI_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_DCMI_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_DCMI_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,13 +262,13 @@ int DoFileMounting(const char *rootfs)
|
|||||||
{
|
{
|
||||||
int ret = MountFile(rootfs, ASCEND_NPU_SMI_PATH);
|
int ret = MountFile(rootfs, ASCEND_NPU_SMI_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_NPU_SMI_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_NPU_SMI_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
MountFile(rootfs, ASCEND_SLOG_CONF_PATH);
|
MountFile(rootfs, ASCEND_SLOG_CONF_PATH);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount %s.", ASCEND_SLOG_CONF_PATH);
|
LOG_ERROR("error: failed to do mount %s.", ASCEND_SLOG_CONF_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,13 +281,13 @@ int DoMounting(const struct ParsedConfig *config)
|
|||||||
|
|
||||||
ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr);
|
ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount devices.");
|
LOG_ERROR("error: failed to mount devices.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DoCtrlDeviceMounting(config->rootfs);
|
ret = DoCtrlDeviceMounting(config->rootfs);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount ctrl devices.");
|
LOG_ERROR("error: failed to mount ctrl devices.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,13 +297,13 @@ int DoMounting(const struct ParsedConfig *config)
|
|||||||
|
|
||||||
ret = DoFileMounting(config->rootfs);
|
ret = DoFileMounting(config->rootfs);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to mount files.");
|
LOG_ERROR("error: failed to mount files.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DoDirectoryMounting(config->rootfs);
|
ret = DoDirectoryMounting(config->rootfs);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to do mount directories.");
|
LOG_ERROR("error: failed to do mount directories.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -28,7 +28,7 @@ int EnterNsByFd(int fd, int nsType)
|
|||||||
{
|
{
|
||||||
int ret = setns(fd, nsType);
|
int ret = setns(fd, nsType);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to set ns: fd(%d).", fd);
|
LOG_ERROR("error: failed to set ns: fd(%d).", fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,13 +42,13 @@ int EnterNsByPath(const char *path, int nsType)
|
|||||||
|
|
||||||
fd = open(path, O_RDONLY); // proc文件接口,非外部输入
|
fd = open(path, O_RDONLY); // proc文件接口,非外部输入
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
LogError("error: failed to open ns path: %s.", path);
|
LOG_ERROR("error: failed to open ns path: %s.", path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = EnterNsByFd(fd, nsType);
|
ret = EnterNsByFd(fd, nsType);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to set ns: %s.", path);
|
LOG_ERROR("error: failed to set ns: %s.", path);
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@@ -22,7 +22,7 @@ static struct {
|
|||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
int ParseRuntimeOptions(const char *options)
|
void ParseRuntimeOptions(const char *options)
|
||||||
{
|
{
|
||||||
// set defaults value
|
// set defaults value
|
||||||
g_runtimeOptions.noDrv = false;
|
g_runtimeOptions.noDrv = false;
|
||||||
@@ -47,7 +47,6 @@ int ParseRuntimeOptions(const char *options)
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(runtimeOptions);
|
free(runtimeOptions);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsOptionNoDrvSet()
|
bool IsOptionNoDrvSet()
|
||||||
|
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
int ParseRuntimeOptions(const char *options);
|
void ParseRuntimeOptions(const char *options);
|
||||||
bool IsOptionNoDrvSet();
|
bool IsOptionNoDrvSet();
|
||||||
bool IsOptionVerboseSet();
|
bool IsOptionVerboseSet();
|
||||||
|
|
||||||
|
@@ -100,19 +100,19 @@ int CreateFile(const char *path, mode_t mode)
|
|||||||
|
|
||||||
int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE);
|
int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LogError("error: failed to make parent dir for file: %s", path);
|
LOG_ERROR("error: failed to make parent dir for file: %s", path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char resolvedPath[PATH_MAX] = {0};
|
char resolvedPath[PATH_MAX] = {0};
|
||||||
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
|
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
|
||||||
LogError("error: failed to resolve path %s.", path);
|
LOG_ERROR("error: failed to resolve path %s.", path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
|
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
LogError("error: cannot create file: %s.", resolvedPath);
|
LOG_ERROR("error: cannot create file: %s.", resolvedPath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
@@ -1,3 +1,7 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
|
||||||
|
* Description: 测试框架
|
||||||
|
*/
|
||||||
#ifndef __PUBLIC_STUB_H__
|
#ifndef __PUBLIC_STUB_H__
|
||||||
#define __PUBLIC_STUB_H__
|
#define __PUBLIC_STUB_H__
|
||||||
|
|
||||||
|
@@ -1,3 +1,7 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
|
||||||
|
* Description: 测试框架
|
||||||
|
*/
|
||||||
#ifndef __UT_COMM_H__
|
#ifndef __UT_COMM_H__
|
||||||
#define __UT_COMM_H__
|
#define __UT_COMM_H__
|
||||||
|
|
||||||
|
@@ -1,33 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
|
||||||
|
* Description: 测试框架
|
||||||
|
*/
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <signal.h>
|
|
||||||
#include <execinfo.h>
|
|
||||||
|
|
||||||
using namespace testing;
|
using namespace testing;
|
||||||
|
|
||||||
void handler(int sig)
|
|
||||||
{
|
|
||||||
void *array[20];
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
// get void*'s for all entries on the stack
|
|
||||||
size = backtrace(array, 20);
|
|
||||||
|
|
||||||
// print out all the frames to stderr
|
|
||||||
fprintf(stderr, "Error: signal %d:\n", sig);
|
|
||||||
backtrace_symbols_fd(array, size, STDERR_FILENO);
|
|
||||||
exit(sig);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
signal(SIGABRT, handler);
|
|
||||||
signal(SIGFPE, handler);
|
|
||||||
signal(SIGILL, handler);
|
|
||||||
signal(SIGINT, handler);
|
|
||||||
signal(SIGSEGV, handler);
|
|
||||||
signal(SIGTERM, handler);
|
|
||||||
|
|
||||||
//testing::InitGoogleTest(&argc, argv);
|
//testing::InitGoogleTest(&argc, argv);
|
||||||
int result = testing::Init_UT(argc, argv,true);
|
int result = testing::Init_UT(argc, argv,true);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user