C. Linux kernel call error errno

Source: Internet
Author: User

When {
Tagshow (event, 'linux ');
} "Href =" javascript:; "target =" _ Self ">LinuxWhen the c api function in is abnormal {
Tagshow (event, 'errno ');
} "Href =" javascript:; "target =" _ Self ">ErrnoThe variable (which must include errno. h) is assigned an integer. Different values indicate different meanings. You can view the cause of the error by checking the value. In the actual {
Tagshow (event, '% B1 % E0 % B3 % CC ');
} "Href =" javascript:; "target =" _ Self ">ProgrammingUsing this solution solves many seemingly inexplicable problems. But errno is a number. The specific meaning must be read in errno. H {
Tagshow (event, '% B6 % A8 % D2 % e5 ');
} "Href =" javascript:; "target =" _ Self ">DefinitionAnd every query is a very tedious task. The following methods can be used to conveniently obtain error information:
(1) void perror (const char * s)
{
Tagshow (event, '% Ba % af % Ca % fs ');
} "Href =" javascript:; "target =" _ Self ">FunctionDescription
Perror () is used to indicate the cause of the previous function error {
Tagshow (event, '% Ca % E4 % B3 % F6 ');
} "Href =" javascript:; "target =" _ Self ">OutputTo stderr, the string referred to by parameter S is printed first, followed by the error cause string. The cause of this error is determined based on the value of the global variable errno.
(2) char * strerror (INT errno)
Convert the error code to a string error message. You can combine the string and other information and output it to the user interface, for example
Fprintf (stderr, "error in CreateProcess % s, process ID % d", strerror (errno), processid)
Note: Assume processid is an acquired integer ID.
(3) printf ("% m", errno );
In addition, not all errors can be obtained through error, for example, the following code segment.

# 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)
{

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;
}


The code above shows that using the gethostbyname () function, you cannot use perror () to output error messages (because the error code is stored in h_errno rather than errno. Therefore, you need to call the herror () function.
You simply pass gethostbyname () A machine name ("bbs.tsinghua.edu.cn"), and then get the IP address and other information from the returned structure struct hostent. the program that outputs the IP address in the program needs to explain: H-> h_addr is a char *, but the inet_ntoa () function needs to pass a struct in_addr structure. Therefore, the above forced conversion of H-> h_addr to struct in_addr *, and then obtained all the data through it.

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.