libc-compat.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * Compatibility interface for userspace libc header coordination:
  4. *
  5. * Define compatibility macros that are used to control the inclusion or
  6. * exclusion of UAPI structures and definitions in coordination with another
  7. * userspace C library.
  8. *
  9. * This header is intended to solve the problem of UAPI definitions that
  10. * conflict with userspace definitions. If a UAPI header has such conflicting
  11. * definitions then the solution is as follows:
  12. *
  13. * * Synchronize the UAPI header and the libc headers so either one can be
  14. * used and such that the ABI is preserved. If this is not possible then
  15. * no simple compatibility interface exists (you need to write translating
  16. * wrappers and rename things) and you can't use this interface.
  17. *
  18. * Then follow this process:
  19. *
  20. * (a) Include libc-compat.h in the UAPI header.
  21. * e.g. #include <linux/libc-compat.h>
  22. * This include must be as early as possible.
  23. *
  24. * (b) In libc-compat.h add enough code to detect that the comflicting
  25. * userspace libc header has been included first.
  26. *
  27. * (c) If the userspace libc header has been included first define a set of
  28. * guard macros of the form __UAPI_DEF_FOO and set their values to 1, else
  29. * set their values to 0.
  30. *
  31. * (d) Back in the UAPI header with the conflicting definitions, guard the
  32. * definitions with:
  33. * #if __UAPI_DEF_FOO
  34. * ...
  35. * #endif
  36. *
  37. * This fixes the situation where the linux headers are included *after* the
  38. * libc headers. To fix the problem with the inclusion in the other order the
  39. * userspace libc headers must be fixed like this:
  40. *
  41. * * For all definitions that conflict with kernel definitions wrap those
  42. * defines in the following:
  43. * #if !__UAPI_DEF_FOO
  44. * ...
  45. * #endif
  46. *
  47. * This prevents the redefinition of a construct already defined by the kernel.
  48. */
  49. #ifndef _LIBC_COMPAT_H
  50. #define _LIBC_COMPAT_H
  51. /* We have included glibc headers... */
  52. #if defined(__GLIBC__)
  53. /* Coordinate with glibc net/if.h header. */
  54. #if defined(_NET_IF_H) && defined(__USE_MISC)
  55. /* GLIBC headers included first so don't define anything
  56. * that would already be defined. */
  57. #define __UAPI_DEF_IF_IFCONF 0
  58. #define __UAPI_DEF_IF_IFMAP 0
  59. #define __UAPI_DEF_IF_IFNAMSIZ 0
  60. #define __UAPI_DEF_IF_IFREQ 0
  61. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  62. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 0
  63. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  64. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
  65. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  66. #endif /* __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO */
  67. #else /* _NET_IF_H */
  68. /* Linux headers included first, and we must define everything
  69. * we need. The expectation is that glibc will check the
  70. * __UAPI_DEF_* defines and adjust appropriately. */
  71. #define __UAPI_DEF_IF_IFCONF 1
  72. #define __UAPI_DEF_IF_IFMAP 1
  73. #define __UAPI_DEF_IF_IFNAMSIZ 1
  74. #define __UAPI_DEF_IF_IFREQ 1
  75. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  76. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
  77. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  78. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  79. #endif /* _NET_IF_H */
  80. /* Coordinate with glibc netinet/in.h header. */
  81. #if defined(_NETINET_IN_H)
  82. /* GLIBC headers included first so don't define anything
  83. * that would already be defined. */
  84. #define __UAPI_DEF_IN_ADDR 0
  85. #define __UAPI_DEF_IN_IPPROTO 0
  86. #define __UAPI_DEF_IN_PKTINFO 0
  87. #define __UAPI_DEF_IP_MREQ 0
  88. #define __UAPI_DEF_SOCKADDR_IN 0
  89. #define __UAPI_DEF_IN_CLASS 0
  90. #define __UAPI_DEF_IN6_ADDR 0
  91. /* The exception is the in6_addr macros which must be defined
  92. * if the glibc code didn't define them. This guard matches
  93. * the guard in glibc/inet/netinet/in.h which defines the
  94. * additional in6_addr macros e.g. s6_addr16, and s6_addr32. */
  95. #if defined(__USE_MISC) || defined (__USE_GNU)
  96. #define __UAPI_DEF_IN6_ADDR_ALT 0
  97. #else
  98. #define __UAPI_DEF_IN6_ADDR_ALT 1
  99. #endif
  100. #define __UAPI_DEF_SOCKADDR_IN6 0
  101. #define __UAPI_DEF_IPV6_MREQ 0
  102. #define __UAPI_DEF_IPPROTO_V6 0
  103. #define __UAPI_DEF_IPV6_OPTIONS 0
  104. #define __UAPI_DEF_IN6_PKTINFO 0
  105. #define __UAPI_DEF_IP6_MTUINFO 0
  106. #else
  107. /* Linux headers included first, and we must define everything
  108. * we need. The expectation is that glibc will check the
  109. * __UAPI_DEF_* defines and adjust appropriately. */
  110. #define __UAPI_DEF_IN_ADDR 1
  111. #define __UAPI_DEF_IN_IPPROTO 1
  112. #define __UAPI_DEF_IN_PKTINFO 1
  113. #define __UAPI_DEF_IP_MREQ 1
  114. #define __UAPI_DEF_SOCKADDR_IN 1
  115. #define __UAPI_DEF_IN_CLASS 1
  116. #define __UAPI_DEF_IN6_ADDR 1
  117. /* We unconditionally define the in6_addr macros and glibc must
  118. * coordinate. */
  119. #define __UAPI_DEF_IN6_ADDR_ALT 1
  120. #define __UAPI_DEF_SOCKADDR_IN6 1
  121. #define __UAPI_DEF_IPV6_MREQ 1
  122. #define __UAPI_DEF_IPPROTO_V6 1
  123. #define __UAPI_DEF_IPV6_OPTIONS 1
  124. #define __UAPI_DEF_IN6_PKTINFO 1
  125. #define __UAPI_DEF_IP6_MTUINFO 1
  126. #endif /* _NETINET_IN_H */
  127. /* Coordinate with glibc netipx/ipx.h header. */
  128. #if defined(__NETIPX_IPX_H)
  129. #define __UAPI_DEF_SOCKADDR_IPX 0
  130. #define __UAPI_DEF_IPX_ROUTE_DEFINITION 0
  131. #define __UAPI_DEF_IPX_INTERFACE_DEFINITION 0
  132. #define __UAPI_DEF_IPX_CONFIG_DATA 0
  133. #define __UAPI_DEF_IPX_ROUTE_DEF 0
  134. #else /* defined(__NETIPX_IPX_H) */
  135. #define __UAPI_DEF_SOCKADDR_IPX 1
  136. #define __UAPI_DEF_IPX_ROUTE_DEFINITION 1
  137. #define __UAPI_DEF_IPX_INTERFACE_DEFINITION 1
  138. #define __UAPI_DEF_IPX_CONFIG_DATA 1
  139. #define __UAPI_DEF_IPX_ROUTE_DEF 1
  140. #endif /* defined(__NETIPX_IPX_H) */
  141. /* Definitions for xattr.h */
  142. #if defined(_SYS_XATTR_H)
  143. #define __UAPI_DEF_XATTR 0
  144. #else
  145. #define __UAPI_DEF_XATTR 1
  146. #endif
  147. /* If we did not see any headers from any supported C libraries,
  148. * or we are being included in the kernel, then define everything
  149. * that we need. Check for previous __UAPI_* definitions to give
  150. * unsupported C libraries a way to opt out of any kernel definition. */
  151. #else /* !defined(__GLIBC__) */
  152. /* Definitions for if.h */
  153. #ifndef __UAPI_DEF_IF_IFCONF
  154. #define __UAPI_DEF_IF_IFCONF 1
  155. #endif
  156. #ifndef __UAPI_DEF_IF_IFMAP
  157. #define __UAPI_DEF_IF_IFMAP 1
  158. #endif
  159. #ifndef __UAPI_DEF_IF_IFNAMSIZ
  160. #define __UAPI_DEF_IF_IFNAMSIZ 1
  161. #endif
  162. #ifndef __UAPI_DEF_IF_IFREQ
  163. #define __UAPI_DEF_IF_IFREQ 1
  164. #endif
  165. /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
  166. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS
  167. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
  168. #endif
  169. /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
  170. #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
  171. #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
  172. #endif
  173. /* Definitions for in.h */
  174. #ifndef __UAPI_DEF_IN_ADDR
  175. #define __UAPI_DEF_IN_ADDR 1
  176. #endif
  177. #ifndef __UAPI_DEF_IN_IPPROTO
  178. #define __UAPI_DEF_IN_IPPROTO 1
  179. #endif
  180. #ifndef __UAPI_DEF_IN_PKTINFO
  181. #define __UAPI_DEF_IN_PKTINFO 1
  182. #endif
  183. #ifndef __UAPI_DEF_IP_MREQ
  184. #define __UAPI_DEF_IP_MREQ 1
  185. #endif
  186. #ifndef __UAPI_DEF_SOCKADDR_IN
  187. #define __UAPI_DEF_SOCKADDR_IN 1
  188. #endif
  189. #ifndef __UAPI_DEF_IN_CLASS
  190. #define __UAPI_DEF_IN_CLASS 1
  191. #endif
  192. /* Definitions for in6.h */
  193. #ifndef __UAPI_DEF_IN6_ADDR
  194. #define __UAPI_DEF_IN6_ADDR 1
  195. #endif
  196. #ifndef __UAPI_DEF_IN6_ADDR_ALT
  197. #define __UAPI_DEF_IN6_ADDR_ALT 1
  198. #endif
  199. #ifndef __UAPI_DEF_SOCKADDR_IN6
  200. #define __UAPI_DEF_SOCKADDR_IN6 1
  201. #endif
  202. #ifndef __UAPI_DEF_IPV6_MREQ
  203. #define __UAPI_DEF_IPV6_MREQ 1
  204. #endif
  205. #ifndef __UAPI_DEF_IPPROTO_V6
  206. #define __UAPI_DEF_IPPROTO_V6 1
  207. #endif
  208. #ifndef __UAPI_DEF_IPV6_OPTIONS
  209. #define __UAPI_DEF_IPV6_OPTIONS 1
  210. #endif
  211. #ifndef __UAPI_DEF_IN6_PKTINFO
  212. #define __UAPI_DEF_IN6_PKTINFO 1
  213. #endif
  214. #ifndef __UAPI_DEF_IP6_MTUINFO
  215. #define __UAPI_DEF_IP6_MTUINFO 1
  216. #endif
  217. /* Definitions for ipx.h */
  218. #ifndef __UAPI_DEF_SOCKADDR_IPX
  219. #define __UAPI_DEF_SOCKADDR_IPX 1
  220. #endif
  221. #ifndef __UAPI_DEF_IPX_ROUTE_DEFINITION
  222. #define __UAPI_DEF_IPX_ROUTE_DEFINITION 1
  223. #endif
  224. #ifndef __UAPI_DEF_IPX_INTERFACE_DEFINITION
  225. #define __UAPI_DEF_IPX_INTERFACE_DEFINITION 1
  226. #endif
  227. #ifndef __UAPI_DEF_IPX_CONFIG_DATA
  228. #define __UAPI_DEF_IPX_CONFIG_DATA 1
  229. #endif
  230. #ifndef __UAPI_DEF_IPX_ROUTE_DEF
  231. #define __UAPI_DEF_IPX_ROUTE_DEF 1
  232. #endif
  233. /* Definitions for xattr.h */
  234. #ifndef __UAPI_DEF_XATTR
  235. #define __UAPI_DEF_XATTR 1
  236. #endif
  237. #endif /* __GLIBC__ */
  238. #endif /* _LIBC_COMPAT_H */