在程序出錯時,我們通過全局變量錯誤號errno和perror函數能夠很快的定位到錯誤原因。
Linux錯誤代碼及其含義
C Name Value Description 含義
Success 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 srmount錯誤
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 A .lib section in an .out is corrupted 庫段. out損壞
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 套接字類型不受支持
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 已運行
EINPROGRESS 115 Operation now in 正在運行
ESTALE 116 Stale NFS file handle 陳舊的NFS文件句柄
EUCLEAN 117 Structure needs cleaning 結構需要清洗
ENOTNAM 118 Not a XENIX-named 不是XENIX命名的
ENAVAIL 119 No XENIX semaphores 沒有XENIX信號量
EISNAM 120 Is a named type file 是一個命名的文件類型
EREMOTEIO 121 Remote I/O error 遠程輸入/輸出錯誤
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而無法操作
暫時記錄這么多,133-255全是Unknown error
二、錯誤處理函數
許多不同的函數庫都在失敗時設置全局變量errno的值來指明失敗原因。值得注意的是函數報告出錯后必須立刻檢查errno變量,因為它有可能被下一個函數調用所覆蓋。
有兩個非常有用的函數可以用來報告出現的錯誤:
#include <string.>
char *strerror(int errnum);
該函數把錯誤代碼映射為一個字符串,對錯誤類型進行說明。
#include <stdio.h>
void perror(const char *s);
該函數也把errno變量中報告的錯誤應為到一個字符串,並把它輸出到標准錯誤輸出流。
---------------------
1-34號錯誤號是在內核源碼的include/asm-generic/errno-base.h定義
35-132則是在include/asm-generic/errno.h中定義
剩下還有一些更大的錯誤號是留給內核級別的,如系統調用等,用戶程序一般是看不見的這些號的,Ubuntu9.10中/usr/src/linux-headers-2.6.31-21-generic/include/linux/errno.h