diff --git a/libs/libc/time/lib_nanosleep.c b/libs/libc/time/lib_nanosleep.c index 58ca2034307..1d74f9036b2 100644 --- a/libs/libc/time/lib_nanosleep.c +++ b/libs/libc/time/lib_nanosleep.c @@ -24,6 +24,7 @@ #include +#include #include /**************************************************************************** @@ -80,10 +81,22 @@ int nanosleep(FAR const struct timespec *rqtp, FAR struct timespec *rmtp) { + int ret; + /* Calling clock_nanosleep() with the value TIMER_ABSTIME not set in the * flags argument and with a clock_id of CLOCK_REALTIME is equivalent t * calling nanosleep() with the same rqtp and rmtp arguments. + * As clock_nanosleep() method return errno on fail, which is not + * compatible with nanosleep(), the nanosleep() need to return -1 on fail, + * so we need to convert the return value. */ - return clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp); + ret = clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp); + if (ret != 0) + { + set_errno(ret); + ret = ERROR; + } + + return ret; } diff --git a/sched/signal/sig_nanosleep.c b/sched/signal/sig_nanosleep.c index ecab0dfad08..8fca6f9f7b2 100644 --- a/sched/signal/sig_nanosleep.c +++ b/sched/signal/sig_nanosleep.c @@ -244,12 +244,13 @@ int nxsig_nanosleep(FAR const struct timespec *rqtp, * actually slept). If the rmtp argument is NULL, the remaining time is not * returned. * - * If clock_nanosleep() fails, it returns a value of -1 and sets errno to - * indicate the error. The clock_nanosleep() function will fail if: + * If clock_nanosleep() fails, it returns a value of errno. The + * clock_nanosleep() function will fail if: * * EINTR - The clock_nanosleep() function was interrupted by a signal. * EINVAL - The rqtp argument specified a nanosecond value less than - * zero or greater than or equal to 1000 million. + * zero or greater than or equal to 1000 million. Or the clockid that + * does not specify a known clock. * ENOSYS - The clock_nanosleep() function is not supported by this * implementation. * @@ -265,6 +266,12 @@ int clock_nanosleep(clockid_t clockid, int flags, enter_cancellation_point(); + if (clockid < CLOCK_REALTIME || clockid > CLOCK_BOOTTIME) + { + leave_cancellation_point(); + return EINVAL; + } + /* Check if absolute time is selected */ if ((flags & TIMER_ABSTIME) != 0) @@ -286,7 +293,7 @@ int clock_nanosleep(clockid_t clockid, int flags, leave_critical_section(irqstate); leave_cancellation_point(); - return ERROR; + return -ret; } clock_timespec_subtract(rqtp, &now, &reltime); @@ -312,10 +319,9 @@ int clock_nanosleep(clockid_t clockid, int flags, if (ret < 0) { - /* If not set the errno variable and return -1 */ + /* If not return the errno */ - set_errno(-ret); - ret = ERROR; + ret = -ret; } leave_cancellation_point();