In the Linux system programming, when the system call error, there is an integer variable will be set, this integer variable is errno, the definition of this variable in the/usr/include/errno.h file
1 #ifndef _errno_h2 3 /*The Includer defined __need_emath If he wants only the definitions4 of EDOM and Erange, and not everything else. */5 #ifndef __need_emath6# define _errno_h17# include <features.h>8 #endif9 Ten __begin_decls One A /*Get the error number constants from the System-specific file. - This file would test __need_emath and _errno_h. */ -#include <bits/errno.h> the #undef__need_emath - - #ifdef _errno_h - + /*Declare the ' errno ' variable, unless it ' s defined as a macro by - bits/errno.h. The case in GNU, where it's a per-thread + variable. This redeclaration using the macro still works, but it A would be a function declaration without a prototype and may trigger at a-wstrict-prototypes warning. */ - #ifndef errno - extern int errno; - #endif - - #ifdef __USE_GNU in - /*The full and simple forms of the The name with which the to invoked. These variables is set up automatically at startup based on + The value of argv[0] (this works with the GNU LD). */ - extern Char*program_invocation_name, *Program_invocation_short_name; the #endif/* __USE_GNU */ * #endif/* _errno_h */ $ Panax Notoginseng __end_decls - the #endif/* _errno_h */ + A /*The Hurd <bits/errno.h> defines ' error_t ' as an enumerated type so the That's printing ' error_t ' values in the debugger shows the names. We + might need this definition sometimes even if the this file is included - before. */ $ #ifDefined __use_gnu | | Defined __need_error_t $ # ifndef __error_t_defined -typedefinterror_t; -# define __error_t_defined1 the # endif - # undef __need_error_tWuyi #endif
When there is no error in the system call, the integer variable is set to a value, each value has a special meaning, each value is represented by a macro, defined in the/usr/include/asm-generic/errno.h file
#ifndef _asm_generic_errno_h#define_asm_generic_errno_h#include<asm-generic/errno-Base.h>#defineEDEADLK//Resource deadlock would occur * *#defineEnametoolong/* File name too long */#defineEnolck Notoginseng */No record locks available */#defineEnosys/* Function not implemented */#defineEnotempty/* Directory not empty */#defineEloop/* Too Many symbolic links encountered * *#defineEwouldblock Eagain/* operation would block */#defineENOMSG * * No message of desired type */#defineEIDRM */Identifier removed */#defineECHRNG */* Channel number out of range */#defineEl2nsync */Level 2 not synchronized */#defineEL3HLT/* Level 3 halted */#defineEl3rst/* Level 3 Reset */#defineELNRNG/* Link number out of range */#defineEunatch */Protocol driver not attached */#defineENOCSI/* NO CSI structure available */#defineEL2HLT/* Level 2 halted */#defineEbade/* Invalid Exchange * *#defineEBADR-*/Invalid Request Descriptor */#defineExfull/* Exchange full */#defineEnoano/* No anode */#defineEBADRQC */Invalid Request code */#defineEBADSLT/* Invalid slot */#defineEdeadlock Edeadlk#defineEbfont/* Bad font file format */#defineENOSTR/* Device not a stream */#defineEnodata/* NO data available */#defineETime */* Timer expired */#defineENOSR */* out of streams resources * *#defineEnonet */Machine was not on the network */#defineENOPKG/* Package not installed */#defineEremote/* Object is remote */#defineEnolink/* Link has been severed */#defineEADV/* Advertise Error */#defineESRMNT/* Srmount Error */#defineECOMM/* Communication error on Send */#defineEproto/* Protocol Error */#defineEmultihop */Multihop attempted * *#defineEdotdot/* RFS specific Error */#defineEBADMSG-*/Not a data message */#defineEoverflow */Value too large for defined data type */#defineEnotuniq/* Name not unique on network */#defineEBADFD */* File descriptor in Bad state */#defineEREMCHG/* Remote address changed */#defineELIBACC/* Can not access a needed shared library */#defineElibbad */Accessing a corrupted shared library */#defineELIBSCN Bayi/* lib section in a.out corrupted */#defineElibmax/attempting to link in too many shared libraries */#defineElibexec */cannot exec a shared library directly */#defineEILSEQ/* Illegal byte sequence */#defineErestart/* Interrupted system call should be restarted */#defineEstrpipe */* Streams pipe Error */#defineEusers */Too Many users * *#defineEnotsock */* Socket operation on Non-socket */#defineEdestaddrreq/* Destination address required */#defineEmsgsize/* Message too long */#defineEprototype/* Protocol wrong type for socket */#defineEnoprotoopt */Protocol Not available */#defineEprotonosupport */Protocol not supported */#defineEsocktnosupport 94/* Socket type not supported */#defineEopnotsupp */Operation not supported on transport endpoint * *#defineEpfnosupport */Protocol family not supported */#defineEafnosupport/* Address family not supported by protocol */#defineEaddrinuse 98/* Address already in use */#defineEaddrnotavail/* Cannot assign requested address * *#defineEnetdown/* Network is down */#defineEnetunreach 101/* Network is unreachable */#defineEnetreset 102/* Network dropped connection because of reset */#defineEconnaborted 103/* Software caused connection abort */#defineEconnreset 104/* Connection Reset by Peer */#defineENOBUFS * * No buffer space Available */#defineEisconn 106/* Transport endpoint is already connected * *#defineEnotconn 107/* Transport endpoint is not connected */#defineEshutdown 108/* Cannot send after transport endpoint shutdown */#defineEtoomanyrefs 109/* Too many references:cannot splice * *#defineEtimedout */Connection timed out */#defineEconnrefused 111/* Connection refused */#defineEhostdown * * * Host is down */#defineEhostunreach 113/* No route to host */#defineEalready */Operation already in progress */#defineEinprogress-*/Operation now in progress */#defineEstale */* Stale NFS file handle */#defineEuclean 117/* Structure needs cleaning * *#defineEnotnam 118/* Not a XENIX named type file */#defineEnavail 119 */No XENIX semaphores Available * *#defineEisnam/* is a named type file */#defineEremoteio 121/* Remote I/O error */#defineEdquot 122/* Quota exceeded */#defineEnomedium 123 */No medium found */#defineEmediumtype 124/* Wrong medium type */#defineecanceled/* Operation Canceled */#defineEnokey 126/* Required key not available */#defineEkeyexpired 127/* Key has expired */#defineekeyrevoked/* Key has been revoked */#defineEkeyrejected 129/* Key was rejected by service *//*For robust mutexes*/#defineEownerdead/* Owner died */#defineEnotrecoverable 131/* State not recoverable */#defineErfkill */Operation not possible due to Rf-kill * *#defineEhwpoison 133/* Memory page has hardware error */#endif
After explaining the meaning of errno, let's use two common error output functions
One is perror.
void perror (constchar *s); // output error messages based on the value of the errno set by the system // the error message will contain the contents of the programmer's own content, with parameters s passed to the programmer to output the content
One is strerror.
Char *strerror (int errnum); // errno is passed into the function strerror, STRERROR returns the text of the corresponding error message
Error handling of Linux system programming