mirror of
https://github.com/Ascend/ascend-docker-runtime.git
synced 2025-10-11 23:10:05 +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 "securec.h"
|
||||||
|
|
||||||
|
#include "u_mount.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
@@ -82,34 +83,17 @@ bool CheckSubStr(char **pLine, const char *subsys)
|
|||||||
|
|
||||||
return strstr(substr, subsys) != NULL;
|
return strstr(substr, subsys) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef char *(*ParseFileLine)(char *, const char *);
|
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;
|
FILE *fp = NULL;
|
||||||
char *result = NULL;
|
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
char resolvedPath[PATH_MAX] = {0x0};
|
char *result = NULL;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
fp = fopen(resolvedPath, "r");
|
fp = fopen(resolvedPath, "r");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
Logger("cannot open file.", LEVEL_ERROR, SCREEN_YES);
|
Logger("cannot open file.", LEVEL_ERROR, SCREEN_YES);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (getline(&line, &len, fp) != -1) {
|
while (getline(&line, &len, fp) != -1) {
|
||||||
@@ -123,6 +107,30 @@ int ParseFileByLine(char* buffer, int bufferSize, const ParseFileLine fn, const
|
|||||||
free(line);
|
free(line);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if (ret != EOK) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,16 +247,11 @@ int SetupDriverCgroup(FILE *cgroupAllow)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char devmmPath[PATH_MAX] = {0};
|
bool is200Rc = false;
|
||||||
char hisiPath[PATH_MAX] = {0};
|
if (!DoMounting200RC(&is200Rc)) {
|
||||||
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);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
struct stat devStat; // 200 soc 不需要挂载此两个设备
|
if (is200Rc) {
|
||||||
if ((stat(devmmPath, &devStat) != 0) && (stat(hisiPath, &devStat) != 0)) {
|
|
||||||
Logger("200 Soc.", LEVEL_ERROR, SCREEN_YES);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,36 +327,21 @@ int GetCgroupPath(int pid, char *effPath, size_t maxSize)
|
|||||||
return 0;
|
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 *str = NULL;
|
||||||
char deviceName[BUF_SIZE] = {0};
|
|
||||||
char resolvedCgroupPath[PATH_MAX] = {0};
|
|
||||||
FILE *cgroupAllow = NULL;
|
FILE *cgroupAllow = NULL;
|
||||||
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
|
char deviceName[BUF_SIZE] = {0};
|
||||||
Logger("cannot canonicalize cgroup.", LEVEL_ERROR, SCREEN_YES);
|
cgroupAllow = fopen(resolvedCgroupPath, "a");
|
||||||
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");
|
|
||||||
if (cgroupAllow == NULL) {
|
if (cgroupAllow == NULL) {
|
||||||
Logger("failed to open cgroup file.", LEVEL_ERROR, SCREEN_YES);
|
Logger("failed to open cgroup file.", LEVEL_ERROR, SCREEN_YES);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SetupDriverCgroup(cgroupAllow) < 0) {
|
if (SetupDriverCgroup(cgroupAllow) < 0) {
|
||||||
fclose(cgroupAllow);
|
fclose(cgroupAllow);
|
||||||
Logger("failed to setup driver cgroup.", LEVEL_ERROR, SCREEN_YES);
|
Logger("failed to setup driver cgroup.", LEVEL_ERROR, SCREEN_YES);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
for (size_t idx = 0; idx < config->devicesNr; idx++) {
|
for (size_t idx = 0; idx < config->devicesNr; idx++) {
|
||||||
if (sprintf_s(deviceName, BUF_SIZE, "%s%u",
|
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]);
|
str = FormatLogMessage("failed to assemble device path for no.%u.", config->devices[idx]);
|
||||||
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
Logger(str, LEVEL_ERROR, SCREEN_YES);
|
||||||
free(str);
|
free(str);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SetupDeviceCgroup(cgroupAllow, (const char *)deviceName) < 0) {
|
if (SetupDeviceCgroup(cgroupAllow, (const char *)deviceName) < 0) {
|
||||||
fclose(cgroupAllow);
|
fclose(cgroupAllow);
|
||||||
Logger("failed to setup cgroup.", LEVEL_ERROR, SCREEN_YES);
|
Logger("failed to setup cgroup.", LEVEL_ERROR, SCREEN_YES);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(str);
|
free(str);
|
||||||
fclose(cgroupAllow);
|
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;
|
return 0;
|
||||||
}
|
}
|
167
cli/src/logger.c
167
cli/src/logger.c
@@ -59,6 +59,22 @@ int CreateLog(const char* filename)
|
|||||||
return 0;
|
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)
|
long GetLogSize(const char* filename)
|
||||||
{
|
{
|
||||||
if (filename == NULL) {
|
if (filename == NULL) {
|
||||||
@@ -71,7 +87,7 @@ long GetLogSize(const char* filename)
|
|||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
FILE *fp = NULL;
|
|
||||||
char path[PATH_MAX + 1] = {0x00};
|
char path[PATH_MAX + 1] = {0x00};
|
||||||
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
|
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -83,17 +99,7 @@ long GetLogSize(const char* filename)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fp = fopen(path, "rb");
|
return GetLogSizeProcess(path);
|
||||||
long length = 0;
|
|
||||||
if (fp != NULL) {
|
|
||||||
fseek(fp, 0, SEEK_END);
|
|
||||||
length = ftell(fp);
|
|
||||||
}
|
|
||||||
if (NULL != fp) {
|
|
||||||
fclose(fp);
|
|
||||||
fp = NULL;
|
|
||||||
}
|
|
||||||
return length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -122,37 +128,13 @@ int LogLoop(const char* filename)
|
|||||||
return ret;
|
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) {
|
if (path == NULL) {
|
||||||
(void)fprintf(stderr, "filename, buffer pointer is null!\n");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename != NULL && buffer != NULL) {
|
|
||||||
char path[PATH_MAX + 1] = {0x00};
|
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
int ret;
|
int ret = 0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fp = fopen(path, "a+");
|
fp = fopen(path, "a+");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
char now[TEMP_BUFFER] = {0};
|
char now[TEMP_BUFFER] = {0};
|
||||||
@@ -166,7 +148,85 @@ void WriteLogFile(const char* filename, long maxSize, const char* buffer, unsign
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
fp = NULL;
|
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)
|
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) {
|
if (MakeDirWithParent(LOG_PATH_DIR, DEFAULT_LOGDIR_MODE) < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int destMax = LOG_LENGTH;
|
|
||||||
if (destMax <= 0) {
|
DivertAndWrite(logPath, msg, level);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
@@ -291,7 +291,6 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
|
|||||||
free(str);
|
free(str);
|
||||||
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) {
|
||||||
Logger("failed to get cgroup path.", LEVEL_ERROR, SCREEN_YES);
|
Logger("failed to get cgroup path.", LEVEL_ERROR, SCREEN_YES);
|
||||||
|
@@ -16,6 +16,21 @@
|
|||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "logger.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)
|
int Mount(const char *src, const char *dst)
|
||||||
{
|
{
|
||||||
if (src == NULL || dst == NULL) {
|
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 mountFlags = MS_BIND;
|
||||||
static const unsigned long remountFlags = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID;
|
static const unsigned long remountFlags = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID;
|
||||||
int ret;
|
if (!checkSrcFile(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 -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
int ret = mount(src, dst, NULL, mountFlags, NULL);
|
||||||
ret = mount(src, dst, NULL, mountFlags, NULL);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
Logger("failed to mount src.", LEVEL_ERROR, SCREEN_YES);
|
Logger("failed to mount src.", LEVEL_ERROR, SCREEN_YES);
|
||||||
return -1;
|
return -1;
|
||||||
@@ -51,24 +58,26 @@ int Mount(const char *src, const char *dst)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
|
static bool GetDeviceCheckParam(const char *rootfs, const char *srcDeviceName,
|
||||||
const char *dstDeviceName, struct PathInfo* pathInfo)
|
struct PathInfo* pathInfo)
|
||||||
{
|
{
|
||||||
if (rootfs == NULL || srcDeviceName == NULL || pathInfo == NULL) {
|
if (rootfs == NULL || srcDeviceName == NULL || pathInfo == NULL) {
|
||||||
Logger("rootfs, srcDeviceName, pathInfo pointer are null!", LEVEL_ERROR, SCREEN_YES);
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int ret = 0;
|
||||||
int ret;
|
|
||||||
errno_t err;
|
errno_t err;
|
||||||
char unresolvedDst[BUF_SIZE] = {0};
|
char unresolvedDst[BUF_SIZE] = {0};
|
||||||
char resolvedDst[PATH_MAX] = {0};
|
char resolvedDst[PATH_MAX] = {0};
|
||||||
|
|
||||||
ret = VerifyPathInfo(pathInfo);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t srcBufSize = pathInfo->srcLen;
|
size_t srcBufSize = pathInfo->srcLen;
|
||||||
size_t dstBufSize = pathInfo->dstLen;
|
size_t dstBufSize = pathInfo->dstLen;
|
||||||
char *src = pathInfo->src;
|
char *src = pathInfo->src;
|
||||||
@@ -98,14 +107,56 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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 MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDeviceName)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char *str = NULL;
|
|
||||||
char src[BUF_SIZE] = {0};
|
char src[BUF_SIZE] = {0};
|
||||||
char dst[BUF_SIZE] = {0};
|
char dst[BUF_SIZE] = {0};
|
||||||
struct PathInfo pathInfo = {src, BUF_SIZE, dst, BUF_SIZE};
|
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);
|
Logger("failed to stat src.", LEVEL_ERROR, SCREEN_YES);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
errno = 0;
|
if (!MountDeviceProcess(dst, src, srcStat.st_mode)) {
|
||||||
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);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = Mount(src, dst);
|
|
||||||
if (ret < 0) {
|
|
||||||
Logger("failed to mount dev.", LEVEL_ERROR, SCREEN_YES);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
free(str);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,6 +281,43 @@ int MountDir(const char *rootfs, const char *src)
|
|||||||
return 0;
|
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)
|
int DoCtrlDeviceMounting(const char *rootfs)
|
||||||
{
|
{
|
||||||
if (rootfs == NULL) {
|
if (rootfs == NULL) {
|
||||||
@@ -265,33 +333,14 @@ int DoCtrlDeviceMounting(const char *rootfs)
|
|||||||
free(str);
|
free(str);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
bool is200Rc = false;
|
||||||
char devmmPath[PATH_MAX] = {0};
|
if (!DoMounting200RC(&is200Rc)) {
|
||||||
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 -1;
|
return -1;
|
||||||
}
|
}
|
||||||
struct stat devStat; // 200 soc 不需要挂载此两个设备
|
if (is200Rc) {
|
||||||
if ((stat(devmmPath, &devStat) != 0) && (stat(hisiPath, &devStat) != 0)) {
|
|
||||||
Logger("200 Soc.", LEVEL_ERROR, SCREEN_YES);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (!DoMountingDevice(rootfs)) {
|
||||||
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);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -5,8 +5,10 @@
|
|||||||
#ifndef _MOUNT_H
|
#ifndef _MOUNT_H
|
||||||
#define _MOUNT_H
|
#define _MOUNT_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include "basic.h"
|
#include "basic.h"
|
||||||
|
|
||||||
int DoMounting(const struct ParsedConfig *config);
|
int DoMounting(const struct ParsedConfig *config);
|
||||||
|
bool DoMounting200RC(bool* is200Rc);
|
||||||
|
|
||||||
#endif
|
#endif
|
@@ -185,28 +185,27 @@ static bool ShowExceptionInfo(const char* exceptionInfo)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
|
||||||
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))) {
|
|
||||||
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 (checkOwner) {
|
||||||
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
||||||
return ShowExceptionInfo("Please check the folder owner!");
|
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用户可写
|
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
|
||||||
return ShowExceptionInfo("Please check the write permission!");
|
return ShowExceptionInfo("Please check the write permission!");
|
||||||
}
|
}
|
||||||
@@ -223,6 +222,35 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
|
|||||||
return true;
|
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,
|
bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||||
const size_t maxFileSzieMb, const bool checkOwner)
|
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)) { // 长度越界
|
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
||||||
return ShowExceptionInfo("filePathLen out of bounds!");
|
return ShowExceptionInfo("filePathLen out of bounds!");
|
||||||
}
|
}
|
||||||
if (strstr(filePath, "..") != NULL) { // 存在".."
|
|
||||||
return ShowExceptionInfo("filePath has an illegal character!");
|
|
||||||
}
|
|
||||||
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
||||||
if ((isalnum(filePath[iLoop]) == 0) && (filePath[iLoop] != '.') && (filePath[iLoop] != '_') &&
|
if (!IsValidChar(filePath[iLoop])) { // 非法字符
|
||||||
(filePath[iLoop] != '-') && (filePath[iLoop] != '/') && (filePath[iLoop] != '~')) { // 非法字符
|
|
||||||
return ShowExceptionInfo("filePath has an illegal character!");
|
return ShowExceptionInfo("filePath has an illegal character!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,28 +33,25 @@ static bool ShowExceptionInfo(const char* exceptionInfo)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
|
||||||
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))) {
|
|
||||||
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 (checkOwner) {
|
||||||
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
||||||
return ShowExceptionInfo("Please check the folder owner!");
|
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用户可写
|
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
|
||||||
return ShowExceptionInfo("Please check the write permission!");
|
return ShowExceptionInfo("Please check the write permission!");
|
||||||
}
|
}
|
||||||
@@ -71,22 +68,62 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
||||||
const size_t maxFileSzieMb, const bool checkOwner)
|
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;
|
int iLoop;
|
||||||
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
||||||
return ShowExceptionInfo("filePathLen out of bounds!");
|
return ShowExceptionInfo("filePathLen out of bounds!");
|
||||||
}
|
}
|
||||||
if (strstr(filePath, "..") != NULL) { // 存在".."
|
|
||||||
return ShowExceptionInfo("filePath has an illegal character!");
|
|
||||||
}
|
|
||||||
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
||||||
if ((isalnum(filePath[iLoop]) == 0) && (filePath[iLoop] != '.') && (filePath[iLoop] != '_') &&
|
if (!IsValidChar(filePath[iLoop])) { // 非法字符
|
||||||
(filePath[iLoop] != '-') && (filePath[iLoop] != '/') && (filePath[iLoop] != '~')) { // 非法字符
|
|
||||||
return ShowExceptionInfo("filePath has an illegal character!");
|
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};
|
char resolvedPath[PATH_MAX] = {0};
|
||||||
if (realpath(filePath, resolvedPath) == NULL && errno != ENOENT) {
|
if (realpath(filePath, resolvedPath) == NULL && errno != ENOENT) {
|
||||||
return ShowExceptionInfo("realpath failed!");
|
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) {
|
if (IdValue < 0 || IdValue > ID_MAX) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool GetAndCheckID(const char *argv[], int *cardId,
|
||||||
|
int *deviceId, int *vDeviceId)
|
||||||
|
{
|
||||||
errno = 0;
|
errno = 0;
|
||||||
int cardId = atoi(argv[PARAMS_SECOND]);
|
*cardId = atoi(argv[PARAMS_SECOND]);
|
||||||
if ((errno != 0) || (cardId < 0) || (cardId > ID_MAX)) {
|
if ((errno != 0) || !CheckLimitId(*cardId)) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
int deviceId = atoi(argv[PARAMS_THIRD]);
|
*deviceId = atoi(argv[PARAMS_THIRD]);
|
||||||
if ((errno != 0) || (deviceId < 0) || (deviceId > ID_MAX)) {
|
if ((errno != 0) || !CheckLimitId(*deviceId)) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
int vDeviceId = atoi(argv[PARAMS_FOURTH]);
|
*vDeviceId = atoi(argv[PARAMS_FOURTH]);
|
||||||
if ((errno != 0) || (vDeviceId < 0) || (vDeviceId > ID_MAX)) {
|
if ((errno != 0) || !CheckLimitId(*vDeviceId)) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
void *handle = NULL;
|
return true;
|
||||||
if (!DeclareDcmiApiAndCheck(&handle)) {
|
}
|
||||||
(void)fprintf(stderr, "Declare dcmi failed.\n");
|
|
||||||
return -1;
|
static bool DcmiInitProcess(void *handle)
|
||||||
|
{
|
||||||
|
if (handle == NULL) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
int (*dcmi_init)(void) = NULL;
|
int (*dcmi_init)(void) = NULL;
|
||||||
int (*dcmi_set_destroy_vdevice)(int, int, int) = NULL;
|
|
||||||
dcmi_init = dlsym(handle, DCMI_INIT);
|
dcmi_init = dlsym(handle, DCMI_INIT);
|
||||||
if (dcmi_init == NULL) {
|
if (dcmi_init == NULL) {
|
||||||
DcmiDlAbnormalExit(&handle, "DeclareDlApi failed");
|
DcmiDlAbnormalExit(&handle, "DeclareDlApi failed");
|
||||||
return -1;
|
return false;
|
||||||
}
|
|
||||||
dcmi_set_destroy_vdevice = dlsym(handle, DCMI_SET_DESTROY_VDEVICE);
|
|
||||||
if (dcmi_set_destroy_vdevice == NULL) {
|
|
||||||
DcmiDlAbnormalExit(&handle, "DeclareDlApi failed");
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
int ret = dcmi_init();
|
int ret = dcmi_init();
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
(void)fprintf(stderr, "dcmi_init failed, ret = %d\n", ret);
|
(void)fprintf(stderr, "dcmi_init failed, ret = %d\n", ret);
|
||||||
DcmiDlclose(&handle);
|
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) {
|
if (ret != 0) {
|
||||||
(void)fprintf(stderr, "dcmi_set_destroy_vdevice failed, ret = %d\n", ret);
|
(void)fprintf(stderr, "dcmi_set_destroy_vdevice failed, ret = %d\n", ret);
|
||||||
DcmiDlclose(&handle);
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
DcmiDlclose(&handle);
|
DcmiDlclose(&handle);
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool EntryCheck(const int argc, const char *argv[])
|
static bool EntryCheck(const int argc, const char *argv[])
|
||||||
|
@@ -180,6 +180,50 @@ static cJSON *CreateContent(const char *runtimePath)
|
|||||||
return root;
|
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)
|
static cJSON *ModifyContent(FILE *pf, const char *runtimePath)
|
||||||
{
|
{
|
||||||
if (pf == NULL || runtimePath == NULL) {
|
if (pf == NULL || runtimePath == NULL) {
|
||||||
@@ -198,42 +242,14 @@ static cJSON *ModifyContent(FILE *pf, const char *runtimePath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 插入ascend runtime */
|
/* 插入ascend runtime */
|
||||||
cJSON *runtimes = NULL;
|
if (!AddAscendRuntime(&root, runtimePath)) {
|
||||||
runtimes = cJSON_GetObjectItem(root, "runtimes");
|
|
||||||
if (runtimes == NULL) {
|
|
||||||
runtimes = CreateRuntimes(runtimePath);
|
|
||||||
if (runtimes == NULL) {
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return NULL;
|
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 */
|
/* 插入defaul runtime */
|
||||||
int ret = DelJsonContent(root, DEFALUT_KEY);
|
if (!AddDefaultRuntime(&root)) {
|
||||||
if (ret != 0) {
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
cJSON *defaultRuntime = cJSON_CreateString(DEFAULT_VALUE);
|
|
||||||
if (defaultRuntime == NULL) {
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
cJSON_AddItemToObject(root, DEFALUT_KEY, defaultRuntime);
|
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
@@ -287,28 +303,25 @@ static bool ShowExceptionInfo(const char* exceptionInfo)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen,
|
static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
|
||||||
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))) {
|
|
||||||
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 (checkOwner) {
|
||||||
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
if ((fileStat.st_uid != ROOT_UID) && (fileStat.st_uid != geteuid())) { // 操作文件owner非root/自己
|
||||||
return ShowExceptionInfo("Please check the folder owner!");
|
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用户可写
|
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
|
||||||
return ShowExceptionInfo("Please check the write permission!");
|
return ShowExceptionInfo("Please check the write permission!");
|
||||||
}
|
}
|
||||||
@@ -325,6 +338,39 @@ static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen
|
|||||||
return true;
|
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,
|
static bool CheckExternalFile(const char* filePath, const size_t filePathLen,
|
||||||
const size_t maxFileSzieMb, const bool checkOwner)
|
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)) { // 长度越界
|
if ((filePathLen > PATH_MAX) || (filePathLen <= 0)) { // 长度越界
|
||||||
return ShowExceptionInfo("filePathLen out of bounds!");
|
return ShowExceptionInfo("filePathLen out of bounds!");
|
||||||
}
|
}
|
||||||
if (strstr(filePath, "..") != NULL) { // 存在".."
|
|
||||||
return ShowExceptionInfo("filePath has an illegal character!");
|
|
||||||
}
|
|
||||||
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
for (iLoop = 0; iLoop < filePathLen; iLoop++) {
|
||||||
if ((isalnum(filePath[iLoop]) == 0) && (filePath[iLoop] != '.') && (filePath[iLoop] != '_') &&
|
if (!IsValidChar(filePath[iLoop])) { // 非法字符
|
||||||
(filePath[iLoop] != '-') && (filePath[iLoop] != '/') && (filePath[iLoop] != '~')) { // 非法字符
|
|
||||||
return ShowExceptionInfo("filePath has an illegal character!");
|
return ShowExceptionInfo("filePath has an illegal character!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -360,102 +402,145 @@ static bool CheckJsonFile(const char *jsonFilePath, const size_t jsonFilePathLen
|
|||||||
return true;
|
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) {
|
if (filePath == NULL || tempPath == NULL || runtimePath == NULL) {
|
||||||
(void)fprintf(stderr, "filePath, tempPath or runtimePath are null!\n");
|
(void)fprintf(stderr, "filePath, tempPath or runtimePath are null!\n");
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CheckJsonFile(filePath, strlen(filePath)) || !CheckJsonFile(tempPath, strlen(tempPath)) ||
|
if (!CheckJsonFile(filePath, strlen(filePath)) || !CheckJsonFile(tempPath, strlen(tempPath)) ||
|
||||||
!CheckJsonFile(runtimePath, strlen(runtimePath))) {
|
!CheckJsonFile(runtimePath, strlen(runtimePath))) {
|
||||||
(void)fprintf(stderr, "filePath, tempPath or runtimePath check failed!\n");
|
(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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *root = NULL;
|
cJSON *root = NULL;
|
||||||
FILE *pf = NULL;
|
if (!GetJsonRoot(&root, filePath, runtimePath) || root == 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 -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (!WriteJsonRoot(&root, tempPath)) {
|
||||||
pf = fopen(tempPath, "w");
|
|
||||||
if (pf == NULL) {
|
|
||||||
(void)fprintf(stderr, "error: failed to create file\n");
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return -1;
|
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;
|
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) {
|
if (filePath == NULL || tempPath == NULL) {
|
||||||
(void)fprintf(stderr, "filePath or tempPath are null!\n");
|
(void)fprintf(stderr, "filePath or tempPath are null!\n");
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CheckJsonFile(filePath, strlen(filePath)) || !CheckJsonFile(tempPath, strlen(tempPath))) {
|
if (!CheckJsonFile(filePath, strlen(filePath)) || !CheckJsonFile(tempPath, strlen(tempPath))) {
|
||||||
(void)fprintf(stderr, "filePath, tempPath check failed!\n");
|
(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;
|
FILE *pf = NULL;
|
||||||
pf = fopen(filePath, "r+");
|
pf = fopen(filePath, "r+");
|
||||||
if (pf == NULL) {
|
if (pf == NULL) {
|
||||||
(void)fprintf(stderr, "error: no json files found\n");
|
(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);
|
(void)fclose(pf);
|
||||||
pf = NULL;
|
pf = NULL;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (newContent == NULL) {
|
static bool WriteNewContent(const char *tempPath, cJSON **newContent)
|
||||||
(void)fprintf(stderr, "error: failed to create json\n");
|
{
|
||||||
return -1;
|
FILE *pf = NULL;
|
||||||
}
|
|
||||||
|
|
||||||
pf = fopen(tempPath, "w");
|
pf = fopen(tempPath, "w");
|
||||||
if (pf == NULL) {
|
if (pf == NULL) {
|
||||||
(void)fprintf(stderr, "error: failed to create file\n");
|
(void)fprintf(stderr, "error: failed to create file\n");
|
||||||
cJSON_Delete(newContent);
|
cJSON_Delete(*newContent);
|
||||||
newContent = NULL;
|
*newContent = NULL;
|
||||||
return -1;
|
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");
|
(void)fprintf(stderr, "error: failed to create file\n");
|
||||||
cJSON_Delete(newContent);
|
cJSON_Delete(*newContent);
|
||||||
newContent = NULL;
|
*newContent = NULL;
|
||||||
(void)fclose(pf);
|
(void)fclose(pf);
|
||||||
pf = NULL;
|
pf = NULL;
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
(void)fclose(pf);
|
(void)fclose(pf);
|
||||||
pf = NULL;
|
pf = NULL;
|
||||||
cJSON_Delete(newContent);
|
cJSON_Delete(*newContent);
|
||||||
newContent = NULL;
|
*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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user