nanosleep - does it make sense with tv_sec < 0?

Garrett Cooper yanegomi at gmail.com
Mon Aug 2 08:09:30 UTC 2010


On Wed, Jul 28, 2010 at 11:01 PM, Garrett Cooper <yanegomi at gmail.com> wrote:
> Hi Hackers,
>    I ran into an oddity with the POSIX spec that seems a bit unrealistic:
>
> [EINVAL]
>    The rqtp argument specified a nanosecond value less than zero or
> greater than or equal to 1000 million.
>
>    Seems like it should also apply for seconds < 0. We current
> silently pass this argument in kern/kern_time.c:kern_nanosleep:
>
> int
> kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
> {
>        struct timespec ts, ts2, ts3;
>        struct timeval tv;
>        int error;
>
>        if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
>                return (EINVAL);
>        if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec ==
> 0)) // <-- first clause here
>                return (0);
>
>    but I'm wondering whether or not it makes logical sense for us to
> do this (sleep for a negative amount of time?)...
>    FWIW Linux returns -1 and sets EINVAL in this case, which makes
> more sense to me.

    After talking with the Austin Group folks, this appears to be an
[optional] implementation detail with the fact that our time_t is
signed. I think that this patch is valid for catching this case,
because sleeping negative time doesn't seem logical...
Thanks,
-Garrett

Index: lib/libc/sys/nanosleep.2
===================================================================
--- lib/libc/sys/nanosleep.2	(revision 210226)
+++ lib/libc/sys/nanosleep.2	(working copy)
@@ -87,19 +87,20 @@
 .It Bq Er EINTR
 The
 .Fn nanosleep
-system call
-was interrupted by the delivery of a signal.
+system call was interrupted by the delivery of a signal.
 .It Bq Er EINVAL
 The
 .Fa rqtp
-argument
-specified a nanosecond value less than zero
+argument specified a nanosecond value less than zero
 or greater than or equal to 1000 million.
+.It Bq Er EINVAL
+The
+.Fa rqtp
+argument specified a second value less than zero.
 .It Bq Er ENOSYS
 The
 .Fn nanosleep
-system call
-is not supported by this implementation.
+system call is not supported by this implementation.
 .El
 .Sh SEE ALSO
 .Xr sigsuspend 2 ,
Index: sys/kern/kern_time.c
===================================================================
--- sys/kern/kern_time.c	(revision 210226)
+++ sys/kern/kern_time.c	(working copy)
@@ -358,7 +358,9 @@

 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
 		return (EINVAL);
-	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
+	if (rqt->tv_sec < 0)
+		return (EINVAL);
+	if (rqt->tv_sec == 0 && rqt->tv_nsec == 0)
 		return (0);
 	getnanouptime(&ts);
 	timespecadd(&ts, rqt);


More information about the freebsd-hackers mailing list