On Fri, Jul 30, 2021 at 10:01:40AM -0600, Todd C. Miller wrote:
> I really don't like "get or set the UTC time" in the NAME section.
> It's not like we have a "get or set the local time" function.
> I would prefer if you left the UTC information for the DESCRIPTION.

Sure, changed to "get or set the time of day".

FWIW, the clock_gettime(2) one-liner is "get or set the time".

> Instead of saying that settimeofday ignores tz if not NULL why not
> just say that settimeofday ignores tz.  Its value is not really
> relevant.

We can't say we ignore tz because we don't.  We still copyin() the
timezone struct if tz is not NULL and set EFAULT if the copy fails.
The contents are ignored, but the pointer given as argument is
validated when it isn't NULL.

We talked about ignoring the tz argument completely when we removed
the timezone from the kernel but ultimately decided against it.

Index: gettimeofday.2
===================================================================
RCS file: /cvs/src/lib/libc/sys/gettimeofday.2,v
retrieving revision 1.31
diff -u -p -r1.31 gettimeofday.2
--- gettimeofday.2      4 Sep 2019 14:27:55 -0000       1.31
+++ gettimeofday.2      30 Jul 2021 16:47:23 -0000
@@ -35,97 +35,121 @@
 .Sh NAME
 .Nm gettimeofday ,
 .Nm settimeofday
-.Nd get/set date and time
+.Nd get or set the time of day
 .Sh SYNOPSIS
 .In sys/time.h
 .Ft int
-.Fn gettimeofday "struct timeval *tp" "struct timezone *tzp"
+.Fn gettimeofday "struct timeval *now" "struct timezone *tz"
 .Ft int
-.Fn settimeofday "const struct timeval *tp" "const struct timezone *tzp"
+.Fn settimeofday "const struct timeval *now" "const struct timezone *tz"
 .Sh DESCRIPTION
-The system's notion of the current Greenwich time is obtained with the
+The
 .Fn gettimeofday
-call and set with the
+function writes the absolute value of the system's Coordinated Universal Time
+.Pq UTC
+clock to
+.Fa now
+unless
+.Fa now
+is
+.Dv NULL .
+.Pp
+The UTC clock's absolute value is the time elapsed since
+Jan 1 1970 00:00:00 +0000
+.Pq the Epoch .
+The clock normally advances continuously,
+though it may jump discontinuously if a process calls
 .Fn settimeofday
-call.
-The time is expressed in seconds and microseconds
-since midnight (0 hour), January 1, 1970.
-The resolution of the system clock is hardware dependent, and the time
-may be updated continuously or in
-.Dq ticks .
-If
-.Fa tp
+or
+.Xr clock_settime 2 .
+For this reason,
+.Fn gettimeofday
+is not generally suitable for measuring elapsed time.
+Whenever possible,
+use
+.Xr clock_gettime 2
+to measure elapsed time with one of the system's monotonic clocks instead.
+.Pp
+The
+.Fn settimeofday
+function sets the system's UTC clock to the absolute value
+.Fa now
+unless
+.Fa now
 is
-.Dv NULL ,
-the time will not be returned or set.
+.Dv NULL .
+Only the superuser may set the clock.
+If the system
+.Xr securelevel 7
+is 2 or greater the clock may only be advanced.
+This limitation prevents a malicious superuser
+from setting arbitrary timestamps on files.
+Setting the clock cancels any ongoing
+.Xr adjtime 2
+adjustment.
+.Pp
 The structure pointed to by
-.Fa tp
+.Fa now
 is defined in
 .In sys/time.h
 as:
 .Bd -literal
 struct timeval {
-       time_t          tv_sec;         /* seconds since Jan. 1, 1970 */
+       time_t          tv_sec;         /* seconds */
        suseconds_t     tv_usec;        /* and microseconds */
 };
 .Ed
 .Pp
 The
-.Fa tzp
-parameter is historical and timezone information is no longer
-tracked by the system.
-All code should pass
-.Dv NULL
-for
-.Fa tzp .
-For
-.Fn gettimeofday ,
-if
-.Fa tzp
-is
-.Pf non- Dv NULL
-an empty
-.Dv timezone
-structure will be returned.
-For
-.Fn settimeofday ,
-if
-.Fa tzp
-is
-.Pf non- Dv NULL
-its contents are ignored.
-.Pp
-Only the superuser may set the time of day.
-If the system securelevel is greater than 1 (see
-.Xr init 8 ) ,
-the time may only be advanced.
-This limitation is imposed to prevent a malicious superuser
-from setting arbitrary time stamps on files.
-The system time can still be adjusted backwards using the
-.Xr adjtime 2
-system call even when the system is secure.
+.Fa tz
+argument is historical:
+the system no longer maintains timezone information in the kernel.
+The
+.Fa tz
+argument should always be
+.Dv NULL .
+.Fn gettimeofday
+zeroes
+.Fa tz
+if it is not
+.Dv NULL .
+.Fn settimeofday
+ignores the contents of
+.Fa tz
+if it is not
+.Dv NULL .
 .Sh RETURN VALUES
 .Rv -std
 .Sh ERRORS
 .Fn gettimeofday
 and
 .Fn settimeofday
-will succeed unless:
+will fail if:
 .Bl -tag -width Er
 .It Bq Er EFAULT
-An argument address referenced invalid memory.
+.Fa now
+or
+.Fa tz
+are not
+.Dv NULL
+and reference invalid memory.
 .El
 .Pp
-In addition,
 .Fn settimeofday
-may return the following errors:
+will also fail if:
 .Bl -tag -width Er
 .It Bq Er EINVAL
-.Fa tp
-specified a microsecond value less than zero or greater than or equal to
-1 million.
+.Fa now
+specifies a microsecond value less than zero or greater than or equal to
+one million.
+.It Bq Er EPERM
+The caller is not the superuser.
 .It Bq Er EPERM
-A user other than the superuser attempted to set the time.
+The system
+.Xr securelevel 7
+is 2 or greater and
+.Fa now
+specifies a time in the past.
 .El
 .Sh SEE ALSO
 .Xr date 1 ,
@@ -133,14 +157,21 @@ A user other than the superuser attempte
 .Xr clock_gettime 2 ,
 .Xr getitimer 2 ,
 .Xr ctime 3 ,
-.Xr time 3
+.Xr time 3 ,
+.Xr timeradd 3
 .Sh STANDARDS
 The
 .Fn gettimeofday
 function conforms to
 .St -p1003.1-2008 .
+.Pp
+The
+.Fn settimeofday
+function is non-standard,
+though many systems offer it.
 .Sh HISTORY
-As predecessors of these functions, former system calls
+As predecessors of these functions,
+former system calls
 .Fn time
 and
 .Fn stime

Reply via email to