blob: 042943005840870505b8c5c5bf78ea45be32918d [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
Yabin Cui7fb680b2015-02-24 13:18:25 -080029// This file perpetuates the mistakes of the past.
Elliott Hughes567a8de2013-10-24 17:14:55 -070030
Elliott Hughesefbdb532014-04-07 15:17:19 -070031#include <ctype.h>
Elliott Hughesd1ead2a2014-06-06 15:24:20 -070032#include <dirent.h>
Elliott Hughes4c5891d2015-02-19 22:49:44 -080033#include <errno.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>
David 'Digit' Turner891dedb2014-06-13 12:28:11 +020039#include <string.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070040#include <sys/resource.h>
Elliott Hughesbd3a98c2014-05-24 17:19:36 -070041#include <sys/syscall.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070042#include <sys/time.h>
43#include <sys/types.h>
44#include <sys/wait.h>
45#include <unistd.h>
Dan Albert001f8f02014-06-04 09:53:06 -070046#include <wchar.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070047
Yabin Cui7fb680b2015-02-24 13:18:25 -080048#include "private/libc_logging.h"
49
Elliott Hughescfd5a462015-12-04 15:57:51 -080050extern "C" {
51
Elliott Hughesfb8fd502015-10-12 17:53:48 -070052// Brillo doesn't need to support any legacy cruft.
53#if !defined(__BRILLO__)
54
55// Most of the cruft is only for 32-bit Android targets.
Yabin Cui7fb680b2015-02-24 13:18:25 -080056#if !defined(__LP64__)
57
Elliott Hughes567a8de2013-10-24 17:14:55 -070058// These were accidentally declared in <unistd.h> because we stupidly used to inline
59// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps.
Elliott Hughescfd5a462015-12-04 15:57:51 -080060unsigned int __page_size = PAGE_SIZE;
61unsigned int __page_shift = 12;
Elliott Hughes567a8de2013-10-24 17:14:55 -070062
63// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
Elliott Hughescfd5a462015-12-04 15:57:51 -080064pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
Elliott Hughes567a8de2013-10-24 17:14:55 -070065 return wait4(pid, status, options, rusage);
66}
67
Elliott Hughes06209252013-11-06 16:20:54 -080068// TODO: does anything still need this?
Elliott Hughescfd5a462015-12-04 15:57:51 -080069int __open() {
Elliott Hughes567a8de2013-10-24 17:14:55 -070070 abort();
71}
72
Elliott Hughes06209252013-11-06 16:20:54 -080073// TODO: does anything still need this?
Elliott Hughescfd5a462015-12-04 15:57:51 -080074void** __get_tls() {
Elliott Hughes06209252013-11-06 16:20:54 -080075#include "private/__get_tls.h"
76 return __get_tls();
77}
78
Elliott Hughes152b9de2014-03-10 15:54:40 -070079// This non-standard function was in our <string.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -080080void memswap(void* m1, void* m2, size_t n) {
Elliott Hughes152b9de2014-03-10 15:54:40 -070081 char* p = reinterpret_cast<char*>(m1);
82 char* p_end = p + n;
83 char* q = reinterpret_cast<char*>(m2);
84 while (p < p_end) {
85 char tmp = *p;
86 *p = *q;
87 *q = tmp;
88 p++;
89 q++;
90 }
91}
92
Elliott Hughescfd5a462015-12-04 15:57:51 -080093int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
Calin Juravlea4eafa62014-03-10 18:10:04 +000094 // This was removed from POSIX.1-2008, and is not implemented on bionic.
95 // Needed for ABI compatibility with the NDK.
96 return ENOSYS;
97}
98
Elliott Hughescfd5a462015-12-04 15:57:51 -080099int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
Calin Juravlea4eafa62014-03-10 18:10:04 +0000100 // This was removed from POSIX.1-2008.
101 // Needed for ABI compatibility with the NDK.
102 *stack_addr = (char*)attr->stack_base + attr->stack_size;
103 return 0;
104}
105
Elliott Hughesefbdb532014-04-07 15:17:19 -0700106// Non-standard cruft that should only ever have been in system/core/toolbox.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800107char* strtotimeval(const char* str, struct timeval* ts) {
Elliott Hughesefbdb532014-04-07 15:17:19 -0700108 char* s;
109 ts->tv_sec = strtoumax(str, &s, 10);
110
111 long fractional_seconds = 0;
112 if (*s == '.') {
113 s++;
114 int count = 0;
115
116 // Read up to 6 digits (microseconds).
117 while (*s && isdigit(*s)) {
118 if (++count < 7) {
119 fractional_seconds = fractional_seconds*10 + (*s - '0');
120 }
121 s++;
122 }
123
124 for (; count < 6; count++) {
125 fractional_seconds *= 10;
126 }
127 }
128
129 ts->tv_usec = fractional_seconds;
130 return s;
131}
132
Elliott Hugheseae59022014-04-22 17:56:42 -0700133static inline int digitval(int ch) {
134 unsigned d;
135
136 d = (unsigned)(ch - '0');
137 if (d < 10) return (int)d;
138
139 d = (unsigned)(ch - 'a');
140 if (d < 6) return (int)(d+10);
141
142 d = (unsigned)(ch - 'A');
143 if (d < 6) return (int)(d+10);
144
145 return -1;
146}
147
148// This non-standard function was in our <inttypes.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800149uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
Elliott Hugheseae59022014-04-22 17:56:42 -0700150 const unsigned char* p = (const unsigned char *)nptr;
151 const unsigned char* end = p + n;
152 int minus = 0;
153 uintmax_t v = 0;
154 int d;
155
156 while (p < end && isspace(*p)) {
157 p++;
158 }
159
160 if (p < end) {
161 char c = p[0];
162 if (c == '-' || c == '+') {
163 minus = (c == '-');
164 p++;
165 }
166 }
167
168 if (base == 0) {
169 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
170 p += 2;
171 base = 16;
172 } else if (p+1 < end && p[0] == '0') {
173 p += 1;
174 base = 8;
175 } else {
176 base = 10;
177 }
178 } else if (base == 16) {
179 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
180 p += 2;
181 }
182 }
183
184 while (p < end && (d = digitval(*p)) >= 0 && d < base) {
185 v = v*base + d;
186 p += 1;
187 }
188
189 if (endptr) {
190 *endptr = (char*) p;
191 }
192
193 return minus ? -v : v;
194}
195
196// This non-standard function was in our <inttypes.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800197intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
Elliott Hugheseae59022014-04-22 17:56:42 -0700198 return (intmax_t) strntoumax(nptr, endptr, base, n);
199}
200
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700201// POSIX calls this dprintf, but LP32 Android had fdprintf instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800202int fdprintf(int fd, const char* fmt, ...) {
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700203 va_list ap;
204 va_start(ap, fmt);
205 int rc = vdprintf(fd, fmt, ap);
206 va_end(ap);
207 return rc;
208}
209
210// POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800211int vfdprintf(int fd, const char* fmt, va_list ap) {
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700212 return vdprintf(fd, fmt, ap);
213}
214
Elliott Hughesb30aff42014-05-28 19:35:33 +0000215#define __futex_wake __real_futex_wake
216#define __futex_wait __real_futex_wait
217#include "private/bionic_futex.h"
218#undef __futex_wake
219#undef __futex_wait
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700220
221// This used to be in <sys/atomics.h>.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800222int __futex_wake(volatile void* ftx, int count) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000223 return __real_futex_wake(ftx, count);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700224}
225
226// This used to be in <sys/atomics.h>.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800227int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000228 return __real_futex_wait(ftx, value, timeout);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700229}
230
Anthony King00170732014-05-24 16:47:14 +0000231// Unity's libmono uses this.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800232int tkill(pid_t tid, int sig) {
Anthony King00170732014-05-24 16:47:14 +0000233 return syscall(__NR_tkill, tid, sig);
234}
235
Elliott Hughes1628eb12014-08-06 10:47:33 -0700236// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800237wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
Dan Albert001f8f02014-06-04 09:53:06 -0700238 return wcsstr(haystack, needle);
239}
240
Dan Albert205dd7d2014-06-04 10:14:19 -0700241// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800242sighandler_t bsd_signal(int signum, sighandler_t handler) {
Dan Albert205dd7d2014-06-04 10:14:19 -0700243 return signal(signum, handler);
244}
245
Elliott Hughes1edfd9e2015-01-26 21:45:56 -0800246#if !defined(__i386__)
Elliott Hughes76f89162015-01-26 13:34:58 -0800247// This was removed from POSIX 2008.
248#undef bcopy
Elliott Hughescfd5a462015-12-04 15:57:51 -0800249void bcopy(const void* src, void* dst, size_t n) {
Rohit Agrawald51a0b02015-12-05 12:39:54 -0800250 memmove(dst, src, n);
Elliott Hughes76f89162015-01-26 13:34:58 -0800251}
Elliott Hughes1edfd9e2015-01-26 21:45:56 -0800252#else
253// x86 has an assembler implementation.
254#endif
Elliott Hughes76f89162015-01-26 13:34:58 -0800255
Dan Albert205dd7d2014-06-04 10:14:19 -0700256// sysv_signal() was never in POSIX.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800257extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags);
258sighandler_t sysv_signal(int signum, sighandler_t handler) {
Dan Albert205dd7d2014-06-04 10:14:19 -0700259 return _signal(signum, handler, SA_RESETHAND);
260}
261
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700262// This is a system call that was never in POSIX. Use readdir(3) instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800263int __getdents64(unsigned int, dirent*, unsigned int);
264int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700265 return __getdents64(fd, dirp, count);
266}
267
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700268// This is a BSDism that we never implemented correctly. Used by Firefox.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800269int issetugid() {
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700270 return 0;
271}
272
Dan Albert8229ae42014-06-13 16:04:41 -0700273// This was removed from POSIX 2004.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800274pid_t wait3(int* status, int options, struct rusage* rusage) {
Dan Albert8229ae42014-06-13 16:04:41 -0700275 return wait4(-1, status, options, rusage);
276}
277
Dan Albert462abab2014-06-13 16:51:24 -0700278// This was removed from POSIX 2004.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800279int getdtablesize() {
Dan Albert462abab2014-06-13 16:51:24 -0700280 struct rlimit r;
281
282 if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
283 return sysconf(_SC_OPEN_MAX);
284 }
285
286 return r.rlim_cur;
287}
288
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800289// A leaked BSD stdio implementation detail that's now a no-op.
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800290void __sinit() {}
291int __sdidinit = 1;
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800292
Elliott Hughes1628eb12014-08-06 10:47:33 -0700293// Only used by ftime, which was removed from POSIX 2008.
Dan Albertac646752014-06-05 02:10:49 +0000294struct timeb {
295 time_t time;
296 unsigned short millitm;
297 short timezone;
298 short dstflag;
299};
300
301// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800302int ftime(struct timeb* tb) {
Dan Albertac646752014-06-05 02:10:49 +0000303 struct timeval tv;
304 struct timezone tz;
305
306 if (gettimeofday(&tv, &tz) < 0)
307 return -1;
308
309 tb->time = tv.tv_sec;
310 tb->millitm = (tv.tv_usec + 500) / 1000;
311
312 if (tb->millitm == 1000) {
313 ++tb->time;
314 tb->millitm = 0;
315 }
316
317 tb->timezone = tz.tz_minuteswest;
318 tb->dstflag = tz.tz_dsttime;
319
320 return 0;
321}
322
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200323// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800324char* index(const char* str, int ch) {
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200325 return strchr(str, ch);
326}
327
Elliott Hughes5dea4722014-09-03 15:53:11 -0700328// This was removed from BSD.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800329void arc4random_stir(void) {
Elliott Hughes5dea4722014-09-03 15:53:11 -0700330 // The current implementation stirs itself as needed.
331}
332
Elliott Hughesfc829732014-09-08 10:25:33 -0700333// This was removed from BSD.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800334void arc4random_addrandom(unsigned char*, int) {
Elliott Hughesfc829732014-09-08 10:25:33 -0700335 // The current implementation adds randomness as needed.
336}
337
Christopher Ferrisf9035582014-09-05 16:39:22 -0700338// Old versions of the NDK did not export malloc_usable_size, but did
339// export dlmalloc_usable_size. We are moving away from dlmalloc in L
340// so make this call malloc_usable_size.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800341size_t dlmalloc_usable_size(void* ptr) {
Christopher Ferrisf9035582014-09-05 16:39:22 -0700342 return malloc_usable_size(ptr);
343}
344
Elliott Hughes0f001b62014-09-12 11:35:05 -0700345// In L we added a public pthread_gettid_np, but some apps were using the private API.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800346pid_t __pthread_gettid(pthread_t t) {
Elliott Hughes0f001b62014-09-12 11:35:05 -0700347 return pthread_gettid_np(t);
348}
349
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700350// Older versions of apportable used dlmalloc directly instead of malloc,
Christopher Ferrisf183f952014-10-08 22:48:20 -0700351// so export this compatibility shim that simply calls malloc.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800352void* dlmalloc(size_t size) {
Christopher Ferrisf183f952014-10-08 22:48:20 -0700353 return malloc(size);
354}
355
Yabin Cui2f836d42015-03-18 14:14:02 -0700356#define __get_thread __real_get_thread
357#include "pthread_internal.h"
358#undef __get_thread
359// Various third-party apps contain a backport of our pthread_rwlock implementation that uses this.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800360pthread_internal_t* __get_thread() {
Yabin Cui2f836d42015-03-18 14:14:02 -0700361 return __real_get_thread();
362}
363
Christopher Ferris9978a9a2015-10-29 18:11:32 -0700364// This one exists only for the LP32 NDK and is not present anywhere else.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800365extern long __set_errno_internal(int);
366long __set_errno(int n) {
Christopher Ferris9978a9a2015-10-29 18:11:32 -0700367 return __set_errno_internal(n);
368}
369
Yabin Cui7fb680b2015-02-24 13:18:25 -0800370#endif // !defined(__LP64__)
371
Elliott Hughesd7c7daa2015-10-28 14:20:16 -0700372// This was never implemented in bionic, only needed for ABI compatibility with the NDK.
373// In the M time frame, over 1000 apps have a reference to this!
Elliott Hughescfd5a462015-12-04 15:57:51 -0800374void endpwent() { }
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700375
376// Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems
377// that use dlmalloc, be consistent and export them everywhere.
378#if defined(USE_JEMALLOC)
Elliott Hughescfd5a462015-12-04 15:57:51 -0800379void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) {
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700380}
Elliott Hughescfd5a462015-12-04 15:57:51 -0800381int dlmalloc_trim(size_t) {
Elliott Hughesfb8fd502015-10-12 17:53:48 -0700382 return 0;
383}
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700384#else
Elliott Hughescfd5a462015-12-04 15:57:51 -0800385void dlmalloc_inspect_all_real(void (*)(void*, void*, size_t, void*), void*);
386void dlmalloc_inspect_all(void (*handler)(void*, void*, size_t, void*), void* arg) {
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700387 dlmalloc_inspect_all_real(handler, arg);
388}
Elliott Hughescfd5a462015-12-04 15:57:51 -0800389int dlmalloc_trim_real(size_t);
390int dlmalloc_trim(size_t pad) {
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700391 return dlmalloc_trim_real(pad);
392}
393#endif
Elliott Hughesfb8fd502015-10-12 17:53:48 -0700394
395#endif // !defined(__BRILLO__)
Elliott Hughescfd5a462015-12-04 15:57:51 -0800396
397} // extern "C"