Skip to content
systemcoding.dev
Systems Programming

Common errno Values and What They Mean

A cheat sheet of the errno values you meet most on Linux, such as ENOENT, EACCES, EINTR and EAGAIN, with the cause, the reaction and when not to retry.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Linux and GNU logos.

Linux defines over a hundred error numbers, but a working programmer meets about two dozen. This cheat sheet lists those, grouped by what they usually tell you, with the common cause and the reaction that makes sense. The numbers shown are those of Linux on x86-64 and most other architectures, taken from asm-generic/errno-base.h and errno.h, and the descriptions are those of the errno(3) man page. On a few architectures such as Alpha and MIPS, some numbers differ, which is the reason code should use the names. Every entry has a "when not to" note, because the common mistake is to apply a fix, usually retrying, where it does not belong. If you are new to errno, start with the post on what errno is.

How to read the entries

Each entry gives the name, the number on x86-64 Linux, the meaning in plain words, a typical cause, and what to do. The "when not to" line says which reaction to avoid.

Path and file errors

ENOENT (2), no such file or directory. The path, or one of its parent directories, does not exist. It also appears when a symbolic link points at a missing target, and when execve cannot find the interpreter named in a script's #! line, in which case the message is misleading because the script itself exists. React by creating the file if that is your intent, or by reporting the path to the user. When not to: do not retry. The file will not appear by waiting, unless another program is creating it, and then you should wait on an event and not loop.

EEXIST (17), file exists. A creation call with O_EXCL, or mkdir, found the name already taken. This is often the success signal you want: open(path, O_CREAT | O_EXCL) failing with EEXIST means another process won the race. When not to: do not delete the file and retry blindly, because that breaks the guarantee the exclusive flag provides.

ENOTDIR (20), not a directory. A component of the path that should be a directory is a file. When not to: do not treat it as ENOENT, since the entry exists and the structure is wrong.

EISDIR (21), is a directory. You tried to open a directory for writing, or to read from one. When not to: do not try to read it as a file. Use opendir and readdir.

ENAMETOOLONG (36), file name too long. A name is over NAME_MAX (255 bytes on most filesystems) or the path is over PATH_MAX (4096 on Linux). When not to: do not truncate the path and try again, because you will address a different file.

Permission errors

EACCES (13), permission denied. The mode bits, an ACL or a security module refused the access, or a directory on the path lacks the execute bit, which is the permission to traverse it. React by reporting the path and the mode needed. When not to: do not retry, and do not widen the permissions from code.

EPERM (1), operation not permitted. The operation needs privilege or ownership that the process lacks, such as changing the owner of a file with chown or setting a real-time priority. The distinction from EACCES is that EPERM refuses the operation itself, not access to a file. When not to: do not retry. Report that the program needs a capability or root.

EROFS (30), read-only file system. A write was attempted on a filesystem mounted read-only. A filesystem can switch to read-only after an I/O error, so seeing this on a path that used to work is a warning about the disk. When not to: do not loop.

Resource errors

ENOMEM (12), out of memory. The kernel could not allocate memory, or an address space limit was reached. malloc itself reports ENOMEM when it fails. React by freeing caches or failing the operation cleanly. When not to: do not retry in a tight loop, because memory will not be freed by spinning.

EMFILE (24), too many open files. The process reached its descriptor limit, set by RLIMIT_NOFILE, commonly 1024 by default for the soft limit. It usually means a descriptor leak. When not to: do not just raise the limit as the first step. Look for the leak.

ENFILE (23), too many open files in system. The system-wide table is full. It is rarer than EMFILE, and it is not the program's fault alone.

ENOSPC (28), no space left on device. The filesystem is full, or it has run out of inodes. Check both with df -h and df -i. When not to: do not retry immediately. Free space first.

EDQUOT (122), disk quota exceeded. The user's or group's quota was reached. The disk may have room, so do not conclude the volume is full.

Transient errors

EINTR (4), interrupted system call. A signal handler ran during a blocking call. React by retrying read, write, open and accept. When not to: do not retry close on Linux, and do not retry connect by calling it again, which can give EALREADY; wait for writability and check SO_ERROR instead.

EAGAIN (11), resource temporarily unavailable. On Linux, EWOULDBLOCK has the same value. A non-blocking read has no data, a non-blocking write has no room, or a fork hit the process limit, or a lock is held. React by waiting for readiness with poll or epoll. When not to: do not spin on it, which burns CPU.

EBUSY (16), device or resource busy. A mount point is in use, or a lock or lock-like resource is held. Retrying after a short delay is sometimes right, for instance when unmounting.

ETIMEDOUT (110), connection timed out. A network operation, usually connect, got no answer in time. React by retrying with back-off, within limits. When not to: do not retry without a cap, or you will hammer a dead host.

Argument and state errors

EINVAL (22), invalid argument. An argument value is not valid for the call: a bad flag, a misaligned buffer for O_DIRECT, a zero length where one is not allowed. It is the most common bug-report error and the least informative. React by reading the man page's EINVAL conditions for that call. When not to: do not retry. The call will fail identically.

EBADF (9), bad file descriptor. The descriptor is not open, or is open in the wrong mode. A closed-and-reused descriptor number is the classic cause. When not to: do not try to close it again; it points to a double close.

EFAULT (14), bad address. A pointer passed to the kernel is outside your address space. It indicates a bug in your code.

EPIPE (32), broken pipe. You wrote to a pipe or socket whose reader has gone. The process also receives SIGPIPE by default and dies before it sees the error, unless the signal is ignored or blocked. When not to: do not retry the write.

ECONNREFUSED (111), connection refused. Nothing is listening at the address. When not to: do not treat it as a timeout. The host answered, and it said no.

A lookup table in code

A table that maps numbers to names helps when you debug from a core dump or a log that has only digits. This program prints the name and message for a few values, and it shows the pattern for building your own.

errno_table.cc
#include <errno.h>
#include <stdio.h>
#include <string.h>

#define E(x) { x, #x }

static const struct { int code; const char *name; } table[] = {
    E(ENOENT), E(EACCES), E(EINTR), E(EAGAIN), E(EINVAL), E(EPIPE),
};

int main(void)
{
    for (size_t i = 0; i < sizeof table / sizeof table[0]; i++)
        printf("%-8s %3d  %s\n", table[i].name, table[i].code,
               strerror(table[i].code));
    return 0;
}
Text
ENOENT     2  No such file or directory
EACCES    13  Permission denied
EINTR      4  Interrupted system call
EAGAIN    11  Resource temporarily unavailable
EINVAL    22  Invalid argument
EPIPE     32  Broken pipe

On a Linux machine, errno -l from the moreutils package lists every name with its number, and errno 13 explains a number.

Share

Keep reading

Flat illustration of two options weighed side by side, with the macOS, Linux and Apple logos.
Kernels

Mach Traps vs BSD System Calls on macOS

Learn how macOS routes one trap instruction to either a Mach trap or a BSD system call, how the number sign and class decide, and why you should call libSystem.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.