portability.h 11 KB

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