title: Block Linux read(2) until all of count bytes have arrived
body: <p>I'm using <code>read (2)</code> to read from a file (<code>/dev/random</code>, where data arrives very slowly).</p>
  
  <p>However, read() returns after reading only a few bytes, while I'd like it to wait until the specified amount of bytes has been read (or an error has occurred), so the return value should always be count, or -1.</p>
  
  <p>Is there any way to enable this behaviour? The <code>open (2)</code> and <code>read (2)</code> manpages do not contain any useful information on that topic, nor have I found any information regarding the topic on the Internet.</p>
  
  <p>I am fully aware of the workaround of just putting the <code>read()</code> inside a while loop and calling it until all data has been read. I'd just like to know if this can be achieved in a proper way that yields deterministic behaviour and involves only O(1) syscalls, instead of the nondeterministic O(n) in case of the while loop solution.</p>
  
  <p>The following minimal example reproduces the problem.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  
  int main() {
          int fd = open("/dev/random", 0);
  
          char buf[128];
          size_t bytes = read(fd, buf, sizeof(buf));
  
          printf("Bytes read: %lu\n", bytes); //output is random, usually 8.
  
          close(fd);
  }
  </code></pre>