2 * Copyright (c) 2023 Thomas Adam <thomas@xteddy.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef _GOT_COMPAT_H_2
18 #define _GOT_COMPAT_H_2
29 #if defined(__FreeBSD__)
30 #include <sys/endian.h>
31 #include <sys/capsicum.h>
32 #elif defined(__APPLE__)
33 #include <machine/endian.h>
34 #include <libkern/OSByteOrder.h>
35 #include "compat/bsd-poll.h"
37 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
39 #define htobe16(x) OSSwapHostToBigInt16(x)
40 #define htole16(x) OSSwapHostToLittleInt16(x)
41 #define be16toh(x) OSSwapBigToHostInt16(x)
42 #define le16toh(x) OSSwapLittleToHostInt16(x)
44 #define htobe32(x) OSSwapHostToBigInt32(x)
45 #define htole32(x) OSSwapHostToLittleInt32(x)
46 #define be32toh(x) OSSwapBigToHostInt32(x)
47 #define le32toh(x) OSSwapLittleToHostInt32(x)
49 #define htobe64(x) OSSwapHostToBigInt64(x)
50 #define htole64(x) OSSwapHostToLittleInt64(x)
51 #define be64toh(x) OSSwapBigToHostInt64(x)
52 #define le64toh(x) OSSwapLittleToHostInt64(x)
54 #define st_atim st_atimespec
55 #define st_ctim st_ctimespec
56 #define st_mtim st_mtimespec
58 #else /* Linux, etc... */
65 #define __attribute__(a)
70 # define UID_MAX UINT_MAX /* max value for a uid_t */
73 # define GID_MAX UINT_MAX /* max value for a gid_t */
85 #define flock(fd, op) (0)
90 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
95 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
99 #define EAI_NODATA EAI_NONAME
103 #define __dead __attribute__ ((__noreturn__))
107 #define __unused __attribute__ ((__unused__))
110 #if !defined(__bounded__) && !defined(__OpenBSD__)
111 #define __bounded__(a, b, c)
115 #define pledge(s, p) (0)
116 #define unveil(s, p) (0)
120 #define cap_enter() (0)
123 #ifndef HAVE_B64_NTOP
126 int b64_ntop(u_char
const *, size_t, char *, size_t);
127 int b64_pton(char const *, u_char
*, size_t);
130 #ifndef HAVE_SETRESGID
131 #define setresgid(a, b, c) (0)
134 #ifndef HAVE_SETRESUID
135 #define setresuid(a, b, c) (0)
138 #ifndef HAVE_LINUX_LANDLOCK_H
139 #define landlock_no_fs() (0)
141 int landlock_no_fs(void);
148 #ifndef HAVE_BSD_UUID
149 #include <uuid/uuid.h>
151 #define uuid_s_bad_version 1
152 #define uuid_s_invalid_string_uuid 2
153 #define uuid_s_no_memory 3
155 /* Length of a node address (an IEEE 802 address). */
156 #define _UUID_NODE_LEN 6
161 uint16_t time_hi_and_version
;
162 uint8_t clock_seq_hi_and_reserved
;
163 uint8_t clock_seq_low
;
164 uint8_t node
[_UUID_NODE_LEN
];
167 int32_t uuid_equal(struct uuid
*, struct uuid
*, uint32_t *);
168 int32_t uuid_is_nil(struct uuid
*, uint32_t *);
169 void uuid_create(uuid_t
*, uint32_t *);
170 void uuid_create_nil(struct uuid
*, uint32_t *);
171 void uuid_from_string(const char *, uuid_t
*, uint32_t *);
172 void uuid_to_string(uuid_t
*, char **, uint32_t *);
178 #include <sys/queue.h>
182 #include "compat/tree.h"
184 #include <sys/tree.h>
191 #ifdef HAVE_LIBUTIL_H
196 # define IOV_MAX 1024
200 #include "compat/imsg.h"
206 #include "compat/siphash.h"
211 /* Include Apple-specific headers. Mostly for crypto.*/
212 #if defined(__APPLE__)
213 #define COMMON_DIGEST_FOR_OPENSSL
214 #include <CommonCrypto/CommonDigest.h>
217 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
218 #define SHA256Init SHA256_Init
219 #define SHA256Update SHA256_Update
220 #define SHA256Final SHA256_Final
221 #define SHA2_CTX SHA256_CTX
239 /* Catch-all for systems where the header files don't exist and/or the below
240 * still are not defined.
242 #ifndef SHA256_DIGEST_LENGTH
243 #define SHA256_DIGEST_LENGTH 32
246 #ifndef SHA256_DIGEST_STRING_LENGTH
247 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
250 #if defined(__DragonFly__)
251 #include <openssl/sha.h>
254 #ifndef SHA1_DIGEST_LENGTH
255 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
256 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
258 #define SHA1_CTX SHA_CTX
259 #define SHA1Init SHA1_Init
260 #define SHA1Update SHA1_Update
261 #define SHA1Final SHA1_Final
264 /* SOCK_NONBLOCK isn't available across BSDs... */
265 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
266 #define SOCK_NONBLOCK 00004000
269 #ifndef HAVE_ASPRINTF
271 int asprintf(char **, const char *, ...);
272 int vasprintf(char **, const char *, va_list);
275 #ifndef HAVE_EXPLICIT_BZERO
276 /* explicit_bzero.c */
277 void explicit_bzero(void *, size_t);
280 #ifndef HAVE_GETDTABLECOUNT
281 /* getdtablecount.c */
282 int getdtablecount(void);
285 #ifndef HAVE_CLOSEFROM
292 char *strsep(char **, const char *);
295 #ifndef HAVE_STRTONUM
297 long long strtonum(const char *, long long, long long, const char **);
302 size_t strlcpy(char *, const char *, size_t);
307 size_t strlcat(char *, const char *, size_t);
312 size_t strnlen(const char *, size_t);
317 char *strndup(const char *, size_t);
320 #ifndef HAVE_GETPROGNAME
322 const char *getprogname(void);
327 ssize_t
getline(char **, size_t *, FILE *);
330 #ifndef HAVE_FREEZERO
332 void freezero(void *, size_t);
335 #ifndef HAVE_GETDTABLECOUNT
336 /* getdtablecount.c */
337 int getdtablecount(void);
340 #ifndef HAVE_REALLOCARRAY
342 void *reallocarray(void *, size_t, size_t);
345 #ifndef HAVE_RECALLOCARRAY
346 /* recallocarray.c */
347 void *recallocarray(void *, size_t, size_t, size_t);
350 #ifndef HAVE_SETPROCTITLE
352 void setproctitle(const char *, ...);
355 #ifndef HAVE_FMT_SCALED
357 int fmt_scaled(long long, char *);
358 int scan_scaled(char *, long long *);
359 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
362 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
364 extern int BSDopterr
;
365 extern int BSDoptind
;
366 extern int BSDoptopt
;
367 extern int BSDoptreset
;
368 extern char *BSDoptarg
;
369 int BSDgetopt(int, char *const *, const char *);
370 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
371 #define opterr BSDopterr
372 #define optind BSDoptind
373 #define optopt BSDoptopt
374 #define optreset BSDoptreset
375 #define optarg BSDoptarg
378 /* Check for some of the non-portable timespec*() functions.
379 * This should largely come from libbsd for systems which
380 * aren't BSD, but this will depend on how old the library
383 #ifndef timespecisset
384 #define timespecisset(tsp) \
385 ((tsp)->tv_sec || (tsp)->tv_nsec)
389 #define timespecsub(tsp, usp, vsp) \
391 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
392 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
393 if ((vsp)->tv_nsec < 0) { \
395 (vsp)->tv_nsec += 1000000000L; \
401 #define timespeccmp(tvp, uvp, cmp) \
402 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
403 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
404 ((tvp)->tv_sec cmp (uvp)->tv_sec))
407 #ifndef HAVE_MERGESORT
409 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));