nanosleep() replacement

Darren Tucker dtucker at zip.com.au
Mon Mar 17 23:07:12 EST 2003


Tim Rice wrote:
> I put together a nanosleep() for systems without it.
> 
> Please review/test before I commit.

Before I forget, a couple of things I noticed while playing around with
this:

[snip]
> +int nanosleep(const struct timespec *req, struct timespec *rem)
> +{
> +       int rc;
> +       extern int errno;
> +       struct timeval tsave, ttmp, time2wait;
> +
> +       TIMESPEC_TO_TIMEVAL(&time2wait, req)
> +
> +       gettimeofday(&tsave, NULL);
> +       rc = select(0, NULL, NULL, NULL, &time2wait);
> +       if (rc) {

Checking for rc == -1, right?  Would "if (rc < 0)" be clearer?

> +               gettimeofday (&ttmp, NULL);

This can reset errno so (errno==EINTR) from select can be lost.

> +               ttmp.tv_sec -= tsave.tv_sec;
> +               ttmp.tv_usec -= tsave.tv_usec;
> +               tsave.tv_sec = (time2wait.tv_sec - ttmp.tv_sec);
> +               tsave.tv_usec = (time2wait.tv_usec - ttmp.tv_usec);
> +               if(tsave.tv_sec < 0){
> +                       tsave.tv_sec = 0;
> +                       tsave.tv_usec += 1000000L;
> +               }
> +               rc = -1;
> +       }
> +
> +       TIMEVAL_TO_TIMESPEC(&tsave, rem)

In the case where select returns normally, the remainder is whatever
gettimeofday() returned which will be *way* bigger than the requested
wait time and any possible remainder.

> +       return(rc);
[snip]

-- 
Darren Tucker (dtucker at zip.com.au)
GPG key 8FF4FA69 / D9A3 86E9 7EEE AF4B B2D4  37C9 C982 80C7 8FF4 FA69
    Good judgement comes with experience. Unfortunately, the experience
usually comes from bad judgement.




More information about the openssh-unix-dev mailing list