You can avoid EINTR
from read()
and write()
by ensuring all your signal handlers are installed with the SA_RESTART
flag of sigaction()
.
However this does not protect you from short reads / writes. This is only possible by putting the read()
/ write()
into a loop (it does not require an additional buffer beyond the one that must already be supplied to the read()
/ write()
call.)
Such a loop would look like:
/* If return value is less than `count', then errno == 0 indicates end of file,
* otherwise errno indicates the error that occurred. */
ssize_t hard_read(int fd, void *buf, size_t count)
{
ssize_t rv;
ssize_t total_read = 0;
while (total_read < count)
{
rv = read(fd, (char *)buf + total_read, count - total_read);
if (rv == 0)
errno = 0;
if (rv < 1)
if (errno == EINTR)
continue;
else
break;
total_read += rv;
}
return rv;
}