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 #include <grp.h>
39 #endif
41 #include <fnmatch.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdint.h>
46 /* For flock. */
47 #ifndef O_EXLOCK
48 #define O_EXLOCK 0
49 #endif
51 #ifndef HAVE_FLOCK
52 #define LOCK_SH 0
53 #define LOCK_EX 0
54 #define LOCK_NB 0
55 #define flock(fd, op) (0)
56 #else
57 #include <sys/file.h>
58 #endif
60 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
61 #ifndef WAIT_ANY
62 #define WAIT_ANY (-1)
63 #endif
65 /* SOCK_NONBLOCK isn't available across BSDs... */
66 #ifndef SOCK_NONBLOCK
67 #define SOCK_NONBLOCK 00004000
68 #endif
70 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
71 * using EAI_NONAME.
72 */
73 #ifndef EAI_NODATA
74 #define EAI_NODATA EAI_NONAME
75 #endif
77 #ifndef __dead
78 #define __dead __attribute__ ((__noreturn__))
79 #endif
81 #ifndef __unused
82 #define __unused __attribute__ ((__unused__))
83 #endif
85 #ifndef __OpenBSD__
86 #define pledge(s, p) (0)
87 #define unveil(s, p) (0)
88 #endif
90 #ifndef __FreeBSD__
91 #define cap_enter() (0)
92 #endif
94 #ifndef HAVE_SETRESGID
95 #define setresgid(a, b, c) (0)
96 #endif
98 #ifndef HAVE_SETRESUID
99 #define setresuid(a, b, c) (0)
100 #endif
102 #ifndef HAVE_LINUX_LANDLOCK_H
103 #define landlock_no_fs() (0)
104 #else
105 int landlock_no_fs(void);
106 #endif
108 #ifndef INFTIM
109 #define INFTIM -1
110 #endif
112 #ifndef HAVE_BSD_UUID
113 #include <uuid/uuid.h>
114 #define uuid_s_ok 0
115 #define uuid_s_bad_version 1
116 #define uuid_s_invalid_string_uuid 2
117 #define uuid_s_no_memory 3
119 /* Length of a node address (an IEEE 802 address). */
120 #define _UUID_NODE_LEN 6
122 struct uuid {
123 uint32_t time_low;
124 uint16_t time_mid;
125 uint16_t time_hi_and_version;
126 uint8_t clock_seq_hi_and_reserved;
127 uint8_t clock_seq_low;
128 uint8_t node[_UUID_NODE_LEN];
129 };
131 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
132 int32_t uuid_is_nil(struct uuid *, uint32_t *);
133 void uuid_create(uuid_t *, uint32_t *);
134 void uuid_create_nil(struct uuid *, uint32_t *);
135 void uuid_from_string(const char *, uuid_t *, uint32_t *);
136 void uuid_to_string(uuid_t *, char **, uint32_t *);
137 #else
138 #include <uuid.h>
139 #endif
141 #ifdef HAVE_STDINT_H
142 #include <stdint.h>
143 #else
144 #include <inttypes.h>
145 #endif
147 /* Only include sys/queue.h if it's not found on the system (libbsd provides
148 * a copy of this).
149 */
150 #if !defined(HAVE_QUEUE_H) && !defined(HAVE_LIBBSD)
151 #include "compat/queue.h"
152 #else
153 #include <sys/queue.h>
154 #endif
156 #ifdef HAVE_TREE_H
157 #include <sys/tree.h>
158 #else
159 #include "compat/tree.h"
160 #endif
162 #ifdef HAVE_UTIL_H
163 #include <util.h>
164 #endif
166 #ifdef HAVE_LIBUTIL_H
167 #include <libutil.h>
168 #endif
170 #ifdef HAVE_IMSG
171 #else
172 #include "compat/imsg.h"
173 #endif
175 #ifdef HAVE_SIPHASH
176 #include <siphash.h>
177 #else
178 #include "compat/siphash.h"
179 #endif
181 /* Include Apple-specific headers. Mostly for crypto.*/
182 #if defined(__APPLE__)
183 #define COMMON_DIGEST_FOR_OPENSSL
184 #include <CommonCrypto/CommonDigest.h>
185 #endif
187 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
188 #include <sha1.h>
189 #elif !defined(__APPLE__) && !defined(__DragonFly__)
190 #include <sha.h>
191 #elif defined(__DragonFly__)
192 #include <openssl/sha.h>
193 #endif
195 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
196 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
197 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
199 #define SHA1_CTX SHA_CTX
200 #define SHA1Init SHA1_Init
201 #define SHA1Update SHA1_Update
202 #define SHA1Final SHA1_Final
203 #endif
205 #ifndef HAVE_ASPRINTF
206 /* asprintf.c */
207 int asprintf(char **, const char *, ...);
208 int vasprintf(char **, const char *, va_list);
209 #endif
211 #ifndef HAVE_EXPLICIT_BZERO
212 /* explicit_bzero.c */
213 void explicit_bzero(void *, size_t);
214 #endif
216 #ifndef HAVE_GETDTABLECOUNT
217 /* getdtablecount.c */
218 int getdtablecount(void);
219 #endif
221 #ifndef HAVE_CLOSEFROM
222 /* closefrom.c */
223 void closefrom(int);
224 #endif
226 #if defined (__FreeBSD__)
227 #define closefrom(fd) (closefrom(fd), 0)
228 #endif
230 #ifndef HAVE_STRSEP
231 /* strsep.c */
232 char *strsep(char **, const char *);
233 #endif
235 #ifndef HAVE_STRTONUM
236 /* strtonum.c */
237 long long strtonum(const char *, long long, long long, const char **);
238 #endif
240 #ifndef HAVE_STRLCPY
241 /* strlcpy.c */
242 size_t strlcpy(char *, const char *, size_t);
243 #endif
245 #ifndef HAVE_STRLCAT
246 /* strlcat.c */
247 size_t strlcat(char *, const char *, size_t);
248 #endif
250 #ifndef HAVE_STRNLEN
251 /* strnlen.c */
252 size_t strnlen(const char *, size_t);
253 #endif
255 #ifndef HAVE_STRNDUP
256 /* strndup.c */
257 char *strndup(const char *, size_t);
258 #endif
260 #ifndef HAVE_GETPROGNAME
261 /* getprogname.c */
262 const char *getprogname(void);
263 #endif
265 #ifndef HAVE_GETLINE
266 /* getline.c */
267 ssize_t getline(char **, size_t *, FILE *);
268 #endif
270 #ifndef HAVE_FREEZERO
271 /* freezero.c */
272 void freezero(void *, size_t);
273 #endif
275 #ifndef HAVE_GETDTABLECOUNT
276 /* getdtablecount.c */
277 int getdtablecount(void);
278 #endif
280 #ifndef HAVE_REALLOCARRAY
281 /* reallocarray.c */
282 void *reallocarray(void *, size_t, size_t);
283 #endif
285 #ifndef HAVE_RECALLOCARRAY
286 /* recallocarray.c */
287 void *recallocarray(void *, size_t, size_t, size_t);
288 #endif
290 #ifndef HAVE_SETPROCTITLE
291 /* setproctitle.c */
292 void setproctitle(const char *, ...);
293 #endif
295 #ifndef HAVE_FMT_SCALED
296 /* fmt_scaled.c */
297 int fmt_scaled(long long, char *);
298 int scan_scaled(char *, long long *);
299 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
300 #endif
302 #ifndef HAVE_LIBBSD
303 /* getopt.c */
304 extern int BSDopterr;
305 extern int BSDoptind;
306 extern int BSDoptopt;
307 extern int BSDoptreset;
308 extern char *BSDoptarg;
309 int BSDgetopt(int, char *const *, const char *);
310 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
311 #define opterr BSDopterr
312 #define optind BSDoptind
313 #define optopt BSDoptopt
314 #define optreset BSDoptreset
315 #define optarg BSDoptarg
316 #endif
317 #endif
319 /* Check for some of the non-portable timespec*() functions.
320 * This should largely come from libbsd for systems which
321 * aren't BSD, but this will depend on how old the library
322 * is.
323 */
324 #ifndef timespecisset
325 #define timespecisset(tsp) \
326 ((tsp)->tv_sec || (tsp)->tv_nsec)
327 #endif
329 #ifndef timespecsub
330 #define timespecsub(tsp, usp, vsp) \
331 do { \
332 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
333 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
334 if ((vsp)->tv_nsec < 0) { \
335 (vsp)->tv_sec--; \
336 (vsp)->tv_nsec += 1000000000L; \
337 } \
338 } while (0)
339 #endif
341 #ifndef timespeccmp
342 #define timespeccmp(tvp, uvp, cmp) \
343 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
344 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
345 ((tvp)->tv_sec cmp (uvp)->tv_sec))
346 #endif
348 #ifndef HAVE_BSD_MERGESORT
349 /* mergesort.c */
350 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
351 #endif