简体   繁体   中英

Is IOCTL return value

I came across the following code.

if((error = ioctl(get_card_fd(card_ref), CARD_SETVERSION, &context)))
{
      return EXIT_FAILURE;
}

My questions are the following:

  1. Does IOCTL always return '0' on success?
  2. How does the following expression evaluate to a positive / TRUE ?

     (error = ioctl(get_card_fd(card_ref), CARD_SETVERSION, &context)) 

How does the above expression evaluate to true for a non-zero return value of ioctl?

It is up to developer who develops driver which handles this ioctl request what value to return on success. Usually, 0 means everything went right. This convention has been used in UNIX systems for a long time.

Anyway, read your documentation regarding this particular file descriptor and know what values particular system calls that are handled by this file descriptor return.

As for the second question, the = operator returns a new value of variable after assignment. So, the return value (which in our case is int for ioctl ) of the expression is evaluated implicitly to true if non-zero value is returned by the assignment operator.

Most ways, non-zero negative values mean faulty execution. In some cases, UNIX system calls return positive values as the read or write system calls do. In case of read and write system calls their positive return value means the number of bytes that were read or written.

It is possible to have ioctl return positive value that may mean that execution went normally and we return some state of whatever this particular file descriptor stands for. Once again, read your documentation carefully.

So, in the code below:

if (error = ioctl(get_card_fd(card_ref), CARD_SETVERSION, &context)) {
        return EXIT_FAILURE;
}

non-zero value is evaluated to true and we enter that conditional block of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM