简体   繁体   English

IOCTL是否返回值

[英]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? IOCTL成功时总是返回“0”吗?
  2. How does the following expression evaluate to a positive / TRUE ? 以下表达式如何评估为正/ 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? 对于ioctl的非零返回值,上面的表达式如何评估为true?

It is up to developer who develops driver which handles this ioctl request what value to return on success. 开发人员可以开发处理此ioctl请求的驱动程序,以获得成功返回的值。 Usually, 0 means everything went right. 通常, 0表示一切正常。 This convention has been used in UNIX systems for a long time. 此约定已在UNIX系统中使用了很长时间。

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. 因此,如果赋值运算符返回非零值,则表达式的返回值(在我们的示例中为ioctl int )将隐式计算为true

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. 在某些情况下,UNIX系统调用会在readwrite系统调用时返回正值。 In case of read and write system calls their positive return value means the number of bytes that were read or written. readwrite系统调用的情况下,它们的正返回值表示读取或写入的字节数。

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. 它可能有ioctl这可能意味着执行去正常返回正值,我们回到一切的这个特殊的文件描述符代表的一些国家 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. 非零值被评估为true ,我们输入该条件代码块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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