Synchronizing Headers

A number of glibc headers have equivalent header in Linux kernel space. While is is generally not a good idea to mix usage of kernel space and user space headers in a program, it is often unavoidable due to decisions made by library developers. The kernel and user space headers can define the same structures, with the resulting conflicts causing errors during compilation. Ideally, we would like to synchronize the kernel UAPI header and the libc headers so either one can be used and both can be included in any order.

Fixing Compatibility

The Linux kernel provides a <linux/libc-compat.h> header that allows defining guard macros to prevent conflicting definitions. The general approach to follow is:

  1. Synchronize the Linux UAPI header with the libc header so that either one can be used while conserving ABI in both projects.
  2. In the Linux source:
    1. Include the libc-compat header in the UAPI header as early as possible.
    2. In libc-compat.h detect whether the conflicting libc header has been included first.
    3. Define guards in the form __UAPI_DEF_FOO and set these to 1 or 0 if the userspace header has been included respectively.

    4. In the UAPI header, guard the definitions with #if __UAPI_DEF_FOO ...

  3. In the glibc userspace header, guard the definitions with #if !__UAPI_DEF_FOO etc.

Point #2 fixes the case where the userspace header is included before the kernel space header, and point #3 fixes the other way.

Example

Example patches fixing the inclusion of both <netinet/in.h> and <linux/in6.h>:

Known Pairs of Headers that Conflict

Fixed Conflicts

None: Synchronizing_Headers (last edited 2022-07-11 14:45:37 by CarlosODonell)