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 | |
Yabin Cui | 7fb680b | 2015-02-24 13:18:25 -0800 | [diff] [blame^] | 29 | // This file perpetuates the mistakes of the past. |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 31 | #include <ctype.h> |
Elliott Hughes | d1ead2a | 2014-06-06 15:24:20 -0700 | [diff] [blame] | 32 | #include <dirent.h> |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 33 | #include <inttypes.h> |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 34 | #include <pthread.h> |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 35 | #include <signal.h> |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 37 | #include <stdlib.h> |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 38 | #include <string.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 39 | #include <sys/resource.h> |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 40 | #include <sys/syscall.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 41 | #include <sys/time.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/wait.h> |
| 44 | #include <unistd.h> |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 45 | #include <wchar.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 46 | |
Yabin Cui | 7fb680b | 2015-02-24 13:18:25 -0800 | [diff] [blame^] | 47 | #include "private/libc_logging.h" |
| 48 | |
| 49 | // The part is only for 32-bit targets. |
| 50 | #if !defined(__LP64__) |
| 51 | |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 52 | // These were accidentally declared in <unistd.h> because we stupidly used to inline |
| 53 | // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. |
| 54 | extern "C" { |
| 55 | unsigned int __page_size = PAGE_SIZE; |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 56 | unsigned int __page_shift = 12; |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). |
| 60 | extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { |
| 61 | return wait4(pid, status, options, rusage); |
| 62 | } |
| 63 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 64 | // TODO: does anything still need this? |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 65 | extern "C" int __open() { |
| 66 | abort(); |
| 67 | } |
| 68 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 69 | // TODO: does anything still need this? |
| 70 | extern "C" void** __get_tls() { |
| 71 | #include "private/__get_tls.h" |
| 72 | return __get_tls(); |
| 73 | } |
| 74 | |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 75 | // This non-standard function was in our <string.h> for some reason. |
| 76 | extern "C" void memswap(void* m1, void* m2, size_t n) { |
| 77 | char* p = reinterpret_cast<char*>(m1); |
| 78 | char* p_end = p + n; |
| 79 | char* q = reinterpret_cast<char*>(m2); |
| 80 | while (p < p_end) { |
| 81 | char tmp = *p; |
| 82 | *p = *q; |
| 83 | *q = tmp; |
| 84 | p++; |
| 85 | q++; |
| 86 | } |
| 87 | } |
| 88 | |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 89 | extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) { |
| 90 | // This was removed from POSIX.1-2008, and is not implemented on bionic. |
| 91 | // Needed for ABI compatibility with the NDK. |
| 92 | return ENOSYS; |
| 93 | } |
| 94 | |
| 95 | extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { |
| 96 | // This was removed from POSIX.1-2008. |
| 97 | // Needed for ABI compatibility with the NDK. |
| 98 | *stack_addr = (char*)attr->stack_base + attr->stack_size; |
| 99 | return 0; |
| 100 | } |
| 101 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 102 | // Non-standard cruft that should only ever have been in system/core/toolbox. |
| 103 | extern "C" char* strtotimeval(const char* str, struct timeval* ts) { |
| 104 | char* s; |
| 105 | ts->tv_sec = strtoumax(str, &s, 10); |
| 106 | |
| 107 | long fractional_seconds = 0; |
| 108 | if (*s == '.') { |
| 109 | s++; |
| 110 | int count = 0; |
| 111 | |
| 112 | // Read up to 6 digits (microseconds). |
| 113 | while (*s && isdigit(*s)) { |
| 114 | if (++count < 7) { |
| 115 | fractional_seconds = fractional_seconds*10 + (*s - '0'); |
| 116 | } |
| 117 | s++; |
| 118 | } |
| 119 | |
| 120 | for (; count < 6; count++) { |
| 121 | fractional_seconds *= 10; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | ts->tv_usec = fractional_seconds; |
| 126 | return s; |
| 127 | } |
| 128 | |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 129 | static inline int digitval(int ch) { |
| 130 | unsigned d; |
| 131 | |
| 132 | d = (unsigned)(ch - '0'); |
| 133 | if (d < 10) return (int)d; |
| 134 | |
| 135 | d = (unsigned)(ch - 'a'); |
| 136 | if (d < 6) return (int)(d+10); |
| 137 | |
| 138 | d = (unsigned)(ch - 'A'); |
| 139 | if (d < 6) return (int)(d+10); |
| 140 | |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | // This non-standard function was in our <inttypes.h> for some reason. |
| 145 | extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { |
| 146 | const unsigned char* p = (const unsigned char *)nptr; |
| 147 | const unsigned char* end = p + n; |
| 148 | int minus = 0; |
| 149 | uintmax_t v = 0; |
| 150 | int d; |
| 151 | |
| 152 | while (p < end && isspace(*p)) { |
| 153 | p++; |
| 154 | } |
| 155 | |
| 156 | if (p < end) { |
| 157 | char c = p[0]; |
| 158 | if (c == '-' || c == '+') { |
| 159 | minus = (c == '-'); |
| 160 | p++; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (base == 0) { |
| 165 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 166 | p += 2; |
| 167 | base = 16; |
| 168 | } else if (p+1 < end && p[0] == '0') { |
| 169 | p += 1; |
| 170 | base = 8; |
| 171 | } else { |
| 172 | base = 10; |
| 173 | } |
| 174 | } else if (base == 16) { |
| 175 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 176 | p += 2; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | while (p < end && (d = digitval(*p)) >= 0 && d < base) { |
| 181 | v = v*base + d; |
| 182 | p += 1; |
| 183 | } |
| 184 | |
| 185 | if (endptr) { |
| 186 | *endptr = (char*) p; |
| 187 | } |
| 188 | |
| 189 | return minus ? -v : v; |
| 190 | } |
| 191 | |
| 192 | // This non-standard function was in our <inttypes.h> for some reason. |
| 193 | extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { |
| 194 | return (intmax_t) strntoumax(nptr, endptr, base, n); |
| 195 | } |
| 196 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 197 | // POSIX calls this dprintf, but LP32 Android had fdprintf instead. |
| 198 | extern "C" int fdprintf(int fd, const char* fmt, ...) { |
| 199 | va_list ap; |
| 200 | va_start(ap, fmt); |
| 201 | int rc = vdprintf(fd, fmt, ap); |
| 202 | va_end(ap); |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | // POSIX calls this vdprintf, but LP32 Android had fdprintf instead. |
| 207 | extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) { |
| 208 | return vdprintf(fd, fmt, ap); |
| 209 | } |
| 210 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 211 | #define __futex_wake __real_futex_wake |
| 212 | #define __futex_wait __real_futex_wait |
| 213 | #include "private/bionic_futex.h" |
| 214 | #undef __futex_wake |
| 215 | #undef __futex_wait |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 216 | |
| 217 | // This used to be in <sys/atomics.h>. |
| 218 | extern "C" int __futex_wake(volatile void* ftx, int count) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 219 | return __real_futex_wake(ftx, count); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // This used to be in <sys/atomics.h>. |
| 223 | 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] | 224 | return __real_futex_wait(ftx, value, timeout); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Anthony King | 0017073 | 2014-05-24 16:47:14 +0000 | [diff] [blame] | 227 | // Unity's libmono uses this. |
| 228 | extern "C" int tkill(pid_t tid, int sig) { |
| 229 | return syscall(__NR_tkill, tid, sig); |
| 230 | } |
| 231 | |
Elliott Hughes | 1628eb1 | 2014-08-06 10:47:33 -0700 | [diff] [blame] | 232 | // This was removed from POSIX 2008. |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 233 | extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { |
| 234 | return wcsstr(haystack, needle); |
| 235 | } |
| 236 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 237 | // This was removed from POSIX 2008. |
| 238 | extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) { |
| 239 | return signal(signum, handler); |
| 240 | } |
| 241 | |
Elliott Hughes | 1edfd9e | 2015-01-26 21:45:56 -0800 | [diff] [blame] | 242 | #if !defined(__i386__) |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 243 | // This was removed from POSIX 2008. |
| 244 | #undef bcopy |
| 245 | extern "C" void bcopy(const void* src, void* dst, size_t n) { |
| 246 | memcpy(dst, src, n); |
| 247 | } |
Elliott Hughes | 1edfd9e | 2015-01-26 21:45:56 -0800 | [diff] [blame] | 248 | #else |
| 249 | // x86 has an assembler implementation. |
| 250 | #endif |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 251 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 252 | // sysv_signal() was never in POSIX. |
| 253 | extern sighandler_t _signal(int signum, sighandler_t handler, int flags); |
| 254 | extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) { |
| 255 | return _signal(signum, handler, SA_RESETHAND); |
| 256 | } |
| 257 | |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 258 | // 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] | 259 | extern "C" int __getdents64(unsigned int, dirent*, unsigned int); |
| 260 | extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) { |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 261 | return __getdents64(fd, dirp, count); |
| 262 | } |
| 263 | |
Elliott Hughes | bffbfee | 2014-06-06 20:41:42 -0700 | [diff] [blame] | 264 | // This is a BSDism that we never implemented correctly. Used by Firefox. |
| 265 | extern "C" int issetugid() { |
| 266 | return 0; |
| 267 | } |
| 268 | |
Dan Albert | 8229ae4 | 2014-06-13 16:04:41 -0700 | [diff] [blame] | 269 | // This was removed from POSIX 2004. |
| 270 | extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) { |
| 271 | return wait4(-1, status, options, rusage); |
| 272 | } |
| 273 | |
Dan Albert | 462abab | 2014-06-13 16:51:24 -0700 | [diff] [blame] | 274 | // This was removed from POSIX 2004. |
| 275 | extern "C" int getdtablesize() { |
| 276 | struct rlimit r; |
| 277 | |
| 278 | if (getrlimit(RLIMIT_NOFILE, &r) < 0) { |
| 279 | return sysconf(_SC_OPEN_MAX); |
| 280 | } |
| 281 | |
| 282 | return r.rlim_cur; |
| 283 | } |
| 284 | |
Elliott Hughes | 1628eb1 | 2014-08-06 10:47:33 -0700 | [diff] [blame] | 285 | // Only used by ftime, which was removed from POSIX 2008. |
Dan Albert | ac64675 | 2014-06-05 02:10:49 +0000 | [diff] [blame] | 286 | struct timeb { |
| 287 | time_t time; |
| 288 | unsigned short millitm; |
| 289 | short timezone; |
| 290 | short dstflag; |
| 291 | }; |
| 292 | |
| 293 | // This was removed from POSIX 2008. |
| 294 | extern "C" int ftime(struct timeb* tb) { |
| 295 | struct timeval tv; |
| 296 | struct timezone tz; |
| 297 | |
| 298 | if (gettimeofday(&tv, &tz) < 0) |
| 299 | return -1; |
| 300 | |
| 301 | tb->time = tv.tv_sec; |
| 302 | tb->millitm = (tv.tv_usec + 500) / 1000; |
| 303 | |
| 304 | if (tb->millitm == 1000) { |
| 305 | ++tb->time; |
| 306 | tb->millitm = 0; |
| 307 | } |
| 308 | |
| 309 | tb->timezone = tz.tz_minuteswest; |
| 310 | tb->dstflag = tz.tz_dsttime; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 315 | // This was removed from POSIX 2008. |
| 316 | extern "C" char* index(const char* str, int ch) { |
| 317 | return strchr(str, ch); |
| 318 | } |
| 319 | |
Elliott Hughes | 5dea472 | 2014-09-03 15:53:11 -0700 | [diff] [blame] | 320 | // This was removed from BSD. |
| 321 | extern "C" void arc4random_stir(void) { |
| 322 | // The current implementation stirs itself as needed. |
| 323 | } |
| 324 | |
Elliott Hughes | fc82973 | 2014-09-08 10:25:33 -0700 | [diff] [blame] | 325 | // This was removed from BSD. |
| 326 | extern "C" void arc4random_addrandom(unsigned char*, int) { |
| 327 | // The current implementation adds randomness as needed. |
| 328 | } |
| 329 | |
Christopher Ferris | f903558 | 2014-09-05 16:39:22 -0700 | [diff] [blame] | 330 | // Old versions of the NDK did not export malloc_usable_size, but did |
| 331 | // export dlmalloc_usable_size. We are moving away from dlmalloc in L |
| 332 | // so make this call malloc_usable_size. |
| 333 | extern "C" size_t dlmalloc_usable_size(void* ptr) { |
| 334 | return malloc_usable_size(ptr); |
| 335 | } |
| 336 | |
Elliott Hughes | 0f001b6 | 2014-09-12 11:35:05 -0700 | [diff] [blame] | 337 | // In L we added a public pthread_gettid_np, but some apps were using the private API. |
| 338 | extern "C" pid_t __pthread_gettid(pthread_t t) { |
| 339 | return pthread_gettid_np(t); |
| 340 | } |
| 341 | |
Christopher Ferris | f183f95 | 2014-10-08 22:48:20 -0700 | [diff] [blame] | 342 | // Older versions of appportable used dlmalloc directly instead of malloc, |
| 343 | // so export this compatibility shim that simply calls malloc. |
| 344 | extern "C" void* dlmalloc(size_t size) { |
| 345 | return malloc(size); |
| 346 | } |
| 347 | |
Yabin Cui | 7fb680b | 2015-02-24 13:18:25 -0800 | [diff] [blame^] | 348 | #endif // !defined(__LP64__) |
| 349 | |
| 350 | // This is never implemented in bionic, only needed for ABI compatibility with the NDK. |
| 351 | extern "C" char* getusershell() { |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | // This is never implemented in bionic, only needed for ABI compatibility with the NDK. |
| 356 | extern "C" void setusershell() { } |
| 357 | |
| 358 | // This is never implemented in bionic, only needed for ABI compatibility with the NDK. |
| 359 | extern "C" void endusershell() { } |