报告错误状态
本节主要是函数错误信息提示,三个点:1. 错误状态报告机制、2. 目前的错误码编号、3. 错误码相关的函数。
错误码机制
对于大多数的库函数,在失败的时候返回 -1、 EOF 或者空指针来告知用户函数条用失败。而错误的原因会保存在一个全局变量里。这个变量即 errno。
errno 的声明如下:
volatile int errno;
声明 volatile
关键字是可以避免异步处理的时候(多线程或者异步信号),编译器将其优化而失去记录错误信息的机制。所以推荐对于异步处理,先保存错误码、再执行函数调用、三而恢复错误码。
由于系统之前的差异,不同的系统可能存在使用不同的错误码表示同一个错误原因,如:EWOULDBLOCK、EAGAIN。所以如果期望有更好的移植性,建议对两个错误码都进行处理。
目前的错误码
目前记录的错误码值从 1 (EPERM
)到 133(EHWPOISON
)。在编码的时候可以使用 #include <errno.h>
来包含这些错误码的宏定义。如果想要查看,可以在依次索引头文件,最终将追查到两个头文件:asm-generic/errno-base.h
和asm-generic/errno.h
。前面个头文件包含前 34 个基本错误码的定义,后一个头文件定义了其与的错误码,可以通过头文件里的注释快速了解错误码的含义,从而采取一定的措施。
有的错误码不会出现在 GNU 系统里或者说是通常情况下不会出现,而是其他系统使用或者特殊条件下出现。比如:错误码 14:EFAULT
表示非法地址访问,对于没有阻塞段错误信号的程序来说,该错误码是不会被设置的。更多细节参见The GNU C Library Reference Manuel,也可以留言交流。
错误信息转换
对于人来说,一段错误描述当然好过一个冷冰冰的数字,所以将错误码转换为一段错误提示是一件很有意义的事情。当然,如果统计一下错误个数,也是极好的一件事情。
分三个点:1. 错误码转错误信息、2. 错误和退出联合函数 3. 错误记录方式
char * strerror (int errnum); // string.h
char * strerror_r(int errnum, char *buf, size_t n); // string.h
void perror(const char *message); // stdio.h
上面的三个函数根据指定或者当前的错误码打印一段错误描述信息。
// GNU 扩展的,如果要使用,需要定义
char * program_invocation_name;
char * program_invocation_short_name;
上面两个变量分别用于记录程序的全路径程序名称和不带路径的短名称。如:/usr/bin/cat
和cat
之间的关系。使用前需要定义#define _GNU_SOURCE
,因为是 GNU 扩展特性。
对于出现错误,有的时候期望错误之后立即退出,或者记录错误个数。于是对于 GNU 有了下面的函数和变量。
void error(int status, int errnum, const char *format, ...);
void error_at_line(int status, int errnum, const char *fname,
unsigned int lineno, const char *format, ...);
void (*error_print_progname) (void);
unsigned int error_message_count;
int error_one_per_line;
如果status
非0,error
和error_at_line
会在打印错误信息之后以exit(status)
方式退出。error_print_progname
如果被指定,将在打印错误信息的时候被调用来替换之前打印程序名称的方法。error_message_count
用于记录错误发生的个数,也就是 error
或error_at_line
被调用的总次数。
对于 error_on_per_line
,仅对error_at_line
管用,当被设置为非0,error_at_line
对于同一个错误位置的错误,只打印一次。
对于 BSD 系统,还在err.h
里定义了类似的 warn
、err
等函数,这里不打算介绍。
更多细节参见The GNU C Library Reference Manuel
附录 – 错误码
基本错误码(asm-generic/errno-base.h)
#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _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 */
#endif
扩展错误码(asm-generic/errno.h)
#ifndef _ASM_GENERIC_ERRNO_H
#define _ASM_GENERIC_ERRNO_H
#include <asm-generic/errno-base.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 */
#endif