toybox/lib/portability.h
<<
>>
Prefs
   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// For musl
   8#define _ALL_SOURCE
   9#ifndef REG_STARTEND
  10#define REG_STARTEND 0
  11#endif
  12
  13#ifdef __APPLE__
  14// macOS 10.13 doesn't have the POSIX 2008 direct access to timespec in
  15// struct stat, but we can ask it to give us something equivalent...
  16// (This must come before any #include!)
  17#define _DARWIN_C_SOURCE
  18// ...and then use macros to paper over the difference.
  19#define st_atim st_atimespec
  20#define st_ctim st_ctimespec
  21#define st_mtim st_mtimespec
  22#endif
  23
  24// Test for gcc (using compiler builtin #define)
  25
  26#ifdef __GNUC__
  27#define printf_format   __attribute__((format(printf, 1, 2)))
  28#else
  29#define printf_format
  30#endif
  31
  32// Always use long file support.
  33#define _FILE_OFFSET_BITS 64
  34
  35// This isn't in the spec, but it's how we determine what libc we're using.
  36
  37// Types various replacement prototypes need.
  38// This also lets us determine what libc we're using. Systems that
  39// have <features.h> will transitively include it, and ones that don't --
  40// macOS -- won't break.
  41#include <sys/types.h>
  42
  43// Various constants old build environments might not have even if kernel does
  44
  45#ifndef AT_FDCWD
  46#define AT_FDCWD -100
  47#endif
  48
  49#ifndef AT_SYMLINK_NOFOLLOW
  50#define AT_SYMLINK_NOFOLLOW 0x100
  51#endif
  52
  53#ifndef AT_REMOVEDIR
  54#define AT_REMOVEDIR 0x200
  55#endif
  56
  57#ifndef RLIMIT_RTTIME
  58#define RLIMIT_RTTIME 15
  59#endif
  60
  61// Introduced in Linux 3.1
  62#ifndef SEEK_DATA
  63#define SEEK_DATA 3
  64#endif
  65#ifndef SEEK_HOLE
  66#define SEEK_HOLE 4
  67#endif
  68
  69// We don't define GNU_dammit because we're not part of the gnu project, and
  70// don't want to get any FSF on us. Unfortunately glibc (gnu libc)
  71// won't give us Linux syscall wrappers without claiming to be part of the
  72// gnu project (because Stallman's "GNU owns Linux" revisionist history
  73// crusade includes the kernel, even though Linux was inspired by Minix).
  74
  75// We use most non-posix Linux syscalls directly through the syscall() wrapper,
  76// but even many posix-2008 functions aren't provided by glibc unless you
  77// claim it's in the name of Gnu.
  78
  79#if defined(__GLIBC__)
  80// "Function prototypes shall be provided." but aren't.
  81// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
  82char *crypt(const char *key, const char *salt);
  83
  84// According to posix, #include header, get a function definition. But glibc...
  85// http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
  86#include <wchar.h>
  87int wcwidth(wchar_t wc);
  88
  89// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
  90#include <time.h>
  91char *strptime(const char *buf, const char *format, struct tm *tm);
  92
  93// They didn't like posix basename so they defined another function with the
  94// same name and if you include libgen.h it #defines basename to something
  95// else (where they implemented the real basename), and that define breaks
  96// the table entry for the basename command. They didn't make a new function
  97// with a different name for their new behavior because gnu.
  98//
  99// Solution: don't use their broken header, provide an inline to redirect the
 100// correct name to the broken name.
 101
 102char *dirname(char *path);
 103char *__xpg_basename(char *path);
 104static inline char *basename(char *path) { return __xpg_basename(path); }
 105char *strcasestr(const char *haystack, const char *needle);
 106#endif // defined(glibc)
 107
 108#if !defined(__GLIBC__)
 109// POSIX basename.
 110#include <libgen.h>
 111#endif
 112
 113// Work out how to do endianness
 114
 115#ifdef __APPLE__
 116
 117#include <libkern/OSByteOrder.h>
 118
 119#ifdef __BIG_ENDIAN__
 120#define IS_BIG_ENDIAN 1
 121#else
 122#define IS_BIG_ENDIAN 0
 123#endif
 124
 125#define bswap_16(x) OSSwapInt16(x)
 126#define bswap_32(x) OSSwapInt32(x)
 127#define bswap_64(x) OSSwapInt64(x)
 128
 129#elif defined(__FreeBSD__)
 130
 131#include <sys/endian.h>
 132
 133#if _BYTE_ORDER == _BIG_ENDIAN
 134#define IS_BIG_ENDIAN 1
 135#else
 136#define IS_BIG_ENDIAN 0
 137#endif
 138
 139#else
 140
 141#include <byteswap.h>
 142#include <endian.h>
 143
 144#if __BYTE_ORDER == __BIG_ENDIAN
 145#define IS_BIG_ENDIAN 1
 146#else
 147#define IS_BIG_ENDIAN 0
 148#endif
 149
 150#endif
 151
 152#if IS_BIG_ENDIAN
 153#define IS_LITTLE_ENDIAN 0
 154#define SWAP_BE16(x) (x)
 155#define SWAP_BE32(x) (x)
 156#define SWAP_BE64(x) (x)
 157#define SWAP_LE16(x) bswap_16(x)
 158#define SWAP_LE32(x) bswap_32(x)
 159#define SWAP_LE64(x) bswap_64(x)
 160#else
 161#define IS_LITTLE_ENDIAN 1
 162#define SWAP_BE16(x) bswap_16(x)
 163#define SWAP_BE32(x) bswap_32(x)
 164#define SWAP_BE64(x) bswap_64(x)
 165#define SWAP_LE16(x) (x)
 166#define SWAP_LE32(x) (x)
 167#define SWAP_LE64(x) (x)
 168#endif
 169
 170// Linux headers not listed by POSIX or LSB
 171#include <sys/mount.h>
 172#ifdef __linux__
 173#include <sys/statfs.h>
 174#include <sys/swap.h>
 175#include <sys/sysinfo.h>
 176#endif
 177
 178#ifdef __APPLE__
 179#include <util.h>
 180#elif !defined(__FreeBSD__)
 181#include <pty.h>
 182#else
 183#include <termios.h>
 184#ifndef IUTF8
 185#define IUTF8 0
 186#endif
 187#endif
 188
 189#ifndef __FreeBSD__
 190#include <sys/xattr.h>
 191#endif
 192
 193// Android is missing some headers and functions
 194// "generated/config.h" is included first
 195#if CFG_TOYBOX_SHADOW
 196#include <shadow.h>
 197#endif
 198#if CFG_TOYBOX_UTMPX
 199#include <utmpx.h>
 200#else
 201struct utmpx {int ut_type;};
 202#define USER_PROCESS 0
 203static inline struct utmpx *getutxent(void) {return 0;}
 204static inline void setutxent(void) {;}
 205static inline void endutxent(void) {;}
 206#endif
 207
 208// Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
 209#include <fcntl.h>
 210#ifndef O_NOFOLLOW
 211#define O_NOFOLLOW 0
 212#endif
 213#ifndef O_NOATIME
 214#define O_NOATIME 01000000
 215#endif
 216#ifndef O_CLOEXEC
 217#define O_CLOEXEC 02000000
 218#endif
 219#ifndef O_PATH
 220#define O_PATH   010000000
 221#endif
 222#ifndef SCHED_RESET_ON_FORK
 223#define SCHED_RESET_ON_FORK (1<<30)
 224#endif
 225
 226// Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
 227// even though linux has nothing to do with the FSF and never has.
 228#ifndef F_SETPIPE_SZ
 229#define F_SETPIPE_SZ 1031
 230#endif
 231
 232#ifndef F_GETPIPE_SZ
 233#define F_GETPIPE_SZ 1032
 234#endif
 235
 236#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
 237    && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
 238typedef double FLOAT;
 239#else
 240typedef float FLOAT;
 241#endif
 242
 243#ifndef __uClinux__
 244pid_t xfork(void);
 245#endif
 246
 247//#define strncpy(...) @@strncpyisbadmmkay@@
 248//#define strncat(...) @@strncatisbadmmkay@@
 249
 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#include <sys/system_properties.h>
 255#else
 256typedef enum android_LogPriority {
 257  ANDROID_LOG_UNKNOWN = 0,
 258  ANDROID_LOG_DEFAULT,
 259  ANDROID_LOG_VERBOSE,
 260  ANDROID_LOG_DEBUG,
 261  ANDROID_LOG_INFO,
 262  ANDROID_LOG_WARN,
 263  ANDROID_LOG_ERROR,
 264  ANDROID_LOG_FATAL,
 265  ANDROID_LOG_SILENT,
 266} android_LogPriority;
 267static inline int __android_log_write(int pri, const char *tag, const char *msg)
 268{
 269  return -1;
 270}
 271#define PROP_VALUE_MAX 92
 272static inline int __system_property_set(const char *key, const char *value)
 273{
 274  return -1;
 275}
 276#endif
 277
 278// libprocessgroup is an Android platform library not included in the NDK.
 279#if defined(__BIONIC__) && !defined(__ANDROID_NDK__)
 280#include <processgroup/sched_policy.h>
 281#else
 282static inline int get_sched_policy(int tid, void *policy) {return 0;}
 283static inline char *get_sched_policy_name(int policy) {return "unknown";}
 284#endif
 285
 286// Android NDKv18 has liblog.so but not liblog.c for static builds,
 287// stub it out for now.
 288#ifdef __ANDROID_NDK__
 289#define __android_log_write(a, b, c) (0)
 290#define adjtime(x, y) (0)
 291#endif
 292
 293#ifndef SYSLOG_NAMES
 294typedef struct {char *c_name; int c_val;} CODE;
 295extern CODE prioritynames[], facilitynames[];
 296#endif
 297
 298#if CFG_TOYBOX_GETRANDOM
 299#include <sys/random.h>
 300#endif
 301int xgetrandom(void *buf, unsigned len, unsigned flags);
 302
 303// Android's bionic libc doesn't have confstr.
 304#ifdef __BIONIC__
 305#define _CS_PATH        0
 306#define _CS_V7_ENV      1
 307#include <string.h>
 308static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
 309#endif
 310
 311// Paper over the differences between BSD kqueue and Linux inotify for tail.
 312
 313struct xnotify {
 314  char **paths;
 315  int max, *fds, count, kq;
 316};
 317
 318struct xnotify *xnotify_init(int max);
 319int xnotify_add(struct xnotify *not, int fd, char *path);
 320int xnotify_wait(struct xnotify *not, char **path);
 321