-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathregister.cpp
More file actions
151 lines (137 loc) · 4.05 KB
/
Copy pathregister.cpp
File metadata and controls
151 lines (137 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <filesystem>
#include <regex>
#include <string>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "log.h"
#include "common.h"
#include "register.h"
using namespace std;
namespace xpu {
#ifndef UNIT_TEST
const static string PROC_CGROUP_PATH = "/proc/self/cgroup";
#else
static string PROC_CGROUP_PATH = "/run/xpu/fake/cgroup";
void SetProcCgroupPath(const string& path)
{
PROC_CGROUP_PATH = path;
}
#endif
const static string RPC_CLIENT_NAME = "xpu-client-tool";
const static string RPC_CLIENT_PATH = "/opt/xpu/bin/xpu-client-tool";
const static int TRY_TIMES = 10;
void FileOperateErrorHandler(const std::ifstream &file, const string &path)
{
if (file.bad()) {
log_err("I/O error while reading file {}", path);
} else if (file.eof()) {
log_err("File {} reached the end", path);
} else if (file.fail()) {
log_err("Non-fatal error occurred while opening {}", path);
} else {
log_err("Unexpected error occurred while opening {}", path);
}
}
int GetCgroupData(const string& groupPath, string& groupData)
{
// open cgroup file
ifstream grp(groupPath);
if (!grp.is_open()) {
FileOperateErrorHandler(grp, groupPath);
return RET_FAIL;
}
// get memory line
string memLine;
const string memoryHeader = "memory:";
string::size_type pos = memLine.npos;
while (getline(grp, memLine)) {
pos = memLine.find(memoryHeader);
if (pos != memLine.npos) {
break;
}
}
if (pos == memLine.npos) {
log_err("find memory cgroup failed");
return RET_FAIL;
}
// get cgroup data
groupData = memLine.substr(pos + memoryHeader.size());
return RET_SUCC;
}
int RegisterWithData(const string& cgroupData)
{
pid_t pid = fork();
if (pid < 0) {
log_err("fork child process failed, errno is {}", strerror(errno));
return RET_FAIL;
} else if (pid == 0) {
// child
if (IsDangerousCommand(cgroupData)) {
exit(EXIT_FAILURE);
}
if (!std::filesystem::exists(RPC_CLIENT_PATH)) {
log_err("{} no exist", RPC_CLIENT_PATH);
exit(EXIT_FAILURE);
}
log_info("run: {} --cgroup-path {}", RPC_CLIENT_PATH, cgroupData);
execl(RPC_CLIENT_PATH.c_str(), RPC_CLIENT_NAME.c_str(),
"--cgroup-path", cgroupData.data(), nullptr);
log_err("run rpc client failed, errno is {}", strerror(errno));
exit(EXIT_FAILURE);
} else {
// parent
int wstatus = 0;
int wret = waitpid(pid, &wstatus, WUNTRACED | WCONTINUED);
if (wret == -1) {
log_err("waitpid failed, error {}", strerror(errno));
return RET_FAIL;
}
if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
log_warn("unexpected exit status {}", wstatus);
return RET_FAIL;
}
log_info("rpc client exit success");
}
return RET_SUCC;
}
/*
* (1) Command should not include dangerous command;
* (2) Dangerous command includes: `, |, ;, &, $, >, <, !, \n, \\, *, ?, {, }, (, )
*/
bool IsDangerousCommand(const string& command)
{
const string blacklist = "!;&$><!\n\\*?{}()";
string::size_type pos = command.find_first_of(blacklist);
if (pos != string::npos) {
log_err("{} is dangerous", command);
return true;
}
log_info("{} is safe", command);
return false;
}
int RegisterToDevicePlugin(void)
{
string groupData;
int ret = GetCgroupData(PROC_CGROUP_PATH, groupData);
if (ret != RET_SUCC) {
log_err("get cgroup data failed, ret is {}", ret);
return ret;
}
for (int i = 0; i < TRY_TIMES; i++) {
ret = RegisterWithData(groupData);
if (ret == RET_SUCC) {
log_info("register with data success");
return RET_SUCC;
}
#ifndef UNIT_TEST
log_info("register with data failed, retry {} time", i + 1);
this_thread::sleep_for(std::chrono::seconds(1));
#else
break;
#endif
}
return RET_FAIL;
}
} // namespace xpu