• Linux erron常量

    2010-02-07 21:39:00
    #define EPERM 1 /* Operation not permitted */#define ENOENT 2 /* No such file or directory */#define ESRCH 3 /* No such process */#define EINTR 
    #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 */
    #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  58
    #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 */
    #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 */

    展开全文
  • [转载] Linux erron介绍

    2018-05-08 11:34:11
    转载于 https://www.cnblogs.com/Jimmy1988/p/7485133.html1.错误码 / errnoLinux中系统调用的错误都存储于 errno中,errno由操作系统维护,存储就近发生的错误,即下一次的错误码会覆盖掉上一次的错误。PS: 只有当...

    转载于 https://www.cnblogs.com/Jimmy1988/p/7485133.html

    1.错误码 / errno

    Linux中系统调用的错误都存储于 errno中,errno由操作系统维护,存储就近发生的错误,即下一次的错误码会覆盖掉上一次的错误。

    PS: 只有当系统调用或者调用lib函数时出错,才会置位errno

    查看系统中所有的errno所代表的含义,可以采用如下的代码:

    /* Function: obtain the errno string
    *   char *strerror(int errno)
    */
    
    #include <stdio.h>
    #include <string.h>     //for strerror()
    //#include <errno.h>
    int main()
    {
        int tmp = 0;
        for(tmp = 0; tmp <=256; tmp++)
        {
            printf("errno: %2d\t%s\n",tmp,strerror(tmp));
        }
        return 0;
    }

    输出信息如下:

    errno:  0       Success
    errno:  1       Operation not permitted
    errno:  2       No such file or directory
    errno:  3       No such process
    errno:  4       Interrupted system call
    errno:  5       Input/output error
    errno:  6       No such device or address
    errno:  7       Argument list too long
    errno:  8       Exec format error
    errno:  9       Bad file descriptor
    errno: 10       No child processes
    errno: 11       Resource temporarily unavailable
    errno: 12       Cannot allocate memory
    errno: 13       Permission denied
    errno: 14       Bad address
    errno: 15       Block device required
    errno: 16       Device or resource busy
    errno: 17       File exists
    errno: 18       Invalid cross-device link
    errno: 19       No such device
    errno: 20       Not a directory
    errno: 21       Is a directory
    errno: 22       Invalid argument
    errno: 23       Too many open files in system
    errno: 24       Too many open files
    errno: 25       Inappropriate ioctl for device
    errno: 26       Text file busy
    errno: 27       File too large
    errno: 28       No space left on device
    errno: 29       Illegal seek
    errno: 30       Read-only file system
    errno: 31       Too many links
    errno: 32       Broken pipe
    errno: 33       Numerical argument out of domain
    errno: 34       Numerical result out of range
    errno: 35       Resource deadlock avoided
    errno: 36       File name too long
    errno: 37       No locks available
    errno: 38       Function not implemented
    errno: 39       Directory not empty
    errno: 40       Too many levels of symbolic links
    errno: 41       Unknown error 41
    errno: 42       No message of desired type
    errno: 43       Identifier removed
    errno: 44       Channel number out of range
    errno: 45       Level 2 not synchronized
    errno: 46       Level 3 halted
    errno: 47       Level 3 reset
    errno: 48       Link number out of range
    errno: 49       Protocol driver not attached
    errno: 50       No CSI structure available
    errno: 51       Level 2 halted
    errno: 52       Invalid exchange
    errno: 53       Invalid request descriptor
    errno: 54       Exchange full
    errno: 55       No anode
    errno: 56       Invalid request code
    errno: 57       Invalid slot
    errno: 58       Unknown error 58
    errno: 59       Bad font file format
    errno: 60       Device not a stream
    errno: 61       No data available
    errno: 62       Timer expired
    errno: 63       Out of streams resources
    errno: 64       Machine is not on the network
    errno: 65       Package not installed
    errno: 66       Object is remote
    errno: 67       Link has been severed
    errno: 68       Advertise error
    errno: 69       Srmount error
    errno: 70       Communication error on send
    errno: 71       Protocol error
    errno: 72       Multihop attempted
    errno: 73       RFS specific error
    errno: 74       Bad message
    errno: 75       Value too large for defined data type
    errno: 76       Name not unique on network
    errno: 77       File descriptor in bad state
    errno: 78       Remote address changed
    errno: 79       Can not access a needed shared library
    errno: 80       Accessing a corrupted shared library
    errno: 81       .lib section in a.out corrupted
    errno: 82       Attempting to link in too many shared libraries
    errno: 83       Cannot exec a shared library directly
    errno: 84       Invalid or incomplete multibyte or wide character
    errno: 85       Interrupted system call should be restarted
    errno: 86       Streams pipe error
    errno: 87       Too many users
    errno: 88       Socket operation on non-socket
    errno: 89       Destination address required
    errno: 90       Message too long
    errno: 91       Protocol wrong type for socket
    errno: 92       Protocol not available
    errno: 93       Protocol not supported
    errno: 94       Socket type not supported
    errno: 95       Operation not supported
    errno: 96       Protocol family not supported
    errno: 97       Address family not supported by protocol
    errno: 98       Address already in use
    errno: 99       Cannot assign requested address
    errno: 100      Network is down
    errno: 101      Network is unreachable
    errno: 102      Network dropped connection on reset
    errno: 103      Software caused connection abort
    errno: 104      Connection reset by peer
    errno: 105      No buffer space available
    errno: 106      Transport endpoint is already connected
    errno: 107      Transport endpoint is not connected
    errno: 108      Cannot send after transport endpoint shutdown
    errno: 109      Too many references: cannot splice
    errno: 110      Connection timed out
    errno: 111      Connection refused
    errno: 112      Host is down
    errno: 113      No route to host
    errno: 114      Operation already in progress
    errno: 115      Operation now in progress
    errno: 116      Stale file handle
    errno: 117      Structure needs cleaning
    errno: 118      Not a XENIX named type file
    errno: 119      No XENIX semaphores available
    errno: 120      Is a named type file
    errno: 121      Remote I/O error
    errno: 122      Disk quota exceeded
    errno: 123      No medium found
    errno: 124      Wrong medium type
    errno: 125      Operation canceled
    errno: 126      Required key not available
    errno: 127      Key has expired
    errno: 128      Key has been revoked
    errno: 129      Key was rejected by service
    errno: 130      Owner died
    errno: 131      State not recoverable
    errno: 132      Operation not possible due to RF-kill
    errno: 133      Memory page has hardware error
    errno: 134~255  unknown error!

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

    #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

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

    #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

    2. 打印错误信息

    1). 打印错误信息 / perror

    • 作用
      打印系统错误信息
    • 头文件

      #include
    • 函数原型

      void perror(const char *s)

    • 参数

      s: 字符串提示符

    • 输出形式
      const char *s: strerror(errno) //提示符:发生系统错误的原因
    • 返回值
      无返回值

    2). 字符串显示错误信息 / strerror

    • 作用
      将错误码以字符串的信息显示出来
    • 头文件

      #include
    • 函数原型

      char *strerror(int errnum);

    • 参数

      errnum: 即errno

    • 返回值
      返回错误码字符串信息
    展开全文
  • Linux中errno使用

    2011-09-01 16:46:00
    linux中的C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因,在实际编程中用这一招解决了不少原本看来莫名其妙的问题。但是errno是...

     当linux中的C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因,在实际编程中用这一招解决了不少原本看来莫名其妙的问题。但是errno是一个数字,代表的具体含义还要到errno.h中去阅读宏定义,而每次查阅是一件很繁琐的事情。有下面几种方法可以方便的得到错误信息
      (1)void perror(const char *s)
    函数说明
    perror ( )用来将上一个函数发生错误的原因输出到标准错误(stderr),参数s 所指的字符串会先打印出,后面再加上错误原因 字符串。此错误原因依照全局变量 errno 的值来决定要输出的字符串。
      (2) char *strerror(int errno)
    将错误代码转换为字符串错误信息,可以将该字符串和其它的信息组合输出到用户界面例如
    fprintf(stderr,"error in CreateProcess %s, Process ID %d ",strerror(errno),processID)
    注:假设processID是一个已经获取了的整形ID
      (3)printf("%m", errno);
    另外不是所有的地方发生错误的时候都可以通过error获取错误代码,例如下面的代码段
    /*注:下面的头文件使用""而没有直接使用尖括号是因为博客大巴中尖括号当作html符号,所以其内部的头文件名字会被直接忽略*/
    #include"stdio.h"
    #include "stdlib.h"
    #include "errno.h"
    #include "netdb.h"
    #include "sys/types.h"
    #include "netinet/in.h"
    int main (int argc, char *argv[])
    {
    struct hostent *h;
    if (argc != 2)
    {
    fprintf (stderr ,"usage: getip address\n");
    exit(1);
    }
    /* 取得主机信息 */
    if((h=gethostbyname(argv[1])) == NULL)
    {
    /* 如果gethostbyname 失败,则给出错误信息 */
    herror(“gethostbyname”);
    exit(1);
    }
    /* 列印程序取得的信息 */
    printf(“Host name : %s\n”, h->h_name);
    printf(“IP Address : %s\n”, inet_ntoa (*((struct in_addr *)h->h_addr)));
    return 0;
    }

    /*************************************/
      通过上面的代码可以看到:使用gethostbyname()函数,你不能使用perror()来输出错误信息(因为错误代码存储在 h_errno 中而不是errno 中。所以,你需要调用herror()函数。
    你简单的传给gethostbyname() 一个机器名(“bbs.tsinghua.edu.cn”),然后就从返回的结构struct hostent 中得到了IP 等其他信息.程序中输出IP 地址的程序需要解释一下:h->h_addr 是一个char*,但是inet_ntoa()函数需要传递的是一个struct in_addr 结构。所以上面将h->h_addr 强制转换为struct in_addr*,然后通过它得到了所有数据。

     

    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

    同时也可以使用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);
    }
    dsp设备忙的话将输出如下:
    errno=16
    Mesg:Device or resource busy

     

    展开全文
  • errno和错误号引入erron说明errno文件打开错误方式的表示①使用printf打印出错误号,通过错误号表示出错原因。②perror函数perror的工作原理 引入erron说明 在我们的例子中,如果open失败了,只是笼统的打印出...

    引入erron说明

    在我们的例子中,如果open失败了,只是笼统的打印出“打开文件失败了”,但是并没有提示具体出错的原因,没有详细的出错原因提示,遇到比较难排查的错误原因时,很难排查出具体的函数错误。 open失败,如何具体打印出详细的出错信息呢?我们在这里就说明errno的作用了。

    errno

    函数调用出错时,Linux系统使用错误编号(整形数)来标记具体出错的原因,每个函数有很多错误号,每个错误号代表了一种错误,产生这个错误时,会自动的将错误号赋值给errno这个全局变量。

    errno是Linux系统定义的全局变量,可以直接使用。

    错误号和errno全局变量都被定义在了errno.h头文件,使用errno时需要包含这个头文件。

    man errno,就可以查到errno.h头文件。

    我们在linux平台平台下面可以通过帮助手册查询到:
    在这里插入图片描述

    这里就会记录很多很多错误号:
    在这里插入图片描述

    常见错误号及解释说明

    在这里插入图片描述

    文件打开错误方式的表示

    那么我们知道了文件在打开的时候有这些具体原因,但是我们如何具体打印出来呢?

    ①使用printf打印出错误号,通过错误号表示出错原因。

    我们进行代码演示:
    在这里插入图片描述
    在这里插入图片描述

    我们加入了头文件,并且在读取文件的时候不存在errno.txt的文件进行测试,平且打印出来错误号:
    执行结果为:
    在这里插入图片描述

    打开错误,错误号为2
    如果没有包含头文件 #include <errno.h>
    使用errno时,编译提示‘errno’ undeclare的错误,表示找不到errno全局变量。

    例如:
    在这里插入图片描述

    我们把#include <errno.h>头文件注释之后查看编译结果:
    在这里插入图片描述

    就会出现找不到errno全局变量。

    错误号确实标记了具体的出错原因,但是我们并不知道这个错误号,具体到底代表的是什么错误。

    ②perror函数

    perror函数可以自动将“错误号”换成对应的文字信息,并打印出来,方便我们理解。

    函数查看:
    在这里插入图片描述

    perror是一个C库函数,不是一个系统函数。

    我们详细看一下描述的这一部分:
    Perror这个函数,不仅仅用于打印系统函数调用的错误,并且用于打印库函数错误。

    使用演示:

    在这里插入图片描述

    使用的时候只是在之前演示的错误码里面加入了perror 的使用。
    执行结果为:
    在这里插入图片描述
    结果直接提示文件不存在
    我们可以看到编译运行之后会直接打印出来错误号,并且perror会直接打印出来出错的原因。

    perror的工作原理

    调用perror函数时,它会自动去一张对照表,将errno中保存的错误号,换成具体的文字信息并打印出来,我们就能知道函数的具体错误原因了。

    man open,可以查看open函数,都有哪些错误号

    我们通过 man 2 open 来查询open函数的错误号:

    每个错误号代表了一种函数可能的出错情况,比如:
    EACCES:不允许你访问文件而出错。

    Liunx为了让错误号能够见名识意,都给这些整形的错误号定义了对应的宏名,这些宏定义都被定义在了error.h头文件中。

    man perror这个函数,也可以看到这个头文件。

    使用 man 2 open 查看到这写错误号及详细原因说明:
    在这里插入图片描述

    我们所说的错误号这里并没有直接体现,这下宏定义的背后对应的就是整型编号的错误号,只是为了好识别所以定义为宏。

    我们在这里提示:错误号不需要记住,使用perror函数,它可以自动翻译,我们这里说明错误号,只是希望理解错误号,在之后的博客中会出现,我们会用到。

    char *strerror(int errnum);

    使用char *strerror(int errnum);
    返回一个字符串,我们可以讲字符串表示的错误进行打印,也可以把字符串保存到文件。

    参数

    errno

    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/stat.h>
    int main()
    {
            int fd = open("log",O_RDONLY);
            if(fd == -1)
            {
                perror("open");
                //使用perror打印出错原因
                printf("open:%s\n",strerror(errno));
                //使用strerror打印错误原因
                printf("open failed\n");
            }
            else
            {
            printf("open success file descriptor  : %d\n",fd);
            }
            close(fd);   
            return 0;
    }
    

    运行结果为:
    在这里插入图片描述

    展开全文
  • Linux错误处理(errno)

    2019-03-27 09:38:53
    Linux系统函数失败后都会返回一个负值,并且整形变量errno通常被设置为错误码。我们在使用错误码时,需要包含如下头文件 #include <errno.h> errno只在函数返回错误时才有意义,当函数执行正常返回时并不会...

    Linux系统函数失败后都会返回一个负值,并且整形变量errno通常被设置为错误码。我们在使用错误码时,需要包含如下头文件

    #include <errno.h>
    

    errno只在函数返回错误时才有意义,当函数执行正常返回时并不会重置errno,因此此时的errno是不能作为错误处理的。
    errno是一个int型变量,除了直接使用该变量,还有两个和其相关的API可以方便的查看错误类型:

    #include <stdio.h>
    void perror(const char *msg);
    

    这个函数会先打印msg信息,然后再输出对应的error类型。

    #include <string.h>
    char *strerror(int errno);
    

    这个函数把errno字符串化,输出对应的错误类型字符串。

    示例

     #include <unistd.h>
     #include <stdio.h>
     #include <string.h>
     #include <errno.h>
     
     int main(int argc, char *argv[])
     {
         errno = EINVAL;
         printf("Error:%s, errno:%d\n", strerror(errno), errno);
         perror(argv[0]);
         exit(0);
     }
    
    

    运行结果:

    $ gcc error.c -o error
    $ ./error
    Error:Invalid argument, errno:22
    ./error: Invalid argument
    
    展开全文
  • fprintf / strerror / errno #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <libgen.h>...#define LOG_ERROR(msg) ...
  • linux之errno与线程安全

    2018-07-10 14:24:18
    linux或者unix环境中,errno是一个十分重要的部分。在调用的函数出现问题的时候,我们可以通过errno的值来确定出错的原因,这就会 涉及到一个问题,那就是如何保证errno在多线程或者进程中安全?我们希望在多线程...
  • strerror和erron参数函数的试用(经典) 2011年12月06日  linux下错误的捕获:errno和strerror的使用  经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会...
  • errno.h linux errno

    2015-12-09 18:29:24
    LINUX/android/kernel/include/uapi/asm-generic/errno-base.h errno 1-34 LINUX/android/kernel/include/uapi/asm-generic/errno.h 35-133 LINUX/android/kernel/include/linux/errno.h 512-529
  • linux下错误的捕获:errno和strerror的使用 经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因。这个...
  •  linux下错误的捕获:errno和strerror的使用  ... 经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的
  • linux errno查看和使用

    2015-07-29 10:28:28
    1. void perror(const char *s) 函数说明 perror ( )用来将上一个函数发生错误的原因输出到标准错误(stderr),参数s 所指的字符串会先打印出,后面再加上错误原因 字符串。此错误原因依照全局变量 errno 的值来决定...
  • getsockopt(socketHandle, SOL_SOCKET, SO_ERROR, &error, &len);...Linux的错误码表(errno table) _ 124 EMEDIUMTYPE_ Wrong medium type _ 123 ENOMEDIUM__ No medium found _ 122 EDQUOT___ Disk quota excee
  • errno=111 linux

    2009-12-24 19:29:00
    connect localhost 127.0.0.1 出现错误,错误码是111,寻找原因。
  • Linux的errno

    2018-12-25 14:35:59
    erron是一个错误码,系统调用出错的时候,会设置一个错误码。因为C语言没有异常处理机制,所以要依靠错误码进行异常情况的处理。 如果系统调用出错(一般是调用函数返回-1的时候)errno会被设置为有关的标记,系统...
  • 经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因。这个时候使用errno这个全局变量就相当有用了。 ...
  • 具体定义在 include/asm-generic/errno-base.h include/asm-generic/errno.h   #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERIC_ERRNO_BASE_H ...#define EPERM 1 /* Operation not permitted */ ...
  • 1.对于Linux内核而言, 所有的文件或设备都对应一个文件描述符(Linux的设计哲学: 一切皆文件), 这样可以简化系统编程的复杂程度; 2.当打开/创建一个文件的时候, 内核向进程返回一个文件描述符(是一个非负整数)....
1 2 3 4 5 ... 20
收藏数 463
精华内容 185
热门标签