hwclock: remove UTC-0 localization hack
Subsystems (2)
PATCH 1/1 hwclock: remove UTC-0 localization hack
There is no need to re-implement timegm() by removing TZ localization for a moment, just use timegm(3) instead.
--- sys-utils/hwclock.c | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-)
sys-utils/hwclock.c +5 -29
@@ -388,27 +388,11 @@ static void mktime_tz(struct tm tm, const bool universal, bool * valid_p, time_t * systime_p) { - time_t mktime_result; /* The value returned by our mktime() call */ -char *zone;/* Local time zone name */ - -/* -* We use the C library function mktime(), but since it only works -* on local time zone input, we may have to fake it out by -* temporarily changing the local time zone to UTC. -*/ - zone =getenv ("TZ" );/* remember original time zone */ -if (universal) { -/* Set timezone to UTC */ -setenv ("TZ" ,"" , TRUE); -/* -* Note: tzset() gets called implicitly by the time code, -* but only the first time. When changing the environment -* variable, better call tzset() explicitly. -*/ -tzset (); - } - mktime_result =mktime (&tm); -if (mktime_result ==-1 ) { + if (universal) + *systime_p =timegm (&tm); + else + *systime_p =mktime (&tm); +if (*systime_p ==-1 ) {/* * This apparently (not specified in mktime() documentation) * means the 'tm' structure does not contain valid values
@@ -416,7 +400,6 @@ mktime_tz(struct tm tm, const bool universal,* mktime() returns -1). */ *valid_p = FALSE; - *systime_p =0 ;if (debug) printf(_("Invalid values in hardware clock: " "%4d/%.2d/%.2d %.2d:%.2d:%.2d\n"),
@@ -424,7 +407,6 @@ mktime_tz(struct tm tm, const bool universal, tm.tm_hour, tm.tm_min, tm.tm_sec); }else { *valid_p = TRUE; - *systime_p = mktime_result;if (debug) printf(_ ("Hw clock time : %4d/%.2d/%.2d %.2d:%.2d:%.2d = "
@@ -432,12 +414,6 @@ mktime_tz(struct tm tm, const bool universal, tm.tm_mon +1 , tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, (long)*systime_p); } -/* now put back the original zone. */ -if (zone) -setenv ("TZ" , zone, TRUE); -else -unsetenv ("TZ" ); -tzset (); }/*
Signature · 1 line
2.9.0
We use mktime(3) for portability.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#index-timegm
Portability note: mktime is essentially universally available. timegm is rather rare. For the most portable conversion from a UTC broken-down time to a simple time, set the TZ environment variable to UTC, call mktime, then set TZ back.
Quoted 75 lines · Sami Kerola
There is no need to re-implement
timegm()by removing TZ localization for a moment, just use timegm(3) instead.Signed-off-by Sami Kerola <[email protected]>--- sys-utils/hwclock.c | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-)sys-utils/hwclock.c +5
-29@@ -388,27 +388,11 @@ static void mktime_tz(struct tm tm, const bool universal, bool * valid_p, time_t * systime_p) { - time_t mktime_result; /* The value returned by our mktime() call */ -char *zone;/* Local time zone name */ - -/* -* We use the C library function mktime(), but since it only works -* on local time zone input, we may have to fake it out by -* temporarily changing the local time zone to UTC. -*/ - zone =getenv ("TZ" );/* remember original time zone */ -if (universal) { -/* Set timezone to UTC */ -setenv ("TZ" ,"" , TRUE); -/* -* Note: tzset() gets called implicitly by the time code, -* but only the first time. When changing the environment -* variable, better call tzset() explicitly. -*/ -tzset (); - } - mktime_result =mktime (&tm); -if (mktime_result ==-1 ) { + if (universal) + *systime_p =timegm (&tm); + else + *systime_p =mktime (&tm); +if (*systime_p ==-1 ) {/* * This apparently (not specified in mktime() documentation) * means the 'tm' structure does not contain valid values @@ -416,7 +400,6 @@ mktime_tz(struct tm tm, const bool universal,* mktime() returns -1). */ *valid_p = FALSE; - *systime_p =0 ;if (debug) printf(_("Invalid values in hardware clock: " "%4d/%.2d/%.2d %.2d:%.2d:%.2d\n"),@@ -424,7 +407,6 @@ mktime_tz(struct tm tm, const bool universal, tm.tm_hour, tm.tm_min, tm.tm_sec); }else { *valid_p = TRUE; - *systime_p = mktime_result;if (debug) printf(_ ("Hw clock time : %4d/%.2d/%.2d %.2d:%.2d:%.2d = "@@ -432,12 +414,6 @@ mktime_tz(struct tm tm, const bool universal, tm.tm_mon +1 , tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, (long)*systime_p); } -/* now put back the original zone. */ -if (zone) -setenv ("TZ" , zone, TRUE); -else -unsetenv ("TZ" ); -tzset (); }/*
Quoted 9 lines · J William Piggott
We use mktime(3) for portability.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#index-timegm
Portability note: mktime is essentially universally available. timegm is rather rare. For the most portable conversion from a UTC broken-down time to a simple time, set the TZ environment variable to UTC, call mktime, then set TZ back.
I don't think hwclock(8) is portable. It has ioctl(2) calls, and such.
Signature · 2 lines
Sami Kerola http://www.iki.fi/kerolasa/
Quoted 1 line · Sami Kerola
On 11 July 2016 at 21:23, J William Piggott <[email protected]>
Quoted 5 lines · wrote:
Quoted 5 lines
We use mktime(3) for portability.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#index-t
imegm
Quoted 7 lines
Quoted 4 lines
Portability note: mktime is essentially universally available. timegm is rather rare. For the most portable conversion from a UTC broken-down time to a simple time, set the TZ environment variable to UTC, call mktime, then set TZ back.
I don't think hwclock(8) is portable. It has ioctl(2) calls, and such.
But there are non-gnu libc's for Linux.
cu,
Rudi
Quoted 13 lines · Ruediger Meier
Quoted 1 line · Sami Kerola
On 11 July 2016 at 21:23, J William Piggott <[email protected]>
Quoted 10 lines · wrote:
Quoted 9 lines
We use mktime(3) for portability.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#index-t
imegmPortability note: mktime is essentially universally available. timegm is rather rare. For the most portable conversion from a UTC broken-down time to a simple time, set the TZ environment variable to UTC, call mktime, then set TZ back.
Quoted 4 lines
Quoted 2 lines
I don't think hwclock(8) is portable. It has ioctl(2) calls, and such.
But there are non-gnu libc's for Linux.
Fair point, but can you name one that does not have timegm().
https://github.com/cloudius-systems/musl/blob/master/src/time/timegm.c
https://github.com/ensc/dietlibc/blob/master/libugly/timegm.c
http://www.uclibc-ng.org/browser/uclibc-ng/libc/misc/time/timegm.c?order=name
Signature · 2 lines
Sami Kerola http://www.iki.fi/kerolasa/
Quoted 18 lines · Sami Kerola
Quoted 17 lines · Ruediger Meier
Quoted 1 line · Sami Kerola
On 11 July 2016 at 21:23, J William Piggott <[email protected]>
Quoted 12 lines · wrote:
Quoted 9 lines
We use mktime(3) for portability.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#inde
x-t imegmPortability note: mktime is essentially universally available. timegm is rather rare. For the most portable conversion from a UTC broken-down time to a simple time, set the TZ environment variable to UTC, call mktime, then set TZ back.
I don't think hwclock(8) is portable. It has ioctl(2) calls, and such.
Quoted 8 lines
Quoted 1 line
But there are non-gnu libc's for Linux.
Fair point, but can you name one that does not have
timegm().https://github.com/cloudius-systems/musl/blob/master/src/time/timegm.
c https://github.com/ensc/dietlibc/blob/master/libugly/timegm.c
http://www.uclibc-ng.org/browser/uclibc-ng/libc/misc/time/timegm.c?or
der=name
Yep, also musl has timegm. But at least it's good that William reminded us to check that! :)
cu,
Rudi
Quoted 23 lines · Ruediger Meier
Quoted 22 lines · Sami Kerola
Quoted 14 lines · Ruediger Meier
Quoted 1 line · Sami Kerola
On 11 July 2016 at 21:23, J William Piggott <[email protected]>
Quoted 10 lines · wrote:
Quoted 9 lines
We use mktime(3) for portability.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#inde
x-t imegmPortability note: mktime is essentially universally available. timegm is rather rare. For the most portable conversion from a UTC broken-down time to a simple time, set the TZ environment variable to UTC, call mktime, then set TZ back.
Quoted 4 lines
Quoted 2 lines
I don't think hwclock(8) is portable. It has ioctl(2) calls, and such.
But there are non-gnu libc's for Linux.
Fair point, but can you name one that does not have
timegm().
Quoted 8 lines
Quoted 4 lines
https://github.com/cloudius-systems/musl/blob/master/src/time/timegm.
c https://github.com/ensc/dietlibc/blob/master/libugly/timegm.c
http://www.uclibc-ng.org/browser/uclibc-ng/libc/misc/time/timegm.c?or
der=nameYep, also musl has timegm. But at least it's good that William reminded us to check that! :)
Oh well. I guess it does not hurt to support incomplete libraries even if we might not be able to name them.
https://github.com/kerolasa/lelux-utiliteetit/commit/5dff764bb38f0745a8bb128337d2534f109a37c8
and the same as email body so that review is possible.
--->8----
From: Sami Kerola <[email protected]>
Date: Tue, 12 Jul 2016 22:21:10 +0100
Subject: [PATCH] lib: add portability functions, start with timegm()
The lib/portability.c is a file where functions that may be missing from libraries can be added.
--- configure.ac | 1 + include/Makemodule.am | 1 + include/portability.h | 8 ++++++++ lib/Makemodule.am | 1 + lib/portability.c | 28 ++++++++++++++++++++++++++++ login-utils/utmpdump.c | 1 + sys-utils/hwclock.c | 1 + 7 files changed, 41 insertions(+) create mode 100644 include/portability.h create mode 100644 lib/portability.c
configure.ac +1 -0
@@ -413,6 +413,7 @@ AC_CHECK_FUNCS([ \ strnlen \ sysconf \ sysinfo \ + timegm \ updwtmp \ usleep \ warn \
include/Makemodule.am +1 -0
@@ -35,6 +35,7 @@ dist_noinst_HEADERS += \ include/path.h \ include/pathnames.h \ include/plymouth-ctrl.h \ + include/portability.h \ include/procutils.h \ include/pt-bsd.h \ include/pt-mbr.h \
include/portability.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef UTIL_LINUX_PORTABILITY_H +#define UTIL_LINUX_PORTABILITY_H + +#ifndef HAVE_TIMEGM +extern time_t timegm (struct tm *tm); +#endif + +#endif /* UTIL_LINUX_PORTABILITY_H */
lib/Makemodule.am +1 -0
@@ -16,6 +16,7 @@ libcommon_la_SOURCES = \ lib/md5.c \ lib/pager.c \ lib/path.c \ + lib/portability.c \ lib/randutils.c \ lib/setproctitle.c \ lib/strutils.c \
lib/portability.c new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Functions in this file are replacements when library functions neither +* unimplemented or unavailable due old version. In best case these +* functions are never needed. +*/ + +#include <time.h> +#include <stdlib.h> + +#include "portability.h" + +#ifndef HAVE_TIMEGM +time_t timegm (struct tm *tm) +{ +const char *zone =getenv ("TZ" ); +time_t ret; + +setenv ("TZ" ,"" ,1 ); +tzset (); + ret =mktime (tm); +if (zone) +setenv ("TZ" , zone,1 ); +else +unsetenv ("TZ" ); +tzset (); +return ret; +} +#endif /* HAVE_TIMEGM */
login-utils/utmpdump.c +1 -0
@@ -45,6 +45,7 @@#include "timeutils.h" #include "xalloc.h" #include "closestream.h" +#include "portability.h" static time_t strtotime (const char *s_time) {
sys-utils/hwclock.c +1 -0
@@ -78,6 +78,7 @@#include "nls.h" #include "optutils.h" #include "pathnames.h" +#include "portability.h" #include "strutils.h" #include "hwclock.h" #include "timeutils.h"
Signature · 1 line
2.9.0
Karel Zak
maintainerQuoted 1 line · Sami Kerola
lib/portability.c | 28 ++++++++++++++++++++++++++++
We already care about portability and we have many fallbacks, so why we need lib/portability.c now? It would better to use timeutils.c (or inline function in c.h).
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 6 lines · Karel Zak
Quoted 1 line · Sami Kerola
lib/portability.c | 28 ++++++++++++++++++++++++++++We already care about portability and we have many fallbacks, so why we need
lib/portability.cnow? It would better to usetimeutils.c(or inline function inc.h).
Hi Karel.
I didn't do my homework carefully enough. After assuming the timeutils.c should be kept close to simiar with systemd version I thought some other file should be used. But now when I look systemd version of the time-utils.c and compare it with the util-linux copy it's pretty clear these are two very different pieces of code. Change below moved the replacement function to the timeutils.c with necessary commit message update.
https://github.com/kerolasa/lelux-utiliteetit/commit/0f916063a4f887f198e005a7a0f7878aa0bfbe05
Signature · 2 lines
Sami Kerola http://www.iki.fi/kerolasa/
Quoted 10 lines · Karel Zak
Quoted 1 line · Sami Kerola
lib/portability.c | 28 ++++++++++++++++++++++++++++We already care about portability and we have many fallbacks, so why we need
lib/portability.cnow? It would better to usetimeutils.c(or inline function inc.h).Karel
What is this projects position on POSIX compatibility? A few comments in the *-ReleaseNotes is all I found in /Documentation. Just curious, because mktime() is and timegm() is not POSIX.
Quoted 11 lines · J William Piggott
Quoted 6 lines · Karel Zak
Quoted 1 line · Sami Kerola
lib/portability.c | 28 ++++++++++++++++++++++++++++We already care about portability and we have many fallbacks, so why we need
lib/portability.cnow? It would better to usetimeutils.c(or inline function inc.h).What is this projects position on POSIX compatibility? A few comments in the *-ReleaseNotes is all I found in /Documentation. Just curious, because
mktime()is andtimegm()is not POSIX.
That is a question to maintainer, but Karel seems to be busy so let me try to phrase how I see the things.
The util-linux project is a dumping ground for various linux specific, and other tools. Some of the tools make are sensible only in context of linux, such as dmesg(1) or mkswap(8), while other could technically be compiled on other operating environments, more(1) and getopt(1) are examples of later category.
As mentioned in this thread the util-linux is one of the core packages, and it is assumed to be found from all sorts of systems. That in mind the portability question has got more to do whether the project works without problems with alternative libc implementations. Ideally the util-linux should compile fine on any linux no matter what libc is used. But please notice that some utilities might not get compiled if/when autotools notice requirements are not fulfilled.
In short. While portability is not a main goal of the project neither it is completely neglected either. Keeping ulibc going is important, making the util-linux work with mingw is less important.
Signature · 2 lines
Sami Kerola http://www.iki.fi/kerolasa/
Quoted 30 lines · Sami Kerola
Quoted 11 lines · J William Piggott
Quoted 6 lines · Karel Zak
Quoted 1 line · Sami Kerola
lib/portability.c | 28 ++++++++++++++++++++++++++++We already care about portability and we have many fallbacks, so why we need
lib/portability.cnow? It would better to usetimeutils.c(or inline function inc.h).What is this projects position on POSIX compatibility? A few comments in the *-ReleaseNotes is all I found in /Documentation. Just curious, because
mktime()is andtimegm()is not POSIX.That is a question to maintainer, but Karel seems to be busy so let me try to phrase how I see the things.
The util-linux project is a dumping ground for various linux specific, and other tools. Some of the tools make are sensible only in context of linux, such as dmesg(1) or mkswap(8), while other could technically be compiled on other operating environments, more(1) and getopt(1) are examples of later category.
As mentioned in this thread the util-linux is one of the core packages, and it is assumed to be found from all sorts of systems. That in mind the portability question has got more to do whether the project works without problems with alternative libc implementations. Ideally the util-linux should compile fine on any linux no matter what libc is used. But please notice that some utilities might not get compiled if/when autotools notice requirements are not fulfilled.
Quoted 4 lines
In short. While portability is not a main goal of the project neither it is completely neglected either. Keeping ulibc going is important, making the util-linux work with mingw is less important.
Hey Sami,
Thanks for your reply. The position you describe is what I had surmised as a general statement (with the addition that portability also involves different hardware architectures). I just wanted to know if Karel had an official stance specifically on POSIX compliance. POSIX appears to only be used as an ad hoc reference?
Karel Zak
maintainerQuoted 9 lines · Sami Kerola
Quoted 8 lines · J William Piggott
Quoted 6 lines · Karel Zak
Quoted 1 line · Sami Kerola
lib/portability.c | 28 ++++++++++++++++++++++++++++We already care about portability and we have many fallbacks, so why we need
lib/portability.cnow? It would better to usetimeutils.c(or inline function inc.h).
Quoted 6 lines
Quoted 3 lines
What is this projects position on POSIX compatibility? A few comments in the *-ReleaseNotes is all I found in /Documentation. Just curious, because
mktime()is andtimegm()is not POSIX.That is a question to maintainer, but Karel seems to be busy so let me try to phrase how I see the things.
I had vacation last 2 weeks.
Quoted 11 lines
As mentioned in this thread the util-linux is one of the core packages, and it is assumed to be found from all sorts of systems. That in mind the portability question has got more to do whether the project works without problems with alternative libc implementations. Ideally the util-linux should compile fine on any linux no matter what libc is used. But please notice that some utilities might not get compiled if/when autotools notice requirements are not fulfilled.
In short. While portability is not a main goal of the project neither it is completely neglected either. Keeping ulibc going is important, making the util-linux work with mingw is less important.
Yes.
The primary goal is Linux with all usable libc, the another operation systems and exotic libraries are supported only if the support has no negative impact to the Linux use-cases and code maintenance.
As in another cases we assume common sense rather than any strict rules ;-)
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com