title: stdbuf with setuid/capabilities
body: <p>I am reading output from another process which generates output (slow and infinite). Because I want to read this data in real-time I use "stdbuf -oL" (line-buffered, data is text). I do not have control of the generating process so I cannot modify the source to force flushing.</p>
  
  <p>So far stdbuf works just fine, however the process uses SOCK_RAW and needs either to be run as root, have setuid(0) or the <code>cap_net_raw</code> capability. When running as non-root with setuid or capabilities stdbuf seems to be ignored. Let me demonstrate the problem:</p>
  
  <p>This is a simple writer:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(){
          int i;
          for ( i = 0;; i++){
                  fprintf(stdout, "%d\n", i);
                  sleep(1);
          }
  }
  </code></pre>
  
  <p>And a simple reader:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(){
          char* line = NULL;
          size_t n = 0;
          while (getline(&amp;line, &amp;n, stdin) != -1 ) {
                  fputs(line, stdout);
          }
  }
  </code></pre>
  
  <p>As expected, by executing <code>./writer | ./reader</code> nothing shows up until the buffer is filled. Prepending <code>stdbuf -oL</code> enables line-buffering and I get the lines into the reader:</p>
  
  <pre><code>% stdbuf -oL ./writer | ./reader
  0
  1
  2
  ...
  </code></pre>
  
  <p>But if I add <code>cap_net_raw+ep</code> it stops working:</p>
  
  <pre><code>% sudo setcap cap_net_raw+ep ./writer
  % stdbuf -oL ./writer | ./reader
  (no output)
  </code></pre>
  
  <p>The same behaviour is observed when using setuid:</p>
  
  <pre><code>% sudo chown root:root ./writer
  % sudo chmod +s ./writer
  % stdbuf -oL ./writer | ./reader
  (no output)
  </code></pre>
  
  <p>I'm interested in understanding why this happens and how I can continue to use stdbuf without running as root. I admit that I do not fully understand what setuid is doing behind the scenes.</p>