errno use in Linux

Source: Internet
Author: User
Tags exit error code printf reset resource socket connection reset

When there is an exception to the C API function in Linux, generally will errno variable (need include errno.h) to assign an integer value, different values to represent different meanings, you can see the value of the reason for the error, in the actual programming with this trick to solve a lot of original seemingly inexplicable problems. But errno is a number, the specific meaning of the representative to go to the errno.h to read the macro definition, and each lookup is a very tedious thing. There are several ways to get the wrong message easily

(1) void perror (const char *s)

Function description

Perror () is used to output the error of the previous function to the standard error (STDERR), the string that the parameter S refers to is printed first, followed by the error reason string. This error reason determines the string to output according to the value of the global variable errno.

(2) Char *strerror (int errno)

Converts the error code to a string error message, which you can combine to output the string and other information to the user interface, such as

fprintf (stderr, "Error in CreateProcess%s, Process ID%d", strerror (errno), ProcessID)

Note: Suppose ProcessID is an already-acquired cosmetic ID

(3) printf ("%m", errno);

In addition, not all of the local error can be obtained by error code, such as the following code snippet

/* Note: The following header file uses "" instead of using the angle bracket directly because the tip bracket in the blog bus is used as an HTML symbol, so its internal header file name is ignored directly.

#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 addressn");

Exit (1);

}

/* Get host information * *

if ((H=gethostbyname (argv[1)) = = NULL)

{

/* If the gethostbyname fails, give the error message * *

Herror ("gethostbyname");

Exit (1);

}

/* The information obtained by the printing process * *

printf ("Host Name:%sn", h->h_name);

printf ("IP Address:%sn", Inet_ntoa (* (struct in_addr *) h->h_addr));

return 0;

}

/*************************************/

You can see Using the gethostbyname () function, you can't use perror () to output the error message (because the error code is stored in H_errno instead of errno). So you need to call the Herror () function.

You simply pass to gethostbyname () a machine name ("bbs.tsinghua.edu.cn"), and then you get other information, such as IP, from the returned structure struct hostent. program to output IP address needs to explain: h->h _addr is a char*, but the Inet_ntoa () function needs to pass a struct IN_ADDR structure. So the above casts the h->h_addr to the struct in_addr*, and then it gets all the data.

The error code values defined in Errno.h are as follows:

Viewing error code errno is an important way to debug a program. When an exception occurs in the Linuc C API function, an integer value is typically assigned to the errno variable (with the Include errno.h), and different values represent different meanings, and the reason for the error can be inferred by looking at the value. In the actual programming with this trick to solve a lot of original seems inexplicable problems. The more troublesome is to go to the Linux source code inside every time to look for the meaning of the error code, now put it out, later need to check when it came here to see.

The following/usr/include/asm/errno.h from the Linux 2.4.20-18 kernel code

#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/* No Child processes * *

#define EAGAIN/* Try again * *

#define ENOMEM/* Out of memory * *

#define EACCES/* Permission denied * *

#define EFAULT/* Bad address * *

#define ENOTBLK/* block Device required * *

#define EBUSY/* Device or resource busy * *

#define EEXIST/* File exists * *

#define EXDEV/* cross-device Link * *

#define ENODEV/* No such device * *

#define ENOTDIR/* Not a directory * *

#define EISDIR/* is a directory * *

#define EINVAL/* Invalid argument * *

#define ENFILE/* File Table Overflow * *

#define EMFILE/* Too many open files */

#define ENOTTY/* Not a typewriter * *

#define ETXTBSY/* Text file Busy * *

#define EFBIG/* File too large * *

#define ENOSPC/* No spaces left on device * * *

#define ESPIPE/* Illegal seek * *

#define EROFS/* read-only File System * *

#define EMLINK/* Too many links */

#define EPIPE/* Broken Pipe * *

#define EDOM/* Math argument out of domain of Func *

#define ERANGE/* Math result is not representable * *

#define EDEADLK/* Resource deadlock would occur * *

#define ENAMETOOLONG/* File name too long * *

#define ENOLCK/No Record locks available * *

#define ENOSYS/* Function not implemented * *

#define ENOTEMPTY/* Directory not empty * *

#define ELOOP/* Too Many symbolic links encountered * *

#define Ewouldblock eagain/* Operation would block * *

#define ENOMSG/* No message of desired type * *

#define EIDRM/* Identifier removed * *

#define ECHRNG/* Channel number out of range * *

#define EL2NSYNC/* Level 2 is not synchronized * *

#define EL3HLT/* Level 3 halted * *

#define EL3RST/* Level 3 Reset */

#define ELNRNG/* Link number out of range * *

#define EUNATCH/* Protocol driver not attached * *

#define ENOCSI/* NO CSI structure Available * *

#define EL2HLT/* Level 2 halted * *

#define EBADE/* Invalid Exchange */

#define EBADR/* Invalid Request Descriptor * *

#define EXFULL/* Exchange full */

#define Enoano/* No Anode * *

#define EBADRQC/* Invalid Request Code * *

#define EBADSLT/* Invalid Slot * *

#define Edeadlock Edeadlk

#define EBFONT/* Bad font file format * *

#define ENOSTR/* Device not a stream * *

#define ENODATA/NO data available * *

#define ETIME/* Timer Expired * *

#define ENOSR/* Out of streams resources * *

#define ENONET/* Machine isn't on the network * *

#define ENOPKG/* Package not installed * *

#define EREMOTE/* Object is remote * *

#define ENOLINK/* Link has been severed * *

#define EADV/* Advertise Error * *

#define ESRMNT/* Srmount Error * *

#define ECOMM/* Communication error on Send * * *

#define EPROTO/* PROTOCOL Error * *

#define EMULTIHOP/* Multihop attempted * *

#define EDOTDOT/* RFS specific Error * *

#define EBADMSG/* Not a data message * *

#define EOVERFLOW/* Value too large for defined data type * *

#define ENOTUNIQ/* Name not unique on network * *

#define EBADFD/* File descriptor in Bad state * *

#define EREMCHG/Remote address changed * *

#define ELIBACC/* Can not access a needed shared library */

#define ELIBBAD/* Accessing a corrupted shared library */

#define ELIBSCN Bayi/*. lib section in a.out corrupted * *

#define ELIBMAX/* Attempting to link too many shared libraries * *

#define ELIBEXEC/* cannot exec a shared library directly * *

#define EILSEQ/* Illegal byte sequence * *

#define ERESTART/* Interrupted system call should is restarted * *

#define ESTRPIPE/* Streams Pipe Error * *

#define EUSERS/* Too many users * *

#define ENOTSOCK/* Socket operation on Non-socket * *

#define EDESTADDRREQ/* Destination Address required * *

#define EMSGSIZE/* Message too long * *

#define EPROTOTYPE/* Protocol wrong type for socket */

#define ENOPROTOOPT/* Protocol not available * *

#define EPROTONOSUPPORT/* Protocol not supported * *

#define ESOCKTNOSUPPORT * * Socket type not supported * *

#define EOPNOTSUPP/* Operation not supported on transport endpoint * *

#define EPFNOSUPPORT/* Protocol Family not supported * *

#define EAFNOSUPPORT * Address family not supported by protocol *

#define EADDRINUSE/* address already with use * *

#define EADDRNOTAVAIL/* Cannot assign requested address * *

#define ENETDOWN/* Network is down */

#define ENETUNREACH/* Network is unreachable * *

#define ENETRESET 102/* Network dropped connection because of reset */

#define ECONNABORTED/* Software caused connection Abort * *

#define ECONNRESET/* Connection Reset by Peer *

#define ENOBUFS/No buffer space Available * *

#define EISCONN/* Transport endpoint is already connected * *

#define ENOTCONN/* Transport endpoint is not connected * *

#define ESHUTDOWN 108/* Cannot send after transport endpoint shutdown * *

#define ETOOMANYREFS 109/* Too many references:cannot splice * *

#define ETIMEDOUT/* Connection timed out * *

#define ECONNREFUSED/* Connection refused * *

#define EHOSTDOWN 112/* Host is down */

#define EHOSTUNREACH 113/* No route to host * *

#define EALREADY 114/* Operation already in progress * *

#define EINPROGRESS * * Operation now 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/* 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

You can also use strerror () to translate your own

Such as:

#include

#include

#include

int main (void)

{

int FD;

extern int errno;

if (FD = open ("/DEV/DSP", O_wronly)) < 0)

{

printf ("Errno=%dn", errno);

char * MESG = strerror (errno);

printf ("Mesg:%sn", MESG);

}

Exit (0);

}

If the DSP device is busy, the output is as follows:

Errno=16

Mesg:device or resource busy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.