1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
3 * Compatibility interface for userspace libc header coordination:
5 * Define compatibility macros that are used to control the inclusion or
6 * exclusion of UAPI structures and definitions in coordination with another
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:
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.
18 * Then follow this process:
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.
24 * (b) In libc-compat.h add enough code to detect that the comflicting
25 * userspace libc header has been included first.
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.
31 * (d) Back in the UAPI header with the conflicting definitions, guard the
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:
41 * * For all definitions that conflict with kernel definitions wrap those
42 * defines in the following:
47 * This prevents the redefinition of a construct already defined by the kernel.
49 #ifndef _UAPI_LIBC_COMPAT_H
50 #define _UAPI_LIBC_COMPAT_H
52 /* We have included glibc headers... */
53 #if defined(__GLIBC__)
55 /* Coordinate with glibc net/if.h header. */
56 #if defined(_NET_IF_H) && defined(__USE_MISC)
58 /* GLIBC headers included first so don't define anything
59 * that would already be defined. */
61 #define __UAPI_DEF_IF_IFCONF 0
62 #define __UAPI_DEF_IF_IFMAP 0
63 #define __UAPI_DEF_IF_IFNAMSIZ 0
64 #define __UAPI_DEF_IF_IFREQ 0
65 /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
66 #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 0
67 /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
68 #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
69 #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
70 #endif /* __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO */
74 /* Linux headers included first, and we must define everything
75 * we need. The expectation is that glibc will check the
76 * __UAPI_DEF_* defines and adjust appropriately. */
78 #define __UAPI_DEF_IF_IFCONF 1
79 #define __UAPI_DEF_IF_IFMAP 1
80 #define __UAPI_DEF_IF_IFNAMSIZ 1
81 #define __UAPI_DEF_IF_IFREQ 1
82 /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
83 #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
84 /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
85 #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
87 #endif /* _NET_IF_H */
89 /* Coordinate with glibc netinet/in.h header. */
90 #if defined(_NETINET_IN_H)
92 /* GLIBC headers included first so don't define anything
93 * that would already be defined. */
94 #define __UAPI_DEF_IN_ADDR 0
95 #define __UAPI_DEF_IN_IPPROTO 0
96 #define __UAPI_DEF_IN_PKTINFO 0
97 #define __UAPI_DEF_IP_MREQ 0
98 #define __UAPI_DEF_SOCKADDR_IN 0
99 #define __UAPI_DEF_IN_CLASS 0
101 #define __UAPI_DEF_IN6_ADDR 0
102 /* The exception is the in6_addr macros which must be defined
103 * if the glibc code didn't define them. This guard matches
104 * the guard in glibc/inet/netinet/in.h which defines the
105 * additional in6_addr macros e.g. s6_addr16, and s6_addr32. */
106 #if defined(__USE_MISC) || defined (__USE_GNU)
107 #define __UAPI_DEF_IN6_ADDR_ALT 0
109 #define __UAPI_DEF_IN6_ADDR_ALT 1
111 #define __UAPI_DEF_SOCKADDR_IN6 0
112 #define __UAPI_DEF_IPV6_MREQ 0
113 #define __UAPI_DEF_IPPROTO_V6 0
114 #define __UAPI_DEF_IPV6_OPTIONS 0
115 #define __UAPI_DEF_IN6_PKTINFO 0
116 #define __UAPI_DEF_IP6_MTUINFO 0
120 /* Linux headers included first, and we must define everything
121 * we need. The expectation is that glibc will check the
122 * __UAPI_DEF_* defines and adjust appropriately. */
123 #define __UAPI_DEF_IN_ADDR 1
124 #define __UAPI_DEF_IN_IPPROTO 1
125 #define __UAPI_DEF_IN_PKTINFO 1
126 #define __UAPI_DEF_IP_MREQ 1
127 #define __UAPI_DEF_SOCKADDR_IN 1
128 #define __UAPI_DEF_IN_CLASS 1
130 #define __UAPI_DEF_IN6_ADDR 1
131 /* We unconditionally define the in6_addr macros and glibc must
133 #define __UAPI_DEF_IN6_ADDR_ALT 1
134 #define __UAPI_DEF_SOCKADDR_IN6 1
135 #define __UAPI_DEF_IPV6_MREQ 1
136 #define __UAPI_DEF_IPPROTO_V6 1
137 #define __UAPI_DEF_IPV6_OPTIONS 1
138 #define __UAPI_DEF_IN6_PKTINFO 1
139 #define __UAPI_DEF_IP6_MTUINFO 1
141 #endif /* _NETINET_IN_H */
143 /* Definitions for xattr.h */
144 #if defined(_SYS_XATTR_H)
145 #define __UAPI_DEF_XATTR 0
147 #define __UAPI_DEF_XATTR 1
150 /* If we did not see any headers from any supported C libraries,
151 * or we are being included in the kernel, then define everything
152 * that we need. Check for previous __UAPI_* definitions to give
153 * unsupported C libraries a way to opt out of any kernel definition. */
154 #else /* !defined(__GLIBC__) */
156 /* Definitions for if.h */
157 #ifndef __UAPI_DEF_IF_IFCONF
158 #define __UAPI_DEF_IF_IFCONF 1
160 #ifndef __UAPI_DEF_IF_IFMAP
161 #define __UAPI_DEF_IF_IFMAP 1
163 #ifndef __UAPI_DEF_IF_IFNAMSIZ
164 #define __UAPI_DEF_IF_IFNAMSIZ 1
166 #ifndef __UAPI_DEF_IF_IFREQ
167 #define __UAPI_DEF_IF_IFREQ 1
169 /* Everything up to IFF_DYNAMIC, matches net/if.h until glibc 2.23 */
170 #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS
171 #define __UAPI_DEF_IF_NET_DEVICE_FLAGS 1
173 /* For the future if glibc adds IFF_LOWER_UP, IFF_DORMANT and IFF_ECHO */
174 #ifndef __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO
175 #define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO 1
178 /* Definitions for in.h */
179 #ifndef __UAPI_DEF_IN_ADDR
180 #define __UAPI_DEF_IN_ADDR 1
182 #ifndef __UAPI_DEF_IN_IPPROTO
183 #define __UAPI_DEF_IN_IPPROTO 1
185 #ifndef __UAPI_DEF_IN_PKTINFO
186 #define __UAPI_DEF_IN_PKTINFO 1
188 #ifndef __UAPI_DEF_IP_MREQ
189 #define __UAPI_DEF_IP_MREQ 1
191 #ifndef __UAPI_DEF_SOCKADDR_IN
192 #define __UAPI_DEF_SOCKADDR_IN 1
194 #ifndef __UAPI_DEF_IN_CLASS
195 #define __UAPI_DEF_IN_CLASS 1
198 /* Definitions for in6.h */
199 #ifndef __UAPI_DEF_IN6_ADDR
200 #define __UAPI_DEF_IN6_ADDR 1
202 #ifndef __UAPI_DEF_IN6_ADDR_ALT
203 #define __UAPI_DEF_IN6_ADDR_ALT 1
205 #ifndef __UAPI_DEF_SOCKADDR_IN6
206 #define __UAPI_DEF_SOCKADDR_IN6 1
208 #ifndef __UAPI_DEF_IPV6_MREQ
209 #define __UAPI_DEF_IPV6_MREQ 1
211 #ifndef __UAPI_DEF_IPPROTO_V6
212 #define __UAPI_DEF_IPPROTO_V6 1
214 #ifndef __UAPI_DEF_IPV6_OPTIONS
215 #define __UAPI_DEF_IPV6_OPTIONS 1
217 #ifndef __UAPI_DEF_IN6_PKTINFO
218 #define __UAPI_DEF_IN6_PKTINFO 1
220 #ifndef __UAPI_DEF_IP6_MTUINFO
221 #define __UAPI_DEF_IP6_MTUINFO 1
224 /* Definitions for xattr.h */
225 #ifndef __UAPI_DEF_XATTR
226 #define __UAPI_DEF_XATTR 1
229 #endif /* __GLIBC__ */
231 #endif /* _UAPI_LIBC_COMPAT_H */