blob: 43078fdcc2509d0f0f907385378f3744e7a7d3e4 [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 Hughesefbdb532014-04-07 15:17:19 -070034#include <inttypes.h>
Calin Juravlea4eafa62014-03-10 18:10:04 +000035#include <pthread.h>
Dan Albert205dd7d2014-06-04 10:14:19 -070036#include <signal.h>
Elliott Hughesfcac8ff2014-05-22 01:24:30 -070037#include <stdio.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070038#include <stdlib.h>
39#include <sys/resource.h>
Elliott Hughesbd3a98c2014-05-24 17:19:36 -070040#include <sys/syscall.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070041#include <sys/time.h>
42#include <sys/types.h>
43#include <sys/wait.h>
44#include <unistd.h>
Dan Albert001f8f02014-06-04 09:53:06 -070045#include <wchar.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070046
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.
49extern "C" {
50 unsigned int __page_size = PAGE_SIZE;
Elliott Hughes0e44bc32014-02-24 15:55:31 -080051 unsigned int __page_shift = 12;
Elliott Hughes567a8de2013-10-24 17:14:55 -070052}
53
54// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
55extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
56 return wait4(pid, status, options, rusage);
57}
58
Elliott Hughes06209252013-11-06 16:20:54 -080059// TODO: does anything still need this?
Elliott Hughes567a8de2013-10-24 17:14:55 -070060extern "C" int __open() {
61 abort();
62}
63
Elliott Hughes06209252013-11-06 16:20:54 -080064// TODO: does anything still need this?
65extern "C" void** __get_tls() {
66#include "private/__get_tls.h"
67 return __get_tls();
68}
69
Elliott Hughes152b9de2014-03-10 15:54:40 -070070// This non-standard function was in our <string.h> for some reason.
71extern "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 Juravlea4eafa62014-03-10 18:10:04 +000084extern "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
90extern "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 Hughesefbdb532014-04-07 15:17:19 -070097// Non-standard cruft that should only ever have been in system/core/toolbox.
98extern "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 Hugheseae59022014-04-22 17:56:42 -0700124static 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.
140extern "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.
188extern "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 Hughesfcac8ff2014-05-22 01:24:30 -0700192// POSIX calls this dprintf, but LP32 Android had fdprintf instead.
193extern "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.
202extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) {
203 return vdprintf(fd, fmt, ap);
204}
205
Elliott Hughesb30aff42014-05-28 19:35:33 +0000206#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 Hughesbd3a98c2014-05-24 17:19:36 -0700211
212// This used to be in <sys/atomics.h>.
213extern "C" int __futex_wake(volatile void* ftx, int count) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000214 return __real_futex_wake(ftx, count);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700215}
216
217// This used to be in <sys/atomics.h>.
218extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000219 return __real_futex_wait(ftx, value, timeout);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700220}
221
Anthony King00170732014-05-24 16:47:14 +0000222// Unity's libmono uses this.
223extern "C" int tkill(pid_t tid, int sig) {
224 return syscall(__NR_tkill, tid, sig);
225}
226
Dan Albert001f8f02014-06-04 09:53:06 -0700227extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
228 return wcsstr(haystack, needle);
229}
230
Dan Albert205dd7d2014-06-04 10:14:19 -0700231// This was removed from POSIX 2008.
232extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) {
233 return signal(signum, handler);
234}
235
236// sysv_signal() was never in POSIX.
237extern sighandler_t _signal(int signum, sighandler_t handler, int flags);
238extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) {
239 return _signal(signum, handler, SA_RESETHAND);
240}
241
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700242// This is a system call that was never in POSIX. Use readdir(3) instead.
Elliott Hughesd1ead2a2014-06-06 15:24:20 -0700243extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
244extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700245 return __getdents64(fd, dirp, count);
246}
247
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700248// This is a BSDism that we never implemented correctly. Used by Firefox.
249extern "C" int issetugid() {
250 return 0;
251}
252
Dan Albert8229ae42014-06-13 16:04:41 -0700253// This was removed from POSIX 2004.
254extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) {
255 return wait4(-1, status, options, rusage);
256}
257
Dan Albert462abab2014-06-13 16:51:24 -0700258// This was removed from POSIX 2004.
259extern "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 Hughes567a8de2013-10-24 17:14:55 -0700269#endif