1567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes/* 2567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 3567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * All rights reserved. 4567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * 5567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * Redistribution and use in source and binary forms, with or without 6567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * modification, are permitted provided that the following conditions 7567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * are met: 8567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * * Redistributions of source code must retain the above copyright 9567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * notice, this list of conditions and the following disclaimer. 10567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * * Redistributions in binary form must reproduce the above copyright 11567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * notice, this list of conditions and the following disclaimer in 12567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * the documentation and/or other materials provided with the 13567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * distribution. 14567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * 15567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes * SUCH DAMAGE. 27567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes */ 28567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes 297fb680bfda059746c9bb8939ffaed600eecc3a29Yabin Cui// This file perpetuates the mistakes of the past. 30567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes 31efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes#include <ctype.h> 32d1ead2af8bf2f6bb801da272e3778f89efefe613Elliott Hughes#include <dirent.h> 334c5891d93dc151e2f07fc87ae967f23e6c3d6e0cElliott Hughes#include <errno.h> 34efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes#include <inttypes.h> 35a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle#include <pthread.h> 36205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert#include <signal.h> 37fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes#include <stdio.h> 38567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes#include <stdlib.h> 39891dedb935b50f1abb39eedfe33049a1e49283cbDavid 'Digit' Turner#include <string.h> 40567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes#include <sys/resource.h> 41bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes#include <sys/syscall.h> 42567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes#include <sys/time.h> 43567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes#include <sys/types.h> 44567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes#include <sys/wait.h> 45567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes#include <unistd.h> 46001f8f041bf21c53d4a0b919ef2c859b047bac0aDan Albert#include <wchar.h> 47567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes 487fb680bfda059746c9bb8939ffaed600eecc3a29Yabin Cui#include "private/libc_logging.h" 497fb680bfda059746c9bb8939ffaed600eecc3a29Yabin Cui 50cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesextern "C" { 51cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughes 52bc2e88a85d5fd3e59c041a667283378c1503a16cDimitry Ivanov// Brillo and LP64 don't need to support any legacy cruft. 53bc2e88a85d5fd3e59c041a667283378c1503a16cDimitry Ivanov#if !defined(__BRILLO__) && !defined(__LP64__) 547fb680bfda059746c9bb8939ffaed600eecc3a29Yabin Cui 55567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes// These were accidentally declared in <unistd.h> because we stupidly used to inline 56567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. 57cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesunsigned int __page_size = PAGE_SIZE; 58cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesunsigned int __page_shift = 12; 59567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes 60567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). 61cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughespid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { 62567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes return wait4(pid, status, options, rusage); 63567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes} 64567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes 65062092543fdbd6fa2676e4f5b37a15f7bba94332Elliott Hughes// TODO: does anything still need this? 66cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint __open() { 67567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes abort(); 68567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes} 69567a8de3ca5b4c4f033bfbcdd339a3fd59a76227Elliott Hughes 70062092543fdbd6fa2676e4f5b37a15f7bba94332Elliott Hughes// TODO: does anything still need this? 71cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid** __get_tls() { 72062092543fdbd6fa2676e4f5b37a15f7bba94332Elliott Hughes#include "private/__get_tls.h" 73062092543fdbd6fa2676e4f5b37a15f7bba94332Elliott Hughes return __get_tls(); 74062092543fdbd6fa2676e4f5b37a15f7bba94332Elliott Hughes} 75062092543fdbd6fa2676e4f5b37a15f7bba94332Elliott Hughes 76152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes// This non-standard function was in our <string.h> for some reason. 77cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid memswap(void* m1, void* m2, size_t n) { 78152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes char* p = reinterpret_cast<char*>(m1); 79152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes char* p_end = p + n; 80152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes char* q = reinterpret_cast<char*>(m2); 81152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes while (p < p_end) { 82152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes char tmp = *p; 83152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes *p = *q; 84152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes *q = tmp; 85152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes p++; 86152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes q++; 87152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes } 88152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes} 89152b9de19ade833ada124390ef153e53d3d3e2edElliott Hughes 90cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint pthread_attr_setstackaddr(pthread_attr_t*, void*) { 91a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle // This was removed from POSIX.1-2008, and is not implemented on bionic. 92a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle // Needed for ABI compatibility with the NDK. 93a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle return ENOSYS; 94a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle} 95a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle 96cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { 97a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle // This was removed from POSIX.1-2008. 98a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle // Needed for ABI compatibility with the NDK. 99a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle *stack_addr = (char*)attr->stack_base + attr->stack_size; 100a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle return 0; 101a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle} 102a4eafa6dbc98378f3fa759fec8590871e2b5ae29Calin Juravle 103efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes// Non-standard cruft that should only ever have been in system/core/toolbox. 104cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hugheschar* strtotimeval(const char* str, struct timeval* ts) { 105efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes char* s; 106efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes ts->tv_sec = strtoumax(str, &s, 10); 107efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes 108efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes long fractional_seconds = 0; 109efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes if (*s == '.') { 110efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes s++; 111efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes int count = 0; 112efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes 113efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes // Read up to 6 digits (microseconds). 114efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes while (*s && isdigit(*s)) { 115efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes if (++count < 7) { 116efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes fractional_seconds = fractional_seconds*10 + (*s - '0'); 117efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes } 118efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes s++; 119efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes } 120efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes 121efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes for (; count < 6; count++) { 122efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes fractional_seconds *= 10; 123efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes } 124efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes } 125efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes 126efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes ts->tv_usec = fractional_seconds; 127efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes return s; 128efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes} 129efbdb53f84f4e1faf38f3c1a4cb60a83b9885ef4Elliott Hughes 130eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughesstatic inline int digitval(int ch) { 131eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes unsigned d; 132eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 133eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes d = (unsigned)(ch - '0'); 134eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (d < 10) return (int)d; 135eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 136eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes d = (unsigned)(ch - 'a'); 137eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (d < 6) return (int)(d+10); 138eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 139eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes d = (unsigned)(ch - 'A'); 140eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (d < 6) return (int)(d+10); 141eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 142eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes return -1; 143eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes} 144eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 145eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes// This non-standard function was in our <inttypes.h> for some reason. 146cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesuintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { 147eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes const unsigned char* p = (const unsigned char *)nptr; 148eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes const unsigned char* end = p + n; 149eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes int minus = 0; 150eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes uintmax_t v = 0; 151eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes int d; 152eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 153eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes while (p < end && isspace(*p)) { 154eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes p++; 155eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 156eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 157eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (p < end) { 158eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes char c = p[0]; 159eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (c == '-' || c == '+') { 160eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes minus = (c == '-'); 161eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes p++; 162eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 163eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 164eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 165eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (base == 0) { 166eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 167eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes p += 2; 168eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes base = 16; 169eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } else if (p+1 < end && p[0] == '0') { 170eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes p += 1; 171eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes base = 8; 172eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } else { 173eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes base = 10; 174eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 175eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } else if (base == 16) { 176eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 177eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes p += 2; 178eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 179eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 180eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 181eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes while (p < end && (d = digitval(*p)) >= 0 && d < base) { 182eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes v = v*base + d; 183eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes p += 1; 184eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 185eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 186eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes if (endptr) { 187eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes *endptr = (char*) p; 188eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes } 189eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 190eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes return minus ? -v : v; 191eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes} 192eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 193eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes// This non-standard function was in our <inttypes.h> for some reason. 194cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesintmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { 195eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes return (intmax_t) strntoumax(nptr, endptr, base, n); 196eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes} 197eae5902e73dc4381811e08fd2334bf4a9300a928Elliott Hughes 198fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes// POSIX calls this dprintf, but LP32 Android had fdprintf instead. 199cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint fdprintf(int fd, const char* fmt, ...) { 200fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes va_list ap; 201fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes va_start(ap, fmt); 202fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes int rc = vdprintf(fd, fmt, ap); 203fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes va_end(ap); 204fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes return rc; 205fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes} 206fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes 207fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes// POSIX calls this vdprintf, but LP32 Android had fdprintf instead. 208cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint vfdprintf(int fd, const char* fmt, va_list ap) { 209fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes return vdprintf(fd, fmt, ap); 210fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes} 211fcac8ff97f6b6cced6546e0096cadc1039b68b5eElliott Hughes 212b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#define __futex_wake __real_futex_wake 213b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#define __futex_wait __real_futex_wait 214b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#include "private/bionic_futex.h" 215b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#undef __futex_wake 216b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#undef __futex_wait 217bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes 218bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes// This used to be in <sys/atomics.h>. 219cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint __futex_wake(volatile void* ftx, int count) { 220b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes return __real_futex_wake(ftx, count); 221bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes} 222bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes 223bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes// This used to be in <sys/atomics.h>. 224cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) { 225b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes return __real_futex_wait(ftx, value, timeout); 226bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes} 227bd3a98c6b9850a8e55fb0e0ed9f045212c494881Elliott Hughes 228001707363ddee680d466c88dd330900683dadf63Anthony King// Unity's libmono uses this. 229cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint tkill(pid_t tid, int sig) { 230001707363ddee680d466c88dd330900683dadf63Anthony King return syscall(__NR_tkill, tid, sig); 231001707363ddee680d466c88dd330900683dadf63Anthony King} 232001707363ddee680d466c88dd330900683dadf63Anthony King 2331628eb1d43008c52b00ec98d10c4fd9e8b6fc7c5Elliott Hughes// This was removed from POSIX 2008. 234cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hugheswchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { 235001f8f041bf21c53d4a0b919ef2c859b047bac0aDan Albert return wcsstr(haystack, needle); 236001f8f041bf21c53d4a0b919ef2c859b047bac0aDan Albert} 237001f8f041bf21c53d4a0b919ef2c859b047bac0aDan Albert 238205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert// This was removed from POSIX 2008. 239cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughessighandler_t bsd_signal(int signum, sighandler_t handler) { 240205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert return signal(signum, handler); 241205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert} 242205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert 2431edfd9e36acff1824c5e8dcfbdc0580dcc74e1bbElliott Hughes#if !defined(__i386__) 24476f8916b904db14facf811ae44e1265261349702Elliott Hughes// This was removed from POSIX 2008. 24576f8916b904db14facf811ae44e1265261349702Elliott Hughes#undef bcopy 246cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid bcopy(const void* src, void* dst, size_t n) { 247d51a0b0f9da767d5d67c9f3565c34afcaab1a908Rohit Agrawal memmove(dst, src, n); 24876f8916b904db14facf811ae44e1265261349702Elliott Hughes} 2491edfd9e36acff1824c5e8dcfbdc0580dcc74e1bbElliott Hughes#else 2501edfd9e36acff1824c5e8dcfbdc0580dcc74e1bbElliott Hughes// x86 has an assembler implementation. 2511edfd9e36acff1824c5e8dcfbdc0580dcc74e1bbElliott Hughes#endif 25276f8916b904db14facf811ae44e1265261349702Elliott Hughes 253205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert// sysv_signal() was never in POSIX. 254cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesextern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags); 255cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughessighandler_t sysv_signal(int signum, sighandler_t handler) { 256205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert return _signal(signum, handler, SA_RESETHAND); 257205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert} 258205dd7d9b3ba314ef8601e4613823c2d62a0605bDan Albert 2593d5cb30d23cfc6a72f01c00246e69a2c614c8228Elliott Hughes// This is a system call that was never in POSIX. Use readdir(3) instead. 260cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint __getdents64(unsigned int, dirent*, unsigned int); 261cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint getdents(unsigned int fd, dirent* dirp, unsigned int count) { 2623d5cb30d23cfc6a72f01c00246e69a2c614c8228Elliott Hughes return __getdents64(fd, dirp, count); 2633d5cb30d23cfc6a72f01c00246e69a2c614c8228Elliott Hughes} 2643d5cb30d23cfc6a72f01c00246e69a2c614c8228Elliott Hughes 265bffbfeed7a595dcbe5843a77d84c409a0225b4e1Elliott Hughes// This is a BSDism that we never implemented correctly. Used by Firefox. 266cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint issetugid() { 267bffbfeed7a595dcbe5843a77d84c409a0225b4e1Elliott Hughes return 0; 268bffbfeed7a595dcbe5843a77d84c409a0225b4e1Elliott Hughes} 269bffbfeed7a595dcbe5843a77d84c409a0225b4e1Elliott Hughes 2708229ae46f4fb165a18da4dca1be74c4c099c3ee7Dan Albert// This was removed from POSIX 2004. 271cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughespid_t wait3(int* status, int options, struct rusage* rusage) { 2728229ae46f4fb165a18da4dca1be74c4c099c3ee7Dan Albert return wait4(-1, status, options, rusage); 2738229ae46f4fb165a18da4dca1be74c4c099c3ee7Dan Albert} 2748229ae46f4fb165a18da4dca1be74c4c099c3ee7Dan Albert 275462abab12b074c62c0999859e65d5a32ebb41951Dan Albert// This was removed from POSIX 2004. 276cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint getdtablesize() { 277462abab12b074c62c0999859e65d5a32ebb41951Dan Albert struct rlimit r; 278462abab12b074c62c0999859e65d5a32ebb41951Dan Albert 279462abab12b074c62c0999859e65d5a32ebb41951Dan Albert if (getrlimit(RLIMIT_NOFILE, &r) < 0) { 280462abab12b074c62c0999859e65d5a32ebb41951Dan Albert return sysconf(_SC_OPEN_MAX); 281462abab12b074c62c0999859e65d5a32ebb41951Dan Albert } 282462abab12b074c62c0999859e65d5a32ebb41951Dan Albert 283462abab12b074c62c0999859e65d5a32ebb41951Dan Albert return r.rlim_cur; 284462abab12b074c62c0999859e65d5a32ebb41951Dan Albert} 285462abab12b074c62c0999859e65d5a32ebb41951Dan Albert 286bb46afd6c44a847efe96e30d72708fd2d0906e8cElliott Hughes// A leaked BSD stdio implementation detail that's now a no-op. 287bb46afd6c44a847efe96e30d72708fd2d0906e8cElliott Hughesvoid __sinit() {} 288bb46afd6c44a847efe96e30d72708fd2d0906e8cElliott Hughesint __sdidinit = 1; 289bb46afd6c44a847efe96e30d72708fd2d0906e8cElliott Hughes 2901628eb1d43008c52b00ec98d10c4fd9e8b6fc7c5Elliott Hughes// Only used by ftime, which was removed from POSIX 2008. 291ac6467587e864d199377f14281da4641f979a68aDan Albertstruct timeb { 292ac6467587e864d199377f14281da4641f979a68aDan Albert time_t time; 293ac6467587e864d199377f14281da4641f979a68aDan Albert unsigned short millitm; 294ac6467587e864d199377f14281da4641f979a68aDan Albert short timezone; 295ac6467587e864d199377f14281da4641f979a68aDan Albert short dstflag; 296ac6467587e864d199377f14281da4641f979a68aDan Albert}; 297ac6467587e864d199377f14281da4641f979a68aDan Albert 298ac6467587e864d199377f14281da4641f979a68aDan Albert// This was removed from POSIX 2008. 299cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint ftime(struct timeb* tb) { 300ac6467587e864d199377f14281da4641f979a68aDan Albert struct timeval tv; 301ac6467587e864d199377f14281da4641f979a68aDan Albert struct timezone tz; 302ac6467587e864d199377f14281da4641f979a68aDan Albert 303ac6467587e864d199377f14281da4641f979a68aDan Albert if (gettimeofday(&tv, &tz) < 0) 304ac6467587e864d199377f14281da4641f979a68aDan Albert return -1; 305ac6467587e864d199377f14281da4641f979a68aDan Albert 306ac6467587e864d199377f14281da4641f979a68aDan Albert tb->time = tv.tv_sec; 307ac6467587e864d199377f14281da4641f979a68aDan Albert tb->millitm = (tv.tv_usec + 500) / 1000; 308ac6467587e864d199377f14281da4641f979a68aDan Albert 309ac6467587e864d199377f14281da4641f979a68aDan Albert if (tb->millitm == 1000) { 310ac6467587e864d199377f14281da4641f979a68aDan Albert ++tb->time; 311ac6467587e864d199377f14281da4641f979a68aDan Albert tb->millitm = 0; 312ac6467587e864d199377f14281da4641f979a68aDan Albert } 313ac6467587e864d199377f14281da4641f979a68aDan Albert 314ac6467587e864d199377f14281da4641f979a68aDan Albert tb->timezone = tz.tz_minuteswest; 315ac6467587e864d199377f14281da4641f979a68aDan Albert tb->dstflag = tz.tz_dsttime; 316ac6467587e864d199377f14281da4641f979a68aDan Albert 317ac6467587e864d199377f14281da4641f979a68aDan Albert return 0; 318ac6467587e864d199377f14281da4641f979a68aDan Albert} 319ac6467587e864d199377f14281da4641f979a68aDan Albert 320891dedb935b50f1abb39eedfe33049a1e49283cbDavid 'Digit' Turner// This was removed from POSIX 2008. 321cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hugheschar* index(const char* str, int ch) { 322891dedb935b50f1abb39eedfe33049a1e49283cbDavid 'Digit' Turner return strchr(str, ch); 323891dedb935b50f1abb39eedfe33049a1e49283cbDavid 'Digit' Turner} 324891dedb935b50f1abb39eedfe33049a1e49283cbDavid 'Digit' Turner 3255dea47221a116aa87b0c286230c944ed6b629636Elliott Hughes// This was removed from BSD. 326cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid arc4random_stir(void) { 3275dea47221a116aa87b0c286230c944ed6b629636Elliott Hughes // The current implementation stirs itself as needed. 3285dea47221a116aa87b0c286230c944ed6b629636Elliott Hughes} 3295dea47221a116aa87b0c286230c944ed6b629636Elliott Hughes 330fc829736e1f399a2b82058b24bb94f6bdab22469Elliott Hughes// This was removed from BSD. 331cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid arc4random_addrandom(unsigned char*, int) { 332fc829736e1f399a2b82058b24bb94f6bdab22469Elliott Hughes // The current implementation adds randomness as needed. 333fc829736e1f399a2b82058b24bb94f6bdab22469Elliott Hughes} 334fc829736e1f399a2b82058b24bb94f6bdab22469Elliott Hughes 335f90355844663b85dd57c3adcd9e92c2ee6850412Christopher Ferris// Old versions of the NDK did not export malloc_usable_size, but did 336f90355844663b85dd57c3adcd9e92c2ee6850412Christopher Ferris// export dlmalloc_usable_size. We are moving away from dlmalloc in L 337f90355844663b85dd57c3adcd9e92c2ee6850412Christopher Ferris// so make this call malloc_usable_size. 338cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughessize_t dlmalloc_usable_size(void* ptr) { 339f90355844663b85dd57c3adcd9e92c2ee6850412Christopher Ferris return malloc_usable_size(ptr); 340f90355844663b85dd57c3adcd9e92c2ee6850412Christopher Ferris} 341f90355844663b85dd57c3adcd9e92c2ee6850412Christopher Ferris 3420f001b67fea27f8d60afc4677e8970d32a4e3792Elliott Hughes// In L we added a public pthread_gettid_np, but some apps were using the private API. 343bba395492a0bb6ee72d0ad8e4d468e852392220eDimitry Ivanovpid_t __pthread_gettid(pthread_t t) { 3440f001b67fea27f8d60afc4677e8970d32a4e3792Elliott Hughes return pthread_gettid_np(t); 3450f001b67fea27f8d60afc4677e8970d32a4e3792Elliott Hughes} 3460f001b67fea27f8d60afc4677e8970d32a4e3792Elliott Hughes 347f9554a17765fd91d97d1f74913a626f01e880ceeChristopher Ferris// Older versions of apportable used dlmalloc directly instead of malloc, 348f183f95946c699924d8ce6c0ee723bf7f4fdfe85Christopher Ferris// so export this compatibility shim that simply calls malloc. 349cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid* dlmalloc(size_t size) { 350f183f95946c699924d8ce6c0ee723bf7f4fdfe85Christopher Ferris return malloc(size); 351f183f95946c699924d8ce6c0ee723bf7f4fdfe85Christopher Ferris} 352f183f95946c699924d8ce6c0ee723bf7f4fdfe85Christopher Ferris 3532f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui#define __get_thread __real_get_thread 3542f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui#include "pthread_internal.h" 3552f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui#undef __get_thread 3562f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui// Various third-party apps contain a backport of our pthread_rwlock implementation that uses this. 357cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughespthread_internal_t* __get_thread() { 3582f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui return __real_get_thread(); 3592f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui} 3602f836d4989845c0c82a1e4f99206fb0ff0d137a2Yabin Cui 3619978a9a82e3b883b32a7a0bcd5cac18f31e7a68dChristopher Ferris// This one exists only for the LP32 NDK and is not present anywhere else. 362cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesextern long __set_errno_internal(int); 363cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hugheslong __set_errno(int n) { 3649978a9a82e3b883b32a7a0bcd5cac18f31e7a68dChristopher Ferris return __set_errno_internal(n); 3659978a9a82e3b883b32a7a0bcd5cac18f31e7a68dChristopher Ferris} 3669978a9a82e3b883b32a7a0bcd5cac18f31e7a68dChristopher Ferris 367d7c7daada276276e77446c099a67dd3ea15ea72eElliott Hughes// This was never implemented in bionic, only needed for ABI compatibility with the NDK. 368d7c7daada276276e77446c099a67dd3ea15ea72eElliott Hughes// In the M time frame, over 1000 apps have a reference to this! 369cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid endpwent() { } 370f9554a17765fd91d97d1f74913a626f01e880ceeChristopher Ferris 371f9554a17765fd91d97d1f74913a626f01e880ceeChristopher Ferris// Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems 372f9554a17765fd91d97d1f74913a626f01e880ceeChristopher Ferris// that use dlmalloc, be consistent and export them everywhere. 373cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesvoid dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) { 374f9554a17765fd91d97d1f74913a626f01e880ceeChristopher Ferris} 375cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughesint dlmalloc_trim(size_t) { 376fb8fd5076ea7ccc77960346e52b81134ccbb9ea3Elliott Hughes return 0; 377fb8fd5076ea7ccc77960346e52b81134ccbb9ea3Elliott Hughes} 378fb8fd5076ea7ccc77960346e52b81134ccbb9ea3Elliott Hughes 379bc2e88a85d5fd3e59c041a667283378c1503a16cDimitry Ivanov#endif // !defined(__BRILLO__) && !defined (__LP64__) 380cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughes 381cfd5a46b092536905ffe45a19cfe1b9e35aa8d10Elliott Hughes} // extern "C" 382