xref: /freebsd/lib/libc/gen/sleep.c (revision 7dd9070e4425a1c2e2418694fd632425a283d558)
17d233b22SKonstantin Belousov /*-
27d233b22SKonstantin Belousov  * SPDX-License-Identifier: BSD-3-Clause
37d233b22SKonstantin Belousov  *
47d233b22SKonstantin Belousov  * Copyright (c) 1989, 1993
57d233b22SKonstantin Belousov  *	The Regents of the University of California.  All rights reserved.
67d233b22SKonstantin Belousov  *
77d233b22SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
87d233b22SKonstantin Belousov  * modification, are permitted provided that the following conditions
97d233b22SKonstantin Belousov  * are met:
107d233b22SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
117d233b22SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
127d233b22SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
137d233b22SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
147d233b22SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
157d233b22SKonstantin Belousov  * 3. Neither the name of the University nor the names of its contributors
167d233b22SKonstantin Belousov  *    may be used to endorse or promote products derived from this software
177d233b22SKonstantin Belousov  *    without specific prior written permission.
187d233b22SKonstantin Belousov  *
197d233b22SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
207d233b22SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217d233b22SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227d233b22SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237d233b22SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247d233b22SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257d233b22SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267d233b22SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277d233b22SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287d233b22SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297d233b22SKonstantin Belousov  * SUCH DAMAGE.
307d233b22SKonstantin Belousov  */
317d233b22SKonstantin Belousov 
327d233b22SKonstantin Belousov #include "namespace.h"
337d233b22SKonstantin Belousov #include <errno.h>
347d233b22SKonstantin Belousov #include <limits.h>
357d233b22SKonstantin Belousov #include <time.h>
367d233b22SKonstantin Belousov #include <unistd.h>
377d233b22SKonstantin Belousov #include "un-namespace.h"
387d233b22SKonstantin Belousov 
397d233b22SKonstantin Belousov #include "libc_private.h"
407d233b22SKonstantin Belousov 
417d233b22SKonstantin Belousov unsigned int __sleep(unsigned int);
427d233b22SKonstantin Belousov 
437d233b22SKonstantin Belousov unsigned int
447d233b22SKonstantin Belousov __sleep(unsigned int seconds)
457d233b22SKonstantin Belousov {
467d233b22SKonstantin Belousov 	struct timespec time_to_sleep;
477d233b22SKonstantin Belousov 	struct timespec time_remaining;
487d233b22SKonstantin Belousov 
497d233b22SKonstantin Belousov 	/*
507d233b22SKonstantin Belousov 	 * Avoid overflow when `seconds' is huge.  This assumes that
517d233b22SKonstantin Belousov 	 * the maximum value for a time_t is >= INT_MAX.
527d233b22SKonstantin Belousov 	 */
537d233b22SKonstantin Belousov 	if (seconds > INT_MAX)
547d233b22SKonstantin Belousov 		return (seconds - INT_MAX + __sleep(INT_MAX));
557d233b22SKonstantin Belousov 
567d233b22SKonstantin Belousov 	time_to_sleep.tv_sec = seconds;
577d233b22SKonstantin Belousov 	time_to_sleep.tv_nsec = 0;
58*7dd9070eSBrooks Davis 	if (INTERPOS_SYS(nanosleep, &time_to_sleep, &time_remaining) != -1)
597d233b22SKonstantin Belousov 		return (0);
607d233b22SKonstantin Belousov 	if (errno != EINTR)
617d233b22SKonstantin Belousov 		return (seconds);		/* best guess */
627d233b22SKonstantin Belousov 	return (time_remaining.tv_sec +
637d233b22SKonstantin Belousov 	    (time_remaining.tv_nsec != 0)); /* round up */
647d233b22SKonstantin Belousov }
657d233b22SKonstantin Belousov 
667d233b22SKonstantin Belousov __weak_reference(__sleep, sleep);
677d233b22SKonstantin Belousov __weak_reference(__sleep, _sleep);
68