Skip to content

Commit ea6508d

Browse files
unkn0wNameunkn0wName
authored andcommitted
remove fork sleep
1 parent f3aa4b7 commit ea6508d

File tree

1 file changed

+2
-41
lines changed

1 file changed

+2
-41
lines changed

testRoot/init64_process_helper.h

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,17 @@
77
#include "process64_inject.h"
88
#include "process_cmdline_utils.h"
99

10-
1110
//注入init64进程远程执行命令
1211
static std::string run_init64_cmd_wrapper(
1312
const char* str_root_key,
1413
const char *cmd,
1514
ssize_t & out_err) {
1615
pid_t target_pid = 1;
17-
std::string str_cmd_result;
18-
19-
//让init64进程启动一个子进程
20-
char guid[32] = { 0 };
21-
rand_str(guid, sizeof(guid));
22-
std::string strGuid(guid, sizeof(guid));
23-
24-
std::string fork_proc_cmd = "sleep 0.15";
25-
fork_proc_cmd += " --guid=";
26-
fork_proc_cmd += strGuid;
27-
28-
std::atomic<bool> thread_exist{true};
29-
std::atomic<bool>* pthread_exist = &thread_exist;
30-
std::thread td_run_child_proc([&]()-> void {
31-
TRACE("init64 run child thread start.\n");
32-
pid_t child_pid;
33-
out_err = wait_and_find_cmdline_process(str_root_key, strGuid.c_str(), 1*1000, child_pid);
34-
TRACE("init64 run child err:%zd, pid:%d\n", out_err, child_pid);
35-
if(out_err == 0 && child_pid > 0) {
36-
//把错误信息也打出来
37-
std::string cmd_add_err_info = cmd;
38-
cmd_add_err_info += " 2>&1";
39-
str_cmd_result = inject_process64_run_cmd_wrapper(str_root_key, child_pid,
40-
cmd_add_err_info.c_str(), out_err, true);
41-
//inject_process_run_exit_wrapper(str_root_key, child_pid);
42-
kill_process(str_root_key, child_pid);
43-
TRACE("init64 run child cmd result:%s\n", str_cmd_result.c_str());
44-
}
45-
*pthread_exist = false;
46-
});
47-
td_run_child_proc.detach(); //因为运行子进程的时候父进程会一直阻塞,所以这里启动一条新的线程去读子进程
48-
49-
TRACE("init64 run child thread cmd:%s\n", fork_proc_cmd.c_str());
50-
inject_process64_run_cmd_wrapper(str_root_key, target_pid,
51-
fork_proc_cmd.c_str(), out_err, false);
16+
std::string str_cmd_result = inject_process64_run_cmd_wrapper(str_root_key, target_pid,
17+
cmd, out_err, false);
5218
if(out_err != 0) {
5319
return {};
5420
}
55-
TRACE("init64 run child successfully.\n");
56-
57-
while(thread_exist) {
58-
sleep(0);
59-
}
6021
return str_cmd_result;
6122
}
6223

0 commit comments

Comments
 (0)