Blob


1 #ifndef _GOT_COMPAT_H
2 #define _GOT_COMPAT_H
4 #include <sys/types.h>
5 #include <sys/ioctl.h>
6 #include <sys/uio.h>
7 #if defined(__FreeBSD__)
8 #include <sys/endian.h>
9 #include <sys/capsicum.h>
10 #elif defined(__APPLE__)
11 #include <machine/endian.h>
12 #include <libkern/OSByteOrder.h>
13 #include "compat/bsd-poll.h"
15 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
17 #define htobe16(x) OSSwapHostToBigInt16(x)
18 #define htole16(x) OSSwapHostToLittleInt16(x)
19 #define be16toh(x) OSSwapBigToHostInt16(x)
20 #define le16toh(x) OSSwapLittleToHostInt16(x)
22 #define htobe32(x) OSSwapHostToBigInt32(x)
23 #define htole32(x) OSSwapHostToLittleInt32(x)
24 #define be32toh(x) OSSwapBigToHostInt32(x)
25 #define le32toh(x) OSSwapLittleToHostInt32(x)
27 #define htobe64(x) OSSwapHostToBigInt64(x)
28 #define htole64(x) OSSwapHostToLittleInt64(x)
29 #define be64toh(x) OSSwapBigToHostInt64(x)
30 #define le64toh(x) OSSwapLittleToHostInt64(x)
32 #define st_atim st_atimespec
33 #define st_ctim st_ctimespec
34 #define st_mtim st_mtimespec
36 #else /* Linux, etc... */
37 #include <endian.h>
38 #endif
40 #include <fnmatch.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdint.h>
45 /* For flock. */
46 #ifndef O_EXLOCK
47 #define O_EXLOCK 0
48 #endif
50 #ifndef HAVE_FLOCK
51 #define LOCK_SH 0
52 #define LOCK_EX 0
53 #define LOCK_NB 0
54 #define flock(fd, op) (0)
55 #else
56 #include <sys/file.h>
57 #endif
59 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
60 #ifndef WAIT_ANY
61 #define WAIT_ANY (-1)
62 #endif
64 /* SOCK_NONBLOCK isn't available across BSDs... */
65 #ifndef SOCK_NONBLOCK
66 #define SOCK_NONBLOCK 00004000
67 #endif
69 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
70 * using EAI_NONAME.
71 */
72 #ifndef EAI_NODATA
73 #define EAI_NODATA EAI_NONAME
74 #endif
76 #ifndef __dead
77 #define __dead __attribute__ ((__noreturn__))
78 #endif
80 #ifndef __unused
81 #define __unused __attribute__ ((__unused__))
82 #endif
84 #ifndef __OpenBSD__
85 #define pledge(s, p) (0)
86 #define unveil(s, p) (0)
87 #endif
89 #ifndef __FreeBSD__
90 #define cap_enter() (0)
91 #endif
93 #ifndef HAVE_SETRESGID
94 #define setresgid(a, b, c) (0)
95 #endif
97 #ifndef HAVE_SETRESUID
98 #define setresuid(a, b, c) (0)
99 #endif
101 #ifndef HAVE_LINUX_LANDLOCK_H
102 #define landlock_no_fs() (0)
103 #else
104 int landlock_no_fs(void);
105 #endif
107 #ifndef INFTIM
108 #define INFTIM -1
109 #endif
111 #ifndef HAVE_BSD_UUID
112 #include <uuid/uuid.h>
113 #define uuid_s_ok 0
114 #define uuid_s_bad_version 1
115 #define uuid_s_invalid_string_uuid 2
116 #define uuid_s_no_memory 3
118 /* Length of a node address (an IEEE 802 address). */
119 #define _UUID_NODE_LEN 6
121 struct uuid {
122 uint32_t time_low;
123 uint16_t time_mid;
124 uint16_t time_hi_and_version;
125 uint8_t clock_seq_hi_and_reserved;
126 uint8_t clock_seq_low;
127 uint8_t node[_UUID_NODE_LEN];
128 };
130 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
131 int32_t uuid_is_nil(struct uuid *, uint32_t *);
132 void uuid_create(uuid_t *, uint32_t *);
133 void uuid_create_nil(struct uuid *, uint32_t *);
134 void uuid_from_string(const char *, uuid_t *, uint32_t *);
135 void uuid_to_string(uuid_t *, char **, uint32_t *);
136 #else
137 #include <uuid.h>
138 #endif
140 #ifdef HAVE_STDINT_H
141 #include <stdint.h>
142 #else
143 #include <inttypes.h>
144 #endif
146 #ifdef HAVE_QUEUE_H
147 #include <sys/queue.h>
148 #else
149 #include "compat/queue.h"
150 #endif
152 #ifdef HAVE_TREE_H
153 #include <sys/tree.h>
154 #else
155 #include "compat/tree.h"
156 #endif
158 #ifdef HAVE_UTIL_H
159 #include <util.h>
160 #endif
162 #ifdef HAVE_LIBUTIL_H
163 #include <libutil.h>
164 #endif
166 #ifdef HAVE_IMSG
167 #else
168 #include "compat/imsg.h"
169 #endif
171 #ifdef HAVE_SIPHASH
172 #include <siphash.h>
173 #else
174 #include "compat/siphash.h"
175 #endif
177 /* Include Apple-specific headers. Mostly for crypto.*/
178 #if defined(__APPLE__)
179 #define COMMON_DIGEST_FOR_OPENSSL
180 #include <CommonCrypto/CommonDigest.h>
181 #endif
183 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
184 #include <sha1.h>
185 #elif !defined(__APPLE__) && !defined(__DragonFly__)
186 #include <sha.h>
187 #elif defined(__DragonFly__)
188 #include <openssl/sha.h>
189 #endif
191 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
192 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
193 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
195 #define SHA1_CTX SHA_CTX
196 #define SHA1Init SHA1_Init
197 #define SHA1Update SHA1_Update
198 #define SHA1Final SHA1_Final
199 #endif
201 #ifndef HAVE_ASPRINTF
202 /* asprintf.c */
203 int asprintf(char **, const char *, ...);
204 int vasprintf(char **, const char *, va_list);
205 #endif
207 #ifndef HAVE_EXPLICIT_BZERO
208 /* explicit_bzero.c */
209 void explicit_bzero(void *, size_t);
210 #endif
212 #ifndef HAVE_GETDTABLECOUNT
213 /* getdtablecount.c */
214 int getdtablecount(void);
215 #endif
217 #ifndef HAVE_CLOSEFROM
218 /* closefrom.c */
219 void closefrom(int);
220 #endif
222 #if defined (__FreeBSD__)
223 #define closefrom(fd) (closefrom(fd), 0)
224 #endif
226 #ifndef HAVE_STRSEP
227 /* strsep.c */
228 char *strsep(char **, const char *);
229 #endif
231 #ifndef HAVE_STRTONUM
232 /* strtonum.c */
233 long long strtonum(const char *, long long, long long, const char **);
234 #endif
236 #ifndef HAVE_STRLCPY
237 /* strlcpy.c */
238 size_t strlcpy(char *, const char *, size_t);
239 #endif
241 #ifndef HAVE_STRLCAT
242 /* strlcat.c */
243 size_t strlcat(char *, const char *, size_t);
244 #endif
246 #ifndef HAVE_STRNLEN
247 /* strnlen.c */
248 size_t strnlen(const char *, size_t);
249 #endif
251 #ifndef HAVE_STRNDUP
252 /* strndup.c */
253 char *strndup(const char *, size_t);
254 #endif
256 #ifndef HAVE_GETPROGNAME
257 /* getprogname.c */
258 const char *getprogname(void);
259 #endif
261 #ifndef HAVE_GETLINE
262 /* getline.c */
263 ssize_t getline(char **, size_t *, FILE *);
264 #endif
266 #ifndef HAVE_FREEZERO
267 /* freezero.c */
268 void freezero(void *, size_t);
269 #endif
271 #ifndef HAVE_GETDTABLECOUNT
272 /* getdtablecount.c */
273 int getdtablecount(void);
274 #endif
276 #ifndef HAVE_REALLOCARRAY
277 /* reallocarray.c */
278 void *reallocarray(void *, size_t, size_t);
279 #endif
281 #ifndef HAVE_RECALLOCARRAY
282 /* recallocarray.c */
283 void *recallocarray(void *, size_t, size_t, size_t);
284 #endif
286 #ifndef HAVE_SETPROCTITLE
287 /* setproctitle.c */
288 void setproctitle(const char *, ...);
289 #endif
291 #ifndef HAVE_FMT_SCALED
292 /* fmt_scaled.c */
293 int fmt_scaled(long long, char *);
294 int scan_scaled(char *, long long *);
295 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
296 #endif
298 #ifndef HAVE_LIBBSD
299 /* getopt.c */
300 extern int BSDopterr;
301 extern int BSDoptind;
302 extern int BSDoptopt;
303 extern int BSDoptreset;
304 extern char *BSDoptarg;
305 int BSDgetopt(int, char *const *, const char *);
306 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
307 #define opterr BSDopterr
308 #define optind BSDoptind
309 #define optopt BSDoptopt
310 #define optreset BSDoptreset
311 #define optarg BSDoptarg
312 #endif
313 #endif
315 /* Check for some of the non-portable timespec*() functions.
316 * This should largely come from libbsd for systems which
317 * aren't BSD, but this will depend on how old the library
318 * is.
319 */
320 #ifndef timespecisset
321 #define timespecisset(tsp) \
322 ((tsp)->tv_sec || (tsp)->tv_nsec)
323 #endif
325 #ifndef timespecsub
326 #define timespecsub(tsp, usp, vsp) \
327 do { \
328 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
329 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
330 if ((vsp)->tv_nsec < 0) { \
331 (vsp)->tv_sec--; \
332 (vsp)->tv_nsec += 1000000000L; \
333 } \
334 } while (0)
335 #endif
337 #ifndef timespeccmp
338 #define timespeccmp(tvp, uvp, cmp) \
339 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
340 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
341 ((tvp)->tv_sec cmp (uvp)->tv_sec))
342 #endif
344 #ifndef HAVE_BSD_MERGESORT
345 /* mergesort.c */
346 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
347 #endif