blob: 77412aca6904ee291ab9ef8209faca9f295ba708 [file] [log] [blame]
Elliott Hughes567a8de2013-10-24 17:14:55 -07001/*
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 Hughesefbdb532014-04-07 15:17:19 -070032#include <ctype.h>
Elliott Hughesd1ead2a2014-06-06 15:24:20 -070033#include <dirent.h>
Elliott Hughes4c5891d2015-02-19 22:49:44 -080034#include <errno.h>
Elliott Hughesefbdb532014-04-07 15:17:19 -070035#include <inttypes.h>
Calin Juravlea4eafa62014-03-10 18:10:04 +000036#include <pthread.h>
Dan Albert205dd7d2014-06-04 10:14:19 -070037#include <signal.h>
Elliott Hughesfcac8ff2014-05-22 01:24:30 -070038#include <stdio.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070039#include <stdlib.h>
David 'Digit' Turner891dedb2014-06-13 12:28:11 +020040#include <string.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070041#include <sys/resource.h>
Elliott Hughesbd3a98c2014-05-24 17:19:36 -070042#include <sys/syscall.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070043#include <sys/time.h>
44#include <sys/types.h>
45#include <sys/wait.h>
46#include <unistd.h>
Dan Albert001f8f02014-06-04 09:53:06 -070047#include <wchar.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070048
49// These were accidentally declared in <unistd.h> because we stupidly used to inline
50// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps.
51extern "C" {
52 unsigned int __page_size = PAGE_SIZE;
Elliott Hughes0e44bc32014-02-24 15:55:31 -080053 unsigned int __page_shift = 12;
Elliott Hughes567a8de2013-10-24 17:14:55 -070054}
55
56// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
57extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
58 return wait4(pid, status, options, rusage);
59}
60
Elliott Hughes06209252013-11-06 16:20:54 -080061// TODO: does anything still need this?
Elliott Hughes567a8de2013-10-24 17:14:55 -070062extern "C" int __open() {
63 abort();
64}
65
Elliott Hughes06209252013-11-06 16:20:54 -080066// TODO: does anything still need this?
67extern "C" void** __get_tls() {
68#include "private/__get_tls.h"
69 return __get_tls();
70}
71
Elliott Hughes152b9de2014-03-10 15:54:40 -070072// This non-standard function was in our <string.h> for some reason.
73extern "C" void memswap(void* m1, void* m2, size_t n) {
74 char* p = reinterpret_cast<char*>(m1);
75 char* p_end = p + n;
76 char* q = reinterpret_cast<char*>(m2);
77 while (p < p_end) {
78 char tmp = *p;
79 *p = *q;
80 *q = tmp;
81 p++;
82 q++;
83 }
84}
85
Calin Juravlea4eafa62014-03-10 18:10:04 +000086extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
87 // This was removed from POSIX.1-2008, and is not implemented on bionic.
88 // Needed for ABI compatibility with the NDK.
89 return ENOSYS;
90}
91
92extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
93 // This was removed from POSIX.1-2008.
94 // Needed for ABI compatibility with the NDK.
95 *stack_addr = (char*)attr->stack_base + attr->stack_size;
96 return 0;
97}
98
Elliott Hughesefbdb532014-04-07 15:17:19 -070099// Non-standard cruft that should only ever have been in system/core/toolbox.
100extern "C" char* strtotimeval(const char* str, struct timeval* ts) {
101 char* s;
102 ts->tv_sec = strtoumax(str, &s, 10);
103
104 long fractional_seconds = 0;
105 if (*s == '.') {
106 s++;
107 int count = 0;
108
109 // Read up to 6 digits (microseconds).
110 while (*s && isdigit(*s)) {
111 if (++count < 7) {
112 fractional_seconds = fractional_seconds*10 + (*s - '0');
113 }
114 s++;
115 }
116
117 for (; count < 6; count++) {
118 fractional_seconds *= 10;
119 }
120 }
121
122 ts->tv_usec = fractional_seconds;
123 return s;
124}
125
Elliott Hugheseae59022014-04-22 17:56:42 -0700126static inline int digitval(int ch) {
127 unsigned d;
128
129 d = (unsigned)(ch - '0');
130 if (d < 10) return (int)d;
131
132 d = (unsigned)(ch - 'a');
133 if (d < 6) return (int)(d+10);
134
135 d = (unsigned)(ch - 'A');
136 if (d < 6) return (int)(d+10);
137
138 return -1;
139}
140
141// This non-standard function was in our <inttypes.h> for some reason.
142extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
143 const unsigned char* p = (const unsigned char *)nptr;
144 const unsigned char* end = p + n;
145 int minus = 0;
146 uintmax_t v = 0;
147 int d;
148
149 while (p < end && isspace(*p)) {
150 p++;
151 }
152
153 if (p < end) {
154 char c = p[0];
155 if (c == '-' || c == '+') {
156 minus = (c == '-');
157 p++;
158 }
159 }
160
161 if (base == 0) {
162 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
163 p += 2;
164 base = 16;
165 } else if (p+1 < end && p[0] == '0') {
166 p += 1;
167 base = 8;
168 } else {
169 base = 10;
170 }
171 } else if (base == 16) {
172 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
173 p += 2;
174 }
175 }
176
177 while (p < end && (d = digitval(*p)) >= 0 && d < base) {
178 v = v*base + d;
179 p += 1;
180 }
181
182 if (endptr) {
183 *endptr = (char*) p;
184 }
185
186 return minus ? -v : v;
187}
188
189// This non-standard function was in our <inttypes.h> for some reason.
190extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
191 return (intmax_t) strntoumax(nptr, endptr, base, n);
192}
193
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700194// POSIX calls this dprintf, but LP32 Android had fdprintf instead.
195extern "C" int fdprintf(int fd, const char* fmt, ...) {
196 va_list ap;
197 va_start(ap, fmt);
198 int rc = vdprintf(fd, fmt, ap);
199 va_end(ap);
200 return rc;
201}
202
203// POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
204extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) {
205 return vdprintf(fd, fmt, ap);
206}
207
Elliott Hughesb30aff42014-05-28 19:35:33 +0000208#define __futex_wake __real_futex_wake
209#define __futex_wait __real_futex_wait
210#include "private/bionic_futex.h"
211#undef __futex_wake
212#undef __futex_wait
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700213
214// This used to be in <sys/atomics.h>.
215extern "C" int __futex_wake(volatile void* ftx, int count) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000216 return __real_futex_wake(ftx, count);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700217}
218
219// This used to be in <sys/atomics.h>.
220extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000221 return __real_futex_wait(ftx, value, timeout);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700222}
223
Anthony King00170732014-05-24 16:47:14 +0000224// Unity's libmono uses this.
225extern "C" int tkill(pid_t tid, int sig) {
226 return syscall(__NR_tkill, tid, sig);
227}
228
Elliott Hughes1628eb12014-08-06 10:47:33 -0700229// This was removed from POSIX 2008.
Dan Albert001f8f02014-06-04 09:53:06 -0700230extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
231 return wcsstr(haystack, needle);
232}
233
Dan Albert205dd7d2014-06-04 10:14:19 -0700234// This was removed from POSIX 2008.
235extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) {
236 return signal(signum, handler);
237}
238
Elliott Hughes1edfd9e2015-01-26 21:45:56 -0800239#if !defined(__i386__)
Elliott Hughes76f89162015-01-26 13:34:58 -0800240// This was removed from POSIX 2008.
241#undef bcopy
242extern "C" void bcopy(const void* src, void* dst, size_t n) {
243 memcpy(dst, src, n);
244}
Elliott Hughes1edfd9e2015-01-26 21:45:56 -0800245#else
246// x86 has an assembler implementation.
247#endif
Elliott Hughes76f89162015-01-26 13:34:58 -0800248
Dan Albert205dd7d2014-06-04 10:14:19 -0700249// sysv_signal() was never in POSIX.
250extern sighandler_t _signal(int signum, sighandler_t handler, int flags);
251extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) {
252 return _signal(signum, handler, SA_RESETHAND);
253}
254
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700255// This is a system call that was never in POSIX. Use readdir(3) instead.
Elliott Hughesd1ead2a2014-06-06 15:24:20 -0700256extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
257extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700258 return __getdents64(fd, dirp, count);
259}
260
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700261// This is a BSDism that we never implemented correctly. Used by Firefox.
262extern "C" int issetugid() {
263 return 0;
264}
265
Dan Albert8229ae42014-06-13 16:04:41 -0700266// This was removed from POSIX 2004.
267extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) {
268 return wait4(-1, status, options, rusage);
269}
270
Dan Albert462abab2014-06-13 16:51:24 -0700271// This was removed from POSIX 2004.
272extern "C" int getdtablesize() {
273 struct rlimit r;
274
275 if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
276 return sysconf(_SC_OPEN_MAX);
277 }
278
279 return r.rlim_cur;
280}
281
Elliott Hughes1628eb12014-08-06 10:47:33 -0700282// Only used by ftime, which was removed from POSIX 2008.
Dan Albertac646752014-06-05 02:10:49 +0000283struct timeb {
284 time_t time;
285 unsigned short millitm;
286 short timezone;
287 short dstflag;
288};
289
290// This was removed from POSIX 2008.
291extern "C" int ftime(struct timeb* tb) {
292 struct timeval tv;
293 struct timezone tz;
294
295 if (gettimeofday(&tv, &tz) < 0)
296 return -1;
297
298 tb->time = tv.tv_sec;
299 tb->millitm = (tv.tv_usec + 500) / 1000;
300
301 if (tb->millitm == 1000) {
302 ++tb->time;
303 tb->millitm = 0;
304 }
305
306 tb->timezone = tz.tz_minuteswest;
307 tb->dstflag = tz.tz_dsttime;
308
309 return 0;
310}
311
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200312// This was removed from POSIX 2008.
313extern "C" char* index(const char* str, int ch) {
314 return strchr(str, ch);
315}
316
Elliott Hughes5dea4722014-09-03 15:53:11 -0700317// This was removed from BSD.
318extern "C" void arc4random_stir(void) {
319 // The current implementation stirs itself as needed.
320}
321
Elliott Hughesfc829732014-09-08 10:25:33 -0700322// This was removed from BSD.
323extern "C" void arc4random_addrandom(unsigned char*, int) {
324 // The current implementation adds randomness as needed.
325}
326
Christopher Ferrisf9035582014-09-05 16:39:22 -0700327// Old versions of the NDK did not export malloc_usable_size, but did
328// export dlmalloc_usable_size. We are moving away from dlmalloc in L
329// so make this call malloc_usable_size.
330extern "C" size_t dlmalloc_usable_size(void* ptr) {
331 return malloc_usable_size(ptr);
332}
333
Elliott Hughes0f001b62014-09-12 11:35:05 -0700334// In L we added a public pthread_gettid_np, but some apps were using the private API.
335extern "C" pid_t __pthread_gettid(pthread_t t) {
336 return pthread_gettid_np(t);
337}
338
Christopher Ferrisf183f952014-10-08 22:48:20 -0700339// Older versions of appportable used dlmalloc directly instead of malloc,
340// so export this compatibility shim that simply calls malloc.
341extern "C" void* dlmalloc(size_t size) {
342 return malloc(size);
343}
344
Elliott Hughes567a8de2013-10-24 17:14:55 -0700345#endif