Home > Back-end >  Ptrace (PTRACE_TRACEME, 0, 0, 0) set up after the pclose why are hung live?
Ptrace (PTRACE_TRACEME, 0, 0, 0) set up after the pclose why are hung live?

Time:06-06

# include & lt; Stdio. H>
# include & lt; Sys/ptrace. H>
# include & lt; Unistd. H>
# include & lt; String. H>
# include & lt; iostream>

using namespace std;

The static void cmd_exe_test ()
{
Const char * CMD="ls -l";
Char buf [1024]={0};
The FILE * fp=popen (CMD, "r");
Cout & lt; <"Create popen ok." & lt; If (fp==NULL)
{
Cout & lt; <"Popen failed." & lt; return;
}
Cout & lt; <"Before fread." & lt; Int ret=fread (buf, 1, 1023, fp);
Cout & lt; <"Fread:" & lt; If (ret & lt; 0)
{
The pclose (fp);
Cout & lt; <"Read CMD result failed." & lt; return;
}
Cout & lt; <"Before the pclose." & lt; The pclose (fp);
Cout & lt; <"After the pclose." & lt; Cout & lt; <"CMD result:" & lt; return;
}

Int main (int arg c, char * * argv)
{
If (ptrace (PTRACE_TRACEME, 0, 0, 0) & lt; 0)
{
Printf (" traced! \n");
return 1;
}
Cout & lt; <"Before cmd_exe_test" & lt; Cmd_exe_test ();
Cout & lt; <"Cmd_exe_test exec () over" & lt;
return 0;
}
See related test code, compile the execution result:
[root @ localhost test_ptrace] # g + + a.c pp
[root @ localhost test_ptrace] #./a.out
Before cmd_exe_test
The create popen ok.
Before fread.
Fread: 264 buf: total 44
Rw - r - r -. 1 root root 931 Nov 26 covenant a.c pp
- rwxr-xr-x mto. 1 root root 13616 Nov 26 18:08 a.out
- rwxr-xr-x mto. 1 root root 13680 Nov 26 shew forth main
Rw - r - r -. 1 root root 1290 Nov 26 shew forth main. CPP
Rw - r - r -. 1 root root 2826 Nov 26 14:13 test_ptrace. C

Before pclose.

[1] + Stopped./a.out
[root @ localhost test_ptrace] #
Excuse me each friend and mentor, why to perform to the pclose process status when they became traced? In front of the system call why it hadn't been in? Is this way of using wrong? Please correct me!

CodePudding user response:

I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html