Search code examples
linux-kernelsmartphone

Why do user space apps need kernel headers?


I am studying a smartphone project. During compilation process it's installing kernel header files for user space building.

Why do user space apps need kernel headers?


Solution

  • In general, those headers are needed because userspace applications often talk to kernel, passing some data. To do this, they have to agree on the structure of data passed between them.

    Most of the kernel headers are only needed by libc library (if you're using one) as it usually hides all the lowlevel aspects from you by the providing abstractions conforming to some standards like POSIX (it will usually provide its own include files). Those headers will, for example, provide all the syscall numbers and definitions of all the structures used by their arguments.

    The are, however, some "custom services" provided by kernel that are not handled by libc. One example is creating userspace programs that talk directly to some hardware drivers. That may require passing some data structures (so you need some struct definitions), knowing some magic numbers (so you need some defines), etc.

    As an example, take a look at hid-example.c from kernel sources. It will, for example, call this ioctl:

    struct hidraw_report_descriptor rpt_desc;
    [...]
    ioctl(fd, HIDIOCGRDESC, &rpt_desc);
    

    But where did it get HIDIOCGRDESC or know the structure of struct hidraw_report_descriptor? They are of course defined in linux/hidraw.h which this application included.