Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is IOCTL return value

Tags:

c

ioctl

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?

like image 313
liv2hak Avatar asked Apr 03 '14 21:04

liv2hak


People also ask

What is the use of ioctl?

The ioctl function is useful for implementing a device driver to set the configuration on the device. e.g. a printer that has configuration options to check and set the font family, font size etc. ioctl could be used to get the current font as well as set the font to a new one.

Is ioctl deprecated?

Adding more to confuse :" ioctl : However, ioctl is deprecated in the kernel, and you will find it hard to get any drivers with new uses of ioctl accepted upstream.

Is ioctl blocking call?

The IOCTL call has many functions; establishing blocking mode is only one of its functions. The value in COMMAND determines which function IOCTL will perform. The REQARG of 0 specifies non-blocking (a REQARG of 1 would request that socket S be set to blocking mode).

What is the role of ioctl in the OS kernel?

IOCTL is referred as Input and Output Control, which is used to talk with device drivers. IOCTL is a system call where system call is the programmatic way in which a computer program in user space requests a service from the kernel space of the operating system.


1 Answers

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.

like image 130
mesmerizingr Avatar answered Sep 24 '22 23:09

mesmerizingr