Macro base::handle_eintr

source ·
macro_rules! handle_eintr {
    ($x:expr) => { ... };
}
Expand description

Macro that retries the given expression every time its result indicates it was interrupted (i.e. returned EINTR). This is useful for operations that are prone to being interrupted by signals, such as blocking syscalls.

The given expression $x can return

  • crate::linux::Result in which case the expression is retried if the Error::errno() is EINTR.
  • std::io::Result in which case the expression is retried if the ErrorKind is ErrorKind::Interrupted.

Note that if expression returns i32 (i.e. either -1 or error code), then handle_eintr_errno() or handle_eintr_rc() should be used instead.

In all cases where the result does not indicate that the expression was interrupted, the result is returned verbatim to the caller of this macro.

See the section titled Interruption of system calls and library functions by signal handlers on the man page for signal(7) to see more information about interruptible syscalls.

To summarize, routines that use one of these syscalls might need to handle EINTR:

  • accept(2)
  • clock_nanosleep(2)
  • connect(2)
  • epoll_pwait(2)
  • epoll_wait(2)
  • fcntl(2)
  • fifo(7)
  • flock(2)
  • futex(2)
  • getrandom(2)
  • inotify(7)
  • io_getevents(2)
  • ioctl(2)
  • mq_receive(3)
  • mq_send(3)
  • mq_timedreceive(3)
  • mq_timedsend(3)
  • msgrcv(2)
  • msgsnd(2)
  • nanosleep(2)
  • open(2)
  • pause(2)
  • poll(2)
  • ppoll(2)
  • pselect(2)
  • pthread_cond_wait(3)
  • pthread_mutex_lock(3)
  • read(2)
  • readv(2)
  • recv(2)
  • recvfrom(2)
  • recvmmsg(2)
  • recvmsg(2)
  • select(2)
  • sem_timedwait(3)
  • sem_wait(3)
  • semop(2)
  • semtimedop(2)
  • send(2)
  • sendmsg(2)
  • sendto(2)
  • setsockopt(2)
  • sigsuspend(2)
  • sigtimedwait(2)
  • sigwaitinfo(2)
  • sleep(3)
  • usleep(3)
  • wait(2)
  • wait3(2)
  • wait4(2)
  • waitid(2)
  • waitpid(2)
  • write(2)
  • writev(2)

§Examples

let mut line = String::new();
let res = handle_eintr!(stdin().read_line(&mut line));