[Linux服务器] 【Linux】Linux系统错误码errno详解

123 0
Honkers 2025-5-29 11:49:30 来自手机 | 显示全部楼层 |阅读模式

<>博客简介:Linux、rtos系统,arm、stm32等芯片,嵌入式高级工程师、面试官、架构师,日常技术干货、个人总结、职场经验分享

<>公众号:嵌入式技术部落

<>系列专栏:C/C++、Linux、rtos、嵌入式开发、流媒体、数据结构、网络协议、开源库、CMake、Makefile、架构设计模式等

一、前言

在嵌入式Linux开发中,使用errno进行错误处理是非常常见和必要的。嵌入式系统通常需要处理各种硬件和软件的交互,错误处理和调试是确保系统稳定性和可靠性的关键部分。

errno在嵌入式Linux开发中是一个重要的工具,通过合理使用errno,可以有效地捕获和处理各种错误情况,确保系统的稳定性和可靠性。

二、errno 简介

在Linux系统中,errno是一个全局变量,用于表示最近一次系统调用或库函数调用出错时的错误码。每个错误码对应一个特定的错误类型。了解这些错误码有助于调试和处理错误情况。

要使用errno,需要包含头文件#include

Linux系统调用或库函数在出错时会自动设置errno为相应的错误码。可以在调用函数后检查errno的值来确定错误类型,通过 perror() 或 strerror() 函数将错误码转换为可读的错误消息。针对perror() 和 strerror() 函数的详解,可查看文章《【嵌入式】Linux系统错误码errno详解》。

三、errno 注意事项

1、系统调用或库函数返回后应立即检查errno,因为后续的调用可能会修改errno的值。
2、在多线程环境中,errno通常是线程局部的,每个线程都有自己的errno。这意味着在多线程程序中使用errno是安全的。

四、Linux errno 错误码对照表

在头文件「/usr/include/asm-generic/errno-base.h」中对基础的常用 errno 进行了宏定义:

defineerrnoexplain
EPERM1Operation not permitted
ENOENT2No such file or directory
ESRCH3No such process
EINTR4Interrupted system call
EIO5I/O error
ENXIO6No such device or address
E2BIG7Argument list too long
ENOEXEC8Exec format error
EBADF9Bad file number
ECHILD10No child processes
EAGAIN11Try again
ENOMEM12Out of memory
EACCES13Permission denied
EFAULT14Bad address
ENOTBLK15Block device required
EBUSY16Device or resource busy
EEXIST17File exists
EXDEV18Cross-device link
ENODEV19No such device
ENOTDIR20Not a directory
EISDIR21Is a directory
EINVAL22Invalid argument
ENFILE23File table overflow
EMFILE24Too many open files
ENOTTY25Not a typewriter
ETXTBSY26Text file busy
EFBIG27File too large
ENOSPC28No space left on device
ESPIPE29Illegal seek
EROFS30Read-only file system
EMLINK31Too many links
EPIPE32Broken pipe
EDOM33Math argument out of domain of func
ERANGE34Math result not representable

在 「/usr/include/asm-generic/errno.h」 中,对剩余的 errno 做了宏定义:

defineerrnoexplain
EDEADLK35Resource deadlock would occur
ENAMETOOLONG36File name too long
ENOLCK37No record locks available
ENOSYS38Function not implemented
ENOTEMPTY39Directory not empty
ELOOP40Too many symbolic links encountered
EWOULDBLOCKEAGAINOperation would block
ENOMSG42No message of desired type
EIDRM43Identifier removed
ECHRNG44Channel number out of range
EL2NSYNC45Level 2 not synchronized
EL3HLT46Level 3 halted
EL3RST47Level 3 reset
ELNRNG48Link number out of range
EUNATCH49Protocol driver not attached
ENOCSI50No CSI structure available
EL2HLT51Level 2 halted
EBADE52Invalid exchange
EBADR53Invalid request descriptor
EXFULL54Exchange full
ENOANO55No anode
EBADRQC56Invalid request code
EBADSLT57Invalid slot
EDEADLOCKEDEADLK
EBFONT59Bad font file format
ENOSTR60Device not a stream
ENODATA61No data available
ETIME62Timer expired
ENOSR63Out of streams resources
ENONET64Machine is not on the network
ENOPKG65Package not installed
EREMOTE66Object is remote
ENOLINK67Link has been severed
EADV68Advertise error
ESRMNT69Srmount error
ECOMM70Communication error on send
EPROTO71Protocol error
EMULTIHOP72Multihop attempted
EDOTDOT73RFS specific error
EBADMSG74Not a data message
EOVERFLOW75Value too large for defined data type
ENOTUNIQ76Name not unique on network
EBADFD77File descriptor in bad state
EREMCHG78Remote address changed
ELIBACC79Can not access a needed shared library
ELIBBAD80Accessing a corrupted shared library
ELIBSCN81.lib section in a.out corrupted
ELIBMAX82Attempting to link in too many shared libraries
ELIBEXEC83Cannot exec a shared library directly
EILSEQ84Illegal byte sequence
ERESTART85Interrupted system call should be restarted
ESTRPIPE86Streams pipe error
EUSERS87Too many users
ENOTSOCK88Socket operation on non-socket
EDESTADDRREQ89Destination address required
EMSGSIZE90Message too long
EPROTOTYPE91Protocol wrong type for socket
ENOPROTOOPT92Protocol not available
EPROTONOSUPPORT93Protocol not supported
ESOCKTNOSUPPORT94Socket type not supported
EOPNOTSUPP95Operation not supported on transport endpoint
EPFNOSUPPORT96Protocol family not supported
EAFNOSUPPORT97Address family not supported by protocol
EADDRINUSE98Address already in use
EADDRNOTAVAIL99Cannot assign requested address
ENETDOWN100Network is down
ENETUNREACH101Network is unreachable
ENETRESET102Network dropped connection because of reset
ECONNABORTED103Software caused connection abort
ECONNRESET104Connection reset by peer
ENOBUFS105No buffer space available
EISCONN106Transport endpoint is already connected
ENOTCONN107Transport endpoint is not connected
ESHUTDOWN108Cannot send after transport endpoint shutdown
ETOOMANYREFS109Too many references: cannot splice
ETIMEDOUT110Connection timed out
ECONNREFUSED111Connection refused
EHOSTDOWN112Host is down
EHOSTUNREACH113No route to host
EALREADY114Operation already in progress
EINPROGRESS115Operation now in progress
ESTALE116Stale file handle
EUCLEAN117Structure needs cleaning
ENOTNAM118Not a XENIX named type file
ENAVAIL119No XENIX semaphores available
EISNAM120Is a named type file
EREMOTEIO121Remote I/O error
EDQUOT122Quota exceeded
ENOMEDIUM123No medium found
EMEDIUMTYPE124Wrong medium type
ECANCELED125Operation Canceled
ENOKEY126Required key not available
EKEYEXPIRED127Key has expired
EKEYREVOKED128Key has been revoked
EKEYREJECTED129Key was rejected by service
EOWNERDEAD130Owner died
ENOTRECOVERABLE131State not recoverable
ERFKILL132Operation not possible due to RF-kill
EHWPOISON133Memory page has hardware error

五、示例

main.c源码

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. int main(int argc, char *argv[])
  5. {
  6. FILE *file = fopen("./test.txt", "r");
  7. if(NULL == file)
  8. {
  9. printf("fopen err: %s errno = %d\n", strerror(errno), errno);
  10. }
  11. return 0;
  12. }
复制代码

编译运行

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行