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.
30
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <pthread.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/resource.h>
41 #include <sys/syscall.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <unistd.h>
46 #include <wchar.h>
47
48 #include "private/bionic_macros.h"
49 #include "private/libc_logging.h"
50
51 extern "C" {
52
53 // LP64 doesn't need to support any legacy cruft.
54 #if !defined(__LP64__)
55
56 // These were accidentally declared in <unistd.h> because we stupidly used to inline
57 // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps.
58 unsigned int __page_size = PAGE_SIZE;
59 unsigned int __page_shift = 12;
60
61 // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
__wait4(pid_t pid,int * status,int options,struct rusage * rusage)62 pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
63 return wait4(pid, status, options, rusage);
64 }
65
66 // TODO: does anything still need this?
__open()67 int __open() {
68 abort();
69 }
70
71 // TODO: does anything still need this?
__get_tls()72 void** __get_tls() {
73 #include "private/__get_tls.h"
74 return __get_tls();
75 }
76
77 // This non-standard function was in our <string.h> for some reason.
memswap(void * m1,void * m2,size_t n)78 void memswap(void* m1, void* m2, size_t n) {
79 char* p = reinterpret_cast<char*>(m1);
80 char* p_end = p + n;
81 char* q = reinterpret_cast<char*>(m2);
82 while (p < p_end) {
83 char tmp = *p;
84 *p = *q;
85 *q = tmp;
86 p++;
87 q++;
88 }
89 }
90
pthread_attr_setstackaddr(pthread_attr_t *,void *)91 int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
92 // This was removed from POSIX.1-2008, and is not implemented on bionic.
93 // Needed for ABI compatibility with the NDK.
94 return ENOSYS;
95 }
96
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** stack_addr)97 int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
98 // This was removed from POSIX.1-2008.
99 // Needed for ABI compatibility with the NDK.
100 *stack_addr = (char*)attr->stack_base + attr->stack_size;
101 return 0;
102 }
103
104 // Non-standard cruft that should only ever have been in system/core/toolbox.
strtotimeval(const char * str,struct timeval * ts)105 char* strtotimeval(const char* str, struct timeval* ts) {
106 char* s;
107 ts->tv_sec = strtoumax(str, &s, 10);
108
109 long fractional_seconds = 0;
110 if (*s == '.') {
111 s++;
112 int count = 0;
113
114 // Read up to 6 digits (microseconds).
115 while (*s && isdigit(*s)) {
116 if (++count < 7) {
117 fractional_seconds = fractional_seconds*10 + (*s - '0');
118 }
119 s++;
120 }
121
122 for (; count < 6; count++) {
123 fractional_seconds *= 10;
124 }
125 }
126
127 ts->tv_usec = fractional_seconds;
128 return s;
129 }
130
digitval(int ch)131 static inline int digitval(int ch) {
132 unsigned d;
133
134 d = (unsigned)(ch - '0');
135 if (d < 10) return (int)d;
136
137 d = (unsigned)(ch - 'a');
138 if (d < 6) return (int)(d+10);
139
140 d = (unsigned)(ch - 'A');
141 if (d < 6) return (int)(d+10);
142
143 return -1;
144 }
145
146 // This non-standard function was in our <inttypes.h> for some reason.
strntoumax(const char * nptr,char ** endptr,int base,size_t n)147 uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
148 const unsigned char* p = (const unsigned char *)nptr;
149 const unsigned char* end = p + n;
150 int minus = 0;
151 uintmax_t v = 0;
152 int d;
153
154 while (p < end && isspace(*p)) {
155 p++;
156 }
157
158 if (p < end) {
159 char c = p[0];
160 if (c == '-' || c == '+') {
161 minus = (c == '-');
162 p++;
163 }
164 }
165
166 if (base == 0) {
167 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
168 p += 2;
169 base = 16;
170 } else if (p+1 < end && p[0] == '0') {
171 p += 1;
172 base = 8;
173 } else {
174 base = 10;
175 }
176 } else if (base == 16) {
177 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
178 p += 2;
179 }
180 }
181
182 while (p < end && (d = digitval(*p)) >= 0 && d < base) {
183 v = v*base + d;
184 p += 1;
185 }
186
187 if (endptr) {
188 *endptr = (char*) p;
189 }
190
191 return minus ? -v : v;
192 }
193
194 // This non-standard function was in our <inttypes.h> for some reason.
strntoimax(const char * nptr,char ** endptr,int base,size_t n)195 intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
196 return (intmax_t) strntoumax(nptr, endptr, base, n);
197 }
198
199 // POSIX calls this dprintf, but LP32 Android had fdprintf instead.
fdprintf(int fd,const char * fmt,...)200 int fdprintf(int fd, const char* fmt, ...) {
201 va_list ap;
202 va_start(ap, fmt);
203 int rc = vdprintf(fd, fmt, ap);
204 va_end(ap);
205 return rc;
206 }
207
208 // POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
vfdprintf(int fd,const char * fmt,va_list ap)209 int vfdprintf(int fd, const char* fmt, va_list ap) {
210 return vdprintf(fd, fmt, ap);
211 }
212
213 #define __futex_wake __real_futex_wake
214 #define __futex_wait __real_futex_wait
215 #include "private/bionic_futex.h"
216 #undef __futex_wake
217 #undef __futex_wait
218
219 // This used to be in <sys/atomics.h>.
__futex_wake(volatile void * ftx,int count)220 int __futex_wake(volatile void* ftx, int count) {
221 return __real_futex_wake(ftx, count);
222 }
223
224 // This used to be in <sys/atomics.h>.
__futex_wait(volatile void * ftx,int value,const struct timespec * timeout)225 int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
226 return __real_futex_wait(ftx, value, timeout);
227 }
228
229 // Unity's libmono uses this.
tkill(pid_t tid,int sig)230 int tkill(pid_t tid, int sig) {
231 return syscall(__NR_tkill, tid, sig);
232 }
233
234 // This was removed from POSIX 2008.
wcswcs(wchar_t * haystack,wchar_t * needle)235 wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
236 return wcsstr(haystack, needle);
237 }
238
239 // This was removed from POSIX 2008.
bsd_signal(int signum,sighandler_t handler)240 sighandler_t bsd_signal(int signum, sighandler_t handler) {
241 return signal(signum, handler);
242 }
243
244 // This was removed from POSIX 2008.
245 #undef bcopy
bcopy(const void * src,void * dst,size_t n)246 void bcopy(const void* src, void* dst, size_t n) {
247 memmove(dst, src, n);
248 }
249
250 // This was removed from POSIX 2008.
251 #undef bzero
bzero(void * dst,size_t n)252 void bzero(void* dst, size_t n) {
253 memset(dst, 0, n);
254 }
255
256 // sysv_signal() was never in POSIX.
257 extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags);
sysv_signal(int signum,sighandler_t handler)258 sighandler_t sysv_signal(int signum, sighandler_t handler) {
259 return _signal(signum, handler, SA_RESETHAND);
260 }
261
262 // This is a system call that was never in POSIX. Use readdir(3) instead.
263 int __getdents64(unsigned int, dirent*, unsigned int);
getdents(unsigned int fd,dirent * dirp,unsigned int count)264 int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
265 return __getdents64(fd, dirp, count);
266 }
267
268 // This is a BSDism that we never implemented correctly. Used by Firefox.
issetugid()269 int issetugid() {
270 return 0;
271 }
272
273 // This was removed from POSIX 2004.
wait3(int * status,int options,struct rusage * rusage)274 pid_t wait3(int* status, int options, struct rusage* rusage) {
275 return wait4(-1, status, options, rusage);
276 }
277
278 // This was removed from POSIX 2004.
getdtablesize()279 int getdtablesize() {
280 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
289 // A leaked BSD stdio implementation detail that's now a no-op.
__sinit()290 void __sinit() {}
291 int __sdidinit = 1;
292
293 // Only used by ftime, which was removed from POSIX 2008.
294 struct timeb {
295 time_t time;
296 unsigned short millitm;
297 short timezone;
298 short dstflag;
299 };
300
301 // This was removed from POSIX 2008.
ftime(struct timeb * tb)302 int ftime(struct timeb* tb) {
303 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
323 // This was removed from POSIX 2008.
index(const char * str,int ch)324 char* index(const char* str, int ch) {
325 return strchr(str, ch);
326 }
327
328 // This was removed from BSD.
arc4random_stir(void)329 void arc4random_stir(void) {
330 // The current implementation stirs itself as needed.
331 }
332
333 // This was removed from BSD.
arc4random_addrandom(unsigned char *,int)334 void arc4random_addrandom(unsigned char*, int) {
335 // The current implementation adds randomness as needed.
336 }
337
338 // 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.
dlmalloc_usable_size(void * ptr)341 size_t dlmalloc_usable_size(void* ptr) {
342 return malloc_usable_size(ptr);
343 }
344
345 // In L we added a public pthread_gettid_np, but some apps were using the private API.
__pthread_gettid(pthread_t t)346 pid_t __pthread_gettid(pthread_t t) {
347 return pthread_gettid_np(t);
348 }
349
350 // Older versions of apportable used dlmalloc directly instead of malloc,
351 // so export this compatibility shim that simply calls malloc.
dlmalloc(size_t size)352 void* dlmalloc(size_t size) {
353 return malloc(size);
354 }
355
356 #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.
__get_thread()360 pthread_internal_t* __get_thread() {
361 return __real_get_thread();
362 }
363
364 // This one exists only for the LP32 NDK and is not present anywhere else.
365 extern long __set_errno_internal(int);
__set_errno(int n)366 long __set_errno(int n) {
367 return __set_errno_internal(n);
368 }
369
370 // Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems
371 // that use dlmalloc, be consistent and export them everywhere.
dlmalloc_inspect_all(void (*)(void *,void *,size_t,void *),void *)372 void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) {
373 }
dlmalloc_trim(size_t)374 int dlmalloc_trim(size_t) {
375 return 0;
376 }
377
378 // LP32's <stdio.h> had putw (but not getw).
putw(int value,FILE * fp)379 int putw(int value, FILE* fp) {
380 return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : EOF;
381 }
382
383 #endif // !defined (__LP64__)
384
385 } // extern "C"
386