Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Aug 2010 01:09:29 -0700
From:      Garrett Cooper <yanegomi@gmail.com>
To:        hackers@freebsd.org
Subject:   Re: nanosleep - does it make sense with tv_sec < 0?
Message-ID:  <AANLkTik0e7RPJ6wNsu8RA6UnAVyLrW7Z5-8Cx-3fYs1p@mail.gmail.com>
In-Reply-To: <AANLkTinkwvK2tHZ0okZE48bvW8-N4WBOm284fVK2Xi3F@mail.gmail.com>
References:  <AANLkTinkwvK2tHZ0okZE48bvW8-N4WBOm284fVK2Xi3F@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Jul 28, 2010 at 11:01 PM, Garrett Cooper <yanegomi@gmail.com> wrote=
:
> Hi Hackers,
> =A0 =A0I ran into an oddity with the POSIX spec that seems a bit unrealis=
tic:
>
> [EINVAL]
> =A0 =A0The rqtp argument specified a nanosecond value less than zero or
> greater than or equal to 1000 million.
>
> =A0 =A0Seems 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)
> {
> =A0 =A0 =A0 =A0struct timespec ts, ts2, ts3;
> =A0 =A0 =A0 =A0struct timeval tv;
> =A0 =A0 =A0 =A0int error;
>
> =A0 =A0 =A0 =A0if (rqt->tv_nsec < 0 || rqt->tv_nsec >=3D 1000000000)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return (EINVAL);
> =A0 =A0 =A0 =A0if (rqt->tv_sec < 0 || (rqt->tv_sec =3D=3D 0 && rqt->tv_ns=
ec =3D=3D
> 0)) // <-- first clause here
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return (0);
>
> =A0 =A0but I'm wondering whether or not it makes logical sense for us to
> do this (sleep for a negative amount of time?)...
> =A0 =A0FWIW 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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 >=3D 1000000000)
 		return (EINVAL);
-	if (rqt->tv_sec < 0 || (rqt->tv_sec =3D=3D 0 && rqt->tv_nsec =3D=3D 0))
+	if (rqt->tv_sec < 0)
+		return (EINVAL);
+	if (rqt->tv_sec =3D=3D 0 && rqt->tv_nsec =3D=3D 0)
 		return (0);
 	getnanouptime(&ts);
 	timespecadd(&ts, rqt);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTik0e7RPJ6wNsu8RA6UnAVyLrW7Z5-8Cx-3fYs1p>