Linux下errno错误信息对照

本文详细介绍了Linux系统中errno错误码的具体含义,通过列举从0到255的所有errno值及其对应的错误描述,帮助读者理解不同错误码的意义。此外,还提供了使用strerror函数输出错误信息的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在库函数中有个errno变量,每个errno值对应着以字符串表示的错误类型。

以下主要来自2.6.32的内核代码中的/usr/include/asm-generic/errno.h及errno-base.h,输出错误原因定义归纳整理如下,我们可以使用如下代码输出

/*************************************************************************
    > File Name: 0.c
    > Author: GatieMe
    > Mail: ftcl@163.com
    > Created Time: Sat 23 Jan 2016 12:07:51 AM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char ** argv)
{
    int i = 0;
    for(i = 0; i < 256; i++)
    {
        printf("| %02d | %s |\n", i, strerror(i));
    }
    return 0;
}

注意

perror( ) 用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno 的值来决定要输出的字符串。

当你调用”某些”函数出错时,该函数已经重新设置了errno的值。perror函数只是将你输入的一些信息和现在的errno所对应的错误一起输出。

errno描述
00Success
01Operation not permitted
02No such file or directory
03No such process
04Interrupted system call
05Input/output error
06No such device or address
07Argument list too long
08Exec format error
09Bad file descriptor
10No child processes
11Resource temporarily unavailable
12Cannot allocate memory
13Permission denied
14Bad address
15Block device required
16Device or resource busy
17File exists
18Invalid cross-device link
19No such device
20Not a directory
21Is a directory
22Invalid argument
23Too many open files in system
24Too many open files
25Inappropriate ioctl for device
26Text file busy
27File too large
28No space left on device
29Illegal seek
30Read-only file system
31Too many links
32Broken pipe
33Numerical argument out of domain
34Numerical result out of range
35Resource deadlock avoided
36File name too long
37No locks available
38Function not implemented
39Directory not empty
40Too many levels of symbolic links
41Unknown error 41
42No message of desired type
43Identifier removed
44Channel number out of range
45Level 2 not synchronized
46Level 3 halted
47Level 3 reset
48Link number out of range
49Protocol driver not attached
50No CSI structure available
51Level 2 halted
52Invalid exchange
53Invalid request descriptor
54Exchange full
55No anode
56Invalid request code
57Invalid slot
58Unknown error 58
59Bad font file format
60Device not a stream
61No data available
62Timer expired
63Out of streams resources
64Machine is not on the network
65Package not installed
66Object is remote
67Link has been severed
68Advertise error
69Srmount error
70Communication error on send
71Protocol error
72Multihop attempted
73RFS specific error
74Bad message
75Value too large for defined data type
76Name not unique on network
77File descriptor in bad state
78Remote address changed
79Can not access a needed shared library
80Accessing a corrupted shared library
81.lib section in a.out corrupted
82Attempting to link in too many shared libraries
83Cannot exec a shared library directly
84Invalid or incomplete multibyte or wide character
85Interrupted system call should be restarted
86Streams pipe error
87Too many users
88Socket operation on non-socket
89Destination address required
90Message too long
91Protocol wrong type for socket
92Protocol not available
93Protocol not supported
94Socket type not supported
95Operation not supported
96Protocol family not supported
97Address family not supported by protocol
98Address already in use
99Cannot assign requested address
100Network is down
101Network is unreachable
102Network dropped connection on reset
103Software caused connection abort
104Connection reset by peer
105No buffer space available
106Transport endpoint is already connected
107Transport endpoint is not connected
108Cannot send after transport endpoint shutdown
109Too many references: cannot splice
110Connection timed out
111Connection refused
112Host is down
113No route to host
114Operation already in progress
115Operation now in progress
116Stale NFS file handle
117Structure needs cleaning
118Not a XENIX named type file
119No XENIX semaphores available
120Is a named type file
121Remote I/O error
122Disk quota exceeded
123No medium found
124Wrong medium type
125Operation canceled
126Required key not available
127Key has expired
128Key has been revoked
129Key was rejected by service
130Owner died
131State not recoverable
132~Unknown error
<think>好的,我现在要解决用户在Linux中使用open函数返回错误码-1的问题。用户提到错误是“Unknown error”,这让我想到需要先理解错误码的来源和处理方法。根据用户提供的引用,特别是引用[2]中提到,Linux中系统函数失败时会返回-1,并设置errno来指示错误类型。用户遇到的“Unknown error”可能是因为错误码没有被正确识别或转换。 首先,我应该检查用户是如何处理错误码的。通常,在调用open函数后,如果返回-1,应该立即检查errno的值,并使用perror或strerror函数来获取对应的错误信息。例如: ```c #include <stdio.h> #include <fcntl.h> #include <errno.h> #include <string.h> int main() { int fd = open("nonexistent.txt", O_RDONLY); if (fd == -1) { perror("open failed"); printf("Error: %s\n", strerror(errno)); } return 0; } ``` 如果用户没有正确包含头文件,比如<errno.h>,或者没有及时获取errno的值(比如在调用其他可能修改errno的函数之后才检查),可能导致获取到错误错误码,从而显示“Unknown error”。此外,某些情况下,错误码可能超出了标准strerror能识别的范围,这时候可能需要检查是否使用了自定义的错误码或第三方库的错误码,但根据引用[1]和引用[3],FFmpeg和NetCDF的错误处理可能与系统错误码不同,但open函数属于系统调用,错误码应来自errno。 接下来,用户可能没有正确安装或配置某些库,导致文件格式不被支持,但open函数本身并不关心文件格式,除非涉及到特定的打开模式。例如,尝试以O_DIRECTORY打开普通文件会导致错误,此时errno会被设置为ENOTDIR。如果用户尝试打开一个不存在的文件,errno会是ENOENT;权限不足则是EACCES等。 另一个可能性是,用户使用的环境或工具链有问题,导致strerror无法正确识别错误码。例如,在某些嵌入式系统中,错误码的描述可能不完整。但一般情况下,标准Linux发行版的strerror应该覆盖所有可能的errno值。 建议用户按照以下步骤排查: 1. 在open调用失败后立即输出errno的值,例如printf("errno: %d\n", errno);,然后对照errno.h中的定义,或者使用命令`errno <编号>`查看具体含义。 2. 确保文件路径正确,权限足够,文件类型正确(例如不是目录)。 3. 检查是否有其他函数在open之后修改了errno,导致获取到错误错误码。 4. 如果错误码确实未知,可能需要检查内核版本或C库版本是否存在已知的问题,或者是否有自定义的错误码被误用。 例如,如果用户调用open时指定的标志有冲突,比如同时使用O_RDONLY和O_WRONLY,可能导致EINVAL,这时候strerror应该能正确解释。但如果返回的错误码不在标准范围内,比如超过系统定义的最大值,就会显示未知错误。这种情况下,可能是系统调用内部出现了异常,或者用户空间与内核头文件不匹配。 此外,根据引用[2],正确获取错误码的方法是立即在函数调用失败后检查errno,因为后续的函数调用可能会覆盖errno的值。例如: ```c fd = open("file", O_RDONLY); if (fd == -1) { int saved_errno = errno; // 立即保存errno fprintf(stderr, "Error opening file: %s\n", strerror(saved_errno)); } ``` 这可以避免在调用其他函数(如perror)前errno被修改。 总结,解决步骤可能包括: - 确认错误码的具体数值。 - 根据错误码查找对应的原因。 - 检查文件路径、权限、模式是否正确。 - 确保正确及时地获取errno的值。</think>在Linux系统中处理`open`函数返回错误码`-1`的解决方案步骤如下: ### 一、立即获取错误码 调用`open`失败后**立即保存`errno`值**,防止其他操作覆盖错误信息: ```c #include <errno.h> int fd = open("test.txt", O_RDONLY); if (fd == -1) { int error_code = errno; // 关键步骤:立即保存错误码 printf("错误码:%d\n", error_code); } ``` ### 二、解析错误类型 通过以下方法将错误码转换为可读信息: 1. **使用`perror`自动附加描述** ```c perror("open失败原因"); // 输出示例:open失败原因: Permission denied ``` 2. **使用`strerror`获取详细信息** ```c #include <string.h> printf("错误描述:%s\n", strerror(error_code)); ``` ### 三、常见错误对照表 | 错误码 | 符号常量 | 典型场景 | |--------|----------------|--------------------------| | 2 | `ENOENT` | 文件/目录不存在 | | 13 | `EACCES` | 权限不足 | | 21 | `EISDIR` | 尝试写操作打开目录 | | 22 | `EINVAL` | 无效参数(如flag冲突) | | 24 | `EMFILE` | 进程打开文件数达上限 | ### 四、针对性解决方案 #### 情况1:文件不存在(ENOENT) ```c // 检查路径是否正确(注意相对路径的基准目录) char *path = "/tmp/testfile"; if (access(path, F_OK) != -1) { fd = open(path, O_RDWR); } else { printf("文件%s不存在\n", path); } ``` #### 情况2:权限不足(EACCES) ```bash # 终端查看文件权限 ls -l /path/to/file # 修改权限 chmod 644 /path/to/file ``` #### 情况3:参数错误(EINVAL) ```c // 检查flags组合是否合法,例如: // 禁止同时指定O_RDONLY和O_WRONLY int flags = O_RDONLY | O_WRONLY; // 错误组合 fd = open("file", flags); // 触发EINVAL ``` ### 五、高级调试技巧 1. **使用`strace`追踪系统调用** ```bash strace -e trace=openat,open ./your_program ``` 2. **检查系统资源限制** ```bash ulimit -n # 查看最大打开文件数 cat /proc/sys/fs/file-max # 系统级文件数上限 ``` [^1]: FFmpeg的错误码处理机制采用POSIX标准错误码的负值形式,类似的错误码转换方法在系统编程中具有参考价值。 [^2]: Windows/Linux系统API错误码获取方式的核心差异在于Windows使用`GetLastError()`,而Linux通过`errno`全局变量实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值