linux error errno 错误对照

本文详细介绍了Linux系统中错误代码的定义及其对应的含义,通过展示errno宏定义和strerror函数使用示例,帮助读者理解各种错误状态。

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

errno 在 <errno.h> 中定义,错误 Exx 的宏定义在 /usr/include/asm-generic 文件夹下面的  errno-base.h 和 errno.h,分别定义了 1-34 、35-132 的错误定义。

strerror() 函数依据 errno 值返回错误描述字符串,下面程序打印对照表:

#include <errno.h>  
#include <string.h>  
#include <stdio.h>  
  
int main()  
{  
    int i;  
    for(i = 0; i < 140; ++i)  
    {  
        errno = i;  
        printf("errno %d :\t\t%s\n",i,strerror(errno));  
    }  
    return 0;  
}  

错误对照表:

errno0 :     Success

errno1 :     Operation not permitted

errno2 :     No such file or directory

errno3 :     No such process

errno4 :     Interrupted system call

errno5 :     Input/output error

errno6 :     No such device or address

errno7 :     Argument list too long

errno8 :     Exec format error

errno9 :     Bad file descriptor

errno10 :    No child processes

errno11 :    Resource temporarily unavailable

errno12 :    Cannot allocate memory

errno13 :    Permission denied

errno14 :    Bad address

errno15 :    Block device required

errno16 :    Device or resource busy

errno17 :    File exists

errno18 :    Invalid cross-device link

errno19 :    No such device

errno20 :    Not a directory

errno21 :    Is a directory

errno22 :    Invalid argument

errno23 :    Too many open files in system

errno24 :    Too many open files

errno25 :    Inappropriate ioctl for device

errno26 :    Text file busy

errno27 :    File too large

errno28 :    No space left on device

errno29 :    Illegal seek

errno30 :    Read-only file system

errno31 :    Too many links

errno32 :    Broken pipe

errno33 :    Numerical argument out of domain

errno34 :    Numerical result out of range

errno35 :    Resource deadlock avoided

errno36 :    File name too long

errno37 :    No locks available

errno38 :    Function not implemented

errno39 :    Directory not empty

errno40 :    Too many levels of symbolic links

errno41 :    Unknown error 41

errno42 :    No message of desired type

errno43 :    Identifier removed

errno44 :    Channel number out of range

errno45 :    Level 2 not synchronized

errno46 :    Level 3 halted

errno47 :    Level 3 reset

errno48 :    Link number out of range

errno49 :    Protocol driver not attached

errno50 :    No CSI structure available

errno51 :    Level 2 halted

errno52 :    Invalid exchange

errno53 :    Invalid request descriptor

errno54 :    Exchange full

errno55 :    No anode

errno56 :    Invalid request code

errno57 :    Invalid slot

errno58 :    Unknown error 58

errno59 :    Bad font file format

errno60 :    Device not a stream

errno61 :    No data available

errno62 :    Timer expired

errno63 :    Out of streams resources

errno64 :    Machine is not on the network

errno65 :    Package not installed

errno66 :    Object is remote

errno67 :    Link has been severed

errno68 :    Advertise error

errno69 :    Srmount error

errno70 :    Communication error on send

errno71 :    Protocol error

errno72 :    Multihop attempted

errno73 :    RFS specific error

errno74 :    Bad message

errno75 :    Value too large for defined datatype

errno76 :    Name not unique on network

errno77 :    File descriptor in bad state

errno78 :    Remote address changed

errno79 :    Can not access a needed sharedlibrary

errno80 :    Accessing a corrupted sharedlibrary

errno81 :    .lib section in a.out corrupted

errno82 :    Attempting to link in too manyshared libraries

errno83 :    Cannot exec a shared librarydirectly

errno84 :    Invalid or incomplete multibyte orwide character

errno85 :    Interrupted system call should berestarted

errno86 :    Streams pipe error

errno87 :    Too many users

errno88 :    Socket operation on non-socket

errno89 :    Destinationaddress required

errno90 :    Message too long

errno91 :    Protocol wrong type for socket

errno92 :    Protocol not available

errno93 :    Protocol not supported

errno94 :    Socket type not supported

errno95 :    Operation not supported

errno96 :    Protocol family not supported

errno97 :    Address family not supported byprotocol

errno98 :    Address already in use

errno99 :    Cannot assign requested address

errno100 :   Network is down

errno101 :   Network is unreachable

errno102 :   Network dropped connection onreset

errno103 :   Software caused connection abort

errno104 :   Connection reset by peer

errno105 :   No buffer space available

errno106 :   Transport endpoint is alreadyconnected

errno107 :   Transport endpoint is notconnected

errno108 :   Cannot send after transportendpoint shutdown

errno109 :   Too many references: cannot splice

errno110 :   Connection timed out

errno111 :   Connection refused

errno112 :   Host is down

errno113 :   No route to host

errno114 :   Operation already in progress

errno115 :   Operation now in progress

errno116 :   Stale NFS file handle

errno117 :   Structure needs cleaning

errno118 :   Not a XENIX named type file

errno119 :   No XENIX semaphores available

errno120 :   Is a named type file

errno121 :   Remote I/O error

errno122 :   Disk quota exceeded

errno123 :   No medium found

errno124 :   Wrong medium type

errno125 :   Operation canceled

errno126 :   Required key not available

errno127 :   Key has expired

errno128 :   Key has been revoked

errno129 :   Key was rejected by service

errno130 :   Owner died

errno131 :   State not recoverable

errno132 :   Operation not possible due toRF-kill

errno133 :   Unknown error 133

errno134 :   Unknown error 134

errno135 :   Unknown error 135

errno136 :   Unknown error 136

errno137 :   Unknown error 137

errno138 :   Unknown error 138

errno139 :   Unknown error 139

 

由上可见Linux对错误宏的定义。

头文件 /usr/include/asm-generic/errno-base.h 的源码:

#define	EPERM		 1	/* Operation not permitted */
#define	ENOENT		 2	/* No such file or directory */
#define	ESRCH		 3	/* No such process */
#define	EINTR		 4	/* Interrupted system call */
#define	EIO		 5	/* I/O error */
#define	ENXIO		 6	/* No such device or address */
#define	E2BIG		 7	/* Argument list too long */
#define	ENOEXEC		 8	/* Exec format error */
#define	EBADF		 9	/* Bad file number */
#define	ECHILD		10	/* No child processes */
#define	EAGAIN		11	/* Try again */
#define	ENOMEM		12	/* Out of memory */
#define	EACCES		13	/* Permission denied */
#define	EFAULT		14	/* Bad address */
#define	ENOTBLK		15	/* Block device required */
#define	EBUSY		16	/* Device or resource busy */
#define	EEXIST		17	/* File exists */
#define	EXDEV		18	/* Cross-device link */
#define	ENODEV		19	/* No such device */
#define	ENOTDIR		20	/* Not a directory */
#define	EISDIR		21	/* Is a directory */
#define	EINVAL		22	/* Invalid argument */
#define	ENFILE		23	/* File table overflow */
#define	EMFILE		24	/* Too many open files */
#define	ENOTTY		25	/* Not a typewriter */
#define	ETXTBSY		26	/* Text file busy */
#define	EFBIG		27	/* File too large */
#define	ENOSPC		28	/* No space left on device */
#define	ESPIPE		29	/* Illegal seek */
#define	EROFS		30	/* Read-only file system */
#define	EMLINK		31	/* Too many links */
#define	EPIPE		32	/* Broken pipe */
#define	EDOM		33	/* Math argument out of domain of func */
#define	ERANGE		34	/* Math result not representable */

 

 

 

头文件/usr/include/asm-generic/errno.h源码:

#define	EDEADLK		35	/* Resource deadlock would occur */
#define	ENAMETOOLONG	36	/* File name too long */
#define	ENOLCK		37	/* No record locks available */
#define	ENOSYS		38	/* Function not implemented */
#define	ENOTEMPTY	39	/* Directory not empty */
#define	ELOOP		40	/* Too many symbolic links encountered */
#define	EWOULDBLOCK	EAGAIN	/* Operation would block */
#define	ENOMSG		42	/* No message of desired type */
#define	EIDRM		43	/* Identifier removed */
#define	ECHRNG		44	/* Channel number out of range */
#define	EL2NSYNC	45	/* Level 2 not synchronized */
#define	EL3HLT		46	/* Level 3 halted */
#define	EL3RST		47	/* Level 3 reset */
#define	ELNRNG		48	/* Link number out of range */
#define	EUNATCH		49	/* Protocol driver not attached */
#define	ENOCSI		50	/* No CSI structure available */
#define	EL2HLT		51	/* Level 2 halted */
#define	EBADE		52	/* Invalid exchange */
#define	EBADR		53	/* Invalid request descriptor */
#define	EXFULL		54	/* Exchange full */
#define	ENOANO		55	/* No anode */
#define	EBADRQC		56	/* Invalid request code */
#define	EBADSLT		57	/* Invalid slot */

#define	EDEADLOCK	EDEADLK

#define	EBFONT		59	/* Bad font file format */
#define	ENOSTR		60	/* Device not a stream */
#define	ENODATA		61	/* No data available */
#define	ETIME		62	/* Timer expired */
#define	ENOSR		63	/* Out of streams resources */
#define	ENONET		64	/* Machine is not on the network */
#define	ENOPKG		65	/* Package not installed */
#define	EREMOTE		66	/* Object is remote */
#define	ENOLINK		67	/* Link has been severed */
#define	EADV		68	/* Advertise error */
#define	ESRMNT		69	/* Srmount error */
#define	ECOMM		70	/* Communication error on send */
#define	EPROTO		71	/* Protocol error */
#define	EMULTIHOP	72	/* Multihop attempted */
#define	EDOTDOT		73	/* RFS specific error */
#define	EBADMSG		74	/* Not a data message */
#define	EOVERFLOW	75	/* Value too large for defined data type */
#define	ENOTUNIQ	76	/* Name not unique on network */
#define	EBADFD		77	/* File descriptor in bad state */
#define	EREMCHG		78	/* Remote address changed */
#define	ELIBACC		79	/* Can not access a needed shared library */
#define	ELIBBAD		80	/* Accessing a corrupted shared library */
#define	ELIBSCN		81	/* .lib section in a.out corrupted */
#define	ELIBMAX		82	/* Attempting to link in too many shared libraries */
#define	ELIBEXEC	83	/* Cannot exec a shared library directly */
#define	EILSEQ		84	/* Illegal byte sequence */
#define	ERESTART	85	/* Interrupted system call should be restarted */
#define	ESTRPIPE	86	/* Streams pipe error */
#define	EUSERS		87	/* Too many users */
#define	ENOTSOCK	88	/* Socket operation on non-socket */
#define	EDESTADDRREQ	89	/* Destination address required */
#define	EMSGSIZE	90	/* Message too long */
#define	EPROTOTYPE	91	/* Protocol wrong type for socket */
#define	ENOPROTOOPT	92	/* Protocol not available */
#define	EPROTONOSUPPORT	93	/* Protocol not supported */
#define	ESOCKTNOSUPPORT	94	/* Socket type not supported */
#define	EOPNOTSUPP	95	/* Operation not supported on transport endpoint */
#define	EPFNOSUPPORT	96	/* Protocol family not supported */
#define	EAFNOSUPPORT	97	/* Address family not supported by protocol */
#define	EADDRINUSE	98	/* Address already in use */
#define	EADDRNOTAVAIL	99	/* Cannot assign requested address */
#define	ENETDOWN	100	/* Network is down */
#define	ENETUNREACH	101	/* Network is unreachable */
#define	ENETRESET	102	/* Network dropped connection because of reset */
#define	ECONNABORTED	103	/* Software caused connection abort */
#define	ECONNRESET	104	/* Connection reset by peer */
#define	ENOBUFS		105	/* No buffer space available */
#define	EISCONN		106	/* Transport endpoint is already connected */
#define	ENOTCONN	107	/* Transport endpoint is not connected */
#define	ESHUTDOWN	108	/* Cannot send after transport endpoint shutdown */
#define	ETOOMANYREFS	109	/* Too many references: cannot splice */
#define	ETIMEDOUT	110	/* Connection timed out */
#define	ECONNREFUSED	111	/* Connection refused */
#define	EHOSTDOWN	112	/* Host is down */
#define	EHOSTUNREACH	113	/* No route to host */
#define	EALREADY	114	/* Operation already in progress */
#define	EINPROGRESS	115	/* Operation now in progress */
#define	ESTALE		116	/* Stale file handle */
#define	EUCLEAN		117	/* Structure needs cleaning */
#define	ENOTNAM		118	/* Not a XENIX named type file */
#define	ENAVAIL		119	/* No XENIX semaphores available */
#define	EISNAM		120	/* Is a named type file */
#define	EREMOTEIO	121	/* Remote I/O error */
#define	EDQUOT		122	/* Quota exceeded */

#define	ENOMEDIUM	123	/* No medium found */
#define	EMEDIUMTYPE	124	/* Wrong medium type */
#define	ECANCELED	125	/* Operation Canceled */
#define	ENOKEY		126	/* Required key not available */
#define	EKEYEXPIRED	127	/* Key has expired */
#define	EKEYREVOKED	128	/* Key has been revoked */
#define	EKEYREJECTED	129	/* Key was rejected by service */

/* for robust mutexes */
#define	EOWNERDEAD	130	/* Owner died */
#define	ENOTRECOVERABLE	131	/* State not recoverable */

#define ERFKILL		132	/* Operation not possible due to RF-kill */

#define EHWPOISON	133	/* Memory page has hardware 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、付费专栏及课程。

余额充值