0

As a server, I want to read from a non-blocking client socket and then write to a file.

According to the man page for sendfile, if errno is set to EAGAIN, then this only signals that, if the output file descriptor is set to be non-blocking, then a call to sendfile would block.

That is, the underlying call that sendfile makes to write would block.

Is there anyway to use sendfile so that errno is EAGAIN if reading would block?

Muno
  • 575
  • 2
  • 5
  • 20

2 Answers2

2

Certainly.

Using the select() library function with your read descriptor, you can check for EWOULDBLOCK via errno. If it is set, then the read would block.

You cannot see if the read would block by checking something on your sendfile() call.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
0

From sendfile(2) man page:

The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket).

Regular file or file that support mmap will always be regarded as ready, i.e. reading from it "never block". Even if it actually DOES block(e.g. waiting for disk).

YouJiacheng
  • 449
  • 3
  • 11