1. Overview

The standard error codes are significant because they help us understand what went wrong and how to fix it. They also allow us to handle different types of errors consistently and predictably. For instance, we can use the exit command to terminate a program with a specific error code. We can also use the trap command to execute a custom action when a certain error code occurs.

In this article, we’ll explore the standard error codes in Linux. We’ll explain their meaning, implications, and some examples and scenarios where they are typically encountered.

2. What Are Error Codes?

Error codes are numerical values that indicate the type and cause of an error that occurred during the execution of a program or a command. They’re useful for debugging and troubleshooting purposes, as well as for scripting and automation.

Error codes can be divided into two categories: standard error codes and user-defined error codes. Standard error codes are predefined by the system and have a common meaning across different programs and commands. User-defined error codes are defined by the programmer and have a specific meaning for a particular program.

3. Standard Error Code Range

The standard error codes in Linux range from 1 to 133. They’re defined in the header file /usr/include/asm-generic/errno-base.h, which contains the basic error codes, and in the header file /usr/include/asm-generic/errno.h. which contains the extended error codes for Linux.

The standard error codes in Linux are compatible with the POSIX standard. However, some error codes may have different meanings or behaviors on different systems.

4. Common Error Codes and Their Meaning

In this section, we’ll discuss some of the most common standard error codes in Linux, their meaning and implications, and some examples and scenarios where they’re typically encountered.

4.1. EPERM 1 Operation Not Permitted

This error code means that the user or the process doesn’t have the required permission to operate. For example, it can occur when trying to modify a read-only file or when trying to execute a command that requires root privileges.

To avoid this error, we need to ensure that we have the appropriate permission for the operation. We can do this either by changing the ownership of the file or by using the sudo command.

4.2. ENONENT 2 No Such File or Directory

This means that the file or the directory specified in the operation doesn’t exist. This error can occur when opening a non-existent file or when changing to a non-existent directory.

We can avoid this error by ensuring we typed the correct name and path of the file or the directory. Also, we can check if another process has deleted or moved the file or directory.

4.3. EIO 5 Input/output Error

This means that an input/output error occurred during the operation. It can occur when there’s a problem with the disk, the network, or the device involved in the operation.

To avoid this, we need to check the status and the connection of the device that’s causing the error. Then, we can fix any issues that may be affecting its functionality.

4.4. EACCES 13 Permission Denied

This means that we can’t complete the operation because the user doesn’t have the required permission to access the resource. This error can occur when trying to open a file that isn’t readable. Also, it can occur when executing a file that isn’t executable.

To avoid this, we need to have the appropriate permission for the resource. This can be done either by changing the file mode or the ownership of the file. It can also be by using the sudo command to run the operation as a superuser.

4.5. EINVAL 22 Invalid Argument

This error code means that the argument passed to the operation is invalid. It can occur when trying to set an invalid option or when trying to use a function with an incompatible parameter.

To avoid this error, we need to pass a valid and supported argument to the operation. Then, we should make sure it matches the expected type and format of the function. We can also consult the manual page of the command for more information about its usage.

4.6. ENOSPC 28 No Space Left on Device

This means that the device specified in the operation has no space left and can’t store any more data. This can occur when trying to write to a full disk or when trying to create a new file on a full filesystem.

To avoid this, we should free up some space on the device by deleting some unnecessary files or by moving data. We can also use tools like df and du to check how much space is available and used on each device and directory.

4.7. EROFS 30 Read-only File System

This means that we can’t perform the operation because the file system specified is read-only, and we can’t modify it. This error can occur when trying to mount a filesystem with write permission.

To avoid this error, we’ll make sure that we’ve mounted the filesystem with the correct options. We can also use the mount command to check the current status and options of each file system.

4.8. EPIPE 32 Broken Pipe

This means that the operation can’t be performed because the pipe used for communication between processes has broken. This can occur when writing to a pipe that has no reader. Also, when reading from a pipe that has no writer.

To avoid this, both ends of the pipe should be connected and active and also synchronized in their communication. We can also use signals or flags to handle the case when one end of the pipe terminates.

5. Interpreting Standard Error Codes

One of the first steps to troubleshoot an error is to understand what it means and what caused it. To do that, we can use some strategies to interpret the error codes effectively.

One strategy is to use the errno command to display the name and description of the error code. For example, if we get an error code of 2, we can run errno 2 to see that it means ENONENT 2 No such file or directory.

Another strategy is to look up the error code in the header files that contain these error codes. For example, if we get an error code of 13, we can open the file and search for 13 to see that it means #define EACCES 13 /* Permission denied */.

We can also access the standard error codes by using the errno variable or the perror function. For example, if we open a non-existent file, we’ll get an error code of 2, which means ENONENT 2 No such file or directory. We can use the errno variable or the perror function to get this information:

#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp = fopen("test.txt", "r"); // try to open a non-existent file
    if (fp == NULL) { // check if the operation failed
        printf("errno = %d\n", errno); // print the errno value
        perror("fopen"); // print the error message
    }
    return 0;
}

If we save this code in main.c and compile it using gcc main.c, it’ll create an executable file a.out. Running this file will give the following output:

errno = 2
fopen: No such file or directory

This shows that errno is a variable that contains the error code and perror prints the error description.

6. Conclusion

In this article, we’ve learned about the standard error codes in Linux. We explained some of the basic standard error codes we can encounter, their implications, and some examples and scenarios of these error codes. We’ve also learned some strategies for interpreting error codes.

It’s quite important to always handle different types of errors predictably when writing scripts. Knowing these standard errors and being able to interpret them is necessary to be able to handle them effectively.