diff options
author | Elliott Hughes <enh@google.com> | 2013-11-20 16:09:06 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2013-11-20 16:24:16 -0800 |
commit | ab61eb366ac48addf2bca6093a34455193f5c8df (patch) | |
tree | 335fe463aa773ac8484c40e955fec90f126c9e4f /libc | |
parent | 39bc7ecd45700e9edac0b29d33cc211383ebb107 (diff) | |
download | bionic-ab61eb366ac48addf2bca6093a34455193f5c8df.zip bionic-ab61eb366ac48addf2bca6093a34455193f5c8df.tar.gz bionic-ab61eb366ac48addf2bca6093a34455193f5c8df.tar.bz2 |
Switch to upstream sleep(3) and usleep(3).
Also fix the signature of usleep, and the definition of useconds_t which
should be unsigned, as the 'u' in its name implies.
This patch also cleans up the existing FreeBSD hacks by moving the libm
stuff from <sys/cdefs.h> to a libm-private header, and adding comments
about the hacks we use to build FreeBSD source.
Change-Id: Ibe5067a380502df94a0a3a7901969b35411085b6
Diffstat (limited to 'libc')
-rw-r--r-- | libc/Android.mk | 4 | ||||
-rw-r--r-- | libc/bionic/sleep.c | 52 | ||||
-rw-r--r-- | libc/bionic/usleep.c | 55 | ||||
-rw-r--r-- | libc/include/sys/cdefs.h | 7 | ||||
-rw-r--r-- | libc/include/sys/types.h | 9 | ||||
-rw-r--r-- | libc/include/unistd.h | 2 | ||||
-rw-r--r-- | libc/upstream-freebsd/freebsd-compat.h | 22 | ||||
-rw-r--r-- | libc/upstream-freebsd/lib/libc/gen/sleep.c | 67 | ||||
-rw-r--r-- | libc/upstream-freebsd/lib/libc/gen/usleep.c | 52 |
9 files changed, 148 insertions, 122 deletions
diff --git a/libc/Android.mk b/libc/Android.mk index 551c633..af96503 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 aafbd60..0000000 --- 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 <time.h> -#include <errno.h> - -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 19e8ee8..0000000 --- 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 <time.h> -#include <errno.h> - -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 175c28b..c7f2ac7 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 bb4a9d4..d793f81 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 91b0d8d..29758f5 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 8030eea..74dc679 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 0000000..b807c2d --- /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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include "namespace.h" +#include <errno.h> +#include <limits.h> +#include <time.h> +#include <unistd.h> +#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 0000000..7d6559b --- /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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include "namespace.h" +#include <time.h> +#include <unistd.h> +#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); |