libc-compat.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /* Coordinate with glibc netipx/ipx.h header. */
  127. #if defined(__NETIPX_IPX_H)
  128. #define __UAPI_DEF_SOCKADDR_IPX 0
  129. #define __UAPI_DEF_IPX_ROUTE_DEFINITION 0
  130. #define __UAPI_DEF_IPX_INTERFACE_DEFINITION 0
  131. #define __UAPI_DEF_IPX_CONFIG_DATA 0
  132. #define __UAPI_DEF_IPX_ROUTE_DEF 0
  133. #else /* defined(__NETIPX_IPX_H) */
  134. #define __UAPI_DEF_SOCKADDR_IPX 1
  135. #define __UAPI_DEF_IPX_ROUTE_DEFINITION 1
  136. #define __UAPI_DEF_IPX_INTERFACE_DEFINITION 1
  137. #define __UAPI_DEF_IPX_CONFIG_DATA 1
  138. #define __UAPI_DEF_IPX_ROUTE_DEF 1
  139. #endif /* defined(__NETIPX_IPX_H) */
  140. /* Definitions for xattr.h */
  141. #if defined(_SYS_XATTR_H)
  142. #define __UAPI_DEF_XATTR 0
  143. #else
  144. #define __UAPI_DEF_XATTR 1
  145. #endif
  146. /* If we did not see any headers from any supported C libraries,
  147. * or we are being included in the kernel, then define everything
  148. * that we need. Check for previous __UAPI_* definitions to give
  149. * unsupported C libraries a way to opt out of any kernel definition. */
  150. #else /* !defined(__GLIBC__) */
  151. /* Definitions for if.h */
  152. #ifndef __UAPI_DEF_IF_IFCONF
  153. #define __UAPI_DEF_IF_IFCONF 1
  154. #endif
  155. #ifndef __UAPI_DEF_IF_IFMAP
  156. #define __UAPI_DEF_IF_IFMAP 1
  157. #endif
  158. #ifndef __UAPI_DEF_IF_IFNAMSIZ
  159. #define __UAPI_DEF_IF_IFNAMSIZ 1
  160. #endif
  161. #ifndef __UAPI_DEF_IF_IFREQ
  162. #define __UAPI_DEF_IF_IFREQ 1
  163. #endif
  164. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  165. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS
  166. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
  167. #endif
  168. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  169. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
  170. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  171. #endif
  172. /* Definitions for in.h */
  173. #ifndef __UAPI_DEF_IN_ADDR
  174. #define __UAPI_DEF_IN_ADDR 1
  175. #endif
  176. #ifndef __UAPI_DEF_IN_IPPROTO
  177. #define __UAPI_DEF_IN_IPPROTO 1
  178. #endif
  179. #ifndef __UAPI_DEF_IN_PKTINFO
  180. #define __UAPI_DEF_IN_PKTINFO 1
  181. #endif
  182. #ifndef __UAPI_DEF_IP_MREQ
  183. #define __UAPI_DEF_IP_MREQ 1
  184. #endif
  185. #ifndef __UAPI_DEF_SOCKADDR_IN
  186. #define __UAPI_DEF_SOCKADDR_IN 1
  187. #endif
  188. #ifndef __UAPI_DEF_IN_CLASS
  189. #define __UAPI_DEF_IN_CLASS 1
  190. #endif
  191. /* Definitions for in6.h */
  192. #ifndef __UAPI_DEF_IN6_ADDR
  193. #define __UAPI_DEF_IN6_ADDR 1
  194. #endif
  195. #ifndef __UAPI_DEF_IN6_ADDR_ALT
  196. #define __UAPI_DEF_IN6_ADDR_ALT 1
  197. #endif
  198. #ifndef __UAPI_DEF_SOCKADDR_IN6
  199. #define __UAPI_DEF_SOCKADDR_IN6 1
  200. #endif
  201. #ifndef __UAPI_DEF_IPV6_MREQ
  202. #define __UAPI_DEF_IPV6_MREQ 1
  203. #endif
  204. #ifndef __UAPI_DEF_IPPROTO_V6
  205. #define __UAPI_DEF_IPPROTO_V6 1
  206. #endif
  207. #ifndef __UAPI_DEF_IPV6_OPTIONS
  208. #define __UAPI_DEF_IPV6_OPTIONS 1
  209. #endif
  210. #ifndef __UAPI_DEF_IN6_PKTINFO
  211. #define __UAPI_DEF_IN6_PKTINFO 1
  212. #endif
  213. #ifndef __UAPI_DEF_IP6_MTUINFO
  214. #define __UAPI_DEF_IP6_MTUINFO 1
  215. #endif
  216. /* Definitions for ipx.h */
  217. #ifndef __UAPI_DEF_SOCKADDR_IPX
  218. #define __UAPI_DEF_SOCKADDR_IPX 1
  219. #endif
  220. #ifndef __UAPI_DEF_IPX_ROUTE_DEFINITION
  221. #define __UAPI_DEF_IPX_ROUTE_DEFINITION 1
  222. #endif
  223. #ifndef __UAPI_DEF_IPX_INTERFACE_DEFINITION
  224. #define __UAPI_DEF_IPX_INTERFACE_DEFINITION 1
  225. #endif
  226. #ifndef __UAPI_DEF_IPX_CONFIG_DATA
  227. #define __UAPI_DEF_IPX_CONFIG_DATA 1
  228. #endif
  229. #ifndef __UAPI_DEF_IPX_ROUTE_DEF
  230. #define __UAPI_DEF_IPX_ROUTE_DEF 1
  231. #endif
  232. /* Definitions for xattr.h */
  233. #ifndef __UAPI_DEF_XATTR
  234. #define __UAPI_DEF_XATTR 1
  235. #endif
  236. #endif /* __GLIBC__ */
  237. #endif /* _UAPI_LIBC_COMPAT_H */