• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Workarounds for horrible build environment idiosyncrasies.
2 
3 // Instead of polluting the code with strange #ifdefs to work around bugs
4 // in specific compiler, library, or OS versions, localize all that here
5 // and in portability.c
6 
7 // Always use long file support.
8 // This must come before we #include any system header file to take effect!
9 #define _FILE_OFFSET_BITS 64
10 
11 #ifdef __APPLE__
12 // macOS 10.13 doesn't have the POSIX 2008 direct access to timespec in
13 // struct stat, but we can ask it to give us something equivalent...
14 // (This must come before any #include!)
15 #define _DARWIN_C_SOURCE
16 // ...and then use macros to paper over the difference.
17 #define st_atim st_atimespec
18 #define st_ctim st_ctimespec
19 #define st_mtim st_mtimespec
20 #endif
21 
22 // For musl
23 #define _ALL_SOURCE
24 #include <regex.h>
25 #ifndef REG_STARTEND
26 #define REG_STARTEND 0
27 #endif
28 
29 // for some reason gnu/libc only sets these if you #define ia_ia_stallman_ftaghn
30 // despite FreeBSD and MacOS having both with the same value, and bionic's
31 // "upstream-openbsd" directory documenting them as "BSD extensions".
32 // (The flexible extension would have been an fnmatch() that returns length
33 // matched at location so we could check trailing data ourselves, but no.
34 // And it's ANSI only case matching instead of UTF8...)
35 #include <fnmatch.h>
36 #ifndef FNM_LEADING_DIR
37 #define FNM_LEADING_DIR   8
38 #endif
39 #ifndef FNM_CASEFOLD
40 #define FNM_CASEFOLD     16
41 #endif
42 
43 // Test for gcc (using compiler builtin #define)
44 
45 #ifdef __GNUC__
46 #ifndef __clang__
47 #define QUIET = 0 // shut up false positive "may be used uninitialized" warning
48 #else
49 #define QUIET
50 #endif
51 #define printf_format	__attribute__((format(printf, 1, 2)))
52 #else
53 #define printf_format
54 #endif
55 
56 // This lets us determine what libc we're using: systems that have <features.h>
57 // will transitively include it, and ones that don't (macOS) won't break.
58 #include <sys/types.h>
59 
60 // Various constants old build environments might not have even if kernel does
61 
62 #ifndef AT_FDCWD             // Kernel commit 5590ff0d5528 2006
63 #define AT_FDCWD -100
64 #endif
65 
66 #ifndef AT_SYMLINK_NOFOLLOW
67 #define AT_SYMLINK_NOFOLLOW 0x100
68 #endif
69 
70 #ifndef AT_REMOVEDIR
71 #define AT_REMOVEDIR 0x200
72 #endif
73 
74 #ifndef RLIMIT_RTTIME // Commit 78f2c7db6068f 2008
75 #define RLIMIT_RTTIME 15
76 #endif
77 
78 // Introduced in Linux 3.1 (Commit 982d816581eee 2011)
79 #ifndef SEEK_DATA
80 #define SEEK_DATA 3
81 #endif
82 #ifndef SEEK_HOLE
83 #define SEEK_HOLE 4
84 #endif
85 
86 // We don't define GNU_dammit because we're not part of the gnu project, and
87 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
88 // won't give us Linux syscall wrappers without claiming to be part of the
89 // gnu project (because Stallman's "GNU owns Linux" revisionist history
90 // crusade includes the kernel, even though Linux was inspired by Minix).
91 
92 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
93 // but even many posix-2008 functions aren't provided by glibc unless you
94 // claim it's in the name of Gnu.
95 
96 #if defined(__GLIBC__)
97 // Glibc violates posix: "Function prototypes shall be provided." but aren't.
98 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
99 char *crypt(const char *key, const char *salt);
100 
101 // According to posix, #include header, get a function definition. But glibc...
102 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
103 #include <wchar.h>
104 int wcwidth(wchar_t wc);
105 
106 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
107 #include <time.h>
108 char *strptime(const char *buf, const char *format, struct tm *tm);
109 
110 // Gnu didn't like posix basename so they defined another function with the
111 // same name and if you include libgen.h it #defines basename to something
112 // else (where they implemented the real basename), and that define breaks
113 // the table entry for the basename command. They didn't make a new function
114 // with a different name for their new behavior because gnu.
115 //
116 // Solution: don't use their broken header and provide an inline to redirect
117 // the standard name to the renamed function with the standard behavior.
118 
119 char *dirname(char *path);
120 char *__xpg_basename(char *path);
basename(char * path)121 static inline char *basename(char *path) { return __xpg_basename(path); }
122 char *strcasestr(const char *haystack, const char *needle);
123 void *memmem(const void *haystack, size_t haystack_length,
124   const void *needle, size_t needle_length);
125 #endif // defined(glibc)
126 
127 #if !defined(__GLIBC__)
128 // POSIX basename.
129 #include <libgen.h>
130 #endif
131 
132 // Work out how to do endianness
133 
134 #ifdef __APPLE__
135 
136 #include <libkern/OSByteOrder.h>
137 
138 #ifdef __BIG_ENDIAN__
139 #define IS_BIG_ENDIAN 1
140 #else
141 #define IS_BIG_ENDIAN 0
142 #endif
143 
144 #define bswap_16(x) OSSwapInt16(x)
145 #define bswap_32(x) OSSwapInt32(x)
146 #define bswap_64(x) OSSwapInt64(x)
147 
148 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
149 
150 #include <sys/endian.h>
151 
152 #if _BYTE_ORDER == _BIG_ENDIAN
153 #define IS_BIG_ENDIAN 1
154 #else
155 #define IS_BIG_ENDIAN 0
156 #endif
157 
158 #define bswap_16(x) bswap16(x)
159 #define bswap_32(x) bswap32(x)
160 #define bswap_64(x) bswap64(x)
161 
162 #else
163 
164 #include <byteswap.h>
165 #include <endian.h>
166 
167 #if __BYTE_ORDER == __BIG_ENDIAN
168 #define IS_BIG_ENDIAN 1
169 #else
170 #define IS_BIG_ENDIAN 0
171 #endif
172 
173 #endif
174 
175 #if IS_BIG_ENDIAN
176 #define IS_LITTLE_ENDIAN 0
177 #define SWAP_BE16(x) (x)
178 #define SWAP_BE32(x) (x)
179 #define SWAP_BE64(x) (x)
180 #define SWAP_LE16(x) bswap_16(x)
181 #define SWAP_LE32(x) bswap_32(x)
182 #define SWAP_LE64(x) bswap_64(x)
183 #else
184 #define IS_LITTLE_ENDIAN 1
185 #define SWAP_BE16(x) bswap_16(x)
186 #define SWAP_BE32(x) bswap_32(x)
187 #define SWAP_BE64(x) bswap_64(x)
188 #define SWAP_LE16(x) (x)
189 #define SWAP_LE32(x) (x)
190 #define SWAP_LE64(x) (x)
191 #endif
192 
193 // Linux headers not listed by POSIX or LSB
194 #include <sys/mount.h>
195 #ifdef __linux__
196 #include <sys/statfs.h>
197 #include <sys/swap.h>
198 #include <sys/sysinfo.h>
199 #endif
200 
201 #ifdef __APPLE__
202 #include <util.h>
203 #elif !defined(__FreeBSD__) && !defined(__OpenBSD__)
204 #include <pty.h>
205 #else
206 #include <termios.h>
207 #ifndef IUTF8
208 #define IUTF8 0
209 #endif
210 #endif
211 
212 #ifdef __linux__
213 #include <sys/personality.h>
214 #else
215 #define PER_LINUX32 0
216 int personality(int);
217 #endif
218 
219 #if defined(__APPLE__) || defined(__linux__)
220 // Linux and macOS has both have getxattr and friends in <sys/xattr.h>, but
221 // they aren't compatible.
222 #include <sys/xattr.h>
223 ssize_t xattr_get(const char *, const char *, void *, size_t);
224 ssize_t xattr_lget(const char *, const char *, void *, size_t);
225 ssize_t xattr_fget(int fd, const char *, void *, size_t);
226 ssize_t xattr_list(const char *, char *, size_t);
227 ssize_t xattr_llist(const char *, char *, size_t);
228 ssize_t xattr_flist(int, char *, size_t);
229 ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
230 ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
231 ssize_t xattr_fset(int, const char*, const void*, size_t, int);
232 #endif
233 
234 #if defined(__APPLE__)
235 // macOS doesn't have these functions, but we can fake them.
236 int mknodat(int, const char*, mode_t, dev_t);
237 int posix_fallocate(int, off_t, off_t);
238 
239 // macOS keeps newlocale(3) in the non-POSIX <xlocale.h> rather than <locale.h>.
240 #include <xlocale.h>
241 #endif
242 
243 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
statfs_bsize(struct statfs * sf)244 static inline long statfs_bsize(struct statfs *sf) { return sf->f_iosize; }
statfs_frsize(struct statfs * sf)245 static inline long statfs_frsize(struct statfs *sf) { return sf->f_bsize; }
246 #else
statfs_bsize(struct statfs * sf)247 static inline long statfs_bsize(struct statfs *sf) { return sf->f_bsize; }
statfs_frsize(struct statfs * sf)248 static inline long statfs_frsize(struct statfs *sf) { return sf->f_frsize; }
249 #endif
250 
251 
252 // Android is missing some headers and functions
253 // "generated/config.h" is included first
254 #if __has_include(<shadow.h>)
255 #include <shadow.h>
256 #endif
257 #if __has_include(<utmpx.h>)
258 #include <utmpx.h>
259 #else
260 struct utmpx {int ut_type;};
261 #define USER_PROCESS 0
getutxent(void)262 static inline struct utmpx *getutxent(void) {return 0;}
setutxent(void)263 static inline void setutxent(void) {;}
endutxent(void)264 static inline void endutxent(void) {;}
265 #endif
266 
267 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
268 #include <fcntl.h>
269 #if defined(__APPLE__)
270 #define O_PATH 0
271 #else
272 #ifndef O_NOFOLLOW
273 #define O_NOFOLLOW 0
274 #endif
275 #ifndef O_NOATIME
276 #define O_NOATIME 01000000
277 #endif
278 #ifndef O_CLOEXEC
279 #define O_CLOEXEC 02000000
280 #endif
281 #ifndef O_PATH
282 #define O_PATH   010000000
283 #endif
284 #ifndef SCHED_RESET_ON_FORK
285 #define SCHED_RESET_ON_FORK (1<<30)
286 #endif
287 #endif
288 
289 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
290 // even though linux has nothing to do with the FSF and never has.
291 #ifndef F_SETPIPE_SZ
292 #define F_SETPIPE_SZ 1031
293 #endif
294 
295 #ifndef F_GETPIPE_SZ
296 #define F_GETPIPE_SZ 1032
297 #endif
298 
299 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
300     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
301 typedef double FLOAT;
302 #else
303 typedef float FLOAT;
304 #endif
305 
306 #ifndef __uClinux__
307 pid_t xfork(void);
308 #endif
309 
310 // gratuitously memsets ALL the extra space with zeroes (not just a terminator)
311 // but to make up for it truncating doesn't null terminate the output at all.
312 // There are occasions to use it, but it is NOT A GENERAL PURPOSE FUNCTION.
313 // #define strncpy(...) @@strncpyisbadmmkay@@
314 // strncat writes a null terminator one byte PAST the buffer size it's given.
315 #define strncat(...) strncatisbadmmkay(__VA_ARGS__)
316 
317 // Support building the Android tools on glibc, so hermetic AOSP builds can
318 // use toybox before they're ready to switch to host bionic.
319 #ifdef __BIONIC__
320 #include <android/log.h>
321 #else
322 typedef enum android_LogPriority {
323   ANDROID_LOG_UNKNOWN = 0,
324   ANDROID_LOG_DEFAULT,
325   ANDROID_LOG_VERBOSE,
326   ANDROID_LOG_DEBUG,
327   ANDROID_LOG_INFO,
328   ANDROID_LOG_WARN,
329   ANDROID_LOG_ERROR,
330   ANDROID_LOG_FATAL,
331   ANDROID_LOG_SILENT,
332 } android_LogPriority;
333 #endif
334 #if !defined(__BIONIC__) || defined(__ANDROID_NDK__)
335 // Android NDKv18 has liblog.so but not liblog.a for static builds.
stub_out_log_write(int pri,const char * tag,const char * msg)336 static inline int stub_out_log_write(int pri, const char *tag, const char *msg)
337 {
338   return -1;
339 }
340 #ifdef __ANDROID_NDK__
341 #define __android_log_write(a, b, c) stub_out_log_write(a, b, c)
342 #endif
343 
344 #endif
345 
346 // libprocessgroup is an Android platform library not included in the NDK.
347 #if defined(__BIONIC__)
348 #if __has_include(<processgroup/sched_policy.h>)
349 #include <processgroup/sched_policy.h>
350 #define GOT_IT
351 #endif
352 #endif
353 #ifdef GOT_IT
354 #undef GOT_IT
355 #else
get_sched_policy(int tid,void * policy)356 static inline int get_sched_policy(int tid, void *policy) {return 0;}
get_sched_policy_name(int policy)357 static inline char *get_sched_policy_name(int policy) {return "unknown";}
358 #endif
359 
360 #ifndef SYSLOG_NAMES
361 typedef struct {char *c_name; int c_val;} CODE;
362 extern CODE prioritynames[], facilitynames[];
363 #endif
364 
365 #if __has_include (<sys/random.h>)
366 #include <sys/random.h>
367 #endif
368 void xgetrandom(void *buf, unsigned len);
369 
370 // Android's bionic libc doesn't have confstr.
371 #ifdef __BIONIC__
372 #define _CS_PATH	0
373 #define _CS_V7_ENV	1
374 #include <string.h>
confstr(int a,char * b,int c)375 static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
376 #endif
377 
378 // Paper over the differences between BSD kqueue and Linux inotify for tail.
379 
380 struct xnotify {
381   char **paths;
382   int max, *fds, count, kq;
383 };
384 
385 struct xnotify *xnotify_init(int max);
386 int xnotify_add(struct xnotify *not, int fd, char *path);
387 int xnotify_wait(struct xnotify *not, char **path);
388 
389 int sig_to_num(char *s);
390 char *num_to_sig(int sig);
391 
392 struct signame {
393   int num;
394   char *name;
395 };
396 void xsignal_all_killers(void *handler);
397 
398 // Different OSes encode major/minor device numbers differently.
399 int dev_minor(int dev);
400 int dev_major(int dev);
401 int dev_makedev(int major, int minor);
402 
403 char *fs_type_name(struct statfs *statfs);
404 
405 int get_block_device_size(int fd, unsigned long long *size);
406 int rename_exchange(char *file1, char *file2);
407 
408 #ifdef __APPLE__
409 // Apple doesn't have POSIX timers; this is "just enough" for timeout(1).
410 typedef int timer_t;
411 struct itimerspec {
412   struct timespec it_value;
413 };
414 int timer_create(clock_t c, struct sigevent *se, timer_t *t);
415 int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old);
416 #elif defined(__GLIBC__)
417 // Work around a glibc bug that interacts badly with a gcc bug.
418 #include <syscall.h>
419 #include <signal.h>
420 #include <time.h>
421 int timer_create_wrap(clockid_t c, struct sigevent *se, timer_t *t);
422 #define timer_create(...) timer_create_wrap(__VA_ARGS__)
423 int timer_settime_wrap(timer_t t, int flags, struct itimerspec *val,
424   struct itimerspec *old);
425 #define timer_settime(...) timer_settime_wrap(__VA_ARGS__)
426 #endif
427