sleep.c revision 277317
134689Sbde/* 250476Speter * Copyright (c) 1989, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 4156813Sru * 5156813Sru * Redistribution and use in source and binary forms, with or without 634689Sbde * modification, are permitted provided that the following conditions 734689Sbde * are met: 834689Sbde * 1. Redistributions of source code must retain the above copyright 938752Sbde * notice, this list of conditions and the following disclaimer. 10173017Sru * 2. Redistributions in binary form must reproduce the above copyright 11228989Srwatson * notice, this list of conditions and the following disclaimer in the 12204738Simp * documentation and/or other materials provided with the distribution. 13204738Simp * 4. Neither the name of the University nor the names of its contributors 1481133Stmm * may be used to endorse or promote products derived from this software 1559897Sjoe * without specific prior written permission. 1679471Smarkm * 17166131Srafan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18122568Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1959353Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2041257Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2182355Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2294690Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2341257Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2456081Sbde * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2594690Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26181344Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2734689Sbde * SUCH DAMAGE. 2834689Sbde */ 29204738Simp 30204738Simp#if defined(LIBC_SCCS) && !defined(lint) 3134689Sbdestatic char sccsid[] = "@(#)sleep.c 8.1 (Berkeley) 6/4/93"; 32205113Simp#endif /* LIBC_SCCS and not lint */ 33205113Simp#include <sys/cdefs.h> 34205113Simp__FBSDID("$FreeBSD: stable/10/lib/libc/gen/sleep.c 277317 2015-01-18 11:54:20Z kib $"); 35205113Simp 36215127Sed#include "namespace.h" 37205113Simp#include <errno.h> 38205113Simp#include <limits.h> 39219019Sgabor#include <time.h> 40205113Simp#include <unistd.h> 41205113Simp#include "un-namespace.h" 42205113Simp 43205113Simp#include "libc_private.h" 44205113Simp 45205113Simpunsigned int 46205113Simp__sleep(unsigned int seconds) 47205113Simp{ 48205113Simp struct timespec time_to_sleep; 49205113Simp struct timespec time_remaining; 50227987Sdim 51227987Sdim /* 52227987Sdim * Avoid overflow when `seconds' is huge. This assumes that 53205113Simp * the maximum value for a time_t is >= INT_MAX. 54233337Sstas */ 55233337Sstas if (seconds > INT_MAX) 56233337Sstas return (seconds - INT_MAX + __sleep(INT_MAX)); 57233337Sstas 58205113Simp time_to_sleep.tv_sec = seconds; 59205113Simp time_to_sleep.tv_nsec = 0; 60205113Simp if (((int (*)(const struct timespec *, struct timespec *)) 61205113Simp __libc_interposing[INTERPOS_nanosleep])( 62205113Simp &time_to_sleep, &time_remaining) != -1) 63215127Sed return (0); 64205113Simp if (errno != EINTR) 65205113Simp return (seconds); /* best guess */ 66205113Simp return (time_remaining.tv_sec + 67205113Simp (time_remaining.tv_nsec != 0)); /* round up */ 68205113Simp} 69205113Simp 70205113Simp__weak_reference(__sleep, sleep); 71205113Simp__weak_reference(__sleep, _sleep); 72205113Simp