Search code examples
clinuxposixsystem-callsinterruption

When to use HANDLE_EINTR or HANDLE_EAGAIN?


I'm writing a web server in C and I often use system calls that on error return -1 and set "errno" variable an appropriate value. Some system calls can return EINTR and/or EAGAIN. I have two wrappers HANDLE_EINTR, HANDLE_EAGAIN that use both these error values and retry the system call.

The man pages usually refer if a system call returns EINTR and/or EAGAIN but for some system calls it really does not. Also some system calls may return EINTR / EAGAIN not directly but through the failure of other system calls that might be used in it.

I would like to ask if I could use HANDLE_EINTR and/or HANDLE_EAGAIN regardless of what the API declares (which is not always complete).

Also, I know from Google people that using HANDLE_EINTR with "close" system call (although the API refer to use it) is not a good idea, so I do not use it. Are there any other system calls that have this behavior?

Thank you.


Solution

  • The semantics of EINTR and EAGAIN are well defined. If a particular system call is not documented as potentially returning a particular errno, there's no harm in having a handler for it.

    It is quite common for large applications to implement a single error handler that takes appropriate action for a wide range of errnos, and use that same error handler to appropriately handle error conditions from many different syscalls, even though a particular system call might not be documented as potentially returning a particular errno.