mirror of
https://github.com/Ascend/ascend-docker-runtime.git
synced 2025-10-11 13:00:03 +08:00
Match-id-cd2735ec23dcbe357a3201737c9de4694aee8177
This commit is contained in:
112
cli/src/cgrp.c
112
cli/src/cgrp.c
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "securec.h"
|
||||
|
||||
#include "u_mount.h"
|
||||
#include "utils.h"
|
||||
#include "options.h"
|
||||
#include "logger.h"
|
||||
@@ -82,34 +83,17 @@ bool CheckSubStr(char **pLine, const char *subsys)
|
||||
|
||||
return strstr(substr, subsys) != NULL;
|
||||
}
|
||||
|
||||
typedef char *(*ParseFileLine)(char *, const char *);
|
||||
int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const char* filepath)
|
||||
static bool GetFileInfo(const char* resolvedPath, char* buffer, const int bufferSize, const ParseFileLine fn)
|
||||
{
|
||||
if (buffer == NULL || filepath == NULL) {
|
||||
(void)fprintf(stderr, "buffer, filepath pointer is null!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *fp = NULL;
|
||||
char *result = NULL;
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
char resolvedPath[PATH_MAX] = {0x0};
|
||||
|
||||
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
|
||||
Logger("Cannot canonicalize path.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
const size_t maxFileSzieMb = 1024; // max 1G
|
||||
if (!CheckExternalFile(resolvedPath, strlen(resolvedPath), maxFileSzieMb, true)) {
|
||||
Logger("Check file legality failed.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
char *result = NULL;
|
||||
fp = fopen(resolvedPath, "r");
|
||||
if (fp == NULL) {
|
||||
Logger("cannot open file.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
while (getline(&line, &len, fp) != -1) {
|
||||
@@ -123,6 +107,30 @@ int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const
|
||||
free(line);
|
||||
fclose(fp);
|
||||
if (ret != EOK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const char* filepath)
|
||||
{
|
||||
if (buffer == NULL || filepath == NULL) {
|
||||
(void)fprintf(stderr, "buffer, filepath pointer is null!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char resolvedPath[PATH_MAX] = {0x0};
|
||||
|
||||
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
|
||||
Logger("Cannot canonicalize path.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
const size_t maxFileSzieMb = 1024; // max 1G
|
||||
if (!CheckExternalFile(resolvedPath, strlen(resolvedPath), maxFileSzieMb, true)) {
|
||||
Logger("Check file legality failed.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
if (!GetFileInfo(resolvedPath, buffer, bufferSize, fn)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -239,16 +247,11 @@ int SetupDriverCgroup(FILE *cgroupAllow)
|
||||
return -1;
|
||||
}
|
||||
|
||||
char devmmPath[PATH_MAX] = {0};
|
||||
char hisiPath[PATH_MAX] = {0};
|
||||
if ((sprintf_s(devmmPath, PATH_MAX, "/dev/%s", DEVMM_SVM) < 0) ||
|
||||
(sprintf_s(hisiPath, PATH_MAX, "/dev/%s", HISI_HDC) < 0)) {
|
||||
Logger("failed to assemble path.", LEVEL_ERROR, SCREEN_YES);
|
||||
bool is200Rc = false;
|
||||
if (!DoMounting200RC(&is200Rc)) {
|
||||
return -1;
|
||||
}
|
||||
struct stat devStat; // 200 soc 不需要挂载此两个设备
|
||||
if ((stat(devmmPath, &devStat) != 0) && (stat(hisiPath, &devStat) != 0)) {
|
||||
Logger("200 Soc.", LEVEL_ERROR, SCREEN_YES);
|
||||
if (is200Rc) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -324,36 +327,21 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SetupCgroup(const struct ParsedConfig *config)
|
||||
static bool SetupCgroupProcess(const char* resolvedCgroupPath, const struct ParsedConfig *config)
|
||||
{
|
||||
if (config == NULL) {
|
||||
(void)fprintf(stderr, "config pointer is null!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *str = NULL;
|
||||
char deviceName[BUF_SIZE] = {0};
|
||||
char resolvedCgroupPath[PATH_MAX] = {0};
|
||||
FILE *cgroupAllow = NULL;
|
||||
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
|
||||
Logger("cannot canonicalize cgroup.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
const size_t maxFileSzieMb = 1024; // max 1G
|
||||
if (!CheckExternalFile(resolvedCgroupPath, strlen(resolvedCgroupPath), maxFileSzieMb, true)) {
|
||||
Logger("Check file legality failed.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
|
||||
char deviceName[BUF_SIZE] = {0};
|
||||
cgroupAllow = fopen(resolvedCgroupPath, "a");
|
||||
if (cgroupAllow == NULL) {
|
||||
Logger("failed to open cgroup file.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SetupDriverCgroup(cgroupAllow) < 0) {
|
||||
fclose(cgroupAllow);
|
||||
Logger("failed to setup driver cgroup.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
for (size_t idx = 0; idx < config->devicesNr; idx++) {
|
||||
if (sprintf_s(deviceName, BUF_SIZE, "%s%u",
|
||||
@@ -363,16 +351,40 @@ int SetupCgroup(const struct ParsedConfig *config)
|
||||
str = FormatLogMessage("failed to assemble device path for no.%u.", config->devices[idx]);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
free(str);
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SetupDeviceCgroup(cgroupAllow, (const char *)deviceName) < 0) {
|
||||
fclose(cgroupAllow);
|
||||
Logger("failed to setup cgroup.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
free(str);
|
||||
fclose(cgroupAllow);
|
||||
return true;
|
||||
}
|
||||
|
||||
int SetupCgroup(const struct ParsedConfig *config)
|
||||
{
|
||||
if (config == NULL) {
|
||||
(void)fprintf(stderr, "config pointer is null!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char resolvedCgroupPath[PATH_MAX] = {0};
|
||||
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
|
||||
Logger("cannot canonicalize cgroup.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
const size_t maxFileSzieMb = 1024; // max 1G
|
||||
if (!CheckExternalFile(resolvedCgroupPath, strlen(resolvedCgroupPath), maxFileSzieMb, true)) {
|
||||
Logger("Check file legality failed.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
if (!SetupCgroupProcess(resolvedCgroupPath, config)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
167
cli/src/logger.c
167
cli/src/logger.c
@@ -59,6 +59,22 @@ int CreateLog(const char* filename)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long GetLogSizeProcess(const char* path)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
fp = fopen(path, "rb");
|
||||
long length = 0;
|
||||
if (fp != NULL) {
|
||||
fseek(fp, 0, SEEK_END);
|
||||
length = ftell(fp);
|
||||
}
|
||||
if (fp != NULL) {
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
long GetLogSize(const char* filename)
|
||||
{
|
||||
if (filename == NULL) {
|
||||
@@ -71,7 +87,7 @@ long GetLogSize(const char* filename)
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
FILE *fp = NULL;
|
||||
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
|
||||
return -1;
|
||||
@@ -83,17 +99,7 @@ long GetLogSize(const char* filename)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
fp = fopen(path, "rb");
|
||||
long length = 0;
|
||||
if (fp != NULL) {
|
||||
fseek(fp, 0, SEEK_END);
|
||||
length = ftell(fp);
|
||||
}
|
||||
if (NULL != fp) {
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
return length;
|
||||
return GetLogSizeProcess(path);
|
||||
}
|
||||
|
||||
|
||||
@@ -122,37 +128,13 @@ int LogLoop(const char* filename)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void WriteLogFile(const char* filename, long maxSize, const char* buffer, unsigned bufferSize)
|
||||
static void WriteLogInfo(const char* path, size_t pathLen, const char* buffer, const unsigned bufferSize)
|
||||
{
|
||||
if (filename == NULL || buffer == NULL) {
|
||||
(void)fprintf(stderr, "filename, buffer pointer is null!\n");
|
||||
if (path == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (filename != NULL && buffer != NULL) {
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
FILE *fp = NULL;
|
||||
int ret;
|
||||
long length = GetLogSize(filename);
|
||||
if (length < 0) {
|
||||
return;
|
||||
}
|
||||
if (length > maxSize) {
|
||||
ret = LogLoop(filename);
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
|
||||
return;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(path, &fileStat) == 0) && (S_ISREG(fileStat.st_mode) != 0)) {
|
||||
const size_t maxFileSzieMb = 50; // max 50MB
|
||||
if (!CheckExternalFile(path, strlen(path), maxFileSzieMb, true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ret = 0;
|
||||
fp = fopen(path, "a+");
|
||||
if (fp != NULL) {
|
||||
char now[TEMP_BUFFER] = {0};
|
||||
@@ -166,7 +148,85 @@ void WriteLogFile(const char* filename, long maxSize, const char* buffer, unsign
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static bool LogConvertStorage(const char* filename, const long maxSize)
|
||||
{
|
||||
long length = GetLogSize(filename);
|
||||
if (length < 0) {
|
||||
return false;
|
||||
}
|
||||
if (length > maxSize) {
|
||||
int ret = LogLoop(filename);
|
||||
if (ret < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void LogFileProcess(const char* filename, const long maxSize, const char* buffer, const unsigned bufferSize)
|
||||
{
|
||||
if (filename == NULL) {
|
||||
return;
|
||||
}
|
||||
int ret = 0;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
if (!LogConvertStorage(filename, maxSize)) {
|
||||
return;
|
||||
}
|
||||
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
|
||||
return;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(path, &fileStat) == 0) && (S_ISREG(fileStat.st_mode) != 0)) {
|
||||
const size_t maxFileSzieMb = 50; // max 50MB
|
||||
if (!CheckExternalFile(path, strlen(path), maxFileSzieMb, true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
WriteLogInfo(path, PATH_MAX + 1, buffer, bufferSize);
|
||||
}
|
||||
|
||||
void WriteLogFile(const char* filename, long maxSize, const char* buffer, unsigned bufferSize)
|
||||
{
|
||||
if (filename == NULL || buffer == NULL) {
|
||||
(void)fprintf(stderr, "filename, buffer pointer is null!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (filename != NULL && buffer != NULL) {
|
||||
LogFileProcess(filename, maxSize, buffer, bufferSize);
|
||||
}
|
||||
}
|
||||
|
||||
static void DivertAndWrite(const char *logPath, const char *msg, const int level)
|
||||
{
|
||||
int ret;
|
||||
char* buffer = malloc(LOG_LENGTH);
|
||||
if (buffer == NULL) {
|
||||
return;
|
||||
}
|
||||
switch (level) {
|
||||
case LEVEL_DEBUG:
|
||||
ret = sprintf_s(buffer, LOG_LENGTH, "[Debug]%s\n", msg);
|
||||
break;
|
||||
case LEVEL_ERROR:
|
||||
ret = sprintf_s(buffer, LOG_LENGTH, "[Error]%s\n", msg);
|
||||
break;
|
||||
case LEVEL_WARN:
|
||||
ret = sprintf_s(buffer, LOG_LENGTH, "[Warn]%s\n", msg);
|
||||
break;
|
||||
default:
|
||||
ret = sprintf_s(buffer, LOG_LENGTH, "[Info]%s\n", msg);
|
||||
}
|
||||
if (ret < 0) {
|
||||
free(buffer);
|
||||
return;
|
||||
}
|
||||
WriteLogFile(logPath, FILE_MAX_SIZE, buffer, strlen(buffer));
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void Logger(const char *msg, int level, int screen)
|
||||
@@ -182,31 +242,6 @@ void Logger(const char *msg, int level, int screen)
|
||||
if (MakeDirWithParent(LOG_PATH_DIR, DEFAULT_LOGDIR_MODE) < 0) {
|
||||
return;
|
||||
}
|
||||
int destMax = LOG_LENGTH;
|
||||
if (destMax <= 0) {
|
||||
return;
|
||||
}
|
||||
char* buffer = malloc(destMax);
|
||||
if (buffer == NULL) {
|
||||
return;
|
||||
}
|
||||
switch (level) {
|
||||
case LEVEL_DEBUG:
|
||||
ret = sprintf_s(buffer, destMax, "[Debug]%s\n", msg);
|
||||
break;
|
||||
case LEVEL_ERROR:
|
||||
ret = sprintf_s(buffer, destMax, "[Error]%s\n", msg);
|
||||
break;
|
||||
case LEVEL_WARN:
|
||||
ret = sprintf_s(buffer, destMax, "[Warn]%s\n", msg);
|
||||
break;
|
||||
default:
|
||||
ret = sprintf_s(buffer, destMax, "[Info]%s\n", msg);
|
||||
}
|
||||
if (ret < 0) {
|
||||
free(buffer);
|
||||
return;
|
||||
}
|
||||
WriteLogFile(logPath, FILE_MAX_SIZE, buffer, strlen(buffer));
|
||||
free(buffer);
|
||||
|
||||
DivertAndWrite(logPath, msg, level);
|
||||
}
|
||||
|
@@ -291,7 +291,6 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
|
||||
free(str);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
|
||||
if (ret < 0) {
|
||||
Logger("failed to get cgroup path.", LEVEL_ERROR, SCREEN_YES);
|
||||
|
@@ -16,6 +16,21 @@
|
||||
#include "options.h"
|
||||
#include "logger.h"
|
||||
|
||||
static bool checkSrcFile(const char *src)
|
||||
{
|
||||
struct stat fileStat;
|
||||
if ((stat(src, &fileStat) == 0) &&
|
||||
((S_ISREG(fileStat.st_mode) != 0) || (S_ISDIR(fileStat.st_mode) != 0))) { // 只校验文件和目录
|
||||
const size_t maxFileSzieMb = 10 * 1024; // max 10 G
|
||||
if (!CheckExternalFile(src, strlen(src), maxFileSzieMb, false)) {
|
||||
char* str = FormatLogMessage("failed to mount src: %s.", src);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int Mount(const char *src, const char *dst)
|
||||
{
|
||||
if (src == NULL || dst == NULL) {
|
||||
@@ -25,18 +40,10 @@ int Mount(const char *src, const char *dst)
|
||||
|
||||
static const unsigned long mountFlags = MS_BIND;
|
||||
static const unsigned long remountFlags = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID;
|
||||
int ret;
|
||||
struct stat fileStat;
|
||||
if ((stat(src, &fileStat) == 0) &&
|
||||
((S_ISREG(fileStat.st_mode) != 0) || (S_ISDIR(fileStat.st_mode) != 0))) { // 只校验文件和目录
|
||||
const size_t maxFileSzieMb = 10 * 1024; // max 10 G
|
||||
if (!CheckExternalFile(src, strlen(src), maxFileSzieMb, false)) {
|
||||
char* str = FormatLogMessage("failed to mount src: %s.", src);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
if (!checkSrcFile(src)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
ret = mount(src, dst, NULL, mountFlags, NULL);
|
||||
int ret = mount(src, dst, NULL, mountFlags, NULL);
|
||||
if (ret < 0) {
|
||||
Logger("failed to mount src.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
@@ -51,24 +58,26 @@ int Mount(const char *src, const char *dst)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
|
||||
const char *dstDeviceName, struct PathInfo* pathInfo)
|
||||
static bool GetDeviceCheckParam(const char *rootfs, const char *srcDeviceName,
|
||||
struct PathInfo* pathInfo)
|
||||
{
|
||||
if (rootfs == NULL || srcDeviceName == NULL || pathInfo == NULL) {
|
||||
Logger("rootfs, srcDeviceName, pathInfo pointer are null!", LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int GetDeviceMntSrcDstProcess(const char *rootfs, const char *srcDeviceName,
|
||||
const char *dstDeviceName, struct PathInfo* pathInfo)
|
||||
{
|
||||
if (!GetDeviceCheckParam(rootfs, srcDeviceName, pathInfo)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret;
|
||||
int ret = 0;
|
||||
errno_t err;
|
||||
char unresolvedDst[BUF_SIZE] = {0};
|
||||
char resolvedDst[PATH_MAX] = {0};
|
||||
|
||||
ret = VerifyPathInfo(pathInfo);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t srcBufSize = pathInfo->srcLen;
|
||||
size_t dstBufSize = pathInfo->dstLen;
|
||||
char *src = pathInfo->src;
|
||||
@@ -98,14 +107,56 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
|
||||
const char *dstDeviceName, struct PathInfo* pathInfo)
|
||||
{
|
||||
if (!GetDeviceCheckParam(rootfs, srcDeviceName, pathInfo)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = VerifyPathInfo(pathInfo);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return GetDeviceMntSrcDstProcess(rootfs, srcDeviceName, dstDeviceName, pathInfo);
|
||||
}
|
||||
|
||||
static bool MountDeviceProcess(const char* dst, const char* src, const mode_t mode)
|
||||
{
|
||||
int ret;
|
||||
errno = 0;
|
||||
struct stat dstStat;
|
||||
ret = stat(dst, &dstStat);
|
||||
if (ret == 0 && S_ISCHR(dstStat.st_mode)) {
|
||||
return 0; // 特权容器自动挂载HOST所有设备,故此处跳过
|
||||
} else if (ret == 0) {
|
||||
Logger("dst already exists but not a char device as expected.", LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
} else if (ret < 0 && errno != ENOENT) {
|
||||
Logger("failed to check dst stat", LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
}
|
||||
ret = MakeMountPoints(dst, mode);
|
||||
if (ret < 0) {
|
||||
Logger("failed to create mount dst file.", LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = Mount(src, dst);
|
||||
if (ret < 0) {
|
||||
Logger("failed to mount dev.", LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDeviceName)
|
||||
{
|
||||
int ret;
|
||||
char *str = NULL;
|
||||
char src[BUF_SIZE] = {0};
|
||||
char dst[BUF_SIZE] = {0};
|
||||
struct PathInfo pathInfo = {src, BUF_SIZE, dst, BUF_SIZE};
|
||||
@@ -121,30 +172,10 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
|
||||
Logger("failed to stat src.", LEVEL_ERROR, SCREEN_YES);
|
||||
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) {
|
||||
Logger("dst already exists but not a char device as expected.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
} else if (ret < 0 && errno != ENOENT) {
|
||||
Logger("failed to check dst stat", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
ret = MakeMountPoints(dst, srcStat.st_mode);
|
||||
if (ret < 0) {
|
||||
Logger("failed to create mount dst file.", LEVEL_ERROR, SCREEN_YES);
|
||||
if (!MountDeviceProcess(dst, src, srcStat.st_mode)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Mount(src, dst);
|
||||
if (ret < 0) {
|
||||
Logger("failed to mount dev.", LEVEL_ERROR, SCREEN_YES);
|
||||
return -1;
|
||||
}
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -250,6 +281,43 @@ int MountDir(const char *rootfs, const char *src)
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool DoMounting200RC(bool* is200Rc)
|
||||
{
|
||||
char devmmPath[PATH_MAX] = {0};
|
||||
char hisiPath[PATH_MAX] = {0};
|
||||
if ((sprintf_s(devmmPath, PATH_MAX, "/dev/%s", DEVMM_SVM) < 0) ||
|
||||
(sprintf_s(hisiPath, PATH_MAX, "/dev/%s", HISI_HDC) < 0)) {
|
||||
Logger("failed to assemble dev path.", LEVEL_ERROR, SCREEN_YES);
|
||||
return false;
|
||||
}
|
||||
struct stat devStat; // 200 soc 不需要挂载此两个设备
|
||||
if ((stat(devmmPath, &devStat) != 0) && (stat(hisiPath, &devStat) != 0)) {
|
||||
Logger("200 Soc.", LEVEL_ERROR, SCREEN_YES);
|
||||
*is200Rc = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool DoMountingDevice(const char *rootfs)
|
||||
{
|
||||
int ret = MountDevice(rootfs, DEVMM_SVM, NULL);
|
||||
if (ret < 0) {
|
||||
char* str = FormatLogMessage("failed to mount device %s.", DEVMM_SVM);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
free(str);
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = MountDevice(rootfs, HISI_HDC, NULL);
|
||||
if (ret < 0) {
|
||||
char* str = FormatLogMessage("failed to mount device %s.", HISI_HDC);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
free(str);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int DoCtrlDeviceMounting(const char *rootfs)
|
||||
{
|
||||
if (rootfs == NULL) {
|
||||
@@ -265,33 +333,14 @@ int DoCtrlDeviceMounting(const char *rootfs)
|
||||
free(str);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char devmmPath[PATH_MAX] = {0};
|
||||
char hisiPath[PATH_MAX] = {0};
|
||||
if ((sprintf_s(devmmPath, PATH_MAX, "/dev/%s", DEVMM_SVM) < 0) ||
|
||||
(sprintf_s(hisiPath, PATH_MAX, "/dev/%s", HISI_HDC) < 0)) {
|
||||
Logger("failed to assemble dev path.", LEVEL_ERROR, SCREEN_YES);
|
||||
bool is200Rc = false;
|
||||
if (!DoMounting200RC(&is200Rc)) {
|
||||
return -1;
|
||||
}
|
||||
struct stat devStat; // 200 soc 不需要挂载此两个设备
|
||||
if ((stat(devmmPath, &devStat) != 0) && (stat(hisiPath, &devStat) != 0)) {
|
||||
Logger("200 Soc.", LEVEL_ERROR, SCREEN_YES);
|
||||
if (is200Rc) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = MountDevice(rootfs, DEVMM_SVM, NULL);
|
||||
if (ret < 0) {
|
||||
char* str = FormatLogMessage("failed to mount device %s.", DEVMM_SVM);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
free(str);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = MountDevice(rootfs, HISI_HDC, NULL);
|
||||
if (ret < 0) {
|
||||
char* str = FormatLogMessage("failed to mount device %s.", HISI_HDC);
|
||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||
free(str);
|
||||
if (!DoMountingDevice(rootfs)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@@ -5,8 +5,10 @@
|
||||
#ifndef _MOUNT_H
|
||||
#define _MOUNT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "basic.h"
|
||||
|
||||
int DoMounting(const struct ParsedConfig *config);
|
||||
bool DoMounting200RC(bool* is200Rc);
|
||||
|
||||
#endif
|
@@ -185,28 +185,27 @@ static bool ShowExceptionInfo(const char* exceptionInfo)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
const unsigned long long maxFileSzieMb, const bool checkOwner)
|
||||
static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
|
||||
{
|
||||
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
|
||||
char buf[PATH_MAX] = {0};
|
||||
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) {
|
||||
return false;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(buf, &fileStat) != 0) ||
|
||||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
|
||||
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
|
||||
}
|
||||
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
|
||||
return ShowExceptionInfo("fileSize out of bounds!");
|
||||
}
|
||||
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
|
||||
if (checkOwner) {
|
||||
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
||||
return ShowExceptionInfo("Please check the folder owner!");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckParentDir(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
struct stat fileStat, const bool checkOwner)
|
||||
{
|
||||
char buf[PATH_MAX] = {0};
|
||||
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) {
|
||||
return false;
|
||||
}
|
||||
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
|
||||
if (!CheckFileOwner(fileStat, checkOwner)) {
|
||||
return false;
|
||||
}
|
||||
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
|
||||
return ShowExceptionInfo("Please check the write permission!");
|
||||
}
|
||||
@@ -223,6 +222,35 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
const unsigned long long maxFileSzieMb, const bool checkOwner)
|
||||
{
|
||||
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
|
||||
struct stat fileStat;
|
||||
if ((stat(resolvedPath, &fileStat) != 0) ||
|
||||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
|
||||
(void)fprintf(stderr, "[3: %s]\n", resolvedPath);
|
||||
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
|
||||
}
|
||||
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
|
||||
return ShowExceptionInfo("fileSize out of bounds!");
|
||||
}
|
||||
return CheckParentDir(resolvedPath, resolvedPathLen, fileStat, checkOwner);
|
||||
}
|
||||
|
||||
static bool IsValidChar(const char c)
|
||||
{
|
||||
if (isalnum(c) != 0) {
|
||||
return true;
|
||||
}
|
||||
// ._-/~为合法字符
|
||||
if ((c == '.') || (c == '_') ||
|
||||
(c == '-') || (c == '/') || (c == '~')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||
const size_t maxFileSzieMb, const bool checkOwner)
|
||||
{
|
||||
@@ -230,12 +258,8 @@ bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
||||
return ShowExceptionInfo("filePathLen out of bounds!");
|
||||
}
|
||||
if (strstr(filePath, "..") != NULL) { // 存在".."
|
||||
return ShowExceptionInfo("filePath has an illegal character!");
|
||||
}
|
||||
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
||||
if ((isalnum(filePath[iLoop]) == 0) && (filePath[iLoop] != '.') && (filePath[iLoop] != '_') &&
|
||||
(filePath[iLoop] != '-') && (filePath[iLoop] != '/') && (filePath[iLoop] != '~')) { // 非法字符
|
||||
if (!IsValidChar(filePath[iLoop])) { // 非法字符
|
||||
return ShowExceptionInfo("filePath has an illegal character!");
|
||||
}
|
||||
}
|
||||
|
@@ -33,28 +33,25 @@ static bool ShowExceptionInfo(const char* exceptionInfo)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
const unsigned long long maxFileSzieMb, const bool checkOwner)
|
||||
static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
|
||||
{
|
||||
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
|
||||
char buf[PATH_MAX] = {0};
|
||||
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) {
|
||||
return false;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(buf, &fileStat) != 0) ||
|
||||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
|
||||
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
|
||||
}
|
||||
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
|
||||
return ShowExceptionInfo("fileSize out of bounds!");
|
||||
}
|
||||
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
|
||||
if (checkOwner) {
|
||||
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
||||
return ShowExceptionInfo("Please check the folder owner!");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckParentDir(char* buf, const size_t bufLen, struct stat fileStat, const bool checkOwner)
|
||||
{
|
||||
if (buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
|
||||
if (!CheckFileOwner(fileStat, checkOwner)) {
|
||||
return false;
|
||||
}
|
||||
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
|
||||
return ShowExceptionInfo("Please check the write permission!");
|
||||
}
|
||||
@@ -71,22 +68,62 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||
const size_t maxFileSzieMb, const bool checkOwner)
|
||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
const unsigned long long maxFileSzieMb, const bool checkOwner)
|
||||
{
|
||||
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
|
||||
char buf[PATH_MAX] = {0};
|
||||
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) {
|
||||
return false;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(buf, &fileStat) != 0) ||
|
||||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
|
||||
(void)fprintf(stderr, "[2: %s]\n", buf);
|
||||
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
|
||||
}
|
||||
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
|
||||
return ShowExceptionInfo("fileSize out of bounds!");
|
||||
}
|
||||
return CheckParentDir(buf, PATH_MAX, fileStat, checkOwner);
|
||||
}
|
||||
|
||||
static bool IsValidChar(const char c)
|
||||
{
|
||||
if (isalnum(c) != 0) {
|
||||
return true;
|
||||
}
|
||||
// ._-/~为合法字符
|
||||
if ((c == '.') || (c == '_') ||
|
||||
(c == '-') || (c == '/') || (c == '~')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool CheckFileName(const char* filePath, const size_t filePathLen)
|
||||
{
|
||||
int iLoop;
|
||||
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
||||
return ShowExceptionInfo("filePathLen out of bounds!");
|
||||
}
|
||||
if (strstr(filePath, "..") != NULL) { // 存在".."
|
||||
return ShowExceptionInfo("filePath has an illegal character!");
|
||||
}
|
||||
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
||||
if ((isalnum(filePath[iLoop]) == 0) && (filePath[iLoop] != '.') && (filePath[iLoop] != '_') &&
|
||||
(filePath[iLoop] != '-') && (filePath[iLoop] != '/') && (filePath[iLoop] != '~')) { // 非法字符
|
||||
if (!IsValidChar(filePath[iLoop])) { // 非法字符
|
||||
return ShowExceptionInfo("filePath has an illegal character!");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||
const size_t maxFileSzieMb, const bool checkOwner)
|
||||
{
|
||||
if (filePath == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (!CheckFileName(filePath, filePathLen)) {
|
||||
return false;
|
||||
}
|
||||
char resolvedPath[PATH_MAX] = {0};
|
||||
if (realpath(filePath, resolvedPath) == NULL && errno != ENOENT) {
|
||||
return ShowExceptionInfo("realpath failed!");
|
||||
@@ -139,55 +176,99 @@ static void DcmiDlclose(void **handle)
|
||||
}
|
||||
}
|
||||
|
||||
static int DestroyEntrance(const char *argv[])
|
||||
static bool CheckLimitId(const int IdValue)
|
||||
{
|
||||
if (argv == NULL) {
|
||||
return -1;
|
||||
if (IdValue < 0 || IdValue > ID_MAX) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool GetAndCheckID(const char *argv[], int *cardId,
|
||||
int *deviceId, int *vDeviceId)
|
||||
{
|
||||
errno = 0;
|
||||
int cardId = atoi(argv[PARAMS_SECOND]);
|
||||
if ((errno != 0) || (cardId < 0) || (cardId > ID_MAX)) {
|
||||
return -1;
|
||||
*cardId = atoi(argv[PARAMS_SECOND]);
|
||||
if ((errno != 0) || !CheckLimitId(*cardId)) {
|
||||
return false;
|
||||
}
|
||||
int deviceId = atoi(argv[PARAMS_THIRD]);
|
||||
if ((errno != 0) || (deviceId < 0) || (deviceId > ID_MAX)) {
|
||||
return -1;
|
||||
*deviceId = atoi(argv[PARAMS_THIRD]);
|
||||
if ((errno != 0) || !CheckLimitId(*deviceId)) {
|
||||
return false;
|
||||
}
|
||||
int vDeviceId = atoi(argv[PARAMS_FOURTH]);
|
||||
if ((errno != 0) || (vDeviceId < 0) || (vDeviceId > ID_MAX)) {
|
||||
return -1;
|
||||
*vDeviceId = atoi(argv[PARAMS_FOURTH]);
|
||||
if ((errno != 0) || !CheckLimitId(*vDeviceId)) {
|
||||
return false;
|
||||
}
|
||||
void *handle = NULL;
|
||||
if (!DeclareDcmiApiAndCheck(&handle)) {
|
||||
(void)fprintf(stderr, "Declare dcmi failed.\n");
|
||||
return -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool DcmiInitProcess(void *handle)
|
||||
{
|
||||
if (handle == NULL) {
|
||||
return false;
|
||||
}
|
||||
int (*dcmi_init)(void) = NULL;
|
||||
int (*dcmi_set_destroy_vdevice)(int, int, int) = NULL;
|
||||
dcmi_init = dlsym(handle, DCMI_INIT);
|
||||
if (dcmi_init == NULL) {
|
||||
DcmiDlAbnormalExit(&handle, "DeclareDlApi failed");
|
||||
return -1;
|
||||
}
|
||||
dcmi_set_destroy_vdevice = dlsym(handle, DCMI_SET_DESTROY_VDEVICE);
|
||||
if (dcmi_set_destroy_vdevice == NULL) {
|
||||
DcmiDlAbnormalExit(&handle, "DeclareDlApi failed");
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
int ret = dcmi_init();
|
||||
if (ret != 0) {
|
||||
(void)fprintf(stderr, "dcmi_init failed, ret = %d\n", ret);
|
||||
DcmiDlclose(&handle);
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
ret = dcmi_set_destroy_vdevice(cardId, deviceId, vDeviceId);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool DcmiDestroyProcess(void *handle, const int cardId,
|
||||
const int deviceId, const int vDeviceId)
|
||||
{
|
||||
if (handle == NULL) {
|
||||
return false;
|
||||
}
|
||||
int (*dcmi_set_destroy_vdevice)(int, int, int) = NULL;
|
||||
dcmi_set_destroy_vdevice = dlsym(handle, DCMI_SET_DESTROY_VDEVICE);
|
||||
if (dcmi_set_destroy_vdevice == NULL) {
|
||||
DcmiDlAbnormalExit(&handle, "DeclareDlApi failed");
|
||||
return false;
|
||||
}
|
||||
int ret = dcmi_set_destroy_vdevice(cardId, deviceId, vDeviceId);
|
||||
if (ret != 0) {
|
||||
(void)fprintf(stderr, "dcmi_set_destroy_vdevice failed, ret = %d\n", ret);
|
||||
DcmiDlclose(&handle);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int DestroyEntrance(const char *argv[])
|
||||
{
|
||||
if (argv == NULL) {
|
||||
return -1;
|
||||
}
|
||||
int cardId = 0;
|
||||
int deviceId = 0;
|
||||
int vDeviceId = 0;
|
||||
if (!GetAndCheckID(argv, &cardId, &deviceId, &vDeviceId)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *handle = NULL;
|
||||
if (!DeclareDcmiApiAndCheck(&handle)) {
|
||||
(void)fprintf(stderr, "Declare dcmi failed.\n");
|
||||
return -1;
|
||||
}
|
||||
if (!DcmiInitProcess(handle)) {
|
||||
return -1;
|
||||
}
|
||||
if (!DcmiDestroyProcess(handle, cardId, deviceId, vDeviceId)) {
|
||||
return -1;
|
||||
}
|
||||
DcmiDlclose(&handle);
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool EntryCheck(const int argc, const char *argv[])
|
||||
|
@@ -180,6 +180,50 @@ static cJSON *CreateContent(const char *runtimePath)
|
||||
return root;
|
||||
}
|
||||
|
||||
static bool AddAscendRuntime(cJSON **root, const char *runtimePath)
|
||||
{
|
||||
cJSON *runtimes = NULL;
|
||||
runtimes = cJSON_GetObjectItem(*root, "runtimes");
|
||||
if (runtimes == NULL) {
|
||||
runtimes = CreateRuntimes(runtimePath);
|
||||
if (runtimes == NULL) {
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
cJSON_AddItemToObject(*root, RUNTIME_KEY, runtimes);
|
||||
} else {
|
||||
int ret = DelJsonContent(runtimes, ASCEND_RUNTIME_NAME);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
cJSON *ascendRuntime = NULL;
|
||||
ascendRuntime = CreateAscendRuntimeInfo(runtimePath);
|
||||
if (ascendRuntime == NULL) {
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
cJSON_AddItemToObject(runtimes, ASCEND_RUNTIME_NAME, ascendRuntime);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool AddDefaultRuntime(cJSON **root)
|
||||
{
|
||||
int ret = DelJsonContent(*root, DEFALUT_KEY);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
cJSON *defaultRuntime = cJSON_CreateString(DEFAULT_VALUE);
|
||||
if (defaultRuntime == NULL) {
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
cJSON_AddItemToObject(*root, DEFALUT_KEY, defaultRuntime);
|
||||
return true;
|
||||
}
|
||||
|
||||
static cJSON *ModifyContent(FILE *pf, const char *runtimePath)
|
||||
{
|
||||
if (pf == NULL || runtimePath == NULL) {
|
||||
@@ -198,42 +242,14 @@ static cJSON *ModifyContent(FILE *pf, const char *runtimePath)
|
||||
}
|
||||
|
||||
/* 插入ascend runtime */
|
||||
cJSON *runtimes = NULL;
|
||||
runtimes = cJSON_GetObjectItem(root, "runtimes");
|
||||
if (runtimes == NULL) {
|
||||
runtimes = CreateRuntimes(runtimePath);
|
||||
if (runtimes == NULL) {
|
||||
cJSON_Delete(root);
|
||||
if (!AddAscendRuntime(&root, runtimePath)) {
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToObject(root, RUNTIME_KEY, runtimes);
|
||||
} else {
|
||||
int ret = DelJsonContent(runtimes, ASCEND_RUNTIME_NAME);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(root);
|
||||
return NULL;
|
||||
}
|
||||
cJSON *ascendRuntime = NULL;
|
||||
ascendRuntime = CreateAscendRuntimeInfo(runtimePath);
|
||||
if (ascendRuntime == NULL) {
|
||||
cJSON_Delete(root);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToObject(runtimes, ASCEND_RUNTIME_NAME, ascendRuntime);
|
||||
}
|
||||
|
||||
/* 插入defaul runtime */
|
||||
int ret = DelJsonContent(root, DEFALUT_KEY);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(root);
|
||||
if (!AddDefaultRuntime(&root)) {
|
||||
return NULL;
|
||||
}
|
||||
cJSON *defaultRuntime = cJSON_CreateString(DEFAULT_VALUE);
|
||||
if (defaultRuntime == NULL) {
|
||||
cJSON_Delete(root);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToObject(root, DEFALUT_KEY, defaultRuntime);
|
||||
|
||||
return root;
|
||||
}
|
||||
@@ -287,28 +303,25 @@ static bool ShowExceptionInfo(const char* exceptionInfo)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
const unsigned long long maxFileSzieMb, const bool checkOwner)
|
||||
static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
|
||||
{
|
||||
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
|
||||
char buf[PATH_MAX] = {0};
|
||||
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) {
|
||||
return false;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(buf, &fileStat) != 0) ||
|
||||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
|
||||
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
|
||||
}
|
||||
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
|
||||
return ShowExceptionInfo("fileSize out of bounds!");
|
||||
}
|
||||
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
|
||||
if (checkOwner) {
|
||||
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
||||
return ShowExceptionInfo("Please check the folder owner!");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckParentDir(char* buf, const size_t bufLen, struct stat fileStat, const bool checkOwner)
|
||||
{
|
||||
if (buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
|
||||
if (!CheckFileOwner(fileStat, checkOwner)) {
|
||||
return false;
|
||||
}
|
||||
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
|
||||
return ShowExceptionInfo("Please check the write permission!");
|
||||
}
|
||||
@@ -325,6 +338,39 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||
const unsigned long long maxFileSzieMb, const bool checkOwner)
|
||||
{
|
||||
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
|
||||
char buf[PATH_MAX] = {0};
|
||||
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) {
|
||||
return false;
|
||||
}
|
||||
struct stat fileStat;
|
||||
if ((stat(buf, &fileStat) != 0) ||
|
||||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
|
||||
(void)fprintf(stderr, "[1: %s]\n", buf);
|
||||
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
|
||||
}
|
||||
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
|
||||
return ShowExceptionInfo("fileSize out of bounds!");
|
||||
}
|
||||
return CheckParentDir(buf, PATH_MAX, fileStat, checkOwner);
|
||||
}
|
||||
|
||||
static bool IsValidChar(const char c)
|
||||
{
|
||||
if (isalnum(c) != 0) {
|
||||
return true;
|
||||
}
|
||||
// ._-/~为合法字符
|
||||
if ((c == '.') || (c == '_') ||
|
||||
(c == '-') || (c == '/') || (c == '~')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||
const size_t maxFileSzieMb, const bool checkOwner)
|
||||
{
|
||||
@@ -332,12 +378,8 @@ static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
||||
return ShowExceptionInfo("filePathLen out of bounds!");
|
||||
}
|
||||
if (strstr(filePath, "..") != NULL) { // 存在".."
|
||||
return ShowExceptionInfo("filePath has an illegal character!");
|
||||
}
|
||||
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
||||
if ((isalnum(filePath[iLoop]) == 0) && (filePath[iLoop] != '.') && (filePath[iLoop] != '_') &&
|
||||
(filePath[iLoop] != '-') && (filePath[iLoop] != '/') && (filePath[iLoop] != '~')) { // 非法字符
|
||||
if (!IsValidChar(filePath[iLoop])) { // 非法字符
|
||||
return ShowExceptionInfo("filePath has an illegal character!");
|
||||
}
|
||||
}
|
||||
@@ -360,102 +402,145 @@ static bool CheckJsonFile(const char *jsonFilePath, const size_t jsonFilePathLen
|
||||
return true;
|
||||
}
|
||||
|
||||
static int DetectAndCreateJsonFile(const char *filePath, const char *tempPath, const char *runtimePath)
|
||||
static bool CheckJsonFileParam(const char *filePath, const char *tempPath, const char *runtimePath)
|
||||
{
|
||||
if (filePath == NULL || tempPath == NULL || runtimePath == NULL) {
|
||||
(void)fprintf(stderr, "filePath, tempPath or runtimePath are null!\n");
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CheckJsonFile(filePath, strlen(filePath)) || !CheckJsonFile(tempPath, strlen(tempPath)) ||
|
||||
!CheckJsonFile(runtimePath, strlen(runtimePath))) {
|
||||
(void)fprintf(stderr, "filePath, tempPath or runtimePath check failed!\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool GetJsonRoot(cJSON **root, const char *filePath, const char *runtimePath)
|
||||
{
|
||||
FILE *pf = NULL;
|
||||
pf = fopen(filePath, "r+");
|
||||
if (pf == NULL) {
|
||||
*root = CreateContent(runtimePath);
|
||||
} else {
|
||||
*root = ModifyContent(pf, runtimePath);
|
||||
fclose(pf);
|
||||
}
|
||||
|
||||
if (*root == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create json\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool WriteJsonRoot(cJSON **root, const char *tempPath)
|
||||
{
|
||||
FILE *pf = NULL;
|
||||
pf = fopen(tempPath, "w");
|
||||
if (pf == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create file\n");
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fprintf(pf, "%s", cJSON_Print(*root)) < 0) {
|
||||
(void)fprintf(stderr, "error: failed to create file\n");
|
||||
(void)fclose(pf);
|
||||
cJSON_Delete(*root);
|
||||
return false;
|
||||
}
|
||||
(void)fclose(pf);
|
||||
|
||||
cJSON_Delete(*root);
|
||||
return true;
|
||||
}
|
||||
|
||||
static int DetectAndCreateJsonFile(const char *filePath, const char *tempPath, const char *runtimePath)
|
||||
{
|
||||
if (!CheckJsonFileParam(filePath, tempPath, runtimePath)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON *root = NULL;
|
||||
FILE *pf = NULL;
|
||||
pf = fopen(filePath, "r+");
|
||||
if (pf == NULL) {
|
||||
root = CreateContent(runtimePath);
|
||||
} else {
|
||||
root = ModifyContent(pf, runtimePath);
|
||||
fclose(pf);
|
||||
}
|
||||
|
||||
if (root == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create json\n");
|
||||
if (!GetJsonRoot(&root, filePath, runtimePath) || root == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pf = fopen(tempPath, "w");
|
||||
if (pf == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create file\n");
|
||||
cJSON_Delete(root);
|
||||
if (!WriteJsonRoot(&root, tempPath)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fprintf(pf, "%s", cJSON_Print(root)) < 0) {
|
||||
(void)fprintf(stderr, "error: failed to create file\n");
|
||||
(void)fclose(pf);
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
(void)fclose(pf);
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CreateRevisedJsonFile(const char *filePath, const char *tempPath)
|
||||
static bool CheckRevisedJsonFileParam(const char *filePath, const char *tempPath)
|
||||
{
|
||||
if (filePath == NULL || tempPath == NULL) {
|
||||
(void)fprintf(stderr, "filePath or tempPath are null!\n");
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CheckJsonFile(filePath, strlen(filePath)) || !CheckJsonFile(tempPath, strlen(tempPath))) {
|
||||
(void)fprintf(stderr, "filePath, tempPath check failed!\n");
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool GetNewContent(const char *filePath, cJSON **newContent)
|
||||
{
|
||||
FILE *pf = NULL;
|
||||
pf = fopen(filePath, "r+");
|
||||
if (pf == NULL) {
|
||||
(void)fprintf(stderr, "error: no json files found\n");
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
cJSON *newContent = NULL;
|
||||
newContent = RemoveContent(pf);
|
||||
*newContent = RemoveContent(pf);
|
||||
(void)fclose(pf);
|
||||
pf = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (newContent == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create json\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool WriteNewContent(const char *tempPath, cJSON **newContent)
|
||||
{
|
||||
FILE *pf = NULL;
|
||||
pf = fopen(tempPath, "w");
|
||||
if (pf == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create file\n");
|
||||
cJSON_Delete(newContent);
|
||||
newContent = NULL;
|
||||
return -1;
|
||||
cJSON_Delete(*newContent);
|
||||
*newContent = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fprintf(pf, "%s", cJSON_Print(newContent)) < 0) {
|
||||
if (fprintf(pf, "%s", cJSON_Print(*newContent)) < 0) {
|
||||
(void)fprintf(stderr, "error: failed to create file\n");
|
||||
cJSON_Delete(newContent);
|
||||
newContent = NULL;
|
||||
cJSON_Delete(*newContent);
|
||||
*newContent = NULL;
|
||||
(void)fclose(pf);
|
||||
pf = NULL;
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
(void)fclose(pf);
|
||||
pf = NULL;
|
||||
cJSON_Delete(newContent);
|
||||
newContent = NULL;
|
||||
cJSON_Delete(*newContent);
|
||||
*newContent = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int CreateRevisedJsonFile(const char *filePath, const char *tempPath)
|
||||
{
|
||||
if (!CheckRevisedJsonFileParam(filePath, tempPath)) {
|
||||
return -1;
|
||||
}
|
||||
cJSON *newContent = NULL;
|
||||
if (!GetNewContent(filePath, &newContent) || newContent == NULL) {
|
||||
(void)fprintf(stderr, "error: failed to create json\n");
|
||||
return -1;
|
||||
}
|
||||
if (!WriteNewContent(tempPath, &newContent)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user