“'errno' undeclared” when compile Linux ke

2019-09-11 13:37发布

问题:

I'm trying to compile and I keep getting the following error: enter image description here

I've included the asm-i386/errno.h once and it didn't work. Also I've tried including linux/errno.h and it didn't work ether.

What file should I include?

回答1:

There is no errno variable in Linux kernel: this variable lives in user space only.

If kernel function wants to report about the error and specify the error code, it encapsulates the error code into the returning value. There are 3 possibilities of such encapsulation, dependent from value type returned on success:

  1. Function returns 0 on success, negated error code on fail.

This mostly used convention is sometimes referenced as 0/-err.

  1. Function returns valid pointer on success, expression ERR_PTR(err) on fail.

This expression is evaluated to the pointer, which can never points to real kernel object. This convention may be used even if NULL is valid result.

  1. Function returns positive integer on success, negated error code on fail: +val/-err.

In case when 0 is valid result, this convention may be used too: +val/0/-err.


When user space library needs to set errno according to kernel's request, it checks result of the system call (which is the only way for perform request to the kernel). Dependent on syscall, either 1 or 3 convention is used (return type of any system call is long).

Example of "setting" errno in kernel space for user space see here.