Saturday, October 11, 2014

Linux read()

A read call can return several ways:

If len is returned, the read is successful as expected
If greater than 0 and less then len is returned, the read may be interrupted or EOF reached.  Reissue the read.
If 0 is returned, EOF is reached
If -1 is returned and errno is EINTR, the process is interrupted by a signal.  Reissue the read
If -1 is retruned and errno is EAGAIN, this is a non=blocking read and currently no data is available.  Reissue the call at a latter time
If -1 is returned with errno set to value other than EINTR and EAGAIN, an error has happened and reissuing the call will probably not successful.

EBADF - bad fd passed
EFAULT - the buffer to hold the data is in the process address space
EINVAL - the fd does not allow reading
EIO - IO error has occurred

Note that read can return with partial result (when len is less than the size passed).  Therefore, read should be done in a loop to reissue the call under some conditions above.

No comments: