开发者

Where is the errnos defined? Example linux c/c++ program for i2c

When something goes wrong in a classic linux c/c++ software we have the magic variable errno that gives us a clue on what just went开发者_开发百科 wrong.

But where is those errors defined?

Let's take a example (it's actually a piece from a Qt app, therefore the qDebug()).

if (ioctl(file, I2C_SLAVE, address) < 0) {
    int err = errno;
    qDebug() << __FILE__ << __FUNCTION__ << __LINE__ 
        << "Can't set address:" << address
        << "Errno:" << err << strerror(err);
     ....

The next step is to look at what that errno was so we can decide if we just quit or try to do something about the problem.

So we maybe add a if or switch at this point.

if (err == 9)
{
    // do something...
}
else 
{
    //do someting else
}

And my question is where to find the errors that "9" represents? I don't like that kind of magic numbers in my code.

/Thanks


They are generally defined in /usr/include/errno.h* and are accessible by including errno.h:

#include <errno.h>

I write out both the errno value and its textual meaning when reporting an error, getting the textual meaning via strerror():

if (something_went_wrong)
{
    log("Something went wrong: %s (%d)", strerror(errno), errno);
    return false;
}

From a Linux shell, however, you can use the perror utility to find out what different errno values mean:

$ perror 9
OS error code   9:  Bad file descriptor

EDIT: Your code should be changed to use the symbolic value:

if (err == EBADF)
{
    // do something...
}
else 
{
    //do someting else
}

EDIT 2: * Under Linux, at least, the actual values are defined in /usr/include/asm-generic/{errno,errno-base}.h and other places, making it a bit of a pain to find them if you want to look at them.


NAME
       errno - number of last error

SYNOPSIS
       #include <errno.h>

DESCRIPTION
       The  <errno.h>  header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate
       what went wrong.  Its value is significant only when the return value of the call indicated an error (i.e., -1 from most system calls; -1 or NULL from  most
       library functions); a function that succeeds is allowed to change errno.

       Valid error numbers are all non-zero; errno is never set to zero by any system call or library function.


Normally to implement error handling you need to know which particular error codes can be returned by the function. This info is available in man or at http://www.kernel.org/doc/man-pages/.

For example for ioctl call you should expect the following codes:

   EBADF  d is not a valid descriptor.

   EFAULT argp references an inaccessible memory area.

   EINVAL Request or argp is not valid.

   ENOTTY d is not associated with a character special device.

   ENOTTY The specified request does not apply to the kind of object that the
          descriptor d references.

EDIT: If <errno.h> is included all files defining all possible error codes are included as well, so you don't really need to know their exact location.


The file is:

/usr/include/errno.h

--p

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜