networking · beginner · ~15 min

Is the error retriable?

Distinguish transient errors from fatal ones.

Challenge

Some socket errors are transient and mean "just try the call again"; others are fatal. Classify an errno as retriable or not.

Task

Implement int is_retriable(int err) that returns 1 for EAGAIN (would block) or EINTR (interrupted by a signal), otherwise 0.

Input

One int err — an errno value.

Output

Returns 1 if err is EAGAIN or EINTR, else 0.

Example

is_retriable(EAGAIN)         ->   1
is_retriable(EINTR)          ->   1
is_retriable(ECONNREFUSED)   ->   0

Input format

One integer err (an errno value).

Output format

1 if err is EAGAIN or EINTR, else 0.

Constraints

EAGAIN and EINTR are the retriable (transient) errors.

Starter code

#include <errno.h>

int is_retriable(int err) {
    /* TODO */
    return 0;
}

Common mistakes

Assuming read/write transmits all requested bytes at once; ignoring socket file descriptor cleanup; unhandled endianness conversions.

Edge cases to handle

Short reads/writes; peer closing socket (recv returns 0); signal interruption (EINTR); non-blocking EAGAIN.

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.