How to Set Errno in Linux Device Driver

How to set errno in Linux device driver?

Nice question!

Ok, you could think of errno as global variable (to be honnest, it is an extern int). errno has plenty of pre-defined macros for errorcodes in the errno.h library. You can have a look here. It is very likely that some of these errorcodes describe what you want to show. Pick up the right one, set it like if it was a variable you defined, and (important!) exit immediately!

You may ask yourself though if setting errno is the right approach to your problem. You can always define an (*int) and develop your own error codes, and error handling mechanism. Errno's purpose is to show and explain system errors. Do u consider your code part of the "system" (as I can see you develop your own system call, so this might be the case) ? So go on and use errno to explain your "system error".

Edit (On question update): Ok more info. As i said errno is an extern int and is set by the kernel. The value at which errno is set is simply the return value of the system call. Linux kernel then interprets this negative value through the library errno.h. So an example error message is set simply by returning (EBUSY is just an example - you can use all of the predifined error types) the error message you want from your system call. Example:

return -EBUSY

Hope it helps

multiple definition error in linux device driver

There's 3 common options.

  1. Don't define the firmware in a header file, define it in a .c file and create the

    functions you need to work with that firmware within that .c file. Expose those functions in your header file.

  2. Make the array static, so it's not visible in other translation units:

like so:

static const unsigned char ucPICAppsectorFirmware[] = ....

Note that this will create a copy of the array in every .c file where you include this header file.


  1. Place the array in a .c file, and declare it instead of defining it in the header file. This way the array would not be duplicated for each file that includes it, as would be the case when defining the array in the header file.

i.e. the header file would look like.

extern const unsigned char ucPICAppsectorFirmware[];
extern const size_t ucPICAppsectorFirmwareLen;

and the .c file would look like

const unsigned char ucPICAppsectorFirmware[] = ...;
const size_t ucPICAppsectorFirmwareLen = sizeof ucPICAppsectorFirmware;

Is any errno a valid POSIX return value for read()?

The Linux manpages are generally pretty good, but they are not authoritative. For a question like this, you need to go to the official standards. You should only return one of the error codes listed in the POSIX.1-2008 specification for read (scroll down to where it says ERRORS). If you return some other code, application software will not be prepared to handle it and may crash.

Be aware that many of those codes may only be returned under the conditions described for them. For instance, your device driver has no business returning EBADF, EISDIR, EOVERFLOW, ESPIPE, or anything with the word "socket" in its description, because all the circumstances where those are the appropriate failure codes are dealt with before control reaches you. This is probably also the case for EINTR but I'm not sure.

You also should know that Linux doesn't implement some of the optional features of POSIX.1-2008 (most importantly here, pretend everything marked OB_XSR isn't there at all) and that some of the distinctions made by the standard are obsolescent (such as ENOMEM versus ENOBUFS).

Putting it all together, unless your device driver is very unusual, I expect you should only need EIO, EAGAIN, ENOMEM, and maybe ENXIO (if the EIO/ENXIO distinction is meaningful for your hardware).



Related Topics



Leave a reply



Submit