IOCTL call and checking return value

liv2hak picture liv2hak · Feb 19, 2013 · Viewed 12.6k times · Source
if((err = ioctl(fd, IOC_CARD_LOCK, &lock)) < 0)
{
     printf("ioctl failed and returned errno %d \n",err);
}

Is the above code correct and a good programming practice? It does compile on my PC. i.e does it populate err with the return value of ioctl and check if err is < 0

Is the above method a standard way to return "err" returned by IOCTL.

There seem to be some standard variable called errno? what is it? Will that be the same as above?

Answer

liv2hak picture liv2hak · Feb 19, 2013

I found out a better way to do this.

if(ioctl(fd, IOC_CARD_LOCK, &lock) < 0)
{
     printf("ioctl failed and returned errno %s \n",strerror(errno));
}

errno is a global variable that is set for system calls.and strerror converts the code (a negative integer) into a meaningful string ("Invalid argument" for example.)