nanosleep() replacement

Corinna Vinschen vinschen at redhat.com
Mon Mar 17 23:16:21 EST 2003


Hi,

On Mon, Mar 17, 2003 at 10:16:50PM +1100, Darren Tucker wrote:
> I did some fiddling and got pretty close to the real nanosleep with the
> modified function below.

wouldn't it make sense to return the correct errno like this:

> int nanosleep(const struct timespec *req, struct timespec *rem)
> {
>        int rc;

         int sav_errno;

>        extern int errno;
>        struct timeval tstart, tstop, tremain, time2wait;
> 
>        TIMESPEC_TO_TIMEVAL(&time2wait, req)
> 
>        gettimeofday(&tstart, NULL);
>        rc = select(0, NULL, NULL, NULL, &time2wait);

         if (rc < 0)
	   sav_errno = errno;

>        gettimeofday (&tstop, NULL);
> 
>        tremain.tv_sec = tstop.tv_sec - tstart.tv_sec - time2wait.tv_sec;
>        tremain.tv_usec = tstop.tv_usec - tstart.tv_usec -
> time2wait.tv_usec;
>        tremain.tv_sec += tremain.tv_usec / 1000000L;
>        tremain.tv_usec %= 1000000L;
>        TIMEVAL_TO_TIMESPEC(&tremain, rem)
> 
>        if (rc < 0) {
>                rc = -1;

		 errno = sav_errno;

>        }
>        return(rc);
> }

Even though EINTR is the most common error in the degenerated select,
it's not necessarily the only error.  E. g. EINVAL, which is returned
by select if the timer value is invalid.  This is also a common error
code for nanosleep.

Corinna

-- 
Corinna Vinschen
Cygwin Developer
Red Hat, Inc.
mailto:vinschen at redhat.com




More information about the openssh-unix-dev mailing list