Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IS_ERR() macro in Linux

While getting to know how to write device drivers I came across IS_ERR() macro. But I couldn't find how is it working. I have include the code below:

majorNumber = register_chrdev(0, DEVICE_NAME, &fops);

if (majorNumber<0)
{
    printk(KERN_ALERT "Failed to register a major number\n");
    return majorNumber;
}
printk(KERN_INFO "Registered correctly with major number %d\n", majorNumber);

// Register the device class
ebbcharClass = class_create(THIS_MODULE, CLASS_NAME);

if (IS_ERR(ebbcharClass))
{               
  unregister_chrdev(majorNumber, DEVICE_NAME);
  printk(KERN_ALERT "Failed to register device class\n");
  return PTR_ERR(ebbcharClass);          
}

So what does the IS_ERR() macro expand to and how it gets executed.

like image 489
S.I.J Avatar asked May 19 '15 08:05

S.I.J


2 Answers

Tests if the supplied pointer should be considered an error value.

It does not check if the pointer is valid.

In your code IS_ERR is used to check if class_create succeded creating ebbcharClass. If an error occurs unregister the char driver and signal the error.

You can find MACROs and inline funcs in err.h

like image 200
LPs Avatar answered Nov 05 '22 20:11

LPs


Be careful for pitfall:

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
#define MAX_ERRNO       4095

This covers -1 to -4095, which represents error code, not number below 4096, nor NULL (0). Every value from 0 to 4294963201 (0xfffff001) is considered no error. Do not use it to cover NULL checking.

like image 7
bsd unix Avatar answered Nov 05 '22 21:11

bsd unix