Understanding the 'errno' Command (with examples)
The errno
command is a useful utility for programmers and system administrators alike. This command is used to lookup error numbers (errno) alongside their corresponding names and descriptions. It essentially acts as a bridge to understand the error codes generated by system calls and C library functions in Unix-like operating systems. Errors might surface in the form of numerical codes without any explanation, making it challenging to decipher the issue. The errno
utility helps decode these numbers into a more contextual and human-readable format, simplifying troubleshooting and debugging processes.
Use case 1: Lookup errno description by name or code
Code:
errno name|code
Motivation:
When you’re troubleshooting a software error or system issue, you might encounter an error code or name. Understanding what that error signifies is crucial to resolving the problem. By using the errno
command to lookup the description of a specific error code or name, developers and system administrators can quickly gain insights into what went wrong. This helps in pinpointing the issue and deciding on the appropriate course of action.
Explanation:
errno
: This is the command being invoked.name|code
: This argument allows you to specify either the name of the error or its numerical code. For example, you could inputENOMEM
or12
to get information about a “not enough space” error.
Example output:
ENOMEM 12 Not enough space
Use case 2: List all errno names, codes, and descriptions
Code:
errno --list
Motivation:
When managing systems or developing applications, it can be invaluable to have a comprehensive view of all possible error codes and their explanations. This is particularly useful for learning purposes or when writing error-handling code. Knowing all potential errors in advance allows developers to program defensively, planning for unexpected conditions that could arise during a program’s execution.
Explanation:
errno
: This is the command being used.--list
: This option tells the command to provide an extensive list of all errno names, codes, and descriptions. It’s a request to see the entirety of what the utility recognizes in terms of system errors.
Example output:
EPERM 1 Operation not permitted
ENOENT 2 No such file or directory
ESRCH 3 No such process
...
Use case 3: Search for code whose description contains all of the given text
Code:
errno --search text
Motivation:
Sometimes, you might only have a vague understanding of an error message, or perhaps you know the description but not the exact error code. This search functionality allows you to find all errno codes with descriptions that include certain keywords or text fragments. This can dramatically speed up troubleshooting by narrowing down possibilities and focusing only on relevant error descriptions that contain the specific text you are interested in.
Explanation:
errno
: This command is being executed.--search
: This argument specifies that the user wants to perform a search through error descriptions.text
: This is the keyword or phrase that the command will search for within the descriptions of errors.
Example output:
ELOOP 62 Too many levels of symbolic links
ENOTDIR 20 Not a directory
Use case 4: Search for code whose description contains all of the given text (all locales)
Code:
errno --search-all-locales text
Motivation:
In environments where multiple languages or locales are used, it’s important to have the flexibility to search for error codes across all possible language settings. Different locales might present error messages that are phrased differently. Hence, using the --search-all-locales
option ensures that the command queries across all available translations, making it versatile for internationalized systems and applications.
Explanation:
errno
: The main command being executed.--search-all-locales
: This option broadens the search scope to include all locales, ensuring that a search for the given text checks against every available translation or language setting.text
: The specific text or keywords you are searching for within error descriptions.
Example output:
ELOOP 62 Trop de niveaux de liens symboliques
ENOTDIR 20 Pas un répertoire
Conclusion
The errno
command is a powerful tool for anyone dealing with system-level programming or administration on Unix-like systems. It provides invaluable insights into error codes, aiding in diagnosing and resolving issues effectively. Whether you’re seeking an error’s description by name or code, listing all errors, searching by text, or including all locales, errno
streamlines the process of error management, helping maintain robust and reliable systems.