IOCTL call and return value check

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

      

Is the above code correct and good programming practice? It compiles on my PC. that is, it fills with the err

return value ioctl

and checks if there iserr

< 0

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

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

+3


source to share


2 answers


I found 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. strerror converts a code (negative integer) to a meaningful string (for example, "Invalid argument").

+9


source


Just came across this answer. The answer is only partly correct, as printf can overwrite errno - according to the man pages it is required to preserve errno. So the more reliable answer is:



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

      

+1


source







All Articles