|
7 | 7 | #include "process64_inject.h"
|
8 | 8 | #include "process_cmdline_utils.h"
|
9 | 9 |
|
10 |
| - |
11 | 10 | //注入init64进程远程执行命令
|
12 | 11 | static std::string run_init64_cmd_wrapper(
|
13 | 12 | const char* str_root_key,
|
14 | 13 | const char *cmd,
|
15 | 14 | ssize_t & out_err) {
|
16 | 15 | 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); |
52 | 18 | if(out_err != 0) {
|
53 | 19 | return {};
|
54 | 20 | }
|
55 |
| - TRACE("init64 run child successfully.\n"); |
56 |
| - |
57 |
| - while(thread_exist) { |
58 |
| - sleep(0); |
59 |
| - } |
60 | 21 | return str_cmd_result;
|
61 | 22 | }
|
62 | 23 |
|
|
0 commit comments