Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | // This file perpetuates the mistakes of the past, but only for 32-bit targets. |
| 30 | #if !defined(__LP64__) |
| 31 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 32 | #include <ctype.h> |
Elliott Hughes | d1ead2a | 2014-06-06 15:24:20 -0700 | [diff] [blame] | 33 | #include <dirent.h> |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 35 | #include <pthread.h> |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 36 | #include <signal.h> |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 37 | #include <stdio.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 38 | #include <stdlib.h> |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 39 | #include <string.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 40 | #include <sys/resource.h> |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 41 | #include <sys/syscall.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 42 | #include <sys/time.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <unistd.h> |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 46 | #include <wchar.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 47 | |
| 48 | // These were accidentally declared in <unistd.h> because we stupidly used to inline |
| 49 | // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. |
| 50 | extern "C" { |
| 51 | unsigned int __page_size = PAGE_SIZE; |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 52 | unsigned int __page_shift = 12; |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). |
| 56 | extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { |
| 57 | return wait4(pid, status, options, rusage); |
| 58 | } |
| 59 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 60 | // TODO: does anything still need this? |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 61 | extern "C" int __open() { |
| 62 | abort(); |
| 63 | } |
| 64 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 65 | // TODO: does anything still need this? |
| 66 | extern "C" void** __get_tls() { |
| 67 | #include "private/__get_tls.h" |
| 68 | return __get_tls(); |
| 69 | } |
| 70 | |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 71 | // This non-standard function was in our <string.h> for some reason. |
| 72 | extern "C" void memswap(void* m1, void* m2, size_t n) { |
| 73 | char* p = reinterpret_cast<char*>(m1); |
| 74 | char* p_end = p + n; |
| 75 | char* q = reinterpret_cast<char*>(m2); |
| 76 | while (p < p_end) { |
| 77 | char tmp = *p; |
| 78 | *p = *q; |
| 79 | *q = tmp; |
| 80 | p++; |
| 81 | q++; |
| 82 | } |
| 83 | } |
| 84 | |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 85 | extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) { |
| 86 | // This was removed from POSIX.1-2008, and is not implemented on bionic. |
| 87 | // Needed for ABI compatibility with the NDK. |
| 88 | return ENOSYS; |
| 89 | } |
| 90 | |
| 91 | extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { |
| 92 | // This was removed from POSIX.1-2008. |
| 93 | // Needed for ABI compatibility with the NDK. |
| 94 | *stack_addr = (char*)attr->stack_base + attr->stack_size; |
| 95 | return 0; |
| 96 | } |
| 97 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 98 | // Non-standard cruft that should only ever have been in system/core/toolbox. |
| 99 | extern "C" char* strtotimeval(const char* str, struct timeval* ts) { |
| 100 | char* s; |
| 101 | ts->tv_sec = strtoumax(str, &s, 10); |
| 102 | |
| 103 | long fractional_seconds = 0; |
| 104 | if (*s == '.') { |
| 105 | s++; |
| 106 | int count = 0; |
| 107 | |
| 108 | // Read up to 6 digits (microseconds). |
| 109 | while (*s && isdigit(*s)) { |
| 110 | if (++count < 7) { |
| 111 | fractional_seconds = fractional_seconds*10 + (*s - '0'); |
| 112 | } |
| 113 | s++; |
| 114 | } |
| 115 | |
| 116 | for (; count < 6; count++) { |
| 117 | fractional_seconds *= 10; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ts->tv_usec = fractional_seconds; |
| 122 | return s; |
| 123 | } |
| 124 | |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 125 | static inline int digitval(int ch) { |
| 126 | unsigned d; |
| 127 | |
| 128 | d = (unsigned)(ch - '0'); |
| 129 | if (d < 10) return (int)d; |
| 130 | |
| 131 | d = (unsigned)(ch - 'a'); |
| 132 | if (d < 6) return (int)(d+10); |
| 133 | |
| 134 | d = (unsigned)(ch - 'A'); |
| 135 | if (d < 6) return (int)(d+10); |
| 136 | |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | // This non-standard function was in our <inttypes.h> for some reason. |
| 141 | extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { |
| 142 | const unsigned char* p = (const unsigned char *)nptr; |
| 143 | const unsigned char* end = p + n; |
| 144 | int minus = 0; |
| 145 | uintmax_t v = 0; |
| 146 | int d; |
| 147 | |
| 148 | while (p < end && isspace(*p)) { |
| 149 | p++; |
| 150 | } |
| 151 | |
| 152 | if (p < end) { |
| 153 | char c = p[0]; |
| 154 | if (c == '-' || c == '+') { |
| 155 | minus = (c == '-'); |
| 156 | p++; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (base == 0) { |
| 161 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 162 | p += 2; |
| 163 | base = 16; |
| 164 | } else if (p+1 < end && p[0] == '0') { |
| 165 | p += 1; |
| 166 | base = 8; |
| 167 | } else { |
| 168 | base = 10; |
| 169 | } |
| 170 | } else if (base == 16) { |
| 171 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 172 | p += 2; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | while (p < end && (d = digitval(*p)) >= 0 && d < base) { |
| 177 | v = v*base + d; |
| 178 | p += 1; |
| 179 | } |
| 180 | |
| 181 | if (endptr) { |
| 182 | *endptr = (char*) p; |
| 183 | } |
| 184 | |
| 185 | return minus ? -v : v; |
| 186 | } |
| 187 | |
| 188 | // This non-standard function was in our <inttypes.h> for some reason. |
| 189 | extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { |
| 190 | return (intmax_t) strntoumax(nptr, endptr, base, n); |
| 191 | } |
| 192 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 193 | // POSIX calls this dprintf, but LP32 Android had fdprintf instead. |
| 194 | extern "C" int fdprintf(int fd, const char* fmt, ...) { |
| 195 | va_list ap; |
| 196 | va_start(ap, fmt); |
| 197 | int rc = vdprintf(fd, fmt, ap); |
| 198 | va_end(ap); |
| 199 | return rc; |
| 200 | } |
| 201 | |
| 202 | // POSIX calls this vdprintf, but LP32 Android had fdprintf instead. |
| 203 | extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) { |
| 204 | return vdprintf(fd, fmt, ap); |
| 205 | } |
| 206 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 207 | #define __futex_wake __real_futex_wake |
| 208 | #define __futex_wait __real_futex_wait |
| 209 | #include "private/bionic_futex.h" |
| 210 | #undef __futex_wake |
| 211 | #undef __futex_wait |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 212 | |
| 213 | // This used to be in <sys/atomics.h>. |
| 214 | extern "C" int __futex_wake(volatile void* ftx, int count) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 215 | return __real_futex_wake(ftx, count); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // This used to be in <sys/atomics.h>. |
| 219 | extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 220 | return __real_futex_wait(ftx, value, timeout); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Anthony King | 0017073 | 2014-05-24 16:47:14 +0000 | [diff] [blame] | 223 | // Unity's libmono uses this. |
| 224 | extern "C" int tkill(pid_t tid, int sig) { |
| 225 | return syscall(__NR_tkill, tid, sig); |
| 226 | } |
| 227 | |
Elliott Hughes | 1628eb1 | 2014-08-06 10:47:33 -0700 | [diff] [blame] | 228 | // This was removed from POSIX 2008. |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 229 | extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { |
| 230 | return wcsstr(haystack, needle); |
| 231 | } |
| 232 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 233 | // This was removed from POSIX 2008. |
| 234 | extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) { |
| 235 | return signal(signum, handler); |
| 236 | } |
| 237 | |
| 238 | // sysv_signal() was never in POSIX. |
| 239 | extern sighandler_t _signal(int signum, sighandler_t handler, int flags); |
| 240 | extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) { |
| 241 | return _signal(signum, handler, SA_RESETHAND); |
| 242 | } |
| 243 | |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 244 | // This is a system call that was never in POSIX. Use readdir(3) instead. |
Elliott Hughes | d1ead2a | 2014-06-06 15:24:20 -0700 | [diff] [blame] | 245 | extern "C" int __getdents64(unsigned int, dirent*, unsigned int); |
| 246 | extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) { |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 247 | return __getdents64(fd, dirp, count); |
| 248 | } |
| 249 | |
Elliott Hughes | bffbfee | 2014-06-06 20:41:42 -0700 | [diff] [blame] | 250 | // This is a BSDism that we never implemented correctly. Used by Firefox. |
| 251 | extern "C" int issetugid() { |
| 252 | return 0; |
| 253 | } |
| 254 | |
Dan Albert | 8229ae4 | 2014-06-13 16:04:41 -0700 | [diff] [blame] | 255 | // This was removed from POSIX 2004. |
| 256 | extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) { |
| 257 | return wait4(-1, status, options, rusage); |
| 258 | } |
| 259 | |
Dan Albert | 462abab | 2014-06-13 16:51:24 -0700 | [diff] [blame] | 260 | // This was removed from POSIX 2004. |
| 261 | extern "C" int getdtablesize() { |
| 262 | struct rlimit r; |
| 263 | |
| 264 | if (getrlimit(RLIMIT_NOFILE, &r) < 0) { |
| 265 | return sysconf(_SC_OPEN_MAX); |
| 266 | } |
| 267 | |
| 268 | return r.rlim_cur; |
| 269 | } |
| 270 | |
Elliott Hughes | 1628eb1 | 2014-08-06 10:47:33 -0700 | [diff] [blame] | 271 | // Only used by ftime, which was removed from POSIX 2008. |
Dan Albert | ac64675 | 2014-06-05 02:10:49 +0000 | [diff] [blame] | 272 | struct timeb { |
| 273 | time_t time; |
| 274 | unsigned short millitm; |
| 275 | short timezone; |
| 276 | short dstflag; |
| 277 | }; |
| 278 | |
| 279 | // This was removed from POSIX 2008. |
| 280 | extern "C" int ftime(struct timeb* tb) { |
| 281 | struct timeval tv; |
| 282 | struct timezone tz; |
| 283 | |
| 284 | if (gettimeofday(&tv, &tz) < 0) |
| 285 | return -1; |
| 286 | |
| 287 | tb->time = tv.tv_sec; |
| 288 | tb->millitm = (tv.tv_usec + 500) / 1000; |
| 289 | |
| 290 | if (tb->millitm == 1000) { |
| 291 | ++tb->time; |
| 292 | tb->millitm = 0; |
| 293 | } |
| 294 | |
| 295 | tb->timezone = tz.tz_minuteswest; |
| 296 | tb->dstflag = tz.tz_dsttime; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 301 | // This was removed from POSIX 2008. |
| 302 | extern "C" char* index(const char* str, int ch) { |
| 303 | return strchr(str, ch); |
| 304 | } |
| 305 | |
Elliott Hughes | 5dea472 | 2014-09-03 15:53:11 -0700 | [diff] [blame] | 306 | // This was removed from BSD. |
| 307 | extern "C" void arc4random_stir(void) { |
| 308 | // The current implementation stirs itself as needed. |
| 309 | } |
| 310 | |
Elliott Hughes | fc82973 | 2014-09-08 10:25:33 -0700 | [diff] [blame^] | 311 | // This was removed from BSD. |
| 312 | extern "C" void arc4random_addrandom(unsigned char*, int) { |
| 313 | // The current implementation adds randomness as needed. |
| 314 | } |
| 315 | |
Christopher Ferris | f903558 | 2014-09-05 16:39:22 -0700 | [diff] [blame] | 316 | // Old versions of the NDK did not export malloc_usable_size, but did |
| 317 | // export dlmalloc_usable_size. We are moving away from dlmalloc in L |
| 318 | // so make this call malloc_usable_size. |
| 319 | extern "C" size_t dlmalloc_usable_size(void* ptr) { |
| 320 | return malloc_usable_size(ptr); |
| 321 | } |
| 322 | |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 323 | #endif |