ndk_cruft.cpp revision bffbfeed7a595dcbe5843a77d84c409a0225b4e1
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
32#include <ctype.h>
33#include <dirent.h>
34#include <inttypes.h>
35#include <pthread.h>
36#include <signal.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <sys/resource.h>
40#include <sys/syscall.h>
41#include <sys/time.h>
42#include <sys/types.h>
43#include <sys/wait.h>
44#include <unistd.h>
45#include <wchar.h>
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.
49extern "C" {
50  unsigned int __page_size = PAGE_SIZE;
51  unsigned int __page_shift = 12;
52}
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
59// TODO: does anything still need this?
60extern "C" int __open() {
61  abort();
62}
63
64// TODO: does anything still need this?
65extern "C" void** __get_tls() {
66#include "private/__get_tls.h"
67  return __get_tls();
68}
69
70// 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
84extern "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
97// 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
124static 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
192// 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
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
211
212// This used to be in <sys/atomics.h>.
213extern "C" int __futex_wake(volatile void* ftx, int count) {
214  return __real_futex_wake(ftx, count);
215}
216
217// This used to be in <sys/atomics.h>.
218extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
219  return __real_futex_wait(ftx, value, timeout);
220}
221
222// Unity's libmono uses this.
223extern "C" int tkill(pid_t tid, int sig) {
224  return syscall(__NR_tkill, tid, sig);
225}
226
227extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
228  return wcsstr(haystack, needle);
229}
230
231// 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
242// This is a system call that was never in POSIX. Use readdir(3) instead.
243extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
244extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
245  return __getdents64(fd, dirp, count);
246}
247
248// This is a BSDism that we never implemented correctly. Used by Firefox.
249extern "C" int issetugid() {
250  return 0;
251}
252
253#endif
254