Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See <strong><a href="http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/" rel="noreferrer">Redirecting Output from a Running Process</a></strong>.</p> <blockquote> <p>Firstly I run the command <code>cat &gt; foo1</code> in one session and test that data from stdin is copied to the file. Then in another session I redirect the output.</p> <p>Firstly find the PID of the process:</p> <pre><code>$ ps aux | grep cat rjc 6760 0.0 0.0 1580 376 pts/5 S+ 15:31 0:00 cat </code></pre> <p>Now check the file handles it has open:</p> <pre><code>$ ls -l /proc/6760/fd total 3 lrwx—— 1 rjc rjc 64 Feb 27 15:32 0 -&gt; /dev/pts/5 l-wx—— 1 rjc rjc 64 Feb 27 15:32 1 -&gt; /tmp/foo1 lrwx—— 1 rjc rjc 64 Feb 27 15:32 2 -&gt; /dev/pts/5 </code></pre> <p>Now run GDB:</p> <pre><code>$ gdb -p 6760 /bin/cat GNU gdb 6.4.90-debian [license stuff snipped] Attaching to program: /bin/cat, process 6760 [snip other stuff that's not interesting now] (gdb) p close(1) $1 = 0 (gdb) p creat("/tmp/foo3", 0600) $2 = 1 (gdb) q The program is running. Quit anyway (and detach it)? (y or n) y Detaching from program: /bin/cat, process 6760 </code></pre> <p>The <code>p</code> command in GDB will print the value of an expression, an expression can be a function to call, it can be a system call… So I execute a <code>close()</code> system call and pass file handle 1, then I execute a <code>creat()</code> system call to open a new file. The result of the <code>creat()</code> was 1 which means that it replaced the previous file handle. If I wanted to use the same file for stdout and stderr or if I wanted to replace a file handle with some other number then I would need to call the <code>dup2()</code> system call to achieve that result.</p> <p>For this example I chose to use <code>creat()</code> instead of <code>open()</code> because there are fewer parameter. The C macros for the flags are not usable from GDB (it doesn’t use C headers) so I would have to read header files to discover this – it’s not that hard to do so but would take more time. Note that 0600 is the octal permission for the owner having read/write access and the group and others having no access. It would also work to use 0 for that parameter and run chmod on the file later on.</p> <p>After that I verify the result:</p> <pre><code>ls -l /proc/6760/fd/ total 3 lrwx—— 1 rjc rjc 64 2008-02-27 15:32 0 -&gt; /dev/pts/5 l-wx—— 1 rjc rjc 64 2008-02-27 15:32 1 -&gt; /tmp/foo3 &lt;==== lrwx—— 1 rjc rjc 64 2008-02-27 15:32 2 -&gt; /dev/pts/5 </code></pre> <p>Typing more data in to <code>cat</code> results in the file <code>/tmp/foo3</code> being appended to.</p> <p>If you want to close the original session you need to close all file handles for it, open a new device that can be the controlling tty, and then call <code>setsid()</code>.</p> </blockquote>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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