Linux – Why do user space apps need kernel headers

kernel-moduleslinuxlinux-kernel

I'm building busy-box and iptables for an embedded device and one of the dependencies for them are the kernel headers.

I have searched the whole file system for *.ko files and found none. So i concluded the apps aren't creating any loadable drivers (kernel modules).

What are other cases for a user space application to require kernel headers?

Best Answer

Because those programs are build to use things defined in the kernel headers:

busybox-1.22.1]$ egrep -RHn '^#include <linux'
modutils/modutils-24.c:194:#include <linux/elf-em.h>
include/fix_u32.h:17:#include <linux/types.h>
libbb/loop.c:11:#include <linux/version.h>
console-tools/openvt.c:23:#include <linux/vt.h>
console-tools/kbd_mode.c:23:#include <linux/kd.h>
console-tools/showkey.c:19:#include <linux/kd.h>
util-linux/blockdev.c:36:#include <linux/fs.h>
util-linux/mkfs_ext2.c:50:#include <linux/fs.h>
util-linux/mkfs_vfat.c:28:#include <linux/hdreg.h> /* HDIO_GETGEO */
util-linux/mkfs_vfat.c:29:#include <linux/fd.h>    /* FDGETPRM */
....

For each specific tool, you'd need to read the source of the tool and the relevant kernel header to figure out exactly what.

You can see a few things are commented to make it easy.

For example, mkfs_vfat includes linux/fd.h to get FDGETPRM:

$ egrep -RHn FDGETPRM util-linux/mkfs_vfat.c
util-linux/mkfs_vfat.c:29:#include <linux/fd.h>    /* FDGETPRM */
util-linux/mkfs_vfat.c:351:         int not_floppy = ioctl(dev, FDGETPRM, &param);

You could probably remove the relevant #include and watch for compiler errors to make it easier, you'll get warnings that some things are not defined. Those things likely come from the kernel headers.