10

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?

4
  • An easy example to come up with is when something in the user space issues a system call.
    – user48669
    Oct 7, 2014 at 7:41
  • @SamiLaine I would guess the c library would provide the interface to system calls. If what you're saying was the case, wouldn't you, for example, have to import kernel headers just to open a socket? Oct 7, 2014 at 7:58
  • Socket(2), is a system call, not a library function, so yes.
    – user48669
    Oct 7, 2014 at 8:00
  • @SamiLaine Can you give an example where you have to include <linux/*.h> to open a socket? (I was aiming for sys/socket.h) Oct 7, 2014 at 8:19

1 Answer 1

8

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.

7
  • 2
    1. Busybox is designed for embedded and other lightweight systems, where you want to load as less libraries in memory as possible. I have not read the source, but probably BB is basically sidestepping the libraries and link directly against the kernel. 2. No. It couples your userspace binary with a version of the kernel API, which is backwards-compatible: a binary compiled today might not work on an older kernel (depending on the symbols included... read the source code) but will work with future kernels.
    – ignis
    Oct 7, 2014 at 8:46
  • 1
    @suprjami The ABI obeys the standard SysV ABI, which can be reasonably assumed not to change; among other things, SysV ABI is why you can link against the kernel with your compiler (and version) of choice, not necessarily the very same one that emitted the kernel binary.
    – ignis
    Oct 7, 2014 at 19:03
  • 1
    ... it also makes it possible to chroot/LXC into a distro that shipped with a different kernel. etc.
    – ignis
    Oct 7, 2014 at 19:15
  • 1
    (For clarity: note that the kernel has an external ABI, which we are discussing about here, and an internal ABI between the kernel modules, which undergoes frequent and possibly incompatible changes, but it is not seen by userspace and is irrelevant when compiling userspace code.)
    – ignis
    Oct 7, 2014 at 19:18
  • 1
    (Also note that ABI =/= API. The kernel's (external) API includes the symbols referenced in the source code, and the kernel developers have committed to maintain compatibility; unless the symbols are very rarely used in real world software, it would be an unreasonable move for them to make incompatible changes, although no third-party standard mandates the whole of the Linux API and therefore nothing technically impedes it.)
    – ignis
    Oct 7, 2014 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .