【Linux应用编程】Linux编程中常见错误码含义及查询方式


1 前言

  linux应用程序开发过程中,经常会遇到一些错误信息的返回,存在的可能性有,参数有误、非法访问、系统资源限制、设备/文件不存在、访问权限限制等等。对于这类错误,可以通过perror函数输出具体描述,或者通过strerror函数,将错误码转换为具体描述字符。linux函数调用失败时,错误码存放于一个全局变量errno中。


  • 直接输出错误描述
#include <stdio.h>
void perror(const char *string);

/* 使用伪代码 */
ret = ioctl(fd, SIOCGIFCONF, (char*)&ifc);
if(ret)
{
    
    
	perror("ioctl failed");	/* perror输出字符已带换行符,无需再加换行符 */
}

  • 将错误码转换为具体错误描述
#include <errno.h>
#include <string.h>
char *strerror(int errno);

/* 使用伪代码 */
ret = ioctl(fd, SIOCGIFCONF, (char*)&ifc);
if(ret)
{
    
    
	printf("ioctl failed,%s\n", strerror(errno));
}

  然而一些情况下,为了节约存储空间;或者为了不让用户直接看到详细的错误信息描述,我们期望是输出一个错误码到日志文件中,运维或者开发人员根据具体错误码找到对应的错误信息描述。


2 错误码查询

  linux系统下可以安装“errno”工具,这样可以通过工具命令“errno -ls”很方便地查看错误码的具体描述信息。


  linux系统默认一般是不带“errno”工具的,Ubuntu下执行“errno -ls”,提示需安装该工具。

acuity@ubuntu:~$ errno -ls
The program 'errno' is currently not installed. You can install it by typing:
sudo apt install moreutils

  安装“errno”工具。

acuity@ubuntu:~$sudo apt install moreutils

  查看错误码具体描述信息。

acuity@ubuntu:~$ errno -ls
EPERM 1 Operation not permitted
ENOENT 2 No such file or directory
ESRCH 3 No such process
EINTR 4 Interrupted system call
EIO 5 Input/output error
ENXIO 6 No such device or address
E2BIG 7 Argument list too long
ENOEXEC 8 Exec format error
EBADF 9 Bad file descriptor
ECHILD 10 No child processes
EAGAIN 11 Resource temporarily unavailable
ENOMEM 12 Cannot allocate memory
EACCES 13 Permission denied
EFAULT 14 Bad address

3 错误码翻译表

  对于4.15 linux内核版本,错误码定义文件路径如下,不同版本内核可能不一样,特别是相对较旧版本的内核。

  • 基本错误码定义位于“include/uapi/asm-generic/errno-base.h”

  • 其他错误码定义位于“include/uapi/asm-generic/errno.h”


错误码翻译:

宏名称 键值 描述 涵义
- 0 Success 执行成功
EPERM 1 Operation not permitted 无操作权限
ENOENT 2 No such file or directory 文件或目录不存在
ESRCH 3 No such process 进程不存在
EINTR 4 Interrupted system call 系统调用被中断
EIO 5 I/O error I/O错误
ENXIO 6 No such device or address 设备或地址不存在
E2BIG 7 Arg list too long 参数列表过长
ENOEXEC 8 Exec format error 格式化执行错误
EBADF 9 Bad file number 无效文件描述符
ECHILD 10 No child processes 没有子进程
EAGAIN 11 Try again 再次尝试
ENOMEM 12 Out of memory 内存溢出
EACCES 13 Permission denied 无访问权限
EFAULT 14 Bad address 地址错误
ENOTBLK 15 Block device required 块设备请求
EBUSY 16 Device or resource busy 设备或资源已被使用
EEXIST 17 File exists 文件已存在
EXDEV 18 Cross-device link 无效的交叉链接
ENODEV 19 No such device 设备不存在
ENOTDIR 20 Not a directory 非目录文件
EISDIR 21 Is a directory 这是一个目录文件
EINVAL 22 Invalid argument 参数无效
ENFILE 23 File table overflow 文件表溢出
EMFILE 24 Too many open files 打开的文件过多
ENOTTY 25 Not a tty device 不是一个tty设备
ETXTBSY 26 Text file busy 文本文件被占用
EFBIG 27 File too large 文件过大
ENOSPC 28 No space left on device 磁盘空间不足
ESPIPE 29 Illegal seek 非法的地址偏移
EROFS 30 Read-only file system 只读文件系统
EMLINK 31 Too many links 过多的链接
EPIPE 32 Broken pipe 管道破裂
EDOM 33 Math argument out of domain 数值结果超出范围
ERANGE 34 Math result not representable 数值结果不具代表性
EDEADLK 35 Resource deadlock would occur 资源死锁错误
ENAMETOOLONG 36 Filename too long 文件名过长
ENOLCK 37 No record locks available 没有可用的锁
ENOSYS 38 Function not implemented 功能没有实现
ENOTEMPTY 39 Directory not empty 目录不为空
ELOOP 40 Too many symbolic links encountered 符号链接层次太多
EWOULDBLOCK 41 Same as EAGAIN 与“EAGAIN”错误码类似
ENOMSG 42 No message of desired type 没有期望类型的消息
EIDRM 43 Identifier removed 标识符已经被删除
ECHRNG 44 Channel number out of range 通道数目超出范围
EL2NSYNC 45 Level 2 not synchronized 2级不同步
EL3HLT 46 Level 3 halted 3级中断
EL3RST 47 Level 3 reset 3级复位
ELNRNG 48 Link number out of range 链接数超出范围
EUNATCH 49 Protocol driver not attached 协议驱动程序没有连接
ENOCSI 50 No CSI structure available 没有可用CSI结构
EL2HLT 51 Level 2 halted 2级中断
EBADE 52 Invalid exchange 无效的交换
EBADR 53 Invalid request descriptor 请求描述符无效
EXFULL 54 Exchange full 交换空间已满
ENOANO 55 No anode -
EBADRQC 56 Invalid request code 无效的请求代码
EBADSLT 57 Invalid slot 无效的槽函数
EDEADLOCK 58 Same as EDEADLK 与“EDEADLK”错误码类似
EBFONT 59 Bad font file format 错误的字体文件格式
ENOSTR 60 Device not a stream 非数据流设备
ENODATA 61 No data available 无可用数据
ETIME 62 Timer expired 定时器溢出
ENOSR 63 Out of streams resources 数据流相关资源溢出
ENONET 64 Machine is not on the network 机器未连接到网络
ENOPKG 65 Package not installed 软件包未安装
EREMOTE 66 Object is remote -
ENOLINK 67 Link has been severed (软/硬)链接已被断开
EADV 68 Advertise error 广播错误
ESRMNT 69 Srmount error -
ECOMM 70 Communication error on send 通信状态在发送过程出错
EPROTO 71 Protocol error 协议错误
EMULTIHOP 72 Multihop attempted 多次尝试
EDOTDOT 73 RFS specific error RFS特定的错误
EBADMSG 74 Not a data message 非有效的数据消息
EOVERFLOW 75 Value too large for defined data type 数值超出定义的数据类型
ENOTUNIQ 76 Name not unique on network 命名在网络中非唯一
EBADFD 77 File descriptor in bad state 文件描述符损坏
EREMCHG 78 Remote address changed 远程地址已更改
ELIBACC 79 Cannot access a needed shared library 无法访问必要的共享库
ELIBBAD 80 Accessing a corrupted shared library 访问已损坏的共享库
ELIBSCN 81 .lib section in a.out corrupted 库文件部分损坏
ELIBMAX 82 Linking in too many shared libraries 链接共享库过多
ELIBEXEC 83 Cannot exec a shared library directly 不能执行一个共享库
EILSEQ 84 Illegal byte sequence 非法字节序列
ERESTART 85 Interrupted system call should be restarted 需重新启动中断系统
ESTRPIPE 86 Streams pipe error 管道流出错
EUSERS 87 Too many users 访问用户过多
ENOTSOCK 88 Socket operation on non-socket 非套接字函数访问套接字描述符
EDESTADDRREQ 89 Destination address required 请求目的地址
EMSGSIZE 90 Message too long 消息长度过长
EPROTOTYPE 91 Protocol wrong type for socket socket协议类型错误
ENOPROTOOPT 92 Protocol not available 协议不可用
EPROTONOSUPPORT 93 Protocol not supported 不支持的协议
ESOCKTNOSUPPORT 94 Socket type not supported 不支持的socket类型
EOPNOTSUPP 95 Operation not supported on transport 不支持的操作
EPFNOSUPPORT 96 Protocol family not supported 不支持的协议族
EAFNOSUPPORT 97 Address family not supported by protocol 协议不支持该地址
EADDRINUSE 98 Address already in use 地址已被使用
EADDRNOTAVAIL 99 Cannot assign requested address 无法分配请求的地址
ENETDOWN 100 Network is down 网络已被断开
ENETUNREACH 101 Network is unreachable 互联网不可用
ENETRESET 102 Network dropped 网络连接丢失
ECONNABORTED 103 Software caused connection 软件导致连接中断
ECONNRESET 104 Connection reset by 连接被重置
ENOBUFS 105 No buffer space available 没有可用的缓冲空间
EISCONN 106 Transport endpoint 传输端点已经连接
ENOTCONN 107 Transport endpoint 传输终点没有连接
ESHUTDOWN 108 Cannot send after transport 传输后无法发送
ETOOMANYREFS 109 Too many references 请求用户过多
ETIMEDOUT 110 Connection timed 连接超时
ECONNREFUSED 111 Connection refused (远程端)拒绝连接
EHOSTDOWN 112 Host is down 主机已关闭
EHOSTUNREACH 113 No route to host 主机未找到路由
EALREADY 114 Operation already 操作已执行
EINPROGRES 115 Operation now in 操作正在执行
ESTALE 116 Stale NFS file handle NFS文件句柄已过期
EUCLEAN 117 Structure needs cleaning 结构需要清除
ENOTNAM 118 Not a XENIX-named -
ENAVAIL 119 No XENIX semaphores 没有“XENIX”信号量
EISNAM 120 Is a named type file 已定义的文件类型
EREMOTEIO 121 Remote I/O error 远程IO错误
EDQUOT 122 Quota exceeded 超出磁盘配额
ENOMEDIUM 123 No medium found 没有发现媒体介质
EMEDIUMTYPE 124 Wrong medium type 媒体类型错误
ECANCELED 125 Operation Canceled 操作被取消
ENOKEY 126 Required key not available 所需键值不可用
EKEYEXPIRED 127 Key has expired 键值已过期
EKEYREVOKED 128 Key has been revoked 键值已被撤销
EKEYREJECTED 129 Key was rejected by service 键值被拒绝
EOWNERDEAD 130 Owner died 用户注销
ENOTRECOVERABLE 131 State not recoverable 状态不可恢复
ERFKILL 132 Operation not possible due to RF-kill 由于RF-kill而无法操作
EHWPOISON 133 Memory page has hardware error 内存页存在硬件错误
- 134—139 Reserve 保留

猜你喜欢

转载自blog.csdn.net/qq_20553613/article/details/99687463