Mon Apr 22 21:02:18 2024 UTC (17d)
allow usleep(3) with useconds >= 1000000

update manpage to mention this interface is obsolete, remove
EINVAL from the ERRORS and mention EINTR instead.

PR lib/58184 by Taylor R Campbell


(jdolecek)
diff -r1.19 -r1.20 src/lib/libc/gen/usleep.3
diff -r1.20 -r1.21 src/lib/libc/gen/usleep.c

cvs diff -r1.19 -r1.20 src/lib/libc/gen/usleep.3 (expand / switch to unified diff)

--- src/lib/libc/gen/usleep.3 2010/04/29 17:29:56 1.19
+++ src/lib/libc/gen/usleep.3 2024/04/22 21:02:18 1.20
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1.\" $NetBSD: usleep.3,v 1.19 2010/04/29 17:29:56 jruoho Exp $ 1.\" $NetBSD: usleep.3,v 1.20 2024/04/22 21:02:18 jdolecek Exp $
2.\" 2.\"
3.\" Copyright (c) 1986, 1991, 1993 3.\" Copyright (c) 1986, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
5.\" 5.\"
6.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
8.\" are met: 8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright 9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer. 10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the 12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution. 13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors 14.\" 3. Neither the name of the University nor the names of its contributors
@@ -19,73 +19,70 @@ @@ -19,73 +19,70 @@
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE. 28.\" SUCH DAMAGE.
29.\" 29.\"
30.\" @(#)usleep.3 8.1 (Berkeley) 6/4/93 30.\" @(#)usleep.3 8.1 (Berkeley) 6/4/93
31.\" 31.\"
32.Dd April 29, 2010 32.Dd April 22, 2024
33.Dt USLEEP 3 33.Dt USLEEP 3
34.Os 34.Os
35.Sh NAME 35.Sh NAME
36.Nm usleep 36.Nm usleep
37.Nd suspend execution for interval of microseconds 37.Nd suspend execution for interval of microseconds
38.Sh LIBRARY 38.Sh LIBRARY
39.Lb libc 39.Lb libc
40.Sh SYNOPSIS 40.Sh SYNOPSIS
41.In unistd.h 41.In unistd.h
42.Ft int 42.Ft int
43.Fn usleep "useconds_t microseconds" 43.Fn usleep "useconds_t microseconds"
44.Sh DESCRIPTION 44.Sh DESCRIPTION
 45.Bf -symbolic
 46This interface is obsoleted by
 47.Xr nanosleep 3 .
 48.Ef
 49.Pp
45The 50The
46.Fn usleep 51.Fn usleep
47function 52function
48suspends execution of the calling process 53suspends execution of the calling process
49until either the number of microseconds specified by 54until either the number of microseconds specified by
50.Fa microseconds 55.Fa microseconds
51have elapsed or a signal is delivered to the calling process and its 56have elapsed or a signal is delivered to the calling process and its
52action is to invoke a signal catching function or to terminate the 57action is to invoke a signal catching function or to terminate the
53process. 58process.
54The suspension time may be longer than requested due to the 59The suspension time may be longer than requested due to the
55scheduling of other activity by the system. 60scheduling of other activity by the system.
56.Pp 
57The 
58.Fa microseconds 
59argument must be less than 1,000,000. 
60If the value of 
61.Fa microseconds 
62is 0, then the call has no effect. 
63.Sh RETURN VALUES 61.Sh RETURN VALUES
64On successful completion, 62On successful completion,
65.Fn usleep 63.Fn usleep
66returns 0. 64returns 0.
67Otherwise, it returns \-1 and sets 65Otherwise, it returns \-1 and sets
68.Va errno 66.Va errno
69to indicate the error. 67to indicate the error.
70.Sh ERRORS 68.Sh ERRORS
71The 69The
72.Fn usleep 70.Fn usleep
73function may fail if: 71function may fail if:
74.Bl -tag -width Er 72.Bl -tag -width Er
75.It Bq Er EINVAL 73.It Bq Er EINTR
76The 74.Nm
77.Fa microseconds 75was interrupted by the delivery of a signal.
78interval specified 1,000,000 or more microseconds. 
79.El 76.El
80.Sh SEE ALSO 77.Sh SEE ALSO
81.Xr nanosleep 2 , 78.Xr nanosleep 2 ,
82.Xr sleep 3 79.Xr sleep 3
83.Sh STANDARDS 80.Sh STANDARDS
84The 81The
85.Fn usleep 82.Fn usleep
86function conforms to 83function conforms to
87.St -xpg4.2 . 84.St -xpg4.2 .
88It later appeared in the 85It later appeared in the
89.Tn POSIX 86.Tn POSIX
90standard, but in 87standard, but in
91.St -p1003.1-2004 88.St -p1003.1-2004

cvs diff -r1.20 -r1.21 src/lib/libc/gen/usleep.c (expand / switch to unified diff)

--- src/lib/libc/gen/usleep.c 2012/06/25 22:32:44 1.20
+++ src/lib/libc/gen/usleep.c 2024/04/22 21:02:18 1.21
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: usleep.c,v 1.20 2012/06/25 22:32:44 abs Exp $ */ 1/* $NetBSD: usleep.c,v 1.21 2024/04/22 21:02:18 jdolecek Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by J.T. Conklin. 8 * by J.T. Conklin.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -21,45 +21,40 @@ @@ -21,45 +21,40 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint) 33#if defined(LIBC_SCCS) && !defined(lint)
34__RCSID("$NetBSD: usleep.c,v 1.20 2012/06/25 22:32:44 abs Exp $"); 34__RCSID("$NetBSD: usleep.c,v 1.21 2024/04/22 21:02:18 jdolecek Exp $");
35#endif /* LIBC_SCCS and not lint */ 35#endif /* LIBC_SCCS and not lint */
36 36
37#include "namespace.h" 37#include "namespace.h"
38#include <errno.h> 38#include <errno.h>
39#include <time.h> 39#include <time.h>
40#include <unistd.h> 40#include <unistd.h>
41 41
42#ifdef __weak_alias 42#ifdef __weak_alias
43__weak_alias(usleep,_usleep) 43__weak_alias(usleep,_usleep)
44#endif 44#endif
45 45
46int 46int
47usleep(useconds_t useconds) 47usleep(useconds_t useconds)
48{ 48{
49 struct timespec ts; 49 struct timespec ts;
50 50
51 if (useconds == 0) 51 if (useconds == 0)
52 return (0); 52 return (0);
53 53
54 if (useconds >= 1000000) { 54 ts.tv_sec = (useconds / 1000000);
55 errno = EINVAL; 55 ts.tv_nsec = (useconds % 1000000) * 1000;
56 return (-1); 
57 } 
58 
59 ts.tv_sec = 0; 
60 ts.tv_nsec = useconds * 1000; 
61 56
62 nanosleep(&ts, NULL); 57 nanosleep(&ts, NULL);
63 58
64 return (0); 59 return (0);
65} 60}