Skip to content

Commit 4cf4066

Browse files
authored
Merge pull request #197 from robbietu/master
fix for the Zombie process of sh
2 parents 325ae68 + 4d45363 commit 4cf4066

File tree

5 files changed

+160
-26
lines changed

5 files changed

+160
-26
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endif ()
1616
# set PKTMINERG_MAJOR_VERSION, PKTMINERG_MINOR_VERSION, etc.
1717
set(PKTMINERG_MAJOR_VERSION "0")
1818
set(PKTMINERG_MINOR_VERSION "8")
19-
set(PKTMINERG_PATCH_VERSION "4")
19+
set(PKTMINERG_PATCH_VERSION "7")
2020
set(PKTMINERG_VERSION_STRING "${PKTMINERG_MAJOR_VERSION}.${PKTMINERG_MINOR_VERSION}.${PKTMINERG_PATCH_VERSION}")
2121

2222
if (WIN32)

scripts/winscp.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
version=`cat CMakeLists.txt | grep -E "set\(PKTMINERG_(MAJOR|MINOR|PATCH)_VERSION \"" | sed 's/"/ /g'| awk '{ver=ver".";(ver=ver""$2)}END{print substr(ver,2)}'`
44

5-
sfile=netis-packet-agent-$version.Windows.AMD64.zip
5+
sfile=netis-cloud-probe-$version.Windows.AMD64.zip
66
dfile=$sfile
77

88
if [ $3 == "Yes" ]; then
9-
dfile=netis-packet-agent-$version.Windows.AMD64.`date +%Y%m%d%H%M`.zip
9+
dfile=netis-cloud-probe-$version.Windows.AMD64.`date +%Y%m%d%H%M`.zip
1010
cp -f build/_CPack_Packages/win64/ZIP/$sfile build/_CPack_Packages/win64/ZIP/$dfile
1111
fi
1212
echo "wget http://10.1.1.34/pktminerg/release/$dfile"

src/daemonManager.cpp

Lines changed: 149 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,29 @@ bool checkProcessRunning ( ) {
3232
const auto cmd = boost::str(
3333
boost::format("ps -ef|grep pktminerg|grep %1%|grep -v grep |wc -l") %ppid);
3434
fp=popen(cmd.c_str(),"r");
35-
fgets(buffer,sizeof(buffer),fp);
36-
pclose(fp);
35+
if (fp == nullptr) {
36+
auto str = boost::str(boost::format("Failed to check Process and error "
37+
"executing popen command: = %1%.") %strerror(errno));
38+
LOG(ERROR);
39+
std::cerr << str << std::endl;
40+
return false;
41+
}
42+
43+
try {
44+
fgets(buffer, sizeof(buffer), fp);
45+
} catch (...) {
46+
auto str = boost::str(boost::format("Failed to check Process and error "
47+
"executing popen command: = %1%.") %strerror(errno));
48+
LOG(ERROR);
49+
std::cerr << str << std::endl;
50+
}
51+
int status = pclose(fp);
52+
if (status != 0) {
53+
auto str =
54+
boost::str(boost::format("Error executing pclose command: = %1%.") %strerror(errno));
55+
LOG(ERROR);
56+
std::cerr << str << std::endl;
57+
}
3758

3859
if (buffer [0] != '0') {
3960
return true;
@@ -48,6 +69,13 @@ void DaemonManager::killRunningPktg() {
4869
const auto cmd = boost::str(
4970
boost::format("ps -ef|grep pktminerg|grep %1%|grep -v grep |awk '{print $2}'") %ppid);
5071
fp=popen(cmd.c_str(),"r");
72+
if (fp == nullptr) {
73+
auto str = boost::str(boost::format("Fail to get pktg pid and error executing popen command: = %1%.") %strerror(errno));
74+
LOG(ERROR);
75+
std::cerr << str << std::endl;
76+
return;
77+
}
78+
try {
5179
while (fgets(buffer,sizeof(buffer),fp)) {
5280
pid_t pid = atoi(buffer);
5381
auto strInfo = boost::str(boost::format("kill running pid=%1%")% pid);
@@ -72,18 +100,51 @@ void DaemonManager::killRunningPktg() {
72100
clearCgroupfolder(agentPid_);
73101
agentPid_ = 0;
74102
}
103+
} catch (...) {
104+
auto str = boost::str(
105+
boost::format("Fail to get pktg pid and error executing popen command: = %1%.") %strerror(errno));
106+
LOG(ERROR);
107+
std::cerr << str << std::endl;
108+
}
109+
110+
int status = pclose(fp);
111+
if (status != 0) {
112+
auto str =boost::str(boost::format("Error executing pclose command: = %1%.") %strerror(errno));
113+
LOG(ERROR);
114+
std::cerr << str << std::endl;
115+
}
75116
}
76117

77118
void getActiveInstanceNames(std::set<std::string> & names) {
78119
FILE *fp;
79120
char buffer[256];
80121
fp=popen("sh get_name_of_instance.sh","r");
81-
while(!feof(fp)){
82-
fgets(buffer,sizeof(buffer),fp);
83-
std::string str(buffer, strlen(buffer)-1);
84-
names.insert(str);
122+
if (fp == nullptr) {
123+
auto str =boost::str(boost::format("Error executing popen command: = %1%.") %strerror(errno));
124+
LOG(ERROR);
125+
std::cerr << str << std::endl;
126+
return;
127+
}
128+
try {
129+
while (!feof(fp)) {
130+
fgets(buffer, sizeof(buffer), fp);
131+
std::string str(buffer, strlen(buffer) - 1);
132+
names.insert(str);
133+
}
134+
} catch (...) {
135+
auto str = boost::str(boost::format("Failed to get instance name.") %
136+
strerror(errno));
137+
LOG(ERROR);
138+
std::cerr << str << std::endl;
139+
}
140+
141+
int status = pclose(fp);
142+
if (status != 0) {
143+
auto str =
144+
boost::str(boost::format("Error executing pclose command: = %1%.") %strerror(errno));
145+
LOG(ERROR);
146+
std::cerr << str << std::endl;
85147
}
86-
pclose(fp);
87148
return;
88149
}
89150

@@ -464,6 +525,75 @@ std::string DaemonManager::createParams(std::shared_ptr<io::swagger::server::mod
464525
? std::string(" --dir ") + dir : std::string()));
465526
}
466527
528+
int DaemonManager::create_directory(const std::string &path) {
529+
if (mkdir(path.c_str(), 0755) != 0) {
530+
return -1;
531+
}
532+
533+
return 0;
534+
}
535+
536+
void DaemonManager::set_cgroup_cpu(const std::string &pid,
537+
const std::string &cfs_quota_us) {
538+
std::string path("/sys/fs/cgroup/cpu/");
539+
std::string cgroup_dir = path + "pid-" + pid;
540+
if (create_directory(cgroup_dir) != 0) {
541+
std::string str("Failed to create cgroup directory.");
542+
ctx_.log(str, log4cpp::Priority::INFO);
543+
LOG(INFO) << str;
544+
return;
545+
}
546+
547+
if (cfs_quota_us != "0") {
548+
std::ofstream cpu_quota_file(cgroup_dir + "/cpu.cfs_quota_us");
549+
cpu_quota_file << cfs_quota_us;
550+
cpu_quota_file.close();
551+
}
552+
553+
std::ofstream cgroup_procs_file(cgroup_dir + "/cgroup.procs");
554+
cgroup_procs_file << pid;
555+
cgroup_procs_file.close();
556+
}
557+
558+
void DaemonManager::set_cgroup_mem(const std::string &pid,
559+
const std::string &mem_limit) {
560+
std::string path("/sys/fs/cgroup/memory/");
561+
std::string cgroup_dir = path + "pid-" + pid;
562+
563+
if (create_directory(cgroup_dir) != 0) {
564+
std::string str("Failed to create cgroup directory.");
565+
ctx_.log(str, log4cpp::Priority::INFO);
566+
LOG(INFO) << str;
567+
return;
568+
}
569+
570+
if (mem_limit != "0" && mem_limit != "0K" && mem_limit != "0M" &&
571+
mem_limit != "0G" && mem_limit != "0T") {
572+
std::ofstream mem_limit_file(cgroup_dir + "/memory.limit_in_bytes");
573+
mem_limit_file << mem_limit;
574+
mem_limit_file.close();
575+
}
576+
577+
std::ofstream cgroup_procs_file(cgroup_dir + "/cgroup.procs");
578+
cgroup_procs_file << pid;
579+
cgroup_procs_file.close();
580+
}
581+
582+
void DaemonManager::set_cgroup(const std::string &pid,
583+
const std::string &cfs_quota_us,
584+
const std::string &mem_limit) {
585+
586+
const auto str =
587+
boost::str(boost::format("Set cgroup: pid=%1%, cpu=%2% mem=%3%M") % pid %
588+
cfs_quota_us % mem_limit);
589+
ctx_.log(str, log4cpp::Priority::INFO);
590+
LOG(INFO) << str;
591+
report_.addPacketAgentLogs("INFO", str);
592+
set_cgroup_cpu(pid, cfs_quota_us);
593+
//currently, set the buffer of libpcap to limit mem, instead of cgroup.
594+
//set_cgroup_mem(pid, mem_limit);
595+
}
596+
467597
int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::stringstream &result) {
468598
uint16_t port;
469599
if (!body.packetAgentStrategiesIsSet()) {
@@ -478,13 +608,15 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings
478608
return -1;
479609
}
480610
std::set<std::string> names;
481-
try{
482-
getActiveInstanceNames(names);
611+
bool needGetInstanceName = false;
612+
for (auto &data : datas) {
613+
if (data->instanceNamesIsSet()) {
614+
needGetInstanceName = true;
615+
break;
616+
}
483617
}
484-
catch (...) {
485-
std::string str = boost::str(boost::format("Can't get active instance."));
486-
ctx_.log(str, log4cpp::Priority::INFO);
487-
LOG(INFO) << str;
618+
if (needGetInstanceName) {
619+
getActiveInstanceNames(names);
488620
}
489621
uint64_t buffSize;
490622
if(body.getMemLimit() == 0) {
@@ -659,14 +791,9 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings
659791
660792
report_.setPid(pid);
661793
int16_t memLimit = (body.getMemLimit() == 0)?0:body.getMemLimit() - 290;
662-
const auto limit = boost::str(
663-
boost::format("limit_cpu_mem_pid.sh %1% %2% %3%M") % pid %
664-
(static_cast<uint32_t>(body.getCpuLimit() * 1000)) %
665-
memLimit);
666-
ctx_.log(limit, log4cpp::Priority::INFO);
667-
LOG(INFO) << limit;
668-
report_.addPacketAgentLogs("INFO", limit);
669-
::system(limit.c_str());
794+
set_cgroup(std::to_string(pid),
795+
std::to_string(static_cast<uint32_t>(body.getCpuLimit() * 1000)),
796+
std::to_string(memLimit));
670797
body.setName(boost::str(boost::format("%1%") % body.getId()));
671798
const auto waitResult = waitpid(pid, NULL, WNOHANG);
672799
body.setStatus(waitResult == -1 ? "error" : waitResult == pid ? "inactive" : "active");
@@ -1139,7 +1266,7 @@ DaemonManager::DaemonManager(const boost::program_options::variables_map &vm, ti
11391266
}
11401267
}
11411268

1142-
daemon_.setClientVersion("0.8.3");
1269+
daemon_.setClientVersion("0.8.7");
11431270
std::vector<std::string> strs;
11441271
split(strs, SUPPORT_API_VERSIONS, boost::algorithm::is_any_of(","));
11451272
for (const auto& str:strs) {

src/daemonManager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ class DaemonManager
128128

129129
void clearCgroupfolder(pid_t pid);
130130

131+
void set_cgroup_mem(const std::string& pid, const std::string& mem_limit);
132+
133+
void set_cgroup_cpu(const std::string& pid, const std::string& cfs_quota_us);
134+
135+
void set_cgroup(const std::string& pid, const std::string& cfs_quota_us, const std::string& mem_limit);
136+
137+
int create_directory(const std::string &path);
138+
131139
private:
132140
const boost::program_options::variables_map& vm_;
133141
std::unordered_set<std::string> interfaces_;

src/socketvxlan.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ int PcapExportVxlan::exportPacket(size_t index,
145145
uint32_t tv_sec = htonl(header->ts.tv_sec);
146146
//注意:通过libpcap获取的捕获时间精度为微秒,而数据包中附加的时间为纳秒,所以需要*1000
147147
uint32_t tv_nsec = htonl(header->ts.tv_usec*1000);
148-
std::cout << "====got time" << header->ts.tv_usec<< std::endl;
149148
if (_capTime == 1) {
150149
memcpy(
151150
reinterpret_cast<void *>(&(vxlanbuffer[sizeof(vxlan_hdr_t)]) + length),

0 commit comments

Comments
 (0)