mirror of
https://github.com/Ascend/ascend-docker-runtime.git
synced 2025-10-13 21:24:37 +08:00
Match-id-f71d276734eb182df9af864f18cb5af2abe90dc3
This commit is contained in:
@@ -16,6 +16,87 @@
|
||||
#include "utils.h"
|
||||
#include "logging.h"
|
||||
|
||||
bool TakeNthWord(char **pLine, unsigned int n, char **word)
|
||||
{
|
||||
char *w = NULL;
|
||||
for (unsigned int i = 0; i < n; i++) {
|
||||
w = strsep(pLine, " ");
|
||||
if (w == NULL || *w == '\0') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*word = w;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CheckRootDir(char **pLine)
|
||||
{
|
||||
char *rootDir = NULL;
|
||||
if (!TakeNthWord(pLine, ROOT_GAP, &rootDir)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strlen(rootDir) < BUF_SIZE && !StrHasPrefix(rootDir, "/..");
|
||||
}
|
||||
|
||||
bool CheckFsType(char **pLine)
|
||||
{
|
||||
char* fsType = NULL;
|
||||
if (!TakeNthWord(pLine, FSTYPE_GAP, &fsType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return IsStrEqual(fsType, "cgroup");
|
||||
}
|
||||
|
||||
bool CheckSubStr(char **pLine, const char *subsys)
|
||||
{
|
||||
char* substr = NULL;
|
||||
if (!TakeNthWord(pLine, MOUNT_SUBSTR_GAP, &substr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strstr(substr, subsys) != NULL;
|
||||
}
|
||||
|
||||
typedef char *(*ParseFileLine)(char *, const char *);
|
||||
int ParseFileByLine(char* buffer, int bufferSize, ParseFileLine fn, const char* filepath)
|
||||
{
|
||||
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) {
|
||||
LogError("error: cannot canonicalize path %s.", filepath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(resolvedPath, "r");
|
||||
if (fp == NULL) {
|
||||
LogError("cannot open file.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (getline(&line, &len, fp) != -1) {
|
||||
result = fn(line, "devices");
|
||||
if (result != NULL && strlen(result) < bufferSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t ret = strcpy_s(buffer, bufferSize, result);
|
||||
free(line);
|
||||
fclose(fp);
|
||||
if (ret != EOK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *GetCgroupMount(char *line, const char *subsys)
|
||||
{
|
||||
if (!CheckRootDir(&line)) {
|
||||
@@ -130,7 +211,7 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = CatFileContent(mount, BUF_SIZE, GetCgroupMount, mountPath);
|
||||
ret = ParseFileByLine(mount, BUF_SIZE, GetCgroupMount, mountPath);
|
||||
if (ret < 0) {
|
||||
LogError("error: cat file content failed.");
|
||||
return -1;
|
||||
@@ -144,7 +225,7 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = CatFileContent(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
|
||||
ret = ParseFileByLine(cgroup, BUF_SIZE, GetCgroupRoot, cgroupPath);
|
||||
if (ret < 0) {
|
||||
LogError("error: cat file content failed.");
|
||||
return -1;
|
||||
|
@@ -8,11 +8,33 @@
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mount.h>
|
||||
#include "securec.h"
|
||||
#include "utils.h"
|
||||
#include "logging.h"
|
||||
#include "options.h"
|
||||
|
||||
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;
|
||||
|
||||
ret = mount(src, dst, NULL, mountFlags, NULL);
|
||||
if (ret < 0) {
|
||||
LogError("error: failed to mount.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = mount(NULL, dst, NULL, remountFlags, NULL);
|
||||
if (ret < 0) {
|
||||
LogError("error: failed to re-mount.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName,
|
||||
struct PathInfo* pathInfo)
|
||||
{
|
||||
|
102
cli/src/utils.c
102
cli/src/utils.c
@@ -11,13 +11,11 @@
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mount.h>
|
||||
#include "securec.h"
|
||||
#include "logging.h"
|
||||
|
||||
static int IsStrEqual(const char *s1, const char *s2)
|
||||
int IsStrEqual(const char *s1, const char *s2)
|
||||
{
|
||||
return (!strcmp(s1, s2));
|
||||
}
|
||||
@@ -27,84 +25,6 @@ int StrHasPrefix(const char *str, const char *prefix)
|
||||
return (!strncmp(str, prefix, strlen(prefix)));
|
||||
}
|
||||
|
||||
bool CheckRootDir(char **pLine)
|
||||
{
|
||||
char *rootDir = NULL;
|
||||
for (int i = 0; i < ROOT_GAP; i++) {
|
||||
/* root is substr before gap, line is substr after gap */
|
||||
rootDir = strsep(pLine, " ");
|
||||
if (rootDir == NULL || *rootDir == '\0') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return strlen(rootDir) < BUF_SIZE && !StrHasPrefix(rootDir, "/..");
|
||||
}
|
||||
|
||||
bool CheckFsType(char **pLine)
|
||||
{
|
||||
char* fsType = NULL;
|
||||
for (int i = 0; i < FSTYPE_GAP; i++) {
|
||||
fsType = strsep(pLine, " ");
|
||||
if (fsType == NULL || *fsType == '\0') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return IsStrEqual(fsType, "cgroup");
|
||||
}
|
||||
|
||||
bool CheckSubStr(char **pLine, const char *subsys)
|
||||
{
|
||||
char* substr = NULL;
|
||||
for (int i = 0; i < MOUNT_SUBSTR_GAP; i++) {
|
||||
substr = strsep(pLine, " ");
|
||||
if (substr == NULL || *substr == '\0') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return strstr(substr, subsys) != NULL;
|
||||
}
|
||||
|
||||
int CatFileContent(char* buffer, int bufferSize, ParseFileLine fn, const char* filepath)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
char resolvedPath[PATH_MAX] = {0x0};
|
||||
|
||||
if (realpath(filepath, resolvedPath) == NULL && errno != ENOENT) {
|
||||
LogError("error: cannot canonicalize path %s\n", filepath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(resolvedPath, "r");
|
||||
if (fp == NULL) {
|
||||
LogError("cannot open file.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (getline(&line, &len, fp) != -1) {
|
||||
char* result = fn(line, "devices");
|
||||
if (result != NULL && strlen(result) < bufferSize) {
|
||||
errno_t ret = strncpy_s(buffer, BUF_SIZE, result, strlen(result));
|
||||
if (ret != EOK) {
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (line != NULL) {
|
||||
free(line);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MkDir(const char *dir, int mode)
|
||||
{
|
||||
return mkdir(dir, mode);
|
||||
@@ -190,25 +110,5 @@ int CreateFile(const char *path, mode_t mode)
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Mount(const char *src, const char *dst)
|
||||
{
|
||||
static const unsigned long remountFlags = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID;
|
||||
int ret;
|
||||
|
||||
ret = mount(src, dst, NULL, MS_BIND, NULL);
|
||||
if (ret < 0) {
|
||||
LogError("error: failed to mount\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = mount(NULL, dst, NULL, remountFlags, NULL);
|
||||
if (ret < 0) {
|
||||
LogError("error: failed to re-mount\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@@ -9,21 +9,13 @@
|
||||
#include <sys/types.h>
|
||||
#include "basic.h"
|
||||
|
||||
// For cgroup setup
|
||||
int IsStrEqual(const char *s1, const char *s2);
|
||||
int StrHasPrefix(const char *str, const char *prefix);
|
||||
bool CheckRootDir(char **pLine);
|
||||
bool CheckFsType(char **pLine);
|
||||
bool CheckSubStr(char **pLine, const char *subsys);
|
||||
typedef char *(*ParseFileLine)(char *, const char *);
|
||||
int CatFileContent(char* buffer, int bufferSize, ParseFileLine fn, const char* filepath);
|
||||
|
||||
// For mount setup
|
||||
int MkDir(const char *dir, int mode);
|
||||
int VerifyPathInfo(const struct PathInfo* pathInfo);
|
||||
int CheckDirExists(const char *dir);
|
||||
int GetParentPathStr(const char *path, char *parent, size_t bufSize);
|
||||
int MakeParentDir(const char *path, mode_t mode);
|
||||
int CreateFile(const char *path, mode_t mode);
|
||||
int Mount(const char *src, const char *dst);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user