nanosleep() replacement Take 2

Tim Rice tim at multitalents.net
Tue Mar 18 13:06:10 EST 2003


On Tue, 18 Mar 2003, Darren Tucker wrote:

> Darren Tucker wrote:
> > Difference in select() behaviour between patforms?
> [snip]
> 
> Nothing so exotic, just some bugs (sigh).
> 
> The version I tested had "if (rc < 0) errno=EINTR" and the arithmetic
> for the remainder was just plain wrong.  This returned a negative
> remainder which caused select to fail next time round with invalid
> paramters, which the function converted into errno=EINTR.  Amazingly,
> this worked!
> 
> The following works better for me.

Works better here too.

But I think we should have 
    saverrno = errno;
    gettimeofday (&tstop, NULL);
    errno = saverrno;
inside of if (rc == -1) {
and get rid of the second if (rc == -1)
(that's how I tested it)

> 
> 		-Daz.
> 
> int nanosleep(const struct timespec *req, struct timespec *rem)
> {
>        int rc, saverrno;
>        extern int errno;
>        struct timeval tstart, tstop, tremain, time2wait;
> 
>        TIMESPEC_TO_TIMEVAL(&time2wait, req)
>        gettimeofday(&tstart, NULL);
>        rc = select(0, NULL, NULL, NULL, &time2wait);
>        saverrno = errno;
>        gettimeofday (&tstop, NULL);
> 
>        if (rc == -1) {
>                tremain.tv_sec = time2wait.tv_sec - (tstop.tv_sec -
> tstart.tv_sec);
>                tremain.tv_usec = time2wait.tv_usec - (tstop.tv_usec -
> tstart.tv_usec);
>                tremain.tv_sec += tremain.tv_usec / 1000000L;
>                tremain.tv_usec %= 1000000L;
>        } else {
>                 tremain.tv_sec = 0;
>                 tremain.tv_usec = 0;
>        }
>        TIMEVAL_TO_TIMESPEC(&tremain, rem)
> 
>       if (rc == -1)
>               errno = saverrno;
>       return(rc);
> }
> 
> 

-- 
Tim Rice				Multitalents	(707) 887-1469
tim at multitalents.net




More information about the openssh-unix-dev mailing list