libc-compat.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Compatibility interface for userspace libc header coordination:
  3. *
  4. * Define compatibility macros that are used to control the inclusion or
  5. * exclusion of UAPI structures and definitions in coordination with another
  6. * userspace C library.
  7. *
  8. * This header is intended to solve the problem of UAPI definitions that
  9. * conflict with userspace definitions. If a UAPI header has such conflicting
  10. * definitions then the solution is as follows:
  11. *
  12. * * Synchronize the UAPI header and the libc headers so either one can be
  13. * used and such that the ABI is preserved. If this is not possible then
  14. * no simple compatibility interface exists (you need to write translating
  15. * wrappers and rename things) and you can't use this interface.
  16. *
  17. * Then follow this process:
  18. *
  19. * (a) Include libc-compat.h in the UAPI header.
  20. * e.g. #include <linux/libc-compat.h>
  21. * This include must be as early as possible.
  22. *
  23. * (b) In libc-compat.h add enough code to detect that the comflicting
  24. * userspace libc header has been included first.
  25. *
  26. * (c) If the userspace libc header has been included first define a set of
  27. * guard macros of the form __UAPI_DEF_FOO and set their values to 1, else
  28. * set their values to 0.
  29. *
  30. * (d) Back in the UAPI header with the conflicting definitions, guard the
  31. * definitions with:
  32. * #if __UAPI_DEF_FOO
  33. * ...
  34. * #endif
  35. *
  36. * This fixes the situation where the linux headers are included *after* the
  37. * libc headers. To fix the problem with the inclusion in the other order the
  38. * userspace libc headers must be fixed like this:
  39. *
  40. * * For all definitions that conflict with kernel definitions wrap those
  41. * defines in the following:
  42. * #if !__UAPI_DEF_FOO
  43. * ...
  44. * #endif
  45. *
  46. * This prevents the redefinition of a construct already defined by the kernel.
  47. */
  48. #ifndef _LIBC_COMPAT_H
  49. #define _LIBC_COMPAT_H
  50. /* We have included libc headers... */
  51. /* Coordinate with libc net/if.h header. */
  52. #if defined(_NET_IF_H) && defined(__USE_MISC)
  53. /* LIBC headers included first so don't define anything
  54. * that would already be defined. */
  55. #define __UAPI_DEF_IF_IFCONF 0
  56. #define __UAPI_DEF_IF_IFMAP 0
  57. #define __UAPI_DEF_IF_IFNAMSIZ 0
  58. #define __UAPI_DEF_IF_IFREQ 0
  59. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  60. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 0
  61. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  62. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
  63. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  64. #endif /* __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO */
  65. #else /* _NET_IF_H */
  66. /* Linux headers included first, and we must define everything
  67. * we need. The expectation is that glibc will check the
  68. * __UAPI_DEF_* defines and adjust appropriately. */
  69. #define __UAPI_DEF_IF_IFCONF 1
  70. #define __UAPI_DEF_IF_IFMAP 1
  71. #define __UAPI_DEF_IF_IFNAMSIZ 1
  72. #define __UAPI_DEF_IF_IFREQ 1
  73. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  74. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
  75. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  76. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  77. #endif /* _NET_IF_H */
  78. /* musl defines the ethhdr struct itself in its netinet/if_ether.h.
  79. * Glibc just includes the kernel header and uses a different guard. */
  80. #if defined(_NETINET_IF_ETHER_H)
  81. #define __UAPI_DEF_ETHHDR 0
  82. #else
  83. #define __UAPI_DEF_ETHHDR 1
  84. #endif
  85. /* Coordinate with libc netinet/in.h header. */
  86. #if defined(_NETINET_IN_H)
  87. /* LIBC headers included first so don't define anything
  88. * that would already be defined. */
  89. #define __UAPI_DEF_IN_ADDR 0
  90. #define __UAPI_DEF_IN_IPPROTO 0
  91. #define __UAPI_DEF_IN_PKTINFO 0
  92. #define __UAPI_DEF_IP_MREQ 0
  93. #define __UAPI_DEF_SOCKADDR_IN 0
  94. #define __UAPI_DEF_IN_CLASS 0
  95. #define __UAPI_DEF_IN6_ADDR 0
  96. /* The exception is the in6_addr macros which must be defined
  97. * if the glibc code didn't define them. This guard matches
  98. * the guard in glibc/inet/netinet/in.h which defines the
  99. * additional in6_addr macros e.g. s6_addr16, and s6_addr32. */
  100. #if !defined(__GLIBC__) || defined(__USE_MISC) || defined (__USE_GNU)
  101. #define __UAPI_DEF_IN6_ADDR_ALT 0
  102. #else
  103. #define __UAPI_DEF_IN6_ADDR_ALT 1
  104. #endif
  105. #define __UAPI_DEF_SOCKADDR_IN6 0
  106. #define __UAPI_DEF_IPV6_MREQ 0
  107. #define __UAPI_DEF_IPPROTO_V6 0
  108. #define __UAPI_DEF_IPV6_OPTIONS 0
  109. #define __UAPI_DEF_IN6_PKTINFO 0
  110. #define __UAPI_DEF_IP6_MTUINFO 0
  111. #else
  112. /* Linux headers included first, and we must define everything
  113. * we need. The expectation is that the libc will check the
  114. * __UAPI_DEF_* defines and adjust appropriately. */
  115. #define __UAPI_DEF_IN_ADDR 1
  116. #define __UAPI_DEF_IN_IPPROTO 1
  117. #define __UAPI_DEF_IN_PKTINFO 1
  118. #define __UAPI_DEF_IP_MREQ 1
  119. #define __UAPI_DEF_SOCKADDR_IN 1
  120. #define __UAPI_DEF_IN_CLASS 1
  121. #define __UAPI_DEF_IN6_ADDR 1
  122. /* We unconditionally define the in6_addr macros and the libc must
  123. * coordinate. */
  124. #define __UAPI_DEF_IN6_ADDR_ALT 1
  125. #define __UAPI_DEF_SOCKADDR_IN6 1
  126. #define __UAPI_DEF_IPV6_MREQ 1
  127. #define __UAPI_DEF_IPPROTO_V6 1
  128. #define __UAPI_DEF_IPV6_OPTIONS 1
  129. #define __UAPI_DEF_IN6_PKTINFO 1
  130. #define __UAPI_DEF_IP6_MTUINFO 1
  131. #endif /* _NETINET_IN_H */
  132. /* Definitions for xattr.h */
  133. #if defined(_SYS_XATTR_H)
  134. #define __UAPI_DEF_XATTR 0
  135. #else
  136. #define __UAPI_DEF_XATTR 1
  137. #endif
  138. /* If we did not see any headers from any supported C libraries,
  139. * or we are being included in the kernel, then define everything
  140. * that we need. */
  141. #endif /* _LIBC_COMPAT_H */