libc-compat.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 _UAPI_LIBC_COMPAT_H
  49. #define _UAPI_LIBC_COMPAT_H
  50. /* We have included glibc headers... */
  51. #if defined(__GLIBC__)
  52. /* Coordinate with glibc net/if.h header. */
  53. #if defined(_NET_IF_H) && defined(__USE_MISC)
  54. /* GLIBC headers included first so don't define anything
  55. * that would already be defined. */
  56. #define __UAPI_DEF_IF_IFCONF 0
  57. #define __UAPI_DEF_IF_IFMAP 0
  58. #define __UAPI_DEF_IF_IFNAMSIZ 0
  59. #define __UAPI_DEF_IF_IFREQ 0
  60. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  61. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 0
  62. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  63. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
  64. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  65. #endif /* __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO */
  66. #else /* _NET_IF_H */
  67. /* Linux headers included first, and we must define everything
  68. * we need. The expectation is that glibc will check the
  69. * __UAPI_DEF_* defines and adjust appropriately. */
  70. #define __UAPI_DEF_IF_IFCONF 1
  71. #define __UAPI_DEF_IF_IFMAP 1
  72. #define __UAPI_DEF_IF_IFNAMSIZ 1
  73. #define __UAPI_DEF_IF_IFREQ 1
  74. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  75. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
  76. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  77. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  78. #endif /* _NET_IF_H */
  79. /* Coordinate with glibc netinet/in.h header. */
  80. #if defined(_NETINET_IN_H)
  81. /* GLIBC headers included first so don't define anything
  82. * that would already be defined. */
  83. #define __UAPI_DEF_IN_ADDR 0
  84. #define __UAPI_DEF_IN_IPPROTO 0
  85. #define __UAPI_DEF_IN_PKTINFO 0
  86. #define __UAPI_DEF_IP_MREQ 0
  87. #define __UAPI_DEF_SOCKADDR_IN 0
  88. #define __UAPI_DEF_IN_CLASS 0
  89. #define __UAPI_DEF_IN6_ADDR 0
  90. /* The exception is the in6_addr macros which must be defined
  91. * if the glibc code didn't define them. This guard matches
  92. * the guard in glibc/inet/netinet/in.h which defines the
  93. * additional in6_addr macros e.g. s6_addr16, and s6_addr32. */
  94. #if defined(__USE_MISC) || defined (__USE_GNU)
  95. #define __UAPI_DEF_IN6_ADDR_ALT 0
  96. #else
  97. #define __UAPI_DEF_IN6_ADDR_ALT 1
  98. #endif
  99. #define __UAPI_DEF_SOCKADDR_IN6 0
  100. #define __UAPI_DEF_IPV6_MREQ 0
  101. #define __UAPI_DEF_IPPROTO_V6 0
  102. #define __UAPI_DEF_IPV6_OPTIONS 0
  103. #define __UAPI_DEF_IN6_PKTINFO 0
  104. #define __UAPI_DEF_IP6_MTUINFO 0
  105. #else
  106. /* Linux headers included first, and we must define everything
  107. * we need. The expectation is that glibc will check the
  108. * __UAPI_DEF_* defines and adjust appropriately. */
  109. #define __UAPI_DEF_IN_ADDR 1
  110. #define __UAPI_DEF_IN_IPPROTO 1
  111. #define __UAPI_DEF_IN_PKTINFO 1
  112. #define __UAPI_DEF_IP_MREQ 1
  113. #define __UAPI_DEF_SOCKADDR_IN 1
  114. #define __UAPI_DEF_IN_CLASS 1
  115. #define __UAPI_DEF_IN6_ADDR 1
  116. /* We unconditionally define the in6_addr macros and glibc must
  117. * coordinate. */
  118. #define __UAPI_DEF_IN6_ADDR_ALT 1
  119. #define __UAPI_DEF_SOCKADDR_IN6 1
  120. #define __UAPI_DEF_IPV6_MREQ 1
  121. #define __UAPI_DEF_IPPROTO_V6 1
  122. #define __UAPI_DEF_IPV6_OPTIONS 1
  123. #define __UAPI_DEF_IN6_PKTINFO 1
  124. #define __UAPI_DEF_IP6_MTUINFO 1
  125. #endif /* _NETINET_IN_H */
  126. /* Definitions for xattr.h */
  127. #if defined(_SYS_XATTR_H)
  128. #define __UAPI_DEF_XATTR 0
  129. #else
  130. #define __UAPI_DEF_XATTR 1
  131. #endif
  132. /* If we did not see any headers from any supported C libraries,
  133. * or we are being included in the kernel, then define everything
  134. * that we need. */
  135. #else /* !defined(__GLIBC__) */
  136. /* Definitions for if.h */
  137. #define __UAPI_DEF_IF_IFCONF 1
  138. #define __UAPI_DEF_IF_IFMAP 1
  139. #define __UAPI_DEF_IF_IFNAMSIZ 1
  140. #define __UAPI_DEF_IF_IFREQ 1
  141. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  142. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
  143. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  144. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  145. /* Definitions for in.h */
  146. #define __UAPI_DEF_IN_ADDR 1
  147. #define __UAPI_DEF_IN_IPPROTO 1
  148. #define __UAPI_DEF_IN_PKTINFO 1
  149. #define __UAPI_DEF_IP_MREQ 1
  150. #define __UAPI_DEF_SOCKADDR_IN 1
  151. #define __UAPI_DEF_IN_CLASS 1
  152. /* Definitions for in6.h */
  153. #define __UAPI_DEF_IN6_ADDR 1
  154. #define __UAPI_DEF_IN6_ADDR_ALT 1
  155. #define __UAPI_DEF_SOCKADDR_IN6 1
  156. #define __UAPI_DEF_IPV6_MREQ 1
  157. #define __UAPI_DEF_IPPROTO_V6 1
  158. #define __UAPI_DEF_IPV6_OPTIONS 1
  159. #define __UAPI_DEF_IN6_PKTINFO 1
  160. #define __UAPI_DEF_IP6_MTUINFO 1
  161. /* Definitions for xattr.h */
  162. #define __UAPI_DEF_XATTR 1
  163. #endif /* __GLIBC__ */
  164. #endif /* _UAPI_LIBC_COMPAT_H */