Note that there are some explanatory texts on larger screens.

plurals
  1. POcalling ptrace inside a ptraced Linux process
    primarykey
    data
    text
    <p>Someone added to the <a href="http://en.wikipedia.org/wiki/Ptrace" rel="noreferrer">Wikipedia "ptrace" article</a> claiming that, on Linux, a ptraced process couldn't itself ptrace another process. I'm trying to determine if (and if so why) that's the case. Below is a simple program I contrived to test this. My program fails (the sub sub process doesn't run properly) but I'm pretty convinced it's my error and not something fundamental.</p> <p>In essence the initial process <strong>A</strong> forks process <strong>B</strong> which in turn forks <strong>C</strong>. <strong>A</strong> ptraces its child <strong>B</strong>, <strong>B</strong> ptraces its child <strong>C</strong>. Once they're set up, all three processes are written to just print <code>A</code>,<code>B</code>, or <code>C</code> to stdout once every second. </p> <p>In practice what happens is that <strong>A</strong> and <strong>B</strong> work fine, but <strong>C</strong> prints only once and then gets stuck. Checking with <code>ps -eo pid,cmd,wchan</code> shows <strong>C</strong> stuck in kernel function <code>ptrace_stop</code> while the rest are in <code>hrtimer_nanosleep</code> where I'd expect all three to be. </p> <p>Very occasionally all three do work (so the program prints Cs as well as As and Bs), which leads me to believe there's some race condition in the initial setup. </p> <p>My <em>guesses</em> as to what might be wrong are:</p> <ul> <li>something to do with <strong>A</strong> seeing a <code>SIGCHLD</code> related to <strong>B</strong> seeing a <code>SIGCHLD</code> to do with a signal to <strong>C</strong>, and wait(2) reporting both as coming from <strong>B</strong> (but a hacky call of PTRACE_CONT to both pids doesn't fix things)?</li> <li><strong>C</strong> should be ptraced by <strong>B</strong> - has <strong>C</strong> inherited the ptrace by <strong>A</strong> instead (and <strong>B</strong>'s call to ptrace neither errored nor overwrote this)?</li> </ul> <p>Can anyone figure out what I'm doing wrong? Thanks.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;sys/ptrace.h&gt; #include &lt;sys/wait.h&gt; static void a(){ while(1){ printf ("A\n"); fflush(stdout); sleep(1); } } static void b(){ while(1){ printf ("B\n"); fflush(stdout); sleep(1); } } static void c(){ while(1){ printf ("C\n"); fflush(stdout); sleep(1); } } static void sigchld_handler(int sig){ int result; pid_t child_pid = wait(NULL); // find who send us this SIGCHLD printf("SIGCHLD on %d\n", child_pid); result=ptrace(PTRACE_CONT, child_pid, sig, NULL); if(result) { perror("continuing after SIGCHLD"); } } int main(int argc, char **argv){ pid_t mychild_pid; int result; printf("pidA = %d\n", getpid()); signal(SIGCHLD, sigchld_handler); mychild_pid = fork(); if (mychild_pid) { printf("pidB = %d\n", mychild_pid); result = ptrace(PTRACE_ATTACH, mychild_pid, NULL, NULL); if(result==-1){ perror("outer ptrace"); } a(); } else { mychild_pid = fork(); if (mychild_pid) { printf("pidC = %d\n", mychild_pid); result = ptrace(PTRACE_ATTACH, mychild_pid, NULL, NULL); if(result==-1){ perror("inner ptrace"); } b(); } else { c(); } } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload