diff --git a/libc/Android.mk b/libc/Android.mk index 551c6338d..af9650378 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -120,7 +120,6 @@ libc_common_src_files := \ bionic/siglist.c \ bionic/signame.c \ bionic/sigsetmask.c \ - bionic/sleep.c \ bionic/strndup.c \ bionic/strntoimax.c \ bionic/strntoumax.c \ @@ -132,7 +131,6 @@ libc_common_src_files := \ bionic/time64.c \ bionic/umount.c \ bionic/unlockpt.c \ - bionic/usleep.c \ bionic/utmp.c \ bionic/wcscoll.c \ @@ -310,6 +308,8 @@ libc_tzcode_src_files := \ tzcode/strptime.c \ libc_upstream_freebsd_src_files := \ + upstream-freebsd/lib/libc/gen/sleep.c \ + upstream-freebsd/lib/libc/gen/usleep.c \ upstream-freebsd/lib/libc/stdio/clrerr.c \ upstream-freebsd/lib/libc/stdio/fclose.c \ upstream-freebsd/lib/libc/stdio/fdopen.c \ diff --git a/libc/bionic/sleep.c b/libc/bionic/sleep.c deleted file mode 100644 index aafbd60f6..000000000 --- a/libc/bionic/sleep.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#include -#include - -unsigned int sleep(unsigned int seconds) -{ - struct timespec t; - - /* seconds is unsigned, while t.tv_sec is signed - * some people want to do sleep(UINT_MAX), so fake - * support for it by only sleeping 2 billion seconds - */ - if ((int)seconds < 0) - seconds = 0x7fffffff; - - t.tv_sec = seconds; - t.tv_nsec = 0; - - if ( !nanosleep( &t, &t ) ) - return 0; - - if ( errno == EINTR ) - return t.tv_sec; - - return -1; -} diff --git a/libc/bionic/usleep.c b/libc/bionic/usleep.c deleted file mode 100644 index 19e8ee892..000000000 --- a/libc/bionic/usleep.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#include -#include - -int usleep(unsigned long usec) -{ - struct timespec ts; - - ts.tv_sec = usec/1000000UL; - -#ifdef __arm__ - /* avoid divisions and modulos on the ARM */ - ts.tv_nsec = (usec - ts.tv_sec*1000000UL)*1000; -#else - ts.tv_nsec = (usec % 1000000UL) * 1000UL; -#endif - - for (;;) - { - if ( nanosleep( &ts, &ts ) == 0 ) - return 0; - - // We try again if the nanosleep failure is EINTR. - // The other possible failures are EINVAL (which we should pass through), - // and ENOSYS, which doesn't happen. - if ( errno != EINTR ) - return -1; - } -} diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h index 175c28b04..c7f2ac7ac 100644 --- a/libc/include/sys/cdefs.h +++ b/libc/include/sys/cdefs.h @@ -550,11 +550,4 @@ #endif #define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1) -/* Android-added: for FreeBSD's libm. */ -#define __weak_reference(sym,alias) \ - __asm__(".weak " #alias); \ - __asm__(".equ " #alias ", " #sym) -#define __strong_reference(sym,aliassym) \ - extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym))) - #endif /* !_SYS_CDEFS_H_ */ diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h index bb4a9d433..d793f8197 100644 --- a/libc/include/sys/types.h +++ b/libc/include/sys/types.h @@ -55,6 +55,7 @@ typedef __kernel_dev_t dev_t; typedef __kernel_fsblkcnt_t fsblkcnt_t; typedef __kernel_fsfilcnt_t fsfilcnt_t; typedef __kernel_gid32_t gid_t; +typedef __kernel_uid32_t uid_t; typedef __kernel_id_t id_t; typedef __kernel_ino_t ino_t; typedef __kernel_key_t key_t; @@ -94,10 +95,10 @@ typedef .... pthread_t; typedef __kernel_ssize_t ssize_t; #endif -typedef __kernel_suseconds_t suseconds_t; -typedef __kernel_time_t time_t; -typedef __kernel_uid32_t uid_t; -typedef signed long useconds_t; +typedef __kernel_time_t time_t; + +typedef __kernel_suseconds_t suseconds_t; +typedef unsigned long useconds_t; typedef __kernel_daddr_t daddr_t; typedef __kernel_timer_t timer_t; diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 91b0d8d45..29758f5de 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -159,7 +159,7 @@ extern int ftruncate64(int, off64_t); extern int pause(void); extern unsigned int alarm(unsigned int); extern unsigned int sleep(unsigned int); -extern int usleep(unsigned long); +extern int usleep(useconds_t); extern int gethostname(char *, size_t); diff --git a/libc/upstream-freebsd/freebsd-compat.h b/libc/upstream-freebsd/freebsd-compat.h index 8030eeaef..74dc679c3 100644 --- a/libc/upstream-freebsd/freebsd-compat.h +++ b/libc/upstream-freebsd/freebsd-compat.h @@ -20,11 +20,31 @@ #define __USE_BSD #define REPLACE_GETOPT +/* + * FreeBSD's libc has three symbols for every symbol: + * + * __f will be the actual implementation. + * _f will be a weak reference to __f (used for calls to f from within the library). + * f will be a weak reference to __f (used for calls to f from outside the library). + * + * We collapse this into just the one symbol, f. + */ + +/* Prevent weak reference generation. */ +#define __weak_reference(sym,alias) + +/* Ensure that the implementation itself gets the underscore-free name. */ +#define __sleep sleep +#define __usleep usleep + +/* Redirect internal C library calls to the public function. */ #define _close close #define _fcntl fcntl #define _fstat fstat +#define _nanosleep nanosleep #define _open open -#define _sseek __sseek /* Needed as long as we have a mix of OpenBSD and FreeBSD stdio. */ +/* This one is only needed as long as we have a mix of OpenBSD and FreeBSD stdio. */ +#define _sseek __sseek #endif diff --git a/libc/upstream-freebsd/lib/libc/gen/sleep.c b/libc/upstream-freebsd/lib/libc/gen/sleep.c new file mode 100644 index 000000000..b807c2d1d --- /dev/null +++ b/libc/upstream-freebsd/lib/libc/gen/sleep.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)sleep.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ +#include +__FBSDID("$FreeBSD$"); + +#include "namespace.h" +#include +#include +#include +#include +#include "un-namespace.h" + +unsigned int +__sleep(unsigned int seconds) +{ + struct timespec time_to_sleep; + struct timespec time_remaining; + + /* + * Avoid overflow when `seconds' is huge. This assumes that + * the maximum value for a time_t is >= INT_MAX. + */ + if (seconds > INT_MAX) + return (seconds - INT_MAX + __sleep(INT_MAX)); + + time_to_sleep.tv_sec = seconds; + time_to_sleep.tv_nsec = 0; + if (_nanosleep(&time_to_sleep, &time_remaining) != -1) + return (0); + if (errno != EINTR) + return (seconds); /* best guess */ + return (time_remaining.tv_sec + + (time_remaining.tv_nsec != 0)); /* round up */ +} + +__weak_reference(__sleep, sleep); +__weak_reference(__sleep, _sleep); diff --git a/libc/upstream-freebsd/lib/libc/gen/usleep.c b/libc/upstream-freebsd/lib/libc/gen/usleep.c new file mode 100644 index 000000000..7d6559be2 --- /dev/null +++ b/libc/upstream-freebsd/lib/libc/gen/usleep.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)usleep.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ +#include +__FBSDID("$FreeBSD$"); + +#include "namespace.h" +#include +#include +#include "un-namespace.h" + +int +__usleep(useconds_t useconds) +{ + struct timespec time_to_sleep; + + time_to_sleep.tv_nsec = (useconds % 1000000) * 1000; + time_to_sleep.tv_sec = useconds / 1000000; + return (_nanosleep(&time_to_sleep, NULL)); +} + +__weak_reference(__usleep, usleep); +__weak_reference(__usleep, _usleep); diff --git a/libm/Android.mk b/libm/Android.mk index 1d87dc23e..39e556343 100644 --- a/libm/Android.mk +++ b/libm/Android.mk @@ -217,7 +217,11 @@ libm_common_src_files += fake_long_double.c # TODO: re-enable i387/e_sqrtf.S for x86, and maybe others. -libm_common_cflags := -DFLT_EVAL_METHOD=0 -std=c99 +libm_common_cflags := \ + -DFLT_EVAL_METHOD=0 \ + -std=c99 \ + -include $(LOCAL_PATH)/freebsd-compat.h \ + libm_common_includes := $(LOCAL_PATH)/upstream-freebsd/lib/msun/src/ libm_arm_includes := $(LOCAL_PATH)/arm diff --git a/libm/freebsd-compat.h b/libm/freebsd-compat.h new file mode 100644 index 000000000..ca5b42cfe --- /dev/null +++ b/libm/freebsd-compat.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BIONIC_LIBM_FREEBSD_COMPAT_H_included +#define _BIONIC_LIBM_FREEBSD_COMPAT_H_included + +#define __weak_reference(sym,alias) \ + __asm__(".weak " #alias); \ + __asm__(".equ " #alias ", " #sym) + +#define __strong_reference(sym,aliassym) \ + extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym))) + +#endif