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> |
| 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 | |
| 47 | // These were accidentally declared in <unistd.h> because we stupidly used to inline |
| 48 | // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. |
| 49 | extern "C" { |
| 50 | unsigned int __page_size = PAGE_SIZE; |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 51 | unsigned int __page_shift = 12; |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). |
| 55 | extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { |
| 56 | return wait4(pid, status, options, rusage); |
| 57 | } |
| 58 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 59 | // TODO: does anything still need this? |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 60 | extern "C" int __open() { |
| 61 | abort(); |
| 62 | } |
| 63 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 64 | // TODO: does anything still need this? |
| 65 | extern "C" void** __get_tls() { |
| 66 | #include "private/__get_tls.h" |
| 67 | return __get_tls(); |
| 68 | } |
| 69 | |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 70 | // This non-standard function was in our <string.h> for some reason. |
| 71 | extern "C" void memswap(void* m1, void* m2, size_t n) { |
| 72 | char* p = reinterpret_cast<char*>(m1); |
| 73 | char* p_end = p + n; |
| 74 | char* q = reinterpret_cast<char*>(m2); |
| 75 | while (p < p_end) { |
| 76 | char tmp = *p; |
| 77 | *p = *q; |
| 78 | *q = tmp; |
| 79 | p++; |
| 80 | q++; |
| 81 | } |
| 82 | } |
| 83 | |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 84 | extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) { |
| 85 | // This was removed from POSIX.1-2008, and is not implemented on bionic. |
| 86 | // Needed for ABI compatibility with the NDK. |
| 87 | return ENOSYS; |
| 88 | } |
| 89 | |
| 90 | extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { |
| 91 | // This was removed from POSIX.1-2008. |
| 92 | // Needed for ABI compatibility with the NDK. |
| 93 | *stack_addr = (char*)attr->stack_base + attr->stack_size; |
| 94 | return 0; |
| 95 | } |
| 96 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 97 | // Non-standard cruft that should only ever have been in system/core/toolbox. |
| 98 | extern "C" char* strtotimeval(const char* str, struct timeval* ts) { |
| 99 | char* s; |
| 100 | ts->tv_sec = strtoumax(str, &s, 10); |
| 101 | |
| 102 | long fractional_seconds = 0; |
| 103 | if (*s == '.') { |
| 104 | s++; |
| 105 | int count = 0; |
| 106 | |
| 107 | // Read up to 6 digits (microseconds). |
| 108 | while (*s && isdigit(*s)) { |
| 109 | if (++count < 7) { |
| 110 | fractional_seconds = fractional_seconds*10 + (*s - '0'); |
| 111 | } |
| 112 | s++; |
| 113 | } |
| 114 | |
| 115 | for (; count < 6; count++) { |
| 116 | fractional_seconds *= 10; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | ts->tv_usec = fractional_seconds; |
| 121 | return s; |
| 122 | } |
| 123 | |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 124 | static inline int digitval(int ch) { |
| 125 | unsigned d; |
| 126 | |
| 127 | d = (unsigned)(ch - '0'); |
| 128 | if (d < 10) return (int)d; |
| 129 | |
| 130 | d = (unsigned)(ch - 'a'); |
| 131 | if (d < 6) return (int)(d+10); |
| 132 | |
| 133 | d = (unsigned)(ch - 'A'); |
| 134 | if (d < 6) return (int)(d+10); |
| 135 | |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | // This non-standard function was in our <inttypes.h> for some reason. |
| 140 | extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { |
| 141 | const unsigned char* p = (const unsigned char *)nptr; |
| 142 | const unsigned char* end = p + n; |
| 143 | int minus = 0; |
| 144 | uintmax_t v = 0; |
| 145 | int d; |
| 146 | |
| 147 | while (p < end && isspace(*p)) { |
| 148 | p++; |
| 149 | } |
| 150 | |
| 151 | if (p < end) { |
| 152 | char c = p[0]; |
| 153 | if (c == '-' || c == '+') { |
| 154 | minus = (c == '-'); |
| 155 | p++; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (base == 0) { |
| 160 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 161 | p += 2; |
| 162 | base = 16; |
| 163 | } else if (p+1 < end && p[0] == '0') { |
| 164 | p += 1; |
| 165 | base = 8; |
| 166 | } else { |
| 167 | base = 10; |
| 168 | } |
| 169 | } else if (base == 16) { |
| 170 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 171 | p += 2; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | while (p < end && (d = digitval(*p)) >= 0 && d < base) { |
| 176 | v = v*base + d; |
| 177 | p += 1; |
| 178 | } |
| 179 | |
| 180 | if (endptr) { |
| 181 | *endptr = (char*) p; |
| 182 | } |
| 183 | |
| 184 | return minus ? -v : v; |
| 185 | } |
| 186 | |
| 187 | // This non-standard function was in our <inttypes.h> for some reason. |
| 188 | extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { |
| 189 | return (intmax_t) strntoumax(nptr, endptr, base, n); |
| 190 | } |
| 191 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 192 | // POSIX calls this dprintf, but LP32 Android had fdprintf instead. |
| 193 | extern "C" int fdprintf(int fd, const char* fmt, ...) { |
| 194 | va_list ap; |
| 195 | va_start(ap, fmt); |
| 196 | int rc = vdprintf(fd, fmt, ap); |
| 197 | va_end(ap); |
| 198 | return rc; |
| 199 | } |
| 200 | |
| 201 | // POSIX calls this vdprintf, but LP32 Android had fdprintf instead. |
| 202 | extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) { |
| 203 | return vdprintf(fd, fmt, ap); |
| 204 | } |
| 205 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 206 | #define __futex_wake __real_futex_wake |
| 207 | #define __futex_wait __real_futex_wait |
| 208 | #include "private/bionic_futex.h" |
| 209 | #undef __futex_wake |
| 210 | #undef __futex_wait |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 211 | |
| 212 | // This used to be in <sys/atomics.h>. |
| 213 | extern "C" int __futex_wake(volatile void* ftx, int count) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 214 | return __real_futex_wake(ftx, count); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // This used to be in <sys/atomics.h>. |
| 218 | 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] | 219 | return __real_futex_wait(ftx, value, timeout); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Anthony King | 0017073 | 2014-05-24 16:47:14 +0000 | [diff] [blame] | 222 | // Unity's libmono uses this. |
| 223 | extern "C" int tkill(pid_t tid, int sig) { |
| 224 | return syscall(__NR_tkill, tid, sig); |
| 225 | } |
| 226 | |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 227 | extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { |
| 228 | return wcsstr(haystack, needle); |
| 229 | } |
| 230 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 231 | // This was removed from POSIX 2008. |
| 232 | extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) { |
| 233 | return signal(signum, handler); |
| 234 | } |
| 235 | |
| 236 | // sysv_signal() was never in POSIX. |
| 237 | extern sighandler_t _signal(int signum, sighandler_t handler, int flags); |
| 238 | extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) { |
| 239 | return _signal(signum, handler, SA_RESETHAND); |
| 240 | } |
| 241 | |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 242 | // 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] | 243 | extern "C" int __getdents64(unsigned int, dirent*, unsigned int); |
| 244 | extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) { |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 245 | return __getdents64(fd, dirp, count); |
| 246 | } |
| 247 | |
Elliott Hughes | bffbfee | 2014-06-06 20:41:42 -0700 | [diff] [blame] | 248 | // This is a BSDism that we never implemented correctly. Used by Firefox. |
| 249 | extern "C" int issetugid() { |
| 250 | return 0; |
| 251 | } |
| 252 | |
Dan Albert | 8229ae4 | 2014-06-13 16:04:41 -0700 | [diff] [blame] | 253 | // This was removed from POSIX 2004. |
| 254 | extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) { |
| 255 | return wait4(-1, status, options, rusage); |
| 256 | } |
| 257 | |
Dan Albert | 462abab | 2014-06-13 16:51:24 -0700 | [diff] [blame^] | 258 | // This was removed from POSIX 2004. |
| 259 | extern "C" int getdtablesize() { |
| 260 | struct rlimit r; |
| 261 | |
| 262 | if (getrlimit(RLIMIT_NOFILE, &r) < 0) { |
| 263 | return sysconf(_SC_OPEN_MAX); |
| 264 | } |
| 265 | |
| 266 | return r.rlim_cur; |
| 267 | } |
| 268 | |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 269 | #endif |