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 #ifdef HAVE_QUEUE_H
148 #include <sys/queue.h>
149 #endif
151 #ifdef HAVE_TREE_H
152 #include <sys/tree.h>
153 #else
154 #include "compat/tree.h"
155 #endif
157 #ifdef HAVE_UTIL_H
158 #include <util.h>
159 #endif
161 #ifdef HAVE_LIBUTIL_H
162 #include <libutil.h>
163 #endif
165 #ifdef HAVE_IMSG
166 #else
167 #include "compat/imsg.h"
168 #endif
170 #ifdef HAVE_SIPHASH
171 #include <siphash.h>
172 #else
173 #include "compat/siphash.h"
174 #endif
176 /* Include Apple-specific headers. Mostly for crypto.*/
177 #if defined(__APPLE__)
178 #define COMMON_DIGEST_FOR_OPENSSL
179 #include <CommonCrypto/CommonDigest.h>
180 #endif
182 #ifdef HAVE_SHA_AS_SHA1
183 # include <sha.h>
184 #endif
185 #ifdef HAVE_SHA1_AS_SHA1
186 # include <sha1.h>
187 #endif
188 #ifdef HAVE_SHA2
189 # include <sha2.h>
190 #endif
191 #ifdef HAVE_SHA256
192 # include <sha256.h>
193 #endif
195 /* Catch-all for systems where the header files don't exist and/or the below
196 * still are not defined.
197 */
198 #ifndef SHA256_DIGEST_LENGTH
199 #define SHA256_DIGEST_LENGTH 32
200 #endif
202 #ifndef SHA256_DIGEST_STRING_LENGTH
203 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
204 #endif
206 #if defined(__DragonFly__)
207 #include <openssl/sha.h>
208 #endif
210 #ifdef NEEDS_SHA1_DEFS
211 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
212 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
214 #define SHA1_CTX SHA_CTX
215 #define SHA1Init SHA1_Init
216 #define SHA1Update SHA1_Update
217 #define SHA1Final SHA1_Final
218 #endif
220 /*
221 * The following SA_LEN/SS_LEN dance comes from various source, notably
222 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
223 * has tweaked a lot of the following macros to suit the needs of
224 * got-portable.
225 */
227 /* From OpenNTPD portable */
228 #if !defined(SA_LEN)
229 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
230 # define SA_LEN(x) ((x)->sa_len)
231 # else
232 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
233 sizeof(struct sockaddr_in6) : \
234 sizeof(struct sockaddr_in))
235 # endif
237 #endif
239 /* From OpenBGPD portable */
240 #if !defined(SS_LEN)
241 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
242 # define SS_LEN(x) ((x)->ss_len)
243 # else
244 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
245 # endif
246 #endif
248 #ifndef HAVE_ASPRINTF
249 /* asprintf.c */
250 int asprintf(char **, const char *, ...);
251 int vasprintf(char **, const char *, va_list);
252 #endif
254 #ifndef HAVE_EXPLICIT_BZERO
255 /* explicit_bzero.c */
256 void explicit_bzero(void *, size_t);
257 #endif
259 #ifndef HAVE_GETDTABLECOUNT
260 /* getdtablecount.c */
261 int getdtablecount(void);
262 #endif
264 #ifndef HAVE_CLOSEFROM
265 /* closefrom.c */
266 void closefrom(int);
267 #endif
269 #if defined (__FreeBSD__)
270 #define closefrom(fd) (closefrom(fd), 0)
271 #endif
273 #ifndef HAVE_STRSEP
274 /* strsep.c */
275 char *strsep(char **, const char *);
276 #endif
278 #ifndef HAVE_STRTONUM
279 /* strtonum.c */
280 long long strtonum(const char *, long long, long long, const char **);
281 #endif
283 #ifndef HAVE_STRLCPY
284 /* strlcpy.c */
285 size_t strlcpy(char *, const char *, size_t);
286 #endif
288 #ifndef HAVE_STRLCAT
289 /* strlcat.c */
290 size_t strlcat(char *, const char *, size_t);
291 #endif
293 #ifndef HAVE_STRNLEN
294 /* strnlen.c */
295 size_t strnlen(const char *, size_t);
296 #endif
298 #ifndef HAVE_STRNDUP
299 /* strndup.c */
300 char *strndup(const char *, size_t);
301 #endif
303 #ifndef HAVE_GETPROGNAME
304 /* getprogname.c */
305 const char *getprogname(void);
306 #endif
308 #ifndef HAVE_GETLINE
309 /* getline.c */
310 ssize_t getline(char **, size_t *, FILE *);
311 #endif
313 #ifndef HAVE_FREEZERO
314 /* freezero.c */
315 void freezero(void *, size_t);
316 #endif
318 #ifndef HAVE_GETDTABLECOUNT
319 /* getdtablecount.c */
320 int getdtablecount(void);
321 #endif
323 #ifndef HAVE_REALLOCARRAY
324 /* reallocarray.c */
325 void *reallocarray(void *, size_t, size_t);
326 #endif
328 #ifndef HAVE_RECALLOCARRAY
329 /* recallocarray.c */
330 void *recallocarray(void *, size_t, size_t, size_t);
331 #endif
333 #ifndef HAVE_SETPROCTITLE
334 /* setproctitle.c */
335 void setproctitle(const char *, ...);
336 #endif
338 #ifndef HAVE_FMT_SCALED
339 /* fmt_scaled.c */
340 int fmt_scaled(long long, char *);
341 int scan_scaled(char *, long long *);
342 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
343 #endif
345 #ifndef HAVE_LIBBSD
346 /* getopt.c */
347 extern int BSDopterr;
348 extern int BSDoptind;
349 extern int BSDoptopt;
350 extern int BSDoptreset;
351 extern char *BSDoptarg;
352 int BSDgetopt(int, char *const *, const char *);
353 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
354 #define opterr BSDopterr
355 #define optind BSDoptind
356 #define optopt BSDoptopt
357 #define optreset BSDoptreset
358 #define optarg BSDoptarg
359 #endif
360 #endif
362 /* Check for some of the non-portable timespec*() functions.
363 * This should largely come from libbsd for systems which
364 * aren't BSD, but this will depend on how old the library
365 * is.
366 */
367 #ifndef timespecisset
368 #define timespecisset(tsp) \
369 ((tsp)->tv_sec || (tsp)->tv_nsec)
370 #endif
372 #ifndef timespecsub
373 #define timespecsub(tsp, usp, vsp) \
374 do { \
375 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
376 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
377 if ((vsp)->tv_nsec < 0) { \
378 (vsp)->tv_sec--; \
379 (vsp)->tv_nsec += 1000000000L; \
380 } \
381 } while (0)
382 #endif
384 #ifndef timespeccmp
385 #define timespeccmp(tvp, uvp, cmp) \
386 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
387 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
388 ((tvp)->tv_sec cmp (uvp)->tv_sec))
389 #endif
391 #ifndef HAVE_BSD_MERGESORT
392 /* mergesort.c */
393 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
394 #endif