Hi Jens, On 05/13/2018 01:14 PM, Michael Kerrisk (man-pages) wrote: > On 05/12/2018 04:21 PM, Jens Thoms Toerring wrote: >> Hi, >> >> as observed recently settimeofday(2) may fail when called >> with reasonably looking values for the time/date if they're >> near to the Epoch. The reason is that it's not possible to >> set the system time to a value that is lower than the number >> of seconds since the system was booted - otherwise the time >> of boot reported by 'uptime -s' would predate the Epoch. >> >> As this took some time to figure out I think it might be >> worth to mention it in the NOTES section of the man page >> of gettimeofday/settimeofday, see the attached patch. > > Hi Jens, > > It's pretty clear that there are some missing EINVAL cases > in the manual page. However, I think the case should rather > be documented as > > (tv.tv_set < 0 || tv.tv_usec < 0 || tv.tv_usec >= 1000000) > > giving EINVAL. But, perhaps I miss your point. What values > were you specifying to settimeofday() that led to EINVAL? Okay -- an update. The text should I think be phrased in terms of the monotonic clock. In the kernel source file kernel/time/timekeeping.c::do_settimeofday64(), there is this check: if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) { ret = -EINVAL; goto out; } (It took me a little too long too find that check :-/) It appears that the check was added in Linux 4.3: commit e1d7ba8735551ed79c7a0463a042353574b96da3 Author: Wang YanQing <udknight@xxxxxxxxx> Date: Tue Jun 23 18:38:54 2015 +0800 time: Always make sure wall_to_monotonic isn't positive I applied the patch below (which included a related change in the documentation of clock_settime(2)). Does it seem okay? Thanks for the report! Cheers, Michael diff --git a/man2/clock_getres.2 b/man2/clock_getres.2 index c32f9e183..240b87276 100644 --- a/man2/clock_getres.2 +++ b/man2/clock_getres.2 @@ -212,6 +212,19 @@ is negative or .I tp.tv_nsec is outside the range [0..999,999,999]. .TP +.BR EINVAL " (since Linux 4.3)" +.\" commit e1d7ba8735551ed79c7a0463a042353574b96da3 +A call to +.BR clock_settime () +with a +.I clk_id +of +.B CLOCK_REALTIME +attempted to set the time to a value less than +the current value of the +.B CLOCK_MONOTONIC +clock. +.TP .B EPERM .BR clock_settime () does not have permission to set the clock indicated. diff --git a/man2/gettimeofday.2 b/man2/gettimeofday.2 index 05efb67d1..e1c3905e0 100644 --- a/man2/gettimeofday.2 +++ b/man2/gettimeofday.2 @@ -166,6 +166,15 @@ is negative or .I tv.tv_usec is outside the range [0..999,999]. .TP +.BR EINVAL " (since Linux 4.3)" +.\" commit e1d7ba8735551ed79c7a0463a042353574b96da3 +.RB ( settimeofday ()): +An attempt was made to set the time to a value less than +the current value of the +.B CLOCK_MONOTONIC +clock (see +.BR clock_gettime (2)). +.TP .B EPERM The calling process has insufficient privilege to call .BR settimeofday (); -- Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/ -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html