linux下錯誤的捕獲:errno和strerror的使用
經常在呼叫linux 系統api 的時候會出現一些錯誤,比方說使用open() write() creat()之類的函式有些時候會返回-1,也就是呼叫失敗,這個時候往往需要知道失敗的原因。這個時候使用errno這個全域性變數就相當有用了。
在程式程式碼中包含 #include
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
int fd;
extern int errno;
if((fd =open("/dev/dsp",O_WRONLY)) < 0 )
{
printf("errno=%d\n",errno);
}
exit(0);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
如果dsp裝置忙的話errno值將是16。
errno.h中定義的錯誤程式碼值如下:
查 看錯誤程式碼errno是除錯程式的一個重要方法。當linuc C api函式發生異常時,一般會將errno變數(需include errno.h)賦一個整數值,不同的值表示不同的含義,可以通過檢視該值推測出錯的原因。在實際程式設計中用這一招解決了不少原本看來莫名其妙的問題。比較 麻煩的是每次都要去linux原始碼裡面查詢錯誤程式碼的含義,現在把它貼出來,以後需要查時就來這裡看了。
以下來自linux 2.4.20-18的核心程式碼中的/usr/include/asm/errno.h
#ifndef _I386_ERRNO_H
#define _I386_ERRNO_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 /* Arg 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 */
#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 NFS 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 */
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
同時也可以使用strerror()來自己翻譯
如:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
int fd;
extern int errno;
if((fd =open("/dev/dsp",O_WRONLY)) < 0)
{
printf("errno=%d\n",errno);
char * mesg = strerror(errno);
printf("Mesg:%s\n",mesg);
}
exit(0);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
dsp裝置忙的話將輸出如下:
errno=16
Mesg:Device or resource busy
相關推薦
linux下錯誤的捕獲:errno和strerror的使用
經常在呼叫linux 系統api 的時候會出現一些錯誤,比方說使用open() write() creat()之類的函式有些時候會返回-1,也就是呼叫失敗,這個時候往往需要知道失敗的原因。這個時候使用errno這個全域性變數就相當有用了。 在程式程式碼中包含 #incl
linux下的錯誤捕獲errno和strerror()
#define ESUC 0 /* Success */ #define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No suc
總結:linux下安裝mplayer詳細步驟和問題解決
3.安裝skin [[email protected] skin]# ls Blue-1.8.tar.bz2 standard-1.9.tar.bz2 [[email protected] skin]# tar xf Blue-1.8.tar.bz2 [[email pr
處理VS2010+qt4.8.6下編譯不報錯誤,而Linux下報錯:error: 'nullptr' was not declared in this scope
今天處理一個同事在VS2010+qt4.8.6下編寫的程式(能夠正確編譯並執行) 我需要將這個程式移植到arm板上,於是在Linux下對此程式進行交叉編譯 於是,問題來了 這個錯誤提示是:nullptr在當前作用域沒有被宣告。 稍作分析,這個nullptr並不是程式定義的,
在Ubuntu/Linux環境下使用MySQL:啟動和關閉MySQL服務
作業系統:Ubuntu 17.04 64位 MySQL版本:MySQL 5.7 一、啟動MySQL服務 使用以下命令來啟動MySQL: service mysql start 順便提一下,serv
Linux實戰第二篇:Centos6和Centos7密碼破解實戰
root恢復個人筆記分享(在線閱讀):http://note.youdao.com/noteshare?id=744d026219e72c69e606f115bd333ddcPDF版本下載請在附近中下載本文出自 “人才雞雞” 博客,請務必保留此出處http://6575793.blog.51cto.com/6
LINUX下GDB反匯編和調試
出了 恢復 而是 約定 簡單的 難度 tar link 並且 Linux下的匯編與Windows匯編最大的不同就是第一個操作數是原操作數,第二個是目的操作數。而Windows下卻是相反。 1、 基本操作指令 簡單的操作數類型說明。一般有三種。 (1)馬
linux下安裝mongodb的方法和終端下基本操作
base 當前 操作 god --help 客戶 drop tar -s 在linux環境安裝mongoDB: 一般認為偶數版本為穩定版 如 1.6.x,奇數版本為開發版如1.7.x 32bit的mongoDB最大能存放2g的數據,64bit沒有限制 方法
Linux下使用腳本安裝和升級pip
spa style 通過 腳本 linux下 調用 源碼編譯安裝 trap ont 要安裝或升級pip,需要下載 get-pip.py. 地址:https://bootstrap.pypa.io/get-pip.py ]# wget https://bootstrap.py
Linux下的打包與壓縮和tar命令!
tar.gz 安裝 gin font log 針對 更新 mil emp 本文介紹了linux下的打包壓縮程序tar、gzip、gunzip、bzip2、bunzip2、 compress、uncompress、zip、unzip、rar、unrar程序,以及如何使用它們對
Linux Kernel系列一:開篇和Kernel啟動概要
mis misc 跳轉 line global 最終 width lin 通過 前言 最近幾個月將Linux Kernel的大概研究了一下,下面需要進行深入詳細的分析。主要將以S3C2440的一塊開發板為硬件實體。大概包括如下內容: 1 bootloader分析,以uboo
Linux下的命令之clear和reset
linux clear reset 清除屏幕 1) clear : 刷新屏幕 保留歷史命令記錄 說明:此命令本質上是讓終端向後翻一頁 當向上滾動鼠標時,還是可以看到之前的命令操作記錄 2) reset : 重新初始化屏幕,清除歷史命令操作記錄本文出自 “梵高說我腦子有病” 博客,謝絕
linux下生成https的crt和key證書
openssl https今天在配置kibana權限設置時,kibana要求使用https鏈接。於是總結了一下linux下openssl生成 簽名的步驟:x509證書一般會用到三類文,key,csr,crtKey 是私用密鑰openssl格,通常是rsa算法。Csr 是證書請求文件,用於申請證書。在制作
Linux下的指令:tail
打開 span post 內容 gpo color 實時 獲取 定向 tail指令常用來查看服務器中的日誌信息。 有的時候,需要實時獲取日誌信息。 比如,我們向服務器發送了一個請求,此時日誌有更新,而我們又想實時看到尾部更新的內容。 這時候可以使用指令: tail -f x
為Linux配置常用源:epel和IUS
IT 配置 arch pda creates erp mmu version 源代碼 CentOS上,除了os類的yum源,還需要配置幾個常用的源:epel、ius。 有很多國內很多鏡像站點都提供了各類倉庫的鏡像站點,個人感覺比較全的是阿裏雲http://mirrors.
linux 下的軟連接和硬鏈接
linux 軟連接 linux硬鏈接 linux下創建硬鏈接相當於對源文件的拷貝,生成的新文件和源文件的inode值時一致的,當源文件刪除後,硬鏈接不會失效 軟連接類似於源文件的快捷方式,兩者的inode不同,它裏面保存的時源文件的路徑信息,當源文件的刪除消失後,軟連接失效,此時如果在新建源文件,則軟
Linux下iptables 禁止端口和開放端口
怎麽 進行 window 清除 swe dns 53 技術 clas pasv 1、關閉所有的 INPUT FORWARD OUTPUT 只對某些端口開放。下面是命令實現: iptables -P INPUT DROP iptables -P FORWARD DROP i
linux下GTK程式設計:顯示CPU使用率,記憶體使用率,網速。
StartOS 5.1版GNOME核心linux用了一段時間後,一直想視窗程式設計,QT最先搭建好,QT入門做了一個系統資訊,重灌系統後發現不能運行了,要安裝QT環境才能執行,於是想做一種低依賴的系統級程式設計,因為StartOS已經停止更新了,千辛萬苦第三次才成功搭建GTK環境,結合C語言對QT原
Linux下開發環境的安裝和配置
一: 1.先下載映象檔案 2.源配置 阿里雲源配置官網:http://mirrors.aliyun.com/ 源配置:(1) 備份 sudo mv /etc/yu
靜態連結庫的編譯與使用 linux下的動態連結庫和靜態連結庫到底是個什麼鬼?(一)靜態連結庫的編譯與使用
linux下的動態連結庫和靜態連結庫到底是個什麼鬼?(一)靜態連結庫的編譯與使用 知識不等於技術,這句話真的是越工作的時間長越深有體會,學習到的知識只有不斷的實踐,才成真正在自已的心裡紮下根,成為自身的一部分,所以無論如何,我希望我的部落格可以